blob: 6b09a778cc71faffb7672b79b2d32ab0c2f49240 [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>
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Jon Paul Maloyc637c102015-02-05 08:36:41 -050042#include "name_distr.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040043#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040044#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010045#include "netlink.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040046
Ying Xue07f6c4b2015-01-07 13:41:58 +080047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080048#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080049#define TIPC_FWD_MSG 1
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define TIPC_MAX_PORT 0xffffffff
51#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040052
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010053enum {
54 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010055 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010056 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010057 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +010058 TIPC_CONNECTING = TCP_SYN_SENT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010059};
60
Jon Paul Maloy301bae52014-08-22 18:09:20 -040061/**
62 * struct tipc_sock - TIPC socket structure
63 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040064 * @conn_type: TIPC type used when connection was established
65 * @conn_instance: TIPC instance used when connection was established
66 * @published: non-zero if port has one or more associated names
67 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080068 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040069 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy365ad352017-01-03 10:55:11 -050070 * #cong_links: list of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040071 * @publications: list of publications for port
Jon Paul Maloy365ad352017-01-03 10:55:11 -050072 * @blocking_link: address of the congested link we are currently sleeping on
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073 * @pub_count: total # of publications port has made during its lifetime
74 * @probing_state:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040075 * @conn_timeout: the time we can wait for an unresponded setup request
76 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
Jon Paul Maloy365ad352017-01-03 10:55:11 -050077 * @cong_link_cnt: number of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040078 * @sent_unacked: # messages sent by socket, and not yet acked by peer
79 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010080 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080081 * @node: hash table node
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -050082 * @mc_method: cookie for use between socket and broadcast layer
Ying Xue07f6c4b2015-01-07 13:41:58 +080083 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040084 */
85struct tipc_sock {
86 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040087 u32 conn_type;
88 u32 conn_instance;
89 int published;
90 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080091 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040092 struct tipc_msg phdr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -050093 struct list_head cong_links;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040094 struct list_head publications;
95 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 uint conn_timeout;
97 atomic_t dupl_rcvcnt;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010098 bool probe_unacked;
Jon Paul Maloy365ad352017-01-03 10:55:11 -050099 u16 cong_link_cnt;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400100 u16 snt_unacked;
101 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400102 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400103 u16 rcv_unacked;
104 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100105 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800106 struct rhash_head node;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500107 struct tipc_mc_method mc_method;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800108 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400109};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400111static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400112static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800113static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800114static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800115static int tipc_release(struct socket *sock);
116static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Ying Xuef2f2a962015-01-09 15:27:02 +0800117static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400118static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400119 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400120static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400121 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800122static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800123static int tipc_sk_insert(struct tipc_sock *tsk);
124static void tipc_sk_remove(struct tipc_sock *tsk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500125static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
Ying Xue39a02952015-03-02 15:37:47 +0800126static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127
Florian Westphalbca65ea2008-02-07 18:18:01 -0800128static const struct proto_ops packet_ops;
129static const struct proto_ops stream_ops;
130static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100132static const struct rhashtable_params tsk_rht_params;
133
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500134static u32 tsk_own_node(struct tipc_sock *tsk)
135{
136 return msg_prevnode(&tsk->phdr);
137}
138
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400139static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400140{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400141 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400142}
143
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400144static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400145{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400146 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400147}
148
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400149static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400150{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400151 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400152}
153
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400154static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400155{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400156 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400157}
158
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400159static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400160{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400161 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400162}
163
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400164static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400165{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400166 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400167}
168
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400169static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400170{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400171 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400172}
173
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400174static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400175{
176 if (imp > TIPC_CRITICAL_IMPORTANCE)
177 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400178 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400179 return 0;
180}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400181
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400182static struct tipc_sock *tipc_sk(const struct sock *sk)
183{
184 return container_of(sk, struct tipc_sock, sk);
185}
186
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400187static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400188{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500189 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400190}
191
192/* tsk_blocks(): translate a buffer size in bytes to number of
193 * advertisable blocks, taking into account the ratio truesize(len)/len
194 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
195 */
196static u16 tsk_adv_blocks(int len)
197{
198 return len / FLOWCTL_BLK_SZ / 4;
199}
200
201/* tsk_inc(): increment counter for sent or received data
202 * - If block based flow control is not supported by peer we
203 * fall back to message based ditto, incrementing the counter
204 */
205static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
206{
207 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
208 return ((msglen / FLOWCTL_BLK_SZ) + 1);
209 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400210}
211
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400213 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700214 *
215 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400217static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400219 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220}
221
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400222/* tipc_sk_respond() : send response message back to sender
223 */
224static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
225{
226 u32 selector;
227 u32 dnode;
228 u32 onode = tipc_own_addr(sock_net(sk));
229
230 if (!tipc_msg_reverse(onode, &skb, err))
231 return;
232
233 dnode = msg_destnode(buf_msg(skb));
234 selector = msg_origport(buf_msg(skb));
235 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
236}
237
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400239 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700240 *
241 * Caller must hold socket lock
242 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400243static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700244{
Ying Xuea6ca1092014-11-26 11:41:55 +0800245 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700246
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400247 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
248 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249}
250
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100251static bool tipc_sk_connected(struct sock *sk)
252{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100253 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100254}
255
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100256/* tipc_sk_type_connectionless - check if the socket is datagram socket
257 * @sk: socket
258 *
259 * Returns true if connection less, false otherwise
260 */
261static bool tipc_sk_type_connectionless(struct sock *sk)
262{
263 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
264}
265
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400266/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400267 *
268 * Handles cases where the node's network address has changed from
269 * the default of <0.0.0> to its configured setting.
270 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400271static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400272{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100273 struct sock *sk = &tsk->sk;
274 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400275 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400276 u32 orig_node;
277 u32 peer_node;
278
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100279 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400280 return false;
281
282 if (unlikely(msg_origport(msg) != peer_port))
283 return false;
284
285 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400286 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400287
288 if (likely(orig_node == peer_node))
289 return true;
290
Ying Xue34747532015-01-09 15:27:10 +0800291 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400292 return true;
293
Ying Xue34747532015-01-09 15:27:10 +0800294 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400295 return true;
296
297 return false;
298}
299
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100300/* tipc_set_sk_state - set the sk_state of the socket
301 * @sk: socket
302 *
303 * Caller must hold socket lock
304 *
305 * Returns 0 on success, errno otherwise
306 */
307static int tipc_set_sk_state(struct sock *sk, int state)
308{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100309 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100310 int res = -EINVAL;
311
312 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100313 case TIPC_OPEN:
314 res = 0;
315 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100316 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100317 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100318 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100319 res = 0;
320 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100321 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100322 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100323 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100324 res = 0;
325 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100326 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100327 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100328 oldsk_state == TIPC_ESTABLISHED)
329 res = 0;
330 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100331 }
332
333 if (!res)
334 sk->sk_state = state;
335
336 return res;
337}
338
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500339static int tipc_sk_sock_err(struct socket *sock, long *timeout)
340{
341 struct sock *sk = sock->sk;
342 int err = sock_error(sk);
343 int typ = sock->type;
344
345 if (err)
346 return err;
347 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
348 if (sk->sk_state == TIPC_DISCONNECTING)
349 return -EPIPE;
350 else if (!tipc_sk_connected(sk))
351 return -ENOTCONN;
352 }
353 if (!*timeout)
354 return -EAGAIN;
355 if (signal_pending(current))
356 return sock_intr_errno(*timeout);
357
358 return 0;
359}
360
361#define tipc_wait_for_cond(sock_, timeout_, condition_) \
362({ \
363 int rc_ = 0; \
364 int done_ = 0; \
365 \
366 while (!(condition_) && !done_) { \
367 struct sock *sk_ = sock->sk; \
368 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
369 \
370 rc_ = tipc_sk_sock_err(sock_, timeout_); \
371 if (rc_) \
372 break; \
373 prepare_to_wait(sk_sleep(sk_), &wait_, \
374 TASK_INTERRUPTIBLE); \
375 done_ = sk_wait_event(sk_, timeout_, \
376 (condition_), &wait_); \
377 remove_wait_queue(sk_sleep(sk_), &wait_); \
378 } \
379 rc_; \
380})
381
Allan Stephens0c3141e2008-04-15 00:22:02 -0700382/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400383 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700384 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 * @sock: pre-allocated socket structure
386 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800387 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900388 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700389 * This routine creates additional data structures used by the TIPC socket,
390 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 *
392 * Returns 0 on success, errno otherwise
393 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400394static int tipc_sk_create(struct net *net, struct socket *sock,
395 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500397 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700398 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400400 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400401 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700402
403 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 if (unlikely(protocol != 0))
405 return -EPROTONOSUPPORT;
406
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 switch (sock->type) {
408 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700409 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 break;
411 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700412 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 break;
414 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700416 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 break;
Allan Stephens49978652006-06-25 23:47:18 -0700418 default:
Allan Stephens49978652006-06-25 23:47:18 -0700419 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 }
421
Allan Stephens0c3141e2008-04-15 00:22:02 -0700422 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500423 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700424 if (sk == NULL)
425 return -ENOMEM;
426
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400427 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400428 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400429 INIT_LIST_HEAD(&tsk->publications);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500430 INIT_LIST_HEAD(&tsk->cong_links);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400431 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500432 tn = net_generic(sock_net(sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433
Allan Stephens0c3141e2008-04-15 00:22:02 -0700434 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700435 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100437 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800438 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900439 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800440 return -EINVAL;
441 }
Herbert Xu40f9f432017-02-11 19:26:46 +0800442
443 /* Ensure tsk is visible before we read own_addr. */
444 smp_mb();
445
446 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
447 NAMED_H_SIZE, 0);
448
Ying Xue07f6c4b2015-01-07 13:41:58 +0800449 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800450 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100451 sk->sk_shutdown = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400452 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400453 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800454 sk->sk_data_ready = tipc_data_ready;
455 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800456 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400457 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
458 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700459
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400460 /* Start out with safe limits until we receive an advertised window */
461 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
462 tsk->rcv_win = tsk->snd_win;
463
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100464 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400465 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700466 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400467 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100469
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 return 0;
471}
472
Ying Xue07f6c4b2015-01-07 13:41:58 +0800473static void tipc_sk_callback(struct rcu_head *head)
474{
475 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
476
477 sock_put(&tsk->sk);
478}
479
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100480/* Caller should hold socket lock for the socket. */
481static void __tipc_shutdown(struct socket *sock, int error)
482{
483 struct sock *sk = sock->sk;
484 struct tipc_sock *tsk = tipc_sk(sk);
485 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500486 long timeout = CONN_TIMEOUT_DEFAULT;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100487 u32 dnode = tsk_peer_node(tsk);
488 struct sk_buff *skb;
489
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500490 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
491 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
492 !tsk_conn_cong(tsk)));
493
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100494 /* Reject all unreceived messages, except on an active connection
495 * (which disconnects locally & sends a 'FIN+' to peer).
496 */
497 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
498 if (TIPC_SKB_CB(skb)->bytes_read) {
499 kfree_skb(skb);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500500 continue;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100501 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500502 if (!tipc_sk_type_connectionless(sk) &&
503 sk->sk_state != TIPC_DISCONNECTING) {
504 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
505 tipc_node_remove_conn(net, dnode, tsk->portid);
506 }
507 tipc_sk_respond(sk, skb, error);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100508 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500509
510 if (tipc_sk_type_connectionless(sk))
511 return;
512
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100513 if (sk->sk_state != TIPC_DISCONNECTING) {
514 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
515 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
516 tsk_own_node(tsk), tsk_peer_port(tsk),
517 tsk->portid, error);
518 if (skb)
519 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500520 tipc_node_remove_conn(net, dnode, tsk->portid);
521 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100522 }
523}
524
Ying Xuec5fa7b32013-06-17 10:54:39 -0400525/**
Ying Xue247f0f32014-02-18 16:06:46 +0800526 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 * @sock: socket to destroy
528 *
529 * This routine cleans up any messages that are still queued on the socket.
530 * For DGRAM and RDM socket types, all queued messages are rejected.
531 * For SEQPACKET and STREAM socket types, the first message is rejected
532 * and any others are discarded. (If the first message on a STREAM socket
533 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900534 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 * NOTE: Rejected messages are not necessarily returned to the sender! They
536 * are returned or discarded according to the "destination droppable" setting
537 * specified for the message by the sender.
538 *
539 * Returns 0 on success, errno otherwise
540 */
Ying Xue247f0f32014-02-18 16:06:46 +0800541static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400544 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545
Allan Stephens0c3141e2008-04-15 00:22:02 -0700546 /*
547 * Exit if socket isn't fully initialized (occurs when a failed accept()
548 * releases a pre-allocated child socket that was never used)
549 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700550 if (sk == NULL)
551 return 0;
552
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400553 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700554 lock_sock(sk);
555
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100556 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
557 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400558 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800559 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800560 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561
Allan Stephens0c3141e2008-04-15 00:22:02 -0700562 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700563 release_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500564 u32_list_purge(&tsk->cong_links);
565 tsk->cong_link_cnt = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800566 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700567 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200569 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570}
571
572/**
Ying Xue247f0f32014-02-18 16:06:46 +0800573 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 * @sock: socket structure
575 * @uaddr: socket address describing name(s) and desired operation
576 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900577 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 * Name and name sequence binding is indicated using a positive scope value;
579 * a negative scope value unbinds the specified name. Specifying no name
580 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700583 *
584 * NOTE: This routine doesn't need to take the socket lock since it doesn't
585 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 */
Ying Xue247f0f32014-02-18 16:06:46 +0800587static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
588 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589{
Ying Xue84602762013-12-27 10:18:28 +0800590 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400592 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800593 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594
Ying Xue84602762013-12-27 10:18:28 +0800595 lock_sock(sk);
596 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400597 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800598 goto exit;
599 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900600
Ying Xue84602762013-12-27 10:18:28 +0800601 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
602 res = -EINVAL;
603 goto exit;
604 }
605 if (addr->family != AF_TIPC) {
606 res = -EAFNOSUPPORT;
607 goto exit;
608 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 if (addr->addrtype == TIPC_ADDR_NAME)
611 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800612 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
613 res = -EAFNOSUPPORT;
614 goto exit;
615 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900616
Ying Xue13a2e892013-06-17 10:54:40 -0400617 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400618 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800619 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
620 res = -EACCES;
621 goto exit;
622 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400623
Ying Xue84602762013-12-27 10:18:28 +0800624 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400625 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
626 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800627exit:
628 release_sock(sk);
629 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630}
631
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900632/**
Ying Xue247f0f32014-02-18 16:06:46 +0800633 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 * @sock: socket structure
635 * @uaddr: area for returned socket address
636 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700637 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900638 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700640 *
Allan Stephens2da59912008-07-14 22:43:32 -0700641 * NOTE: This routine doesn't need to take the socket lock since it only
642 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000643 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 */
Ying Xue247f0f32014-02-18 16:06:46 +0800645static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
646 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100649 struct sock *sk = sock->sk;
650 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +0800651 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000653 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100655 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100656 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700657 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400658 addr->addr.id.ref = tsk_peer_port(tsk);
659 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700660 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800661 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800662 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664
665 *uaddr_len = sizeof(*addr);
666 addr->addrtype = TIPC_ADDR_ID;
667 addr->family = AF_TIPC;
668 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 addr->addr.name.domain = 0;
670
Allan Stephens0c3141e2008-04-15 00:22:02 -0700671 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672}
673
674/**
Ying Xue247f0f32014-02-18 16:06:46 +0800675 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 * @file: file structure associated with the socket
677 * @sock: socket for which to calculate the poll bits
678 * @wait: ???
679 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700680 * Returns pollmask value
681 *
682 * COMMENTARY:
683 * It appears that the usual socket locking mechanisms are not useful here
684 * since the pollmask info is potentially out-of-date the moment this routine
685 * exits. TCP and other protocols seem to rely on higher level poll routines
686 * to handle any preventable race conditions, so TIPC will do the same ...
687 *
Allan Stephensf662c072010-08-17 11:00:06 +0000688 * IMPORTANT: The fact that a read or write operation is indicated does NOT
689 * imply that the operation will succeed, merely that it should be performed
690 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691 */
Ying Xue247f0f32014-02-18 16:06:46 +0800692static unsigned int tipc_poll(struct file *file, struct socket *sock,
693 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694{
Allan Stephens9b674e82008-03-26 16:48:21 -0700695 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400696 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000697 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700698
Ying Xuef288bef2012-08-21 11:16:57 +0800699 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700700
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100701 if (sk->sk_shutdown & RCV_SHUTDOWN)
702 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
703 if (sk->sk_shutdown == SHUTDOWN_MASK)
704 mask |= POLLHUP;
705
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100706 switch (sk->sk_state) {
707 case TIPC_ESTABLISHED:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500708 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000709 mask |= POLLOUT;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100710 /* fall thru' */
711 case TIPC_LISTEN:
712 case TIPC_CONNECTING:
Allan Stephensf662c072010-08-17 11:00:06 +0000713 if (!skb_queue_empty(&sk->sk_receive_queue))
714 mask |= (POLLIN | POLLRDNORM);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100715 break;
716 case TIPC_OPEN:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500717 if (!tsk->cong_link_cnt)
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100718 mask |= POLLOUT;
719 if (tipc_sk_type_connectionless(sk) &&
720 (!skb_queue_empty(&sk->sk_receive_queue)))
721 mask |= (POLLIN | POLLRDNORM);
722 break;
723 case TIPC_DISCONNECTING:
724 mask = (POLLIN | POLLRDNORM | POLLHUP);
725 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000726 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700727
728 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729}
730
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400731/**
732 * tipc_sendmcast - send multicast message
733 * @sock: socket structure
734 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500735 * @msg: message to send
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500736 * @dlen: length of data to send
737 * @timeout: timeout to wait for wakeup
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400738 *
739 * Called from function tipc_sendmsg(), which has done all sanity checks
740 * Returns the number of bytes sent on success, or errno
741 */
742static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500743 struct msghdr *msg, size_t dlen, long timeout)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400744{
745 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500746 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500747 struct tipc_msg *hdr = &tsk->phdr;
Ying Xuef2f98002015-01-09 15:27:05 +0800748 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500749 int mtu = tipc_bcast_get_mtu(net);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500750 struct tipc_mc_method *method = &tsk->mc_method;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500751 u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500752 struct sk_buff_head pkts;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500753 struct tipc_nlist dsts;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400754 int rc;
755
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500756 /* Block or return if any destination link is congested */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500757 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
758 if (unlikely(rc))
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400759 return rc;
760
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500761 /* Lookup destination nodes */
762 tipc_nlist_init(&dsts, tipc_own_addr(net));
763 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
764 seq->upper, domain, &dsts);
765 if (!dsts.local && !dsts.remote)
766 return -EHOSTUNREACH;
767
768 /* Build message header */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500769 msg_set_type(hdr, TIPC_MCAST_MSG);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500770 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500771 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
772 msg_set_destport(hdr, 0);
773 msg_set_destnode(hdr, 0);
774 msg_set_nametype(hdr, seq->type);
775 msg_set_namelower(hdr, seq->lower);
776 msg_set_nameupper(hdr, seq->upper);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400777
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500778 /* Build message as chain of buffers */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500779 skb_queue_head_init(&pkts);
780 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500781
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500782 /* Send message if build was successful */
783 if (unlikely(rc == dlen))
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500784 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500785 &tsk->cong_link_cnt);
786
787 tipc_nlist_purge(&dsts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500788
789 return rc ? rc : dlen;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400790}
791
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500792/**
793 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
794 * @arrvq: queue with arriving messages, to be cloned after destination lookup
795 * @inputq: queue with cloned messages, delivered to socket after dest lookup
796 *
797 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400798 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500799void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
800 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400801{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500802 struct tipc_msg *msg;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500803 struct list_head dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500804 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400805 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500806 struct sk_buff_head tmpq;
807 uint hsz;
808 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500809
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500810 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500811 INIT_LIST_HEAD(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400812
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500813 skb = tipc_skb_peek(arrvq, &inputq->lock);
814 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
815 msg = buf_msg(skb);
816 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400817
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500818 if (in_own_node(net, msg_orignode(msg)))
819 scope = TIPC_NODE_SCOPE;
820
821 /* Create destination port list and message clones: */
822 tipc_nametbl_mc_translate(net,
823 msg_nametype(msg), msg_namelower(msg),
824 msg_nameupper(msg), scope, &dports);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500825 portid = u32_pop(&dports);
826 for (; portid; portid = u32_pop(&dports)) {
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500827 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
828 if (_skb) {
829 msg_set_destport(buf_msg(_skb), portid);
830 __skb_queue_tail(&tmpq, _skb);
831 continue;
832 }
833 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400834 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500835 /* Append to inputq if not already done by other thread */
836 spin_lock_bh(&inputq->lock);
837 if (skb_peek(arrvq) == skb) {
838 skb_queue_splice_tail_init(&tmpq, inputq);
839 kfree_skb(__skb_dequeue(arrvq));
840 }
841 spin_unlock_bh(&inputq->lock);
842 __skb_queue_purge(&tmpq);
843 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400844 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500845 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400846}
847
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900848/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500849 * tipc_sk_proto_rcv - receive a connection mng protocol message
850 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400851 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500852 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400853static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
854 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500855{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400856 struct sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400857 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400858 struct tipc_msg *hdr = buf_msg(skb);
859 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400860 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400861
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500862 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400863 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500864 goto exit;
865
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100866 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500867
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400868 if (mtyp == CONN_PROBE) {
869 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400870 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
871 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400872 return;
873 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400874 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400875 tsk->snt_unacked -= msg_conn_ack(hdr);
876 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
877 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500878 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400879 sk->sk_write_space(sk);
880 } else if (mtyp != CONN_PROBE_REPLY) {
881 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500882 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500883exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400884 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500885}
886
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500887/**
Ying Xue247f0f32014-02-18 16:06:46 +0800888 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 * @sock: socket structure
890 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500891 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900892 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900894 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
896 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900897 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 * Returns the number of bytes sent on success, or errno otherwise
899 */
Ying Xue1b784142015-03-02 15:37:48 +0800900static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500901 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902{
Ying Xue39a02952015-03-02 15:37:47 +0800903 struct sock *sk = sock->sk;
904 int ret;
905
906 lock_sock(sk);
907 ret = __tipc_sendmsg(sock, m, dsz);
908 release_sock(sk);
909
910 return ret;
911}
912
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500913static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +0800914{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700915 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800916 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500917 struct tipc_sock *tsk = tipc_sk(sk);
918 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
919 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
920 struct list_head *clinks = &tsk->cong_links;
921 bool syn = !tipc_sk_type_connectionless(sk);
922 struct tipc_msg *hdr = &tsk->phdr;
Erik Hugnef2f80362015-03-19 09:02:19 +0100923 struct tipc_name_seq *seq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500924 struct sk_buff_head pkts;
925 u32 type, inst, domain;
926 u32 dnode, dport;
927 int mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500929 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400930 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500931
Erik Hugnef2f80362015-03-19 09:02:19 +0100932 if (unlikely(!dest)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500933 dest = &tsk->peer;
934 if (!syn || dest->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +0100935 return -EDESTADDRREQ;
Erik Hugnef2f80362015-03-19 09:02:19 +0100936 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500937
938 if (unlikely(m->msg_namelen < sizeof(*dest)))
939 return -EINVAL;
940
941 if (unlikely(dest->family != AF_TIPC))
942 return -EINVAL;
943
944 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100945 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a02952015-03-02 15:37:47 +0800946 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100947 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a02952015-03-02 15:37:47 +0800948 return -EISCONN;
949 if (tsk->published)
950 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700951 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400952 tsk->conn_type = dest->addr.name.name.type;
953 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700954 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500956
Erik Hugnef2f80362015-03-19 09:02:19 +0100957 seq = &dest->addr.nameseq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500958 if (dest->addrtype == TIPC_ADDR_MCAST)
959 return tipc_sendmcast(sock, seq, m, dlen, timeout);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500960
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500961 if (dest->addrtype == TIPC_ADDR_NAME) {
962 type = dest->addr.name.name.type;
963 inst = dest->addr.name.name.instance;
964 domain = dest->addr.name.domain;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500965 dnode = domain;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500966 msg_set_type(hdr, TIPC_NAMED_MSG);
967 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
968 msg_set_nametype(hdr, type);
969 msg_set_nameinst(hdr, inst);
970 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800971 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500972 msg_set_destnode(hdr, dnode);
973 msg_set_destport(hdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800974 if (unlikely(!dport && !dnode))
975 return -EHOSTUNREACH;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500976
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500977 } else if (dest->addrtype == TIPC_ADDR_ID) {
978 dnode = dest->addr.id.node;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500979 msg_set_type(hdr, TIPC_DIRECT_MSG);
980 msg_set_lookup_scope(hdr, 0);
981 msg_set_destnode(hdr, dnode);
982 msg_set_destport(hdr, dest->addr.id.ref);
983 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500984 }
985
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500986 /* Block or return if destination link is congested */
987 rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode));
988 if (unlikely(rc))
Ying Xue39a02952015-03-02 15:37:47 +0800989 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500990
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500991 skb_queue_head_init(&pkts);
992 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
993 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
994 if (unlikely(rc != dlen))
995 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500996
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500997 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
998 if (unlikely(rc == -ELINKCONG)) {
999 u32_push(clinks, dnode);
1000 tsk->cong_link_cnt++;
1001 rc = 0;
1002 }
1003
1004 if (unlikely(syn && !rc))
1005 tipc_set_sk_state(sk, TIPC_CONNECTING);
1006
1007 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008}
1009
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001010/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001011 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 * @sock: socket structure
1013 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001014 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001015 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001017 *
1018 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001019 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001021static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001023 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +08001024 int ret;
1025
1026 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001027 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a02952015-03-02 15:37:47 +08001028 release_sock(sk);
1029
1030 return ret;
1031}
1032
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001033static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +08001034{
1035 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001036 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001037 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1038 struct tipc_sock *tsk = tipc_sk(sk);
1039 struct tipc_msg *hdr = &tsk->phdr;
1040 struct net *net = sock_net(sk);
1041 struct sk_buff_head pkts;
1042 u32 dnode = tsk_peer_node(tsk);
1043 int send, sent = 0;
1044 int rc = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001045
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001046 skb_queue_head_init(&pkts);
1047
1048 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001049 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001050
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001051 /* Handle implicit connection setup */
1052 if (unlikely(dest)) {
1053 rc = __tipc_sendmsg(sock, m, dlen);
1054 if (dlen && (dlen == rc))
1055 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1056 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001057 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001059 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001060 rc = tipc_wait_for_cond(sock, &timeout,
1061 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001062 !tsk_conn_cong(tsk) &&
1063 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001064 if (unlikely(rc))
1065 break;
Ying Xue39a02952015-03-02 15:37:47 +08001066
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001067 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1068 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1069 if (unlikely(rc != send))
1070 break;
1071
1072 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1073 if (unlikely(rc == -ELINKCONG)) {
1074 tsk->cong_link_cnt = 1;
1075 rc = 0;
1076 }
1077 if (likely(!rc)) {
1078 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1079 sent += send;
1080 }
1081 } while (sent < dlen && !rc);
1082
1083 return rc ? rc : sent;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001084}
1085
1086/**
1087 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001088 * @sock: socket structure
1089 * @m: message to send
1090 * @dsz: length of data to be transmitted
1091 *
1092 * Used for SOCK_SEQPACKET messages.
1093 *
1094 * Returns the number of bytes sent on success, or errno otherwise
1095 */
Ying Xue1b784142015-03-02 15:37:48 +08001096static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001097{
1098 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1099 return -EMSGSIZE;
1100
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001101 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001102}
1103
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001104/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001106static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001107 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108{
Ying Xue3721e9c2015-01-13 17:07:48 +08001109 struct sock *sk = &tsk->sk;
1110 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001111 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001113 msg_set_destnode(msg, peer_node);
1114 msg_set_destport(msg, peer_port);
1115 msg_set_type(msg, TIPC_CONN_MSG);
1116 msg_set_lookup_scope(msg, 0);
1117 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001118
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01001119 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001120 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001121 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1122 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001123 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001124 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1125 return;
1126
1127 /* Fall back to message based flow control */
1128 tsk->rcv_win = FLOWCTL_MSG_WIN;
1129 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130}
1131
1132/**
1133 * set_orig_addr - capture sender's address for received message
1134 * @m: descriptor for message info
1135 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001136 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 * Note: Address is not captured if not requested by receiver.
1138 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001139static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001141 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001143 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 addr->family = AF_TIPC;
1145 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001146 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 addr->addr.id.ref = msg_origport(msg);
1148 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001149 addr->addr.name.domain = 0; /* could leave uninitialized */
1150 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 m->msg_namelen = sizeof(struct sockaddr_tipc);
1152 }
1153}
1154
1155/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001156 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 * @m: descriptor for message info
1158 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001159 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001160 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001162 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 * Returns 0 if successful, otherwise errno
1164 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001165static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1166 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167{
1168 u32 anc_data[3];
1169 u32 err;
1170 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001171 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 int res;
1173
1174 if (likely(m->msg_controllen == 0))
1175 return 0;
1176
1177 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 err = msg ? msg_errcode(msg) : 0;
1179 if (unlikely(err)) {
1180 anc_data[0] = err;
1181 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001182 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1183 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001185 if (anc_data[1]) {
1186 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1187 msg_data(msg));
1188 if (res)
1189 return res;
1190 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 }
1192
1193 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1195 switch (dest_type) {
1196 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001197 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 anc_data[0] = msg_nametype(msg);
1199 anc_data[1] = msg_namelower(msg);
1200 anc_data[2] = msg_namelower(msg);
1201 break;
1202 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001203 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 anc_data[0] = msg_nametype(msg);
1205 anc_data[1] = msg_namelower(msg);
1206 anc_data[2] = msg_nameupper(msg);
1207 break;
1208 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001209 has_name = (tsk->conn_type != 0);
1210 anc_data[0] = tsk->conn_type;
1211 anc_data[1] = tsk->conn_instance;
1212 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 break;
1214 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001215 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 }
Allan Stephens2db99832010-12-31 18:59:33 +00001217 if (has_name) {
1218 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1219 if (res)
1220 return res;
1221 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222
1223 return 0;
1224}
1225
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001226static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001227{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001228 struct sock *sk = &tsk->sk;
1229 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001230 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001231 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001232 u32 peer_port = tsk_peer_port(tsk);
1233 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001234
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001235 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001236 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001237 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1238 dnode, tsk_own_node(tsk), peer_port,
1239 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001240 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001241 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001242 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001243 msg_set_conn_ack(msg, tsk->rcv_unacked);
1244 tsk->rcv_unacked = 0;
1245
1246 /* Adjust to and advertize the correct window limit */
1247 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1248 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1249 msg_set_adv_win(msg, tsk->rcv_win);
1250 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001251 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001252}
1253
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001254static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001255{
1256 struct sock *sk = sock->sk;
1257 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001258 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001259 int err;
1260
1261 for (;;) {
1262 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001263 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001264 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001265 err = -ENOTCONN;
1266 break;
1267 }
1268 release_sock(sk);
1269 timeo = schedule_timeout(timeo);
1270 lock_sock(sk);
1271 }
1272 err = 0;
1273 if (!skb_queue_empty(&sk->sk_receive_queue))
1274 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001275 err = -EAGAIN;
1276 if (!timeo)
1277 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001278 err = sock_intr_errno(timeo);
1279 if (signal_pending(current))
1280 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001281 }
1282 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001283 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001284 return err;
1285}
1286
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001287/**
Ying Xue247f0f32014-02-18 16:06:46 +08001288 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289 * @m: descriptor for message info
1290 * @buf_len: total size of user buffer area
1291 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001292 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1294 * If the complete message doesn't fit in user area, truncate it.
1295 *
1296 * Returns size of returned message data, errno otherwise
1297 */
Ying Xue1b784142015-03-02 15:37:48 +08001298static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1299 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001301 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001302 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 struct sk_buff *buf;
1304 struct tipc_msg *msg;
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001305 bool is_connectionless = tipc_sk_type_connectionless(sk);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001306 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 unsigned int sz;
1308 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001309 int res, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310
Allan Stephens0c3141e2008-04-15 00:22:02 -07001311 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 if (unlikely(!buf_len))
1313 return -EINVAL;
1314
Allan Stephens0c3141e2008-04-15 00:22:02 -07001315 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001317 if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 res = -ENOTCONN;
1319 goto exit;
1320 }
1321
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001322 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001323restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324
Allan Stephens0c3141e2008-04-15 00:22:02 -07001325 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001326 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001327 if (res)
1328 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001329
1330 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001331 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 msg = buf_msg(buf);
1333 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001334 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 err = msg_errcode(msg);
1336
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001339 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 goto restart;
1341 }
1342
1343 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 set_orig_addr(m, msg);
1345
1346 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001347 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 goto exit;
1350
1351 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 if (!err) {
1353 if (unlikely(buf_len < sz)) {
1354 sz = buf_len;
1355 m->msg_flags |= MSG_TRUNC;
1356 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001357 res = skb_copy_datagram_msg(buf, hlen, m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001358 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 res = sz;
1361 } else {
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001362 if (is_connectionless || err == TIPC_CONN_SHUTDOWN ||
1363 m->msg_control)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 res = 0;
1365 else
1366 res = -ECONNRESET;
1367 }
1368
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001369 if (unlikely(flags & MSG_PEEK))
1370 goto exit;
1371
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001372 if (likely(!is_connectionless)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001373 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1374 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1375 tipc_sk_send_ack(tsk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001376 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001377 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001379 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 return res;
1381}
1382
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001383/**
Ying Xue247f0f32014-02-18 16:06:46 +08001384 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 * @m: descriptor for message info
1386 * @buf_len: total size of user buffer area
1387 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001388 *
1389 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 * will optionally wait for more; never truncates data.
1391 *
1392 * Returns size of returned message data, errno otherwise
1393 */
Ying Xue1b784142015-03-02 15:37:48 +08001394static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1395 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001397 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001398 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399 struct sk_buff *buf;
1400 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001401 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 unsigned int sz;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001403 int target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001406 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407
1408 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409 if (unlikely(!buf_len))
1410 return -EINVAL;
1411
Allan Stephens0c3141e2008-04-15 00:22:02 -07001412 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001414 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415 res = -ENOTCONN;
1416 goto exit;
1417 }
1418
Florian Westphal3720d402010-08-17 11:00:04 +00001419 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001420 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001421
Allan Stephens0c3141e2008-04-15 00:22:02 -07001422restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001423 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001424 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001425 if (res)
1426 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001427
1428 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430 msg = buf_msg(buf);
1431 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001432 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 err = msg_errcode(msg);
1434
1435 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001437 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 goto restart;
1439 }
1440
1441 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 if (sz_copied == 0) {
1443 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001444 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001445 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 goto exit;
1447 }
1448
1449 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450 if (!err) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001451 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1452 u32 needed;
1453 int sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454
Allan Stephens0232fd02011-02-21 09:45:40 -05001455 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456 needed = (buf_len - sz_copied);
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001457 sz_to_copy = min(sz, needed);
Allan Stephens0232fd02011-02-21 09:45:40 -05001458
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001459 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001460 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001462
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 sz_copied += sz_to_copy;
1464
1465 if (sz_to_copy < sz) {
1466 if (!(flags & MSG_PEEK))
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001467 TIPC_SKB_CB(buf)->bytes_read =
1468 offset + sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469 goto exit;
1470 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 } else {
1472 if (sz_copied != 0)
1473 goto exit; /* can't add error msg to valid data */
1474
1475 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1476 res = 0;
1477 else
1478 res = -ECONNRESET;
1479 }
1480
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001481 if (unlikely(flags & MSG_PEEK))
1482 goto exit;
1483
1484 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1485 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1486 tipc_sk_send_ack(tsk);
1487 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488
1489 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001490 if ((sz_copied < buf_len) && /* didn't get all requested data */
1491 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001492 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001493 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 goto restart;
1495
1496exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001498 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499}
1500
1501/**
Ying Xuef288bef2012-08-21 11:16:57 +08001502 * tipc_write_space - wake up thread if port congestion is released
1503 * @sk: socket
1504 */
1505static void tipc_write_space(struct sock *sk)
1506{
1507 struct socket_wq *wq;
1508
1509 rcu_read_lock();
1510 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001511 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001512 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1513 POLLWRNORM | POLLWRBAND);
1514 rcu_read_unlock();
1515}
1516
1517/**
1518 * tipc_data_ready - wake up threads to indicate messages have been received
1519 * @sk: socket
1520 * @len: the length of messages
1521 */
David S. Miller676d2362014-04-11 16:15:36 -04001522static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001523{
1524 struct socket_wq *wq;
1525
1526 rcu_read_lock();
1527 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001528 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001529 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1530 POLLRDNORM | POLLRDBAND);
1531 rcu_read_unlock();
1532}
1533
Ying Xuef4195d12015-11-22 15:46:05 +08001534static void tipc_sock_destruct(struct sock *sk)
1535{
1536 __skb_queue_purge(&sk->sk_receive_queue);
1537}
1538
Ying Xuef288bef2012-08-21 11:16:57 +08001539/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001540 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001541 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001542 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001543 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001544 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001545 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001546static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001547{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001548 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001549 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001550 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001551
Jon Paul Maloycda36962015-07-22 10:11:20 -04001552 if (unlikely(msg_mcast(hdr)))
1553 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001554
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001555 switch (sk->sk_state) {
1556 case TIPC_CONNECTING:
Ying Xue7e6c1312012-11-29 18:39:14 -05001557 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001558 if (unlikely(!msg_connected(hdr)))
1559 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001560
Jon Paul Maloycda36962015-07-22 10:11:20 -04001561 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001562 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugne2c8d8512013-08-28 09:29:58 +02001563 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001564 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001565 }
1566
Jon Paul Maloycda36962015-07-22 10:11:20 -04001567 if (unlikely(!msg_isdata(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001568 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001569 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001570 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001571 }
1572
Jon Paul Maloycda36962015-07-22 10:11:20 -04001573 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1574 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001575
Jon Paul Maloycda36962015-07-22 10:11:20 -04001576 /* If 'ACK+' message, add to socket receive queue */
1577 if (msg_data_sz(hdr))
1578 return true;
1579
1580 /* If empty 'ACK-' message, wake up sleeping connect() */
1581 if (waitqueue_active(sk_sleep(sk)))
1582 wake_up_interruptible(sk_sleep(sk));
1583
1584 /* 'ACK-' message is neither accepted nor rejected: */
1585 msg_set_dest_droppable(hdr, 1);
1586 return false;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001587
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001588 case TIPC_OPEN:
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001589 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001590 break;
1591 case TIPC_LISTEN:
Ying Xue7e6c1312012-11-29 18:39:14 -05001592 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001593 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1594 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001595 break;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001596 case TIPC_ESTABLISHED:
1597 /* Accept only connection-based messages sent by peer */
1598 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1599 return false;
1600
1601 if (unlikely(msg_errcode(hdr))) {
1602 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1603 /* Let timer expire on it's own */
1604 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1605 tsk->portid);
1606 sk->sk_state_change(sk);
1607 }
1608 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001609 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001610 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05001611 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001612
Jon Paul Maloycda36962015-07-22 10:11:20 -04001613 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001614}
1615
1616/**
Ying Xueaba79f32013-01-20 23:30:09 +01001617 * rcvbuf_limit - get proper overload limit of socket receive queue
1618 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001619 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001620 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001621 * For connection oriented messages, irrespective of importance,
1622 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001623 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001624 * For connectionless messages, queue limits are based on message
1625 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001626 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001627 * TIPC_LOW_IMPORTANCE (2 MB)
1628 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1629 * TIPC_HIGH_IMPORTANCE (8 MB)
1630 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001631 *
1632 * Returns overload limit according to corresponding message importance
1633 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001634static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001635{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001636 struct tipc_sock *tsk = tipc_sk(sk);
1637 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001638
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001639 if (unlikely(!msg_connected(hdr)))
1640 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001641
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001642 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1643 return sk->sk_rcvbuf;
1644
1645 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001646}
1647
1648/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001649 * filter_rcv - validate incoming message
1650 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001651 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001652 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001653 * Enqueues message on receive queue if acceptable; optionally handles
1654 * disconnect indication for a connected socket.
1655 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001656 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001657 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001658 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001660static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1661 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001663 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001664 struct tipc_msg *hdr = buf_msg(skb);
1665 unsigned int limit = rcvbuf_limit(sk, skb);
1666 int err = TIPC_OK;
1667 int usr = msg_user(hdr);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001668 u32 onode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669
Jon Paul Maloycda36962015-07-22 10:11:20 -04001670 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001671 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001672 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001673 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001674
Jon Paul Maloycda36962015-07-22 10:11:20 -04001675 if (unlikely(usr == SOCK_WAKEUP)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001676 onode = msg_orignode(hdr);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001677 kfree_skb(skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001678 u32_del(&tsk->cong_links, onode);
1679 tsk->cong_link_cnt--;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001680 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001681 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001682 }
1683
Jon Paul Maloycda36962015-07-22 10:11:20 -04001684 /* Drop if illegal message type */
1685 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1686 kfree_skb(skb);
1687 return false;
1688 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689
Jon Paul Maloycda36962015-07-22 10:11:20 -04001690 /* Reject if wrong message type for current socket state */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001691 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001692 if (msg_connected(hdr)) {
1693 err = TIPC_ERR_NO_PORT;
1694 goto reject;
1695 }
1696 } else if (unlikely(!filter_connect(tsk, skb))) {
1697 err = TIPC_ERR_NO_PORT;
1698 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 }
1700
1701 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001702 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1703 err = TIPC_ERR_OVERLOAD;
1704 goto reject;
1705 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706
Ying Xueaba79f32013-01-20 23:30:09 +01001707 /* Enqueue message */
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001708 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001709 __skb_queue_tail(&sk->sk_receive_queue, skb);
1710 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001711
David S. Miller676d2362014-04-11 16:15:36 -04001712 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001713 return true;
1714
1715reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001716 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1717 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001718 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719}
1720
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001721/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001722 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001723 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001724 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001726 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001727 *
1728 * Returns 0
1729 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001730static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001732 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001733 struct sk_buff_head xmitq;
1734 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001735
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001736 __skb_queue_head_init(&xmitq);
1737
1738 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001739 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001740 return 0;
1741 }
1742
1743 if (skb_queue_empty(&xmitq))
1744 return 0;
1745
1746 /* Send response/rejected message */
1747 skb = __skb_dequeue(&xmitq);
1748 dnode = msg_destnode(buf_msg(skb));
1749 selector = msg_origport(buf_msg(skb));
1750 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751 return 0;
1752}
1753
1754/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001755 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1756 * inputq and try adding them to socket or backlog queue
1757 * @inputq: list of incoming buffers with potentially different destinations
1758 * @sk: socket where the buffers should be enqueued
1759 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001760 *
1761 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001762 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001763static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001764 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001765{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001766 unsigned long time_limit = jiffies + 2;
1767 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001768 unsigned int lim;
1769 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001770 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001771
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001772 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001773 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001774 return;
1775
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001776 skb = tipc_skb_dequeue(inputq, dport);
1777 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001778 return;
1779
1780 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001781 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001782 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001783 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001784 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001785
1786 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001787 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001788 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001789 atomic_set(dcnt, 0);
1790 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1791 if (likely(!sk_add_backlog(sk, skb, lim)))
1792 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001793
1794 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001795 onode = tipc_own_addr(sock_net(sk));
1796 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1797 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001798 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001799 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001800}
1801
1802/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001803 * tipc_sk_rcv - handle a chain of incoming buffers
1804 * @inputq: buffer list containing the buffers
1805 * Consumes all buffers in list until inputq is empty
1806 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001807 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001808void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001809{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001810 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001811 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001812 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001813 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001814 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001815 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001816
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001817 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001818 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001819 dport = tipc_skb_peek_port(inputq, dport);
1820 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001821
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001822 if (likely(tsk)) {
1823 sk = &tsk->sk;
1824 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001825 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001826 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001827 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001828 /* Send pending response/rejected messages, if any */
1829 while ((skb = __skb_dequeue(&xmitq))) {
1830 dnode = msg_destnode(buf_msg(skb));
1831 tipc_node_xmit_skb(net, skb, dnode, dport);
1832 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001833 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001834 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001835 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001836
1837 /* No destination socket => dequeue skb if still there */
1838 skb = tipc_skb_dequeue(inputq, dport);
1839 if (!skb)
1840 return;
1841
1842 /* Try secondary lookup if unresolved named message */
1843 err = TIPC_ERR_NO_PORT;
1844 if (tipc_msg_lookup_dest(net, skb, &err))
1845 goto xmit;
1846
1847 /* Prepare for message rejection */
1848 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001849 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001850xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001851 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001852 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001853 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001854}
1855
Ying Xue78eb3a52014-01-17 09:50:03 +08001856static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1857{
WANG Congd9dc8b02016-11-11 10:20:50 -08001858 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08001859 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08001860 int done;
1861
1862 do {
1863 int err = sock_error(sk);
1864 if (err)
1865 return err;
1866 if (!*timeo_p)
1867 return -ETIMEDOUT;
1868 if (signal_pending(current))
1869 return sock_intr_errno(*timeo_p);
1870
WANG Congd9dc8b02016-11-11 10:20:50 -08001871 add_wait_queue(sk_sleep(sk), &wait);
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001872 done = sk_wait_event(sk, timeo_p,
WANG Congd9dc8b02016-11-11 10:20:50 -08001873 sk->sk_state != TIPC_CONNECTING, &wait);
1874 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08001875 } while (!done);
1876 return 0;
1877}
1878
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879/**
Ying Xue247f0f32014-02-18 16:06:46 +08001880 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881 * @sock: socket structure
1882 * @dest: socket address for destination port
1883 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001884 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 *
1886 * Returns 0 on success, errno otherwise
1887 */
Ying Xue247f0f32014-02-18 16:06:46 +08001888static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1889 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001891 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001892 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001893 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1894 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001895 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001896 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001897 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898
Allan Stephens0c3141e2008-04-15 00:22:02 -07001899 lock_sock(sk);
1900
Erik Hugnef2f80362015-03-19 09:02:19 +01001901 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001902 if (tipc_sk_type_connectionless(sk)) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001903 if (dst->family == AF_UNSPEC) {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001904 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
Sasha Levin610600c2015-03-23 15:30:00 -04001905 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1906 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001907 } else {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001908 memcpy(&tsk->peer, dest, destlen);
Erik Hugnef2f80362015-03-19 09:02:19 +01001909 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001910 goto exit;
1911 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912
Allan Stephensb89741a2008-04-15 00:20:37 -07001913 /*
1914 * Reject connection attempt using multicast address
1915 *
1916 * Note: send_msg() validates the rest of the address fields,
1917 * so there's no need to do it here
1918 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001919 if (dst->addrtype == TIPC_ADDR_MCAST) {
1920 res = -EINVAL;
1921 goto exit;
1922 }
1923
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001924 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001925
1926 switch (sk->sk_state) {
1927 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05001928 /* Send a 'SYN-' to destination */
1929 m.msg_name = dest;
1930 m.msg_namelen = destlen;
1931
1932 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1933 * indicate send_msg() is never blocked.
1934 */
1935 if (!timeout)
1936 m.msg_flags = MSG_DONTWAIT;
1937
Ying Xue39a02952015-03-02 15:37:47 +08001938 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001939 if ((res < 0) && (res != -EWOULDBLOCK))
1940 goto exit;
1941
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001942 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05001943 * difference is that return value in non-blocking
1944 * case is EINPROGRESS, rather than EALREADY.
1945 */
1946 res = -EINPROGRESS;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001947 /* fall thru' */
1948 case TIPC_CONNECTING:
1949 if (!timeout) {
1950 if (previous == TIPC_CONNECTING)
1951 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08001952 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001953 }
Ying Xue78eb3a52014-01-17 09:50:03 +08001954 timeout = msecs_to_jiffies(timeout);
1955 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1956 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001957 break;
1958 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001959 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001960 break;
1961 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001962 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001963 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001964
Allan Stephens0c3141e2008-04-15 00:22:02 -07001965exit:
1966 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001967 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968}
1969
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001970/**
Ying Xue247f0f32014-02-18 16:06:46 +08001971 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001972 * @sock: socket structure
1973 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001974 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975 * Returns 0 on success, errno otherwise
1976 */
Ying Xue247f0f32014-02-18 16:06:46 +08001977static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001979 struct sock *sk = sock->sk;
1980 int res;
1981
1982 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001983 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001984 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001985
Allan Stephens0c3141e2008-04-15 00:22:02 -07001986 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001987}
1988
Ying Xue6398e232014-01-17 09:50:04 +08001989static int tipc_wait_for_accept(struct socket *sock, long timeo)
1990{
1991 struct sock *sk = sock->sk;
1992 DEFINE_WAIT(wait);
1993 int err;
1994
1995 /* True wake-one mechanism for incoming connections: only
1996 * one process gets woken up, not the 'whole herd'.
1997 * Since we do not 'race & poll' for established sockets
1998 * anymore, the common case will execute the loop only once.
1999 */
2000 for (;;) {
2001 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2002 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002003 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002004 release_sock(sk);
2005 timeo = schedule_timeout(timeo);
2006 lock_sock(sk);
2007 }
2008 err = 0;
2009 if (!skb_queue_empty(&sk->sk_receive_queue))
2010 break;
Ying Xue6398e232014-01-17 09:50:04 +08002011 err = -EAGAIN;
2012 if (!timeo)
2013 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002014 err = sock_intr_errno(timeo);
2015 if (signal_pending(current))
2016 break;
Ying Xue6398e232014-01-17 09:50:04 +08002017 }
2018 finish_wait(sk_sleep(sk), &wait);
2019 return err;
2020}
2021
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002022/**
Ying Xue247f0f32014-02-18 16:06:46 +08002023 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002024 * @sock: listening socket
2025 * @newsock: new socket that is to be connected
2026 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002027 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002028 * Returns 0 on success, errno otherwise
2029 */
Ying Xue247f0f32014-02-18 16:06:46 +08002030static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002032 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002033 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002034 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002035 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002036 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002037 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002038
Allan Stephens0c3141e2008-04-15 00:22:02 -07002039 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002040
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002041 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002042 res = -EINVAL;
2043 goto exit;
2044 }
Ying Xue6398e232014-01-17 09:50:04 +08002045 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2046 res = tipc_wait_for_accept(sock, timeo);
2047 if (res)
2048 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002049
2050 buf = skb_peek(&sk->sk_receive_queue);
2051
Parthasarathy Bhuvaragancb5da842016-11-01 14:02:36 +01002052 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002053 if (res)
2054 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002055 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002056
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002057 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002058 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002059 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002060
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002061 /* we lock on new_sk; but lockdep sees the lock on sk */
2062 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002063
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002064 /*
2065 * Reject any stray messages received by new socket
2066 * before the socket lock was taken (very, very unlikely)
2067 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002068 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002070 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002071 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002073 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002074 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002075 new_tsock->conn_type = msg_nametype(msg);
2076 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002078
2079 /*
2080 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2081 * Respond to 'SYN+' by queuing it on new socket.
2082 */
2083 if (!msg_data_sz(msg)) {
2084 struct msghdr m = {NULL,};
2085
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002086 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002087 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002088 } else {
2089 __skb_dequeue(&sk->sk_receive_queue);
2090 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002091 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002092 }
2093 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002094exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002095 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096 return res;
2097}
2098
2099/**
Ying Xue247f0f32014-02-18 16:06:46 +08002100 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002101 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002102 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 *
2104 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002105 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 * Returns 0 on success, errno otherwise
2107 */
Ying Xue247f0f32014-02-18 16:06:46 +08002108static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002109{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002110 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002111 int res;
2112
Allan Stephense247a8f2008-03-06 15:05:38 -08002113 if (how != SHUT_RDWR)
2114 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115
Allan Stephens0c3141e2008-04-15 00:22:02 -07002116 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002118 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2119 sk->sk_shutdown = SEND_SHUTDOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002120
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002121 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002122 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002123 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002124
2125 /* Wake up anyone sleeping in poll */
2126 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002127 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002128 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002129 res = -ENOTCONN;
2130 }
2131
Allan Stephens0c3141e2008-04-15 00:22:02 -07002132 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002133 return res;
2134}
2135
Ying Xuef2f2a962015-01-09 15:27:02 +08002136static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002137{
Ying Xuef2f2a962015-01-09 15:27:02 +08002138 struct tipc_sock *tsk = (struct tipc_sock *)data;
2139 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002140 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002141 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002142 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002143
Jon Paul Maloy57289012014-08-22 18:09:09 -04002144 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002145 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002146 bh_unlock_sock(sk);
2147 goto exit;
2148 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002149 peer_port = tsk_peer_port(tsk);
2150 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002151
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002152 if (tsk->probe_unacked) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002153 if (!sock_owned_by_user(sk)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002154 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugneb3be5e32015-06-09 17:27:12 +02002155 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2156 tsk_peer_port(tsk));
2157 sk->sk_state_change(sk);
2158 } else {
2159 /* Try again later */
2160 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2161 }
2162
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002163 bh_unlock_sock(sk);
2164 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002165 }
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002166
2167 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2168 INT_H_SIZE, 0, peer_node, own_node,
2169 peer_port, tsk->portid, TIPC_OK);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002170 tsk->probe_unacked = true;
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002171 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002172 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002173 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002174 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002175exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002176 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002177}
2178
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002179static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002180 struct tipc_name_seq const *seq)
2181{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002182 struct sock *sk = &tsk->sk;
2183 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002184 struct publication *publ;
2185 u32 key;
2186
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002187 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002188 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002189 key = tsk->portid + tsk->pub_count + 1;
2190 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002191 return -EADDRINUSE;
2192
Ying Xuef2f98002015-01-09 15:27:05 +08002193 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002194 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002195 if (unlikely(!publ))
2196 return -EINVAL;
2197
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002198 list_add(&publ->pport_list, &tsk->publications);
2199 tsk->pub_count++;
2200 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002201 return 0;
2202}
2203
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002204static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002205 struct tipc_name_seq const *seq)
2206{
Ying Xuef2f98002015-01-09 15:27:05 +08002207 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002208 struct publication *publ;
2209 struct publication *safe;
2210 int rc = -EINVAL;
2211
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002212 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002213 if (seq) {
2214 if (publ->scope != scope)
2215 continue;
2216 if (publ->type != seq->type)
2217 continue;
2218 if (publ->lower != seq->lower)
2219 continue;
2220 if (publ->upper != seq->upper)
2221 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002222 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002223 publ->ref, publ->key);
2224 rc = 0;
2225 break;
2226 }
Ying Xuef2f98002015-01-09 15:27:05 +08002227 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002228 publ->ref, publ->key);
2229 rc = 0;
2230 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002231 if (list_empty(&tsk->publications))
2232 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002233 return rc;
2234}
2235
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002236/* tipc_sk_reinit: set non-zero address in all existing sockets
2237 * when we go from standalone to network mode.
2238 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002239void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002240{
Ying Xuee05b31f2015-01-09 15:27:08 +08002241 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002242 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002243 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002244 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002245
Herbert Xu40f9f432017-02-11 19:26:46 +08002246 rhashtable_walk_enter(&tn->sk_rht, &iter);
2247
2248 do {
2249 tsk = ERR_PTR(rhashtable_walk_start(&iter));
2250 if (tsk)
2251 continue;
2252
2253 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002254 spin_lock_bh(&tsk->sk.sk_lock.slock);
2255 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002256 msg_set_prevnode(msg, tn->own_addr);
2257 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002258 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2259 }
Herbert Xu40f9f432017-02-11 19:26:46 +08002260
2261 rhashtable_walk_stop(&iter);
2262 } while (tsk == ERR_PTR(-EAGAIN));
Ying Xue07f6c4b2015-01-07 13:41:58 +08002263}
2264
Ying Xuee05b31f2015-01-09 15:27:08 +08002265static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002266{
Ying Xuee05b31f2015-01-09 15:27:08 +08002267 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002268 struct tipc_sock *tsk;
2269
2270 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002271 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002272 if (tsk)
2273 sock_hold(&tsk->sk);
2274 rcu_read_unlock();
2275
2276 return tsk;
2277}
2278
2279static int tipc_sk_insert(struct tipc_sock *tsk)
2280{
Ying Xuee05b31f2015-01-09 15:27:08 +08002281 struct sock *sk = &tsk->sk;
2282 struct net *net = sock_net(sk);
2283 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002284 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2285 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2286
2287 while (remaining--) {
2288 portid++;
2289 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2290 portid = TIPC_MIN_PORT;
2291 tsk->portid = portid;
2292 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002293 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2294 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002295 return 0;
2296 sock_put(&tsk->sk);
2297 }
2298
2299 return -1;
2300}
2301
2302static void tipc_sk_remove(struct tipc_sock *tsk)
2303{
2304 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002305 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002306
Herbert Xu6cca72892015-03-20 21:57:05 +11002307 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002308 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2309 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002310 }
2311}
2312
Herbert Xu6cca72892015-03-20 21:57:05 +11002313static const struct rhashtable_params tsk_rht_params = {
2314 .nelem_hint = 192,
2315 .head_offset = offsetof(struct tipc_sock, node),
2316 .key_offset = offsetof(struct tipc_sock, portid),
2317 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002318 .max_size = 1048576,
2319 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002320 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002321};
2322
Ying Xuee05b31f2015-01-09 15:27:08 +08002323int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002324{
Ying Xuee05b31f2015-01-09 15:27:08 +08002325 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002326
Herbert Xu6cca72892015-03-20 21:57:05 +11002327 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002328}
2329
Ying Xuee05b31f2015-01-09 15:27:08 +08002330void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002331{
Ying Xuee05b31f2015-01-09 15:27:08 +08002332 struct tipc_net *tn = net_generic(net, tipc_net_id);
2333
Ying Xue07f6c4b2015-01-07 13:41:58 +08002334 /* Wait for socket readers to complete */
2335 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002336
Ying Xuee05b31f2015-01-09 15:27:08 +08002337 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002338}
2339
2340/**
Ying Xue247f0f32014-02-18 16:06:46 +08002341 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002342 * @sock: socket structure
2343 * @lvl: option level
2344 * @opt: option identifier
2345 * @ov: pointer to new option value
2346 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002347 *
2348 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002349 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002350 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002351 * Returns 0 on success, errno otherwise
2352 */
Ying Xue247f0f32014-02-18 16:06:46 +08002353static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2354 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002355{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002356 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002357 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002358 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03002359 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002360
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002361 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2362 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363 if (lvl != SOL_TIPC)
2364 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002365
2366 switch (opt) {
2367 case TIPC_IMPORTANCE:
2368 case TIPC_SRC_DROPPABLE:
2369 case TIPC_DEST_DROPPABLE:
2370 case TIPC_CONN_TIMEOUT:
2371 if (ol < sizeof(value))
2372 return -EINVAL;
2373 res = get_user(value, (u32 __user *)ov);
2374 if (res)
2375 return res;
2376 break;
2377 default:
2378 if (ov || ol)
2379 return -EINVAL;
2380 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002381
Allan Stephens0c3141e2008-04-15 00:22:02 -07002382 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002383
Per Lidenb97bf3f2006-01-02 19:04:38 +01002384 switch (opt) {
2385 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002386 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387 break;
2388 case TIPC_SRC_DROPPABLE:
2389 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002390 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002391 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002392 res = -ENOPROTOOPT;
2393 break;
2394 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002395 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002396 break;
2397 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002398 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002399 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002400 case TIPC_MCAST_BROADCAST:
2401 tsk->mc_method.rcast = false;
2402 tsk->mc_method.mandatory = true;
2403 break;
2404 case TIPC_MCAST_REPLICAST:
2405 tsk->mc_method.rcast = true;
2406 tsk->mc_method.mandatory = true;
2407 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002408 default:
2409 res = -EINVAL;
2410 }
2411
Allan Stephens0c3141e2008-04-15 00:22:02 -07002412 release_sock(sk);
2413
Per Lidenb97bf3f2006-01-02 19:04:38 +01002414 return res;
2415}
2416
2417/**
Ying Xue247f0f32014-02-18 16:06:46 +08002418 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002419 * @sock: socket structure
2420 * @lvl: option level
2421 * @opt: option identifier
2422 * @ov: receptacle for option value
2423 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002424 *
2425 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002426 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002427 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002428 * Returns 0 on success, errno otherwise
2429 */
Ying Xue247f0f32014-02-18 16:06:46 +08002430static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2431 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002432{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002433 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002434 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002435 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002436 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002437 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002438
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002439 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2440 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002441 if (lvl != SOL_TIPC)
2442 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002443 res = get_user(len, ol);
2444 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002445 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002446
Allan Stephens0c3141e2008-04-15 00:22:02 -07002447 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448
2449 switch (opt) {
2450 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002451 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002452 break;
2453 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002454 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002455 break;
2456 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002457 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002458 break;
2459 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002460 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002461 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002462 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002463 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002464 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002465 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002466 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002467 value = skb_queue_len(&sk->sk_receive_queue);
2468 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002469 default:
2470 res = -EINVAL;
2471 }
2472
Allan Stephens0c3141e2008-04-15 00:22:02 -07002473 release_sock(sk);
2474
Paul Gortmaker25860c32010-12-31 18:59:31 +00002475 if (res)
2476 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002477
Paul Gortmaker25860c32010-12-31 18:59:31 +00002478 if (len < sizeof(value))
2479 return -EINVAL;
2480
2481 if (copy_to_user(ov, &value, sizeof(value)))
2482 return -EFAULT;
2483
2484 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002485}
2486
Ying Xuef2f98002015-01-09 15:27:05 +08002487static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002488{
Ying Xuef2f98002015-01-09 15:27:05 +08002489 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002490 struct tipc_sioc_ln_req lnr;
2491 void __user *argp = (void __user *)arg;
2492
2493 switch (cmd) {
2494 case SIOCGETLINKNAME:
2495 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2496 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002497 if (!tipc_node_get_linkname(sock_net(sk),
2498 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002499 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2500 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2501 return -EFAULT;
2502 return 0;
2503 }
2504 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002505 default:
2506 return -ENOIOCTLCMD;
2507 }
2508}
2509
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002510/* Protocol switches for the various types of TIPC sockets */
2511
Florian Westphalbca65ea2008-02-07 18:18:01 -08002512static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002513 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002514 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002515 .release = tipc_release,
2516 .bind = tipc_bind,
2517 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002518 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002519 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002520 .getname = tipc_getname,
2521 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002522 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002523 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002524 .shutdown = tipc_shutdown,
2525 .setsockopt = tipc_setsockopt,
2526 .getsockopt = tipc_getsockopt,
2527 .sendmsg = tipc_sendmsg,
2528 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002529 .mmap = sock_no_mmap,
2530 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002531};
2532
Florian Westphalbca65ea2008-02-07 18:18:01 -08002533static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002534 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002535 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002536 .release = tipc_release,
2537 .bind = tipc_bind,
2538 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002539 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002540 .accept = tipc_accept,
2541 .getname = tipc_getname,
2542 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002543 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002544 .listen = tipc_listen,
2545 .shutdown = tipc_shutdown,
2546 .setsockopt = tipc_setsockopt,
2547 .getsockopt = tipc_getsockopt,
2548 .sendmsg = tipc_send_packet,
2549 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002550 .mmap = sock_no_mmap,
2551 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002552};
2553
Florian Westphalbca65ea2008-02-07 18:18:01 -08002554static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002555 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002556 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002557 .release = tipc_release,
2558 .bind = tipc_bind,
2559 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002560 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002561 .accept = tipc_accept,
2562 .getname = tipc_getname,
2563 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002564 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002565 .listen = tipc_listen,
2566 .shutdown = tipc_shutdown,
2567 .setsockopt = tipc_setsockopt,
2568 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002569 .sendmsg = tipc_sendstream,
Ying Xue247f0f32014-02-18 16:06:46 +08002570 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002571 .mmap = sock_no_mmap,
2572 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002573};
2574
Florian Westphalbca65ea2008-02-07 18:18:01 -08002575static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002576 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002577 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002578 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002579};
2580
2581static struct proto tipc_proto = {
2582 .name = "TIPC",
2583 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002584 .obj_size = sizeof(struct tipc_sock),
2585 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002586};
2587
2588/**
Per Liden4323add2006-01-18 00:38:21 +01002589 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002590 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002591 * Returns 0 on success, errno otherwise
2592 */
Per Liden4323add2006-01-18 00:38:21 +01002593int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002594{
2595 int res;
2596
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002597 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002598 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002599 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002600 goto out;
2601 }
2602
2603 res = sock_register(&tipc_family_ops);
2604 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002605 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002606 proto_unregister(&tipc_proto);
2607 goto out;
2608 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002609 out:
2610 return res;
2611}
2612
2613/**
Per Liden4323add2006-01-18 00:38:21 +01002614 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002615 */
Per Liden4323add2006-01-18 00:38:21 +01002616void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002617{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002618 sock_unregister(tipc_family_ops.family);
2619 proto_unregister(&tipc_proto);
2620}
Richard Alpe34b78a122014-11-20 10:29:10 +01002621
2622/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002623static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002624{
2625 u32 peer_node;
2626 u32 peer_port;
2627 struct nlattr *nest;
2628
2629 peer_node = tsk_peer_node(tsk);
2630 peer_port = tsk_peer_port(tsk);
2631
2632 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2633
2634 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2635 goto msg_full;
2636 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2637 goto msg_full;
2638
2639 if (tsk->conn_type != 0) {
2640 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2641 goto msg_full;
2642 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2643 goto msg_full;
2644 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2645 goto msg_full;
2646 }
2647 nla_nest_end(skb, nest);
2648
2649 return 0;
2650
2651msg_full:
2652 nla_nest_cancel(skb, nest);
2653
2654 return -EMSGSIZE;
2655}
2656
2657/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002658static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2659 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002660{
2661 int err;
2662 void *hdr;
2663 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002664 struct net *net = sock_net(skb->sk);
2665 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002666 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002667
2668 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002669 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002670 if (!hdr)
2671 goto msg_cancel;
2672
2673 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2674 if (!attrs)
2675 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002676 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002677 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002678 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002679 goto attr_msg_cancel;
2680
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002681 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002682 err = __tipc_nl_add_sk_con(skb, tsk);
2683 if (err)
2684 goto attr_msg_cancel;
2685 } else if (!list_empty(&tsk->publications)) {
2686 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2687 goto attr_msg_cancel;
2688 }
2689 nla_nest_end(skb, attrs);
2690 genlmsg_end(skb, hdr);
2691
2692 return 0;
2693
2694attr_msg_cancel:
2695 nla_nest_cancel(skb, attrs);
2696genlmsg_cancel:
2697 genlmsg_cancel(skb, hdr);
2698msg_cancel:
2699 return -EMSGSIZE;
2700}
2701
2702int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2703{
2704 int err;
2705 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002706 const struct bucket_table *tbl;
2707 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002708 struct net *net = sock_net(skb->sk);
2709 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002710 u32 tbl_id = cb->args[0];
2711 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002712
Ying Xue07f6c4b2015-01-07 13:41:58 +08002713 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002714 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002715 for (; tbl_id < tbl->size; tbl_id++) {
2716 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002717 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002718 if (prev_portid && prev_portid != tsk->portid) {
2719 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2720 continue;
2721 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002722
Richard Alped6e164e2015-01-16 12:30:40 +01002723 err = __tipc_nl_add_sk(skb, cb, tsk);
2724 if (err) {
2725 prev_portid = tsk->portid;
2726 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2727 goto out;
2728 }
2729 prev_portid = 0;
2730 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002731 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002732 }
Richard Alped6e164e2015-01-16 12:30:40 +01002733out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002734 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002735 cb->args[0] = tbl_id;
2736 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002737
2738 return skb->len;
2739}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002740
2741/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002742static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2743 struct netlink_callback *cb,
2744 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002745{
2746 void *hdr;
2747 struct nlattr *attrs;
2748
2749 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002750 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002751 if (!hdr)
2752 goto msg_cancel;
2753
2754 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2755 if (!attrs)
2756 goto genlmsg_cancel;
2757
2758 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2759 goto attr_msg_cancel;
2760 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2761 goto attr_msg_cancel;
2762 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2763 goto attr_msg_cancel;
2764 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2765 goto attr_msg_cancel;
2766
2767 nla_nest_end(skb, attrs);
2768 genlmsg_end(skb, hdr);
2769
2770 return 0;
2771
2772attr_msg_cancel:
2773 nla_nest_cancel(skb, attrs);
2774genlmsg_cancel:
2775 genlmsg_cancel(skb, hdr);
2776msg_cancel:
2777 return -EMSGSIZE;
2778}
2779
2780/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002781static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2782 struct netlink_callback *cb,
2783 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002784{
2785 int err;
2786 struct publication *p;
2787
2788 if (*last_publ) {
2789 list_for_each_entry(p, &tsk->publications, pport_list) {
2790 if (p->key == *last_publ)
2791 break;
2792 }
2793 if (p->key != *last_publ) {
2794 /* We never set seq or call nl_dump_check_consistent()
2795 * this means that setting prev_seq here will cause the
2796 * consistence check to fail in the netlink callback
2797 * handler. Resulting in the last NLMSG_DONE message
2798 * having the NLM_F_DUMP_INTR flag set.
2799 */
2800 cb->prev_seq = 1;
2801 *last_publ = 0;
2802 return -EPIPE;
2803 }
2804 } else {
2805 p = list_first_entry(&tsk->publications, struct publication,
2806 pport_list);
2807 }
2808
2809 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2810 err = __tipc_nl_add_sk_publ(skb, cb, p);
2811 if (err) {
2812 *last_publ = p->key;
2813 return err;
2814 }
2815 }
2816 *last_publ = 0;
2817
2818 return 0;
2819}
2820
2821int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2822{
2823 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002824 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002825 u32 last_publ = cb->args[1];
2826 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002827 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002828 struct tipc_sock *tsk;
2829
Ying Xue07f6c4b2015-01-07 13:41:58 +08002830 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002831 struct nlattr **attrs;
2832 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2833
2834 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2835 if (err)
2836 return err;
2837
Richard Alpe45e093a2016-05-16 11:14:54 +02002838 if (!attrs[TIPC_NLA_SOCK])
2839 return -EINVAL;
2840
Richard Alpe1a1a1432014-11-20 10:29:11 +01002841 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2842 attrs[TIPC_NLA_SOCK],
2843 tipc_nl_sock_policy);
2844 if (err)
2845 return err;
2846
2847 if (!sock[TIPC_NLA_SOCK_REF])
2848 return -EINVAL;
2849
Ying Xue07f6c4b2015-01-07 13:41:58 +08002850 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002851 }
2852
2853 if (done)
2854 return 0;
2855
Ying Xuee05b31f2015-01-09 15:27:08 +08002856 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002857 if (!tsk)
2858 return -EINVAL;
2859
2860 lock_sock(&tsk->sk);
2861 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2862 if (!err)
2863 done = 1;
2864 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002865 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002866
Ying Xue07f6c4b2015-01-07 13:41:58 +08002867 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002868 cb->args[1] = last_publ;
2869 cb->args[2] = done;
2870
2871 return skb->len;
2872}