blob: 0546556d351743fcb7ff314cb9f0f29a670743da [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 Maloy3c724ac2015-02-05 08:36:43 -05004 * Copyright (c) 2001-2007, 2012-2015, 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 SS_LISTENING -1 /* socket is listening */
48#define SS_READY -2 /* socket is connectionless */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080051#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080052#define TIPC_FWD_MSG 1
53#define TIPC_CONN_OK 0
54#define TIPC_CONN_PROBING 1
55#define TIPC_MAX_PORT 0xffffffff
56#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040057
58/**
59 * struct tipc_sock - TIPC socket structure
60 * @sk: socket - interacts with 'port' and with user via the socket API
61 * @connected: non-zero if port is currently connected to a peer port
62 * @conn_type: TIPC type used when connection was established
63 * @conn_instance: TIPC instance used when connection was established
64 * @published: non-zero if port has one or more associated names
65 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080066 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040067 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy301bae52014-08-22 18:09:20 -040068 * @publications: list of publications for port
69 * @pub_count: total # of publications port has made during its lifetime
70 * @probing_state:
Ying Xue2f55c432015-01-09 15:27:00 +080071 * @probing_intv:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040072 * @conn_timeout: the time we can wait for an unresponded setup request
73 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
74 * @link_cong: non-zero if owner must sleep because of link congestion
75 * @sent_unacked: # messages sent by socket, and not yet acked by peer
76 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010077 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080078 * @node: hash table node
79 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040080 */
81struct tipc_sock {
82 struct sock sk;
83 int connected;
84 u32 conn_type;
85 u32 conn_instance;
86 int published;
87 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080088 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040089 struct tipc_msg phdr;
90 struct list_head sock_list;
91 struct list_head publications;
92 u32 pub_count;
93 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080094 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040095 uint conn_timeout;
96 atomic_t dupl_rcvcnt;
97 bool link_cong;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -040098 u16 snt_unacked;
99 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400100 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400101 u16 rcv_unacked;
102 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100103 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800104 struct rhash_head node;
105 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400106};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400108static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400109static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800110static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800111static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800112static int tipc_release(struct socket *sock);
113static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400114static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Ying Xuef2f2a962015-01-09 15:27:02 +0800115static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400116static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400117 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400118static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400119 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800120static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800121static int tipc_sk_insert(struct tipc_sock *tsk);
122static void tipc_sk_remove(struct tipc_sock *tsk);
Ying Xue39a02952015-03-02 15:37:47 +0800123static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
124 size_t dsz);
125static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126
Florian Westphalbca65ea2008-02-07 18:18:01 -0800127static const struct proto_ops packet_ops;
128static const struct proto_ops stream_ops;
129static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130static struct proto tipc_proto;
131
Herbert Xu6cca72892015-03-20 21:57:05 +1100132static const struct rhashtable_params tsk_rht_params;
133
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900134/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700135 * Revised TIPC socket locking policy:
136 *
137 * Most socket operations take the standard socket lock when they start
138 * and hold it until they finish (or until they need to sleep). Acquiring
139 * this lock grants the owner exclusive access to the fields of the socket
140 * data structures, with the exception of the backlog queue. A few socket
141 * operations can be done without taking the socket lock because they only
142 * read socket information that never changes during the life of the socket.
143 *
144 * Socket operations may acquire the lock for the associated TIPC port if they
145 * need to perform an operation on the port. If any routine needs to acquire
146 * both the socket lock and the port lock it must take the socket lock first
147 * to avoid the risk of deadlock.
148 *
149 * The dispatcher handling incoming messages cannot grab the socket lock in
150 * the standard fashion, since invoked it runs at the BH level and cannot block.
151 * Instead, it checks to see if the socket lock is currently owned by someone,
152 * and either handles the message itself or adds it to the socket's backlog
153 * queue; in the latter case the queued message is processed once the process
154 * owning the socket lock releases it.
155 *
156 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
157 * the problem of a blocked socket operation preventing any other operations
158 * from occurring. However, applications must be careful if they have
159 * multiple threads trying to send (or receive) on the same socket, as these
160 * operations might interfere with each other. For example, doing a connect
161 * and a receive at the same time might allow the receive to consume the
162 * ACK message meant for the connect. While additional work could be done
163 * to try and overcome this, it doesn't seem to be worthwhile at the present.
164 *
165 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
166 * that another operation that must be performed in a non-blocking manner is
167 * not delayed for very long because the lock has already been taken.
168 *
169 * NOTE: This code assumes that certain fields of a port/socket pair are
170 * constant over its lifetime; such fields can be examined without taking
171 * the socket lock and/or port lock, and do not need to be re-read even
172 * after resuming processing after waiting. These fields include:
173 * - socket type
174 * - pointer to socket sk structure (aka tipc_sock structure)
175 * - pointer to port structure
176 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500179static u32 tsk_own_node(struct tipc_sock *tsk)
180{
181 return msg_prevnode(&tsk->phdr);
182}
183
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400184static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400185{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400186 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400187}
188
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400190{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400191 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400192}
193
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400194static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400195{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400196 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400197}
198
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400199static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400200{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400201 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400202}
203
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400204static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400205{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400206 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400207}
208
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400209static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400210{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400211 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400212}
213
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400214static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400215{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400216 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400217}
218
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400219static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400220{
221 if (imp > TIPC_CRITICAL_IMPORTANCE)
222 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400223 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400224 return 0;
225}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400226
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400227static struct tipc_sock *tipc_sk(const struct sock *sk)
228{
229 return container_of(sk, struct tipc_sock, sk);
230}
231
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400232static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400233{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400234 return tsk->snt_unacked >= tsk->snd_win;
235}
236
237/* tsk_blocks(): translate a buffer size in bytes to number of
238 * advertisable blocks, taking into account the ratio truesize(len)/len
239 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
240 */
241static u16 tsk_adv_blocks(int len)
242{
243 return len / FLOWCTL_BLK_SZ / 4;
244}
245
246/* tsk_inc(): increment counter for sent or received data
247 * - If block based flow control is not supported by peer we
248 * fall back to message based ditto, incrementing the counter
249 */
250static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
251{
252 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
253 return ((msglen / FLOWCTL_BLK_SZ) + 1);
254 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400255}
256
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400258 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700259 *
260 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400262static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400264 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265}
266
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400267/* tipc_sk_respond() : send response message back to sender
268 */
269static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
270{
271 u32 selector;
272 u32 dnode;
273 u32 onode = tipc_own_addr(sock_net(sk));
274
275 if (!tipc_msg_reverse(onode, &skb, err))
276 return;
277
278 dnode = msg_destnode(buf_msg(skb));
279 selector = msg_origport(buf_msg(skb));
280 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
281}
282
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400284 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700285 *
286 * Caller must hold socket lock
287 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400288static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700289{
Ying Xuea6ca1092014-11-26 11:41:55 +0800290 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700291
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400292 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
293 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700294}
295
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400296/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400297 *
298 * Handles cases where the node's network address has changed from
299 * the default of <0.0.0> to its configured setting.
300 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400301static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400302{
Ying Xue34747532015-01-09 15:27:10 +0800303 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400304 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400305 u32 orig_node;
306 u32 peer_node;
307
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400308 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400309 return false;
310
311 if (unlikely(msg_origport(msg) != peer_port))
312 return false;
313
314 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400315 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400316
317 if (likely(orig_node == peer_node))
318 return true;
319
Ying Xue34747532015-01-09 15:27:10 +0800320 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400321 return true;
322
Ying Xue34747532015-01-09 15:27:10 +0800323 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400324 return true;
325
326 return false;
327}
328
Allan Stephens0c3141e2008-04-15 00:22:02 -0700329/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400330 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700331 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 * @sock: pre-allocated socket structure
333 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800334 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700336 * This routine creates additional data structures used by the TIPC socket,
337 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 *
339 * Returns 0 on success, errno otherwise
340 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400341static int tipc_sk_create(struct net *net, struct socket *sock,
342 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500344 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 const struct proto_ops *ops;
346 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400348 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400349 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350
351 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 if (unlikely(protocol != 0))
353 return -EPROTONOSUPPORT;
354
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 switch (sock->type) {
356 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700357 ops = &stream_ops;
358 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 break;
360 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 ops = &packet_ops;
362 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 break;
364 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700366 ops = &msg_ops;
367 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 break;
Allan Stephens49978652006-06-25 23:47:18 -0700369 default:
Allan Stephens49978652006-06-25 23:47:18 -0700370 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 }
372
Allan Stephens0c3141e2008-04-15 00:22:02 -0700373 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500374 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700375 if (sk == NULL)
376 return -ENOMEM;
377
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400378 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400379 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400380 INIT_LIST_HEAD(&tsk->publications);
381 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500382 tn = net_generic(sock_net(sk), tipc_net_id);
383 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400384 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385
Allan Stephens0c3141e2008-04-15 00:22:02 -0700386 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700387 sock->ops = ops;
388 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800390 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900391 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800392 return -EINVAL;
393 }
394 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800395 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400396 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400397 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800398 sk->sk_data_ready = tipc_data_ready;
399 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800400 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400401 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
402 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700403
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400404 /* Start out with safe limits until we receive an advertised window */
405 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
406 tsk->rcv_win = tsk->snd_win;
407
Allan Stephens0c3141e2008-04-15 00:22:02 -0700408 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400409 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700410 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400411 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700412 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 return 0;
414}
415
Ying Xue07f6c4b2015-01-07 13:41:58 +0800416static void tipc_sk_callback(struct rcu_head *head)
417{
418 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
419
420 sock_put(&tsk->sk);
421}
422
Ying Xuec5fa7b32013-06-17 10:54:39 -0400423/**
Ying Xue247f0f32014-02-18 16:06:46 +0800424 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 * @sock: socket to destroy
426 *
427 * This routine cleans up any messages that are still queued on the socket.
428 * For DGRAM and RDM socket types, all queued messages are rejected.
429 * For SEQPACKET and STREAM socket types, the first message is rejected
430 * and any others are discarded. (If the first message on a STREAM socket
431 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900432 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 * NOTE: Rejected messages are not necessarily returned to the sender! They
434 * are returned or discarded according to the "destination droppable" setting
435 * specified for the message by the sender.
436 *
437 * Returns 0 on success, errno otherwise
438 */
Ying Xue247f0f32014-02-18 16:06:46 +0800439static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 struct sock *sk = sock->sk;
Sasha Levin357c4772015-01-13 12:46:41 -0500442 struct net *net;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400443 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800444 struct sk_buff *skb;
Ying Xue1ea23a22015-05-28 13:19:22 +0800445 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446
Allan Stephens0c3141e2008-04-15 00:22:02 -0700447 /*
448 * Exit if socket isn't fully initialized (occurs when a failed accept()
449 * releases a pre-allocated child socket that was never used)
450 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700451 if (sk == NULL)
452 return 0;
453
Sasha Levin357c4772015-01-13 12:46:41 -0500454 net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400455 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700456 lock_sock(sk);
457
458 /*
459 * Reject all unreceived messages, except on an active connection
460 * (which disconnects locally & sends a 'FIN+' to peer)
461 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400462 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800464 skb = __skb_dequeue(&sk->sk_receive_queue);
465 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 break;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +0100467 if (TIPC_SKB_CB(skb)->bytes_read)
Ying Xuea6ca1092014-11-26 11:41:55 +0800468 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700469 else {
470 if ((sock->state == SS_CONNECTING) ||
471 (sock->state == SS_CONNECTED)) {
472 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400473 tsk->connected = 0;
Ying Xuef2f98002015-01-09 15:27:05 +0800474 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700475 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400476 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700477 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478 }
479
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400480 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800481 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800482 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400483 if (tsk->connected) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500484 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +0800485 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500486 tsk_own_node(tsk), tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800487 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800488 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400489 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Ying Xuef2f98002015-01-09 15:27:05 +0800490 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400491 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492
Allan Stephens0c3141e2008-04-15 00:22:02 -0700493 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700494 sock->state = SS_DISCONNECTING;
495 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800496
497 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700498 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200500 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501}
502
503/**
Ying Xue247f0f32014-02-18 16:06:46 +0800504 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 * @sock: socket structure
506 * @uaddr: socket address describing name(s) and desired operation
507 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900508 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 * Name and name sequence binding is indicated using a positive scope value;
510 * a negative scope value unbinds the specified name. Specifying no name
511 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900512 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700514 *
515 * NOTE: This routine doesn't need to take the socket lock since it doesn't
516 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 */
Ying Xue247f0f32014-02-18 16:06:46 +0800518static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
519 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520{
Ying Xue84602762013-12-27 10:18:28 +0800521 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400523 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800524 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525
Ying Xue84602762013-12-27 10:18:28 +0800526 lock_sock(sk);
527 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400528 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800529 goto exit;
530 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900531
Ying Xue84602762013-12-27 10:18:28 +0800532 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
533 res = -EINVAL;
534 goto exit;
535 }
536 if (addr->family != AF_TIPC) {
537 res = -EAFNOSUPPORT;
538 goto exit;
539 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 if (addr->addrtype == TIPC_ADDR_NAME)
542 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800543 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
544 res = -EAFNOSUPPORT;
545 goto exit;
546 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900547
Ying Xue13a2e892013-06-17 10:54:40 -0400548 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400549 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800550 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
551 res = -EACCES;
552 goto exit;
553 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400554
Ying Xue84602762013-12-27 10:18:28 +0800555 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400556 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
557 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800558exit:
559 release_sock(sk);
560 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561}
562
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900563/**
Ying Xue247f0f32014-02-18 16:06:46 +0800564 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 * @sock: socket structure
566 * @uaddr: area for returned socket address
567 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700568 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900569 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700571 *
Allan Stephens2da59912008-07-14 22:43:32 -0700572 * NOTE: This routine doesn't need to take the socket lock since it only
573 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000574 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 */
Ying Xue247f0f32014-02-18 16:06:46 +0800576static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
577 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400580 struct tipc_sock *tsk = tipc_sk(sock->sk);
Ying Xue34747532015-01-09 15:27:10 +0800581 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000583 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700584 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700585 if ((sock->state != SS_CONNECTED) &&
586 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
587 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400588 addr->addr.id.ref = tsk_peer_port(tsk);
589 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700590 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800591 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800592 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594
595 *uaddr_len = sizeof(*addr);
596 addr->addrtype = TIPC_ADDR_ID;
597 addr->family = AF_TIPC;
598 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 addr->addr.name.domain = 0;
600
Allan Stephens0c3141e2008-04-15 00:22:02 -0700601 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602}
603
604/**
Ying Xue247f0f32014-02-18 16:06:46 +0800605 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 * @file: file structure associated with the socket
607 * @sock: socket for which to calculate the poll bits
608 * @wait: ???
609 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700610 * Returns pollmask value
611 *
612 * COMMENTARY:
613 * It appears that the usual socket locking mechanisms are not useful here
614 * since the pollmask info is potentially out-of-date the moment this routine
615 * exits. TCP and other protocols seem to rely on higher level poll routines
616 * to handle any preventable race conditions, so TIPC will do the same ...
617 *
618 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700619 *
Allan Stephensf662c072010-08-17 11:00:06 +0000620 * socket state flags set
621 * ------------ ---------
622 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200623 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000624 *
625 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
626 * no write flags
627 *
628 * connected POLLIN/POLLRDNORM if data in rx queue
629 * POLLOUT if port is not congested
630 *
631 * disconnecting POLLIN/POLLRDNORM/POLLHUP
632 * no write flags
633 *
634 * listening POLLIN if SYN in rx queue
635 * no write flags
636 *
637 * ready POLLIN/POLLRDNORM if data in rx queue
638 * [connectionless] POLLOUT (since port cannot be congested)
639 *
640 * IMPORTANT: The fact that a read or write operation is indicated does NOT
641 * imply that the operation will succeed, merely that it should be performed
642 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 */
Ying Xue247f0f32014-02-18 16:06:46 +0800644static unsigned int tipc_poll(struct file *file, struct socket *sock,
645 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646{
Allan Stephens9b674e82008-03-26 16:48:21 -0700647 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400648 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000649 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700650
Ying Xuef288bef2012-08-21 11:16:57 +0800651 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700652
Allan Stephensf662c072010-08-17 11:00:06 +0000653 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200654 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500655 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200656 mask |= POLLOUT;
657 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000658 case SS_READY:
659 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400660 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000661 mask |= POLLOUT;
662 /* fall thru' */
663 case SS_CONNECTING:
664 case SS_LISTENING:
665 if (!skb_queue_empty(&sk->sk_receive_queue))
666 mask |= (POLLIN | POLLRDNORM);
667 break;
668 case SS_DISCONNECTING:
669 mask = (POLLIN | POLLRDNORM | POLLHUP);
670 break;
671 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700672
673 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674}
675
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400676/**
677 * tipc_sendmcast - send multicast message
678 * @sock: socket structure
679 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500680 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400681 * @dsz: total length of message data
682 * @timeo: timeout to wait for wakeup
683 *
684 * Called from function tipc_sendmsg(), which has done all sanity checks
685 * Returns the number of bytes sent on success, or errno
686 */
687static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500688 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400689{
690 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500691 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800692 struct net *net = sock_net(sk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500693 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100694 struct sk_buff_head pktchain;
Al Virof25dcc72014-11-28 15:52:29 -0500695 struct iov_iter save = msg->msg_iter;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400696 uint mtu;
697 int rc;
698
Parthasarathy Bhuvaragan7cf87fa2016-11-01 14:02:34 +0100699 if (!timeo && tsk->link_cong)
700 return -ELINKCONG;
701
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400702 msg_set_type(mhdr, TIPC_MCAST_MSG);
703 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
704 msg_set_destport(mhdr, 0);
705 msg_set_destnode(mhdr, 0);
706 msg_set_nametype(mhdr, seq->type);
707 msg_set_namelower(mhdr, seq->lower);
708 msg_set_nameupper(mhdr, seq->upper);
709 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
710
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100711 skb_queue_head_init(&pktchain);
712
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400713new_mtu:
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400714 mtu = tipc_bcast_get_mtu(net);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100715 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400716 if (unlikely(rc < 0))
717 return rc;
718
719 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100720 rc = tipc_bcast_xmit(net, &pktchain);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400721 if (likely(!rc))
722 return dsz;
723
724 if (rc == -ELINKCONG) {
725 tsk->link_cong = 1;
726 rc = tipc_wait_for_sndmsg(sock, &timeo);
727 if (!rc)
728 continue;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400729 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100730 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500731 if (rc == -EMSGSIZE) {
732 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400733 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500734 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400735 break;
736 } while (1);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400737 return rc;
738}
739
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500740/**
741 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
742 * @arrvq: queue with arriving messages, to be cloned after destination lookup
743 * @inputq: queue with cloned messages, delivered to socket after dest lookup
744 *
745 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400746 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500747void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
748 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400749{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500750 struct tipc_msg *msg;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500751 struct tipc_plist dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500752 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400753 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500754 struct sk_buff_head tmpq;
755 uint hsz;
756 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500757
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500758 __skb_queue_head_init(&tmpq);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500759 tipc_plist_init(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400760
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500761 skb = tipc_skb_peek(arrvq, &inputq->lock);
762 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
763 msg = buf_msg(skb);
764 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400765
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500766 if (in_own_node(net, msg_orignode(msg)))
767 scope = TIPC_NODE_SCOPE;
768
769 /* Create destination port list and message clones: */
770 tipc_nametbl_mc_translate(net,
771 msg_nametype(msg), msg_namelower(msg),
772 msg_nameupper(msg), scope, &dports);
773 portid = tipc_plist_pop(&dports);
774 for (; portid; portid = tipc_plist_pop(&dports)) {
775 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
776 if (_skb) {
777 msg_set_destport(buf_msg(_skb), portid);
778 __skb_queue_tail(&tmpq, _skb);
779 continue;
780 }
781 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400782 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500783 /* Append to inputq if not already done by other thread */
784 spin_lock_bh(&inputq->lock);
785 if (skb_peek(arrvq) == skb) {
786 skb_queue_splice_tail_init(&tmpq, inputq);
787 kfree_skb(__skb_dequeue(arrvq));
788 }
789 spin_unlock_bh(&inputq->lock);
790 __skb_queue_purge(&tmpq);
791 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400792 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500793 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400794}
795
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900796/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500797 * tipc_sk_proto_rcv - receive a connection mng protocol message
798 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400799 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500800 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400801static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
802 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500803{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400804 struct sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400805 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400806 struct tipc_msg *hdr = buf_msg(skb);
807 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400808 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400809
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500810 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400811 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500812 goto exit;
813
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400814 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500815
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400816 if (mtyp == CONN_PROBE) {
817 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400818 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
819 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400820 return;
821 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400822 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400823 tsk->snt_unacked -= msg_conn_ack(hdr);
824 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
825 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500826 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400827 sk->sk_write_space(sk);
828 } else if (mtyp != CONN_PROBE_REPLY) {
829 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500830 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500831exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400832 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500833}
834
Ying Xue3f405042014-01-17 09:50:05 +0800835static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
836{
837 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400838 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800839 DEFINE_WAIT(wait);
840 int done;
841
842 do {
843 int err = sock_error(sk);
844 if (err)
845 return err;
846 if (sock->state == SS_DISCONNECTING)
847 return -EPIPE;
848 if (!*timeo_p)
849 return -EAGAIN;
850 if (signal_pending(current))
851 return sock_intr_errno(*timeo_p);
852
853 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500854 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800855 finish_wait(sk_sleep(sk), &wait);
856 } while (!done);
857 return 0;
858}
859
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500860/**
Ying Xue247f0f32014-02-18 16:06:46 +0800861 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 * @sock: socket structure
863 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500864 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900865 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900867 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
869 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900870 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 * Returns the number of bytes sent on success, or errno otherwise
872 */
Ying Xue1b784142015-03-02 15:37:48 +0800873static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500874 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875{
Ying Xue39a02952015-03-02 15:37:47 +0800876 struct sock *sk = sock->sk;
877 int ret;
878
879 lock_sock(sk);
880 ret = __tipc_sendmsg(sock, m, dsz);
881 release_sock(sk);
882
883 return ret;
884}
885
886static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
887{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500888 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700889 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400890 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800891 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400892 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500893 u32 dnode, dport;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100894 struct sk_buff_head pktchain;
Ying Xuea6ca1092014-11-26 11:41:55 +0800895 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100896 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500897 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500898 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800899 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100900 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500902 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400903 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100904 if (unlikely(!dest)) {
905 if (tsk->connected && sock->state == SS_READY)
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100906 dest = &tsk->peer;
Erik Hugnef2f80362015-03-19 09:02:19 +0100907 else
908 return -EDESTADDRREQ;
909 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
910 dest->family != AF_TIPC) {
911 return -EINVAL;
912 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500913 if (unlikely(sock->state != SS_READY)) {
Ying Xue39a02952015-03-02 15:37:47 +0800914 if (sock->state == SS_LISTENING)
915 return -EPIPE;
916 if (sock->state != SS_UNCONNECTED)
917 return -EISCONN;
918 if (tsk->published)
919 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700920 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400921 tsk->conn_type = dest->addr.name.name.type;
922 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700923 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100925 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800926 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500927
928 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a02952015-03-02 15:37:47 +0800929 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500930 } else if (dest->addrtype == TIPC_ADDR_NAME) {
931 u32 type = dest->addr.name.name.type;
932 u32 inst = dest->addr.name.name.instance;
933 u32 domain = dest->addr.name.domain;
934
935 dnode = domain;
936 msg_set_type(mhdr, TIPC_NAMED_MSG);
937 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
938 msg_set_nametype(mhdr, type);
939 msg_set_nameinst(mhdr, inst);
940 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800941 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500942 msg_set_destnode(mhdr, dnode);
943 msg_set_destport(mhdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800944 if (unlikely(!dport && !dnode))
945 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500946 } else if (dest->addrtype == TIPC_ADDR_ID) {
947 dnode = dest->addr.id.node;
948 msg_set_type(mhdr, TIPC_DIRECT_MSG);
949 msg_set_lookup_scope(mhdr, 0);
950 msg_set_destnode(mhdr, dnode);
951 msg_set_destport(mhdr, dest->addr.id.ref);
952 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
953 }
954
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100955 skb_queue_head_init(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500956 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500957new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800958 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100959 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500960 if (rc < 0)
Ying Xue39a02952015-03-02 15:37:47 +0800961 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500962
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900963 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100964 skb = skb_peek(&pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +0800965 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100966 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400967 if (likely(!rc)) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500968 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700969 sock->state = SS_CONNECTING;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400970 return dsz;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900971 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400972 if (rc == -ELINKCONG) {
973 tsk->link_cong = 1;
974 rc = tipc_wait_for_sndmsg(sock, &timeo);
975 if (!rc)
976 continue;
977 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100978 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500979 if (rc == -EMSGSIZE) {
980 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500981 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500982 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400983 break;
984 } while (1);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500985
986 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987}
988
Ying Xue391a6dd2014-01-17 09:50:06 +0800989static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
990{
991 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400992 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800993 DEFINE_WAIT(wait);
994 int done;
995
996 do {
997 int err = sock_error(sk);
998 if (err)
999 return err;
1000 if (sock->state == SS_DISCONNECTING)
1001 return -EPIPE;
1002 else if (sock->state != SS_CONNECTED)
1003 return -ENOTCONN;
1004 if (!*timeo_p)
1005 return -EAGAIN;
1006 if (signal_pending(current))
1007 return sock_intr_errno(*timeo_p);
1008
1009 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1010 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001011 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001012 !tsk_conn_cong(tsk)) ||
1013 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +08001014 finish_wait(sk_sleep(sk), &wait);
1015 } while (!done);
1016 return 0;
1017}
1018
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001019/**
Ying Xue247f0f32014-02-18 16:06:46 +08001020 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 * @sock: socket structure
1022 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001023 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001024 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026 *
1027 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001028 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 */
Ying Xue1b784142015-03-02 15:37:48 +08001030static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001032 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +08001033 int ret;
1034
1035 lock_sock(sk);
1036 ret = __tipc_send_stream(sock, m, dsz);
1037 release_sock(sk);
1038
1039 return ret;
1040}
1041
1042static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1043{
1044 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001045 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001046 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001047 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001048 struct sk_buff_head pktchain;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001049 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001050 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001051 int rc = -EINVAL;
1052 long timeo;
1053 u32 dnode;
1054 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001055 struct iov_iter save;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001056 int hlen = MIN_H_SIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001057
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001058 /* Handle implied connection establishment */
1059 if (unlikely(dest)) {
Ying Xue39a02952015-03-02 15:37:47 +08001060 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001061 hlen = msg_hdr_sz(mhdr);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001062 if (dsz && (dsz == rc))
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001063 tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001064 return rc;
1065 }
1066 if (dsz > (uint)INT_MAX)
1067 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001068
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001069 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001070 if (sock->state == SS_DISCONNECTING)
Ying Xue39a02952015-03-02 15:37:47 +08001071 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001072 else
Ying Xue39a02952015-03-02 15:37:47 +08001073 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001074 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001076 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Parthasarathy Bhuvaragan7cf87fa2016-11-01 14:02:34 +01001077 if (!timeo && tsk->link_cong)
1078 return -ELINKCONG;
1079
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001080 dnode = tsk_peer_node(tsk);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001081 skb_queue_head_init(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001082
1083next:
Al Virof25dcc72014-11-28 15:52:29 -05001084 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001085 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001086 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001087 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001088 if (unlikely(rc < 0))
Ying Xue39a02952015-03-02 15:37:47 +08001089 return rc;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001090
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001091 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001092 if (likely(!tsk_conn_cong(tsk))) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001093 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001094 if (likely(!rc)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001095 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001096 sent += send;
1097 if (sent == dsz)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001098 return dsz;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001099 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001100 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001101 if (rc == -EMSGSIZE) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001102 __skb_queue_purge(&pktchain);
Ying Xuef2f98002015-01-09 15:27:05 +08001103 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1104 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001105 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001106 goto next;
1107 }
1108 if (rc != -ELINKCONG)
1109 break;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001110
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001111 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001113 rc = tipc_wait_for_sndpkt(sock, &timeo);
1114 } while (!rc);
Ying Xue39a02952015-03-02 15:37:47 +08001115
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001116 __skb_queue_purge(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001117 return sent ? sent : rc;
1118}
1119
1120/**
1121 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001122 * @sock: socket structure
1123 * @m: message to send
1124 * @dsz: length of data to be transmitted
1125 *
1126 * Used for SOCK_SEQPACKET messages.
1127 *
1128 * Returns the number of bytes sent on success, or errno otherwise
1129 */
Ying Xue1b784142015-03-02 15:37:48 +08001130static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001131{
1132 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1133 return -EMSGSIZE;
1134
Ying Xue1b784142015-03-02 15:37:48 +08001135 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136}
1137
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001138/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001140static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001141 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142{
Ying Xue3721e9c2015-01-13 17:07:48 +08001143 struct sock *sk = &tsk->sk;
1144 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001145 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001147 msg_set_destnode(msg, peer_node);
1148 msg_set_destport(msg, peer_port);
1149 msg_set_type(msg, TIPC_CONN_MSG);
1150 msg_set_lookup_scope(msg, 0);
1151 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001152
Ying Xue2f55c432015-01-09 15:27:00 +08001153 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001154 tsk->probing_state = TIPC_CONN_OK;
1155 tsk->connected = 1;
Ying Xue3721e9c2015-01-13 17:07:48 +08001156 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001157 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1158 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001159 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001160 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1161 return;
1162
1163 /* Fall back to message based flow control */
1164 tsk->rcv_win = FLOWCTL_MSG_WIN;
1165 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166}
1167
1168/**
1169 * set_orig_addr - capture sender's address for received message
1170 * @m: descriptor for message info
1171 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001172 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 * Note: Address is not captured if not requested by receiver.
1174 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001175static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001177 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001179 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 addr->family = AF_TIPC;
1181 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001182 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 addr->addr.id.ref = msg_origport(msg);
1184 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001185 addr->addr.name.domain = 0; /* could leave uninitialized */
1186 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 m->msg_namelen = sizeof(struct sockaddr_tipc);
1188 }
1189}
1190
1191/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001192 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 * @m: descriptor for message info
1194 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001195 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001196 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001198 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 * Returns 0 if successful, otherwise errno
1200 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001201static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1202 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203{
1204 u32 anc_data[3];
1205 u32 err;
1206 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001207 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 int res;
1209
1210 if (likely(m->msg_controllen == 0))
1211 return 0;
1212
1213 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 err = msg ? msg_errcode(msg) : 0;
1215 if (unlikely(err)) {
1216 anc_data[0] = err;
1217 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001218 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1219 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001221 if (anc_data[1]) {
1222 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1223 msg_data(msg));
1224 if (res)
1225 return res;
1226 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227 }
1228
1229 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1231 switch (dest_type) {
1232 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001233 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 anc_data[0] = msg_nametype(msg);
1235 anc_data[1] = msg_namelower(msg);
1236 anc_data[2] = msg_namelower(msg);
1237 break;
1238 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001239 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 anc_data[0] = msg_nametype(msg);
1241 anc_data[1] = msg_namelower(msg);
1242 anc_data[2] = msg_nameupper(msg);
1243 break;
1244 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001245 has_name = (tsk->conn_type != 0);
1246 anc_data[0] = tsk->conn_type;
1247 anc_data[1] = tsk->conn_instance;
1248 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 break;
1250 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001251 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 }
Allan Stephens2db99832010-12-31 18:59:33 +00001253 if (has_name) {
1254 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1255 if (res)
1256 return res;
1257 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258
1259 return 0;
1260}
1261
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001262static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001263{
Ying Xuef2f98002015-01-09 15:27:05 +08001264 struct net *net = sock_net(&tsk->sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001265 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001266 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001267 u32 peer_port = tsk_peer_port(tsk);
1268 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001269
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001270 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001271 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001272 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1273 dnode, tsk_own_node(tsk), peer_port,
1274 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001275 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001276 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001277 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001278 msg_set_conn_ack(msg, tsk->rcv_unacked);
1279 tsk->rcv_unacked = 0;
1280
1281 /* Adjust to and advertize the correct window limit */
1282 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1283 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1284 msg_set_adv_win(msg, tsk->rcv_win);
1285 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001286 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001287}
1288
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001289static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001290{
1291 struct sock *sk = sock->sk;
1292 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001293 long timeo = *timeop;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001294 int err;
1295
1296 for (;;) {
1297 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001298 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001299 if (sock->state == SS_DISCONNECTING) {
1300 err = -ENOTCONN;
1301 break;
1302 }
1303 release_sock(sk);
1304 timeo = schedule_timeout(timeo);
1305 lock_sock(sk);
1306 }
1307 err = 0;
1308 if (!skb_queue_empty(&sk->sk_receive_queue))
1309 break;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001310 err = -EAGAIN;
1311 if (!timeo)
1312 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001313 err = sock_intr_errno(timeo);
1314 if (signal_pending(current))
1315 break;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001316 }
1317 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001318 *timeop = timeo;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001319 return err;
1320}
1321
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001322/**
Ying Xue247f0f32014-02-18 16:06:46 +08001323 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 * @m: descriptor for message info
1325 * @buf_len: total size of user buffer area
1326 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001327 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1329 * If the complete message doesn't fit in user area, truncate it.
1330 *
1331 * Returns size of returned message data, errno otherwise
1332 */
Ying Xue1b784142015-03-02 15:37:48 +08001333static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1334 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001336 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001337 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 struct sk_buff *buf;
1339 struct tipc_msg *msg;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001340 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 unsigned int sz;
1342 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001343 int res, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344
Allan Stephens0c3141e2008-04-15 00:22:02 -07001345 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 if (unlikely(!buf_len))
1347 return -EINVAL;
1348
Allan Stephens0c3141e2008-04-15 00:22:02 -07001349 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350
Allan Stephens0c3141e2008-04-15 00:22:02 -07001351 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 res = -ENOTCONN;
1353 goto exit;
1354 }
1355
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001356 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001357restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358
Allan Stephens0c3141e2008-04-15 00:22:02 -07001359 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001360 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001361 if (res)
1362 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001363
1364 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001365 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 msg = buf_msg(buf);
1367 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001368 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369 err = msg_errcode(msg);
1370
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001373 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 goto restart;
1375 }
1376
1377 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 set_orig_addr(m, msg);
1379
1380 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001381 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001382 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 goto exit;
1384
1385 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 if (!err) {
1387 if (unlikely(buf_len < sz)) {
1388 sz = buf_len;
1389 m->msg_flags |= MSG_TRUNC;
1390 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001391 res = skb_copy_datagram_msg(buf, hlen, m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001392 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 res = sz;
1395 } else {
1396 if ((sock->state == SS_READY) ||
1397 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1398 res = 0;
1399 else
1400 res = -ECONNRESET;
1401 }
1402
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001403 if (unlikely(flags & MSG_PEEK))
1404 goto exit;
1405
1406 if (likely(sock->state != SS_READY)) {
1407 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1408 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1409 tipc_sk_send_ack(tsk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001410 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001411 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001413 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001414 return res;
1415}
1416
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001417/**
Ying Xue247f0f32014-02-18 16:06:46 +08001418 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419 * @m: descriptor for message info
1420 * @buf_len: total size of user buffer area
1421 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001422 *
1423 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 * will optionally wait for more; never truncates data.
1425 *
1426 * Returns size of returned message data, errno otherwise
1427 */
Ying Xue1b784142015-03-02 15:37:48 +08001428static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1429 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001431 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001432 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 struct sk_buff *buf;
1434 struct tipc_msg *msg;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001435 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 unsigned int sz;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001437 int target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001440 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441
1442 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001443 if (unlikely(!buf_len))
1444 return -EINVAL;
1445
Allan Stephens0c3141e2008-04-15 00:22:02 -07001446 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001448 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 res = -ENOTCONN;
1450 goto exit;
1451 }
1452
Florian Westphal3720d402010-08-17 11:00:04 +00001453 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001454 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001455
Allan Stephens0c3141e2008-04-15 00:22:02 -07001456restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001457 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001458 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001459 if (res)
1460 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001461
1462 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001463 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464 msg = buf_msg(buf);
1465 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001466 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 err = msg_errcode(msg);
1468
1469 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001471 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472 goto restart;
1473 }
1474
1475 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 if (sz_copied == 0) {
1477 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001478 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001479 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480 goto exit;
1481 }
1482
1483 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 if (!err) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001485 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1486 u32 needed;
1487 int sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488
Allan Stephens0232fd02011-02-21 09:45:40 -05001489 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490 needed = (buf_len - sz_copied);
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001491 sz_to_copy = min(sz, needed);
Allan Stephens0232fd02011-02-21 09:45:40 -05001492
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001493 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001494 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001496
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 sz_copied += sz_to_copy;
1498
1499 if (sz_to_copy < sz) {
1500 if (!(flags & MSG_PEEK))
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001501 TIPC_SKB_CB(buf)->bytes_read =
1502 offset + sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503 goto exit;
1504 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505 } else {
1506 if (sz_copied != 0)
1507 goto exit; /* can't add error msg to valid data */
1508
1509 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1510 res = 0;
1511 else
1512 res = -ECONNRESET;
1513 }
1514
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001515 if (unlikely(flags & MSG_PEEK))
1516 goto exit;
1517
1518 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1519 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1520 tipc_sk_send_ack(tsk);
1521 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522
1523 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001524 if ((sz_copied < buf_len) && /* didn't get all requested data */
1525 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001526 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001527 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528 goto restart;
1529
1530exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001531 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001532 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001533}
1534
1535/**
Ying Xuef288bef2012-08-21 11:16:57 +08001536 * tipc_write_space - wake up thread if port congestion is released
1537 * @sk: socket
1538 */
1539static void tipc_write_space(struct sock *sk)
1540{
1541 struct socket_wq *wq;
1542
1543 rcu_read_lock();
1544 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001545 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001546 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1547 POLLWRNORM | POLLWRBAND);
1548 rcu_read_unlock();
1549}
1550
1551/**
1552 * tipc_data_ready - wake up threads to indicate messages have been received
1553 * @sk: socket
1554 * @len: the length of messages
1555 */
David S. Miller676d2362014-04-11 16:15:36 -04001556static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001557{
1558 struct socket_wq *wq;
1559
1560 rcu_read_lock();
1561 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001562 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001563 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1564 POLLRDNORM | POLLRDBAND);
1565 rcu_read_unlock();
1566}
1567
Ying Xuef4195d12015-11-22 15:46:05 +08001568static void tipc_sock_destruct(struct sock *sk)
1569{
1570 __skb_queue_purge(&sk->sk_receive_queue);
1571}
1572
Ying Xuef288bef2012-08-21 11:16:57 +08001573/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001574 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001575 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001576 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001577 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001578 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001579 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001580static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001581{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001582 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001583 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001584 struct socket *sock = sk->sk_socket;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001585 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001586
Jon Paul Maloycda36962015-07-22 10:11:20 -04001587 if (unlikely(msg_mcast(hdr)))
1588 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001589
1590 switch ((int)sock->state) {
1591 case SS_CONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001592
Ying Xue7e6c1312012-11-29 18:39:14 -05001593 /* Accept only connection-based messages sent by peer */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001594 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1595 return false;
1596
1597 if (unlikely(msg_errcode(hdr))) {
1598 sock->state = SS_DISCONNECTING;
1599 tsk->connected = 0;
1600 /* Let timer expire on it's own */
1601 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1602 tsk->portid);
Parthasarathy Bhuvaragan4891d8f2016-11-01 14:02:35 +01001603 sk->sk_state_change(sk);
Ying Xue7e6c1312012-11-29 18:39:14 -05001604 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001605 return true;
1606
Ying Xue7e6c1312012-11-29 18:39:14 -05001607 case SS_CONNECTING:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001608
Ying Xue7e6c1312012-11-29 18:39:14 -05001609 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001610 if (unlikely(!msg_connected(hdr)))
1611 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001612
Jon Paul Maloycda36962015-07-22 10:11:20 -04001613 if (unlikely(msg_errcode(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001614 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001615 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001616 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001617 }
1618
Jon Paul Maloycda36962015-07-22 10:11:20 -04001619 if (unlikely(!msg_isdata(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001620 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001621 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001622 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001623 }
1624
Jon Paul Maloycda36962015-07-22 10:11:20 -04001625 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1626 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001627 sock->state = SS_CONNECTED;
1628
Jon Paul Maloycda36962015-07-22 10:11:20 -04001629 /* If 'ACK+' message, add to socket receive queue */
1630 if (msg_data_sz(hdr))
1631 return true;
1632
1633 /* If empty 'ACK-' message, wake up sleeping connect() */
1634 if (waitqueue_active(sk_sleep(sk)))
1635 wake_up_interruptible(sk_sleep(sk));
1636
1637 /* 'ACK-' message is neither accepted nor rejected: */
1638 msg_set_dest_droppable(hdr, 1);
1639 return false;
1640
Ying Xue7e6c1312012-11-29 18:39:14 -05001641 case SS_LISTENING:
1642 case SS_UNCONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001643
Ying Xue7e6c1312012-11-29 18:39:14 -05001644 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001645 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1646 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001647 break;
1648 case SS_DISCONNECTING:
1649 break;
1650 default:
1651 pr_err("Unknown socket state %u\n", sock->state);
1652 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001653 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001654}
1655
1656/**
Ying Xueaba79f32013-01-20 23:30:09 +01001657 * rcvbuf_limit - get proper overload limit of socket receive queue
1658 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001659 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001660 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001661 * For connection oriented messages, irrespective of importance,
1662 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001663 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001664 * For connectionless messages, queue limits are based on message
1665 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001666 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001667 * TIPC_LOW_IMPORTANCE (2 MB)
1668 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1669 * TIPC_HIGH_IMPORTANCE (8 MB)
1670 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001671 *
1672 * Returns overload limit according to corresponding message importance
1673 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001674static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001675{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001676 struct tipc_sock *tsk = tipc_sk(sk);
1677 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001678
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001679 if (unlikely(!msg_connected(hdr)))
1680 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001681
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001682 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1683 return sk->sk_rcvbuf;
1684
1685 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001686}
1687
1688/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689 * filter_rcv - validate incoming message
1690 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001691 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001692 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001693 * Enqueues message on receive queue if acceptable; optionally handles
1694 * disconnect indication for a connected socket.
1695 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001696 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001697 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001698 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001700static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1701 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001703 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001704 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001705 struct tipc_msg *hdr = buf_msg(skb);
1706 unsigned int limit = rcvbuf_limit(sk, skb);
1707 int err = TIPC_OK;
1708 int usr = msg_user(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709
Jon Paul Maloycda36962015-07-22 10:11:20 -04001710 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001711 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001712 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001713 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001714
Jon Paul Maloycda36962015-07-22 10:11:20 -04001715 if (unlikely(usr == SOCK_WAKEUP)) {
1716 kfree_skb(skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001717 tsk->link_cong = 0;
1718 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001719 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001720 }
1721
Jon Paul Maloycda36962015-07-22 10:11:20 -04001722 /* Drop if illegal message type */
1723 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1724 kfree_skb(skb);
1725 return false;
1726 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001727
Jon Paul Maloycda36962015-07-22 10:11:20 -04001728 /* Reject if wrong message type for current socket state */
1729 if (unlikely(sock->state == SS_READY)) {
1730 if (msg_connected(hdr)) {
1731 err = TIPC_ERR_NO_PORT;
1732 goto reject;
1733 }
1734 } else if (unlikely(!filter_connect(tsk, skb))) {
1735 err = TIPC_ERR_NO_PORT;
1736 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 }
1738
1739 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001740 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1741 err = TIPC_ERR_OVERLOAD;
1742 goto reject;
1743 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744
Ying Xueaba79f32013-01-20 23:30:09 +01001745 /* Enqueue message */
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001746 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001747 __skb_queue_tail(&sk->sk_receive_queue, skb);
1748 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001749
David S. Miller676d2362014-04-11 16:15:36 -04001750 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001751 return true;
1752
1753reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001754 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1755 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001756 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757}
1758
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001759/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001760 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001761 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001762 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001763 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001764 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001765 *
1766 * Returns 0
1767 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001768static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001769{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001770 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001771 struct sk_buff_head xmitq;
1772 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001773
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001774 __skb_queue_head_init(&xmitq);
1775
1776 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001777 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001778 return 0;
1779 }
1780
1781 if (skb_queue_empty(&xmitq))
1782 return 0;
1783
1784 /* Send response/rejected message */
1785 skb = __skb_dequeue(&xmitq);
1786 dnode = msg_destnode(buf_msg(skb));
1787 selector = msg_origport(buf_msg(skb));
1788 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001789 return 0;
1790}
1791
1792/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001793 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1794 * inputq and try adding them to socket or backlog queue
1795 * @inputq: list of incoming buffers with potentially different destinations
1796 * @sk: socket where the buffers should be enqueued
1797 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001798 *
1799 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001800 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001801static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001802 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001803{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001804 unsigned long time_limit = jiffies + 2;
1805 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001806 unsigned int lim;
1807 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001808 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001809
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001810 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001811 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001812 return;
1813
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001814 skb = tipc_skb_dequeue(inputq, dport);
1815 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001816 return;
1817
1818 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001819 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001820 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001821 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001822 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001823
1824 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001825 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001826 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001827 atomic_set(dcnt, 0);
1828 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1829 if (likely(!sk_add_backlog(sk, skb, lim)))
1830 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001831
1832 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001833 onode = tipc_own_addr(sock_net(sk));
1834 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1835 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001836 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001837 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001838}
1839
1840/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001841 * tipc_sk_rcv - handle a chain of incoming buffers
1842 * @inputq: buffer list containing the buffers
1843 * Consumes all buffers in list until inputq is empty
1844 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001845 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001846void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001847{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001848 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001849 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001850 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001851 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001852 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001853 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001854
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001855 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001856 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001857 dport = tipc_skb_peek_port(inputq, dport);
1858 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001859
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001860 if (likely(tsk)) {
1861 sk = &tsk->sk;
1862 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001863 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001864 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001865 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001866 /* Send pending response/rejected messages, if any */
1867 while ((skb = __skb_dequeue(&xmitq))) {
1868 dnode = msg_destnode(buf_msg(skb));
1869 tipc_node_xmit_skb(net, skb, dnode, dport);
1870 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001871 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001872 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001873 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001874
1875 /* No destination socket => dequeue skb if still there */
1876 skb = tipc_skb_dequeue(inputq, dport);
1877 if (!skb)
1878 return;
1879
1880 /* Try secondary lookup if unresolved named message */
1881 err = TIPC_ERR_NO_PORT;
1882 if (tipc_msg_lookup_dest(net, skb, &err))
1883 goto xmit;
1884
1885 /* Prepare for message rejection */
1886 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001887 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001888xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001889 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001890 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001891 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001892}
1893
Ying Xue78eb3a52014-01-17 09:50:03 +08001894static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1895{
1896 struct sock *sk = sock->sk;
1897 DEFINE_WAIT(wait);
1898 int done;
1899
1900 do {
1901 int err = sock_error(sk);
1902 if (err)
1903 return err;
1904 if (!*timeo_p)
1905 return -ETIMEDOUT;
1906 if (signal_pending(current))
1907 return sock_intr_errno(*timeo_p);
1908
1909 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1910 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1911 finish_wait(sk_sleep(sk), &wait);
1912 } while (!done);
1913 return 0;
1914}
1915
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916/**
Ying Xue247f0f32014-02-18 16:06:46 +08001917 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918 * @sock: socket structure
1919 * @dest: socket address for destination port
1920 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001921 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001922 *
1923 * Returns 0 on success, errno otherwise
1924 */
Ying Xue247f0f32014-02-18 16:06:46 +08001925static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1926 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001928 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001929 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001930 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1931 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001932 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Ying Xue78eb3a52014-01-17 09:50:03 +08001933 socket_state previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001934 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001935
Allan Stephens0c3141e2008-04-15 00:22:02 -07001936 lock_sock(sk);
1937
Erik Hugnef2f80362015-03-19 09:02:19 +01001938 /* DGRAM/RDM connect(), just save the destaddr */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001939 if (sock->state == SS_READY) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001940 if (dst->family == AF_UNSPEC) {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001941 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
Erik Hugnef2f80362015-03-19 09:02:19 +01001942 tsk->connected = 0;
Sasha Levin610600c2015-03-23 15:30:00 -04001943 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1944 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001945 } else {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001946 memcpy(&tsk->peer, dest, destlen);
Erik Hugnef2f80362015-03-19 09:02:19 +01001947 tsk->connected = 1;
1948 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001949 goto exit;
1950 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951
Allan Stephensb89741a2008-04-15 00:20:37 -07001952 /*
1953 * Reject connection attempt using multicast address
1954 *
1955 * Note: send_msg() validates the rest of the address fields,
1956 * so there's no need to do it here
1957 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001958 if (dst->addrtype == TIPC_ADDR_MCAST) {
1959 res = -EINVAL;
1960 goto exit;
1961 }
1962
Ying Xue78eb3a52014-01-17 09:50:03 +08001963 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001964 switch (sock->state) {
1965 case SS_UNCONNECTED:
1966 /* Send a 'SYN-' to destination */
1967 m.msg_name = dest;
1968 m.msg_namelen = destlen;
1969
1970 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1971 * indicate send_msg() is never blocked.
1972 */
1973 if (!timeout)
1974 m.msg_flags = MSG_DONTWAIT;
1975
Ying Xue39a02952015-03-02 15:37:47 +08001976 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001977 if ((res < 0) && (res != -EWOULDBLOCK))
1978 goto exit;
1979
1980 /* Just entered SS_CONNECTING state; the only
1981 * difference is that return value in non-blocking
1982 * case is EINPROGRESS, rather than EALREADY.
1983 */
1984 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001985 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001986 if (previous == SS_CONNECTING)
1987 res = -EALREADY;
1988 if (!timeout)
1989 goto exit;
1990 timeout = msecs_to_jiffies(timeout);
1991 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1992 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001993 break;
1994 case SS_CONNECTED:
1995 res = -EISCONN;
1996 break;
1997 default:
1998 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001999 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07002000 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07002001exit:
2002 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002003 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004}
2005
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002006/**
Ying Xue247f0f32014-02-18 16:06:46 +08002007 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01002008 * @sock: socket structure
2009 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002010 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011 * Returns 0 on success, errno otherwise
2012 */
Ying Xue247f0f32014-02-18 16:06:46 +08002013static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002014{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002015 struct sock *sk = sock->sk;
2016 int res;
2017
2018 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002019
Ying Xue245f3d32011-07-06 06:01:13 -04002020 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002021 res = -EINVAL;
2022 else {
2023 sock->state = SS_LISTENING;
2024 res = 0;
2025 }
2026
2027 release_sock(sk);
2028 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002029}
2030
Ying Xue6398e232014-01-17 09:50:04 +08002031static int tipc_wait_for_accept(struct socket *sock, long timeo)
2032{
2033 struct sock *sk = sock->sk;
2034 DEFINE_WAIT(wait);
2035 int err;
2036
2037 /* True wake-one mechanism for incoming connections: only
2038 * one process gets woken up, not the 'whole herd'.
2039 * Since we do not 'race & poll' for established sockets
2040 * anymore, the common case will execute the loop only once.
2041 */
2042 for (;;) {
2043 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2044 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002045 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002046 release_sock(sk);
2047 timeo = schedule_timeout(timeo);
2048 lock_sock(sk);
2049 }
2050 err = 0;
2051 if (!skb_queue_empty(&sk->sk_receive_queue))
2052 break;
2053 err = -EINVAL;
2054 if (sock->state != SS_LISTENING)
2055 break;
Ying Xue6398e232014-01-17 09:50:04 +08002056 err = -EAGAIN;
2057 if (!timeo)
2058 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002059 err = sock_intr_errno(timeo);
2060 if (signal_pending(current))
2061 break;
Ying Xue6398e232014-01-17 09:50:04 +08002062 }
2063 finish_wait(sk_sleep(sk), &wait);
2064 return err;
2065}
2066
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002067/**
Ying Xue247f0f32014-02-18 16:06:46 +08002068 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069 * @sock: listening socket
2070 * @newsock: new socket that is to be connected
2071 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002072 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073 * Returns 0 on success, errno otherwise
2074 */
Ying Xue247f0f32014-02-18 16:06:46 +08002075static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002077 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002079 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002080 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002081 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002082 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083
Allan Stephens0c3141e2008-04-15 00:22:02 -07002084 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002085
Allan Stephens0c3141e2008-04-15 00:22:02 -07002086 if (sock->state != SS_LISTENING) {
2087 res = -EINVAL;
2088 goto exit;
2089 }
Ying Xue6398e232014-01-17 09:50:04 +08002090 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2091 res = tipc_wait_for_accept(sock, timeo);
2092 if (res)
2093 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002094
2095 buf = skb_peek(&sk->sk_receive_queue);
2096
Parthasarathy Bhuvaragancb5da842016-11-01 14:02:36 +01002097 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002098 if (res)
2099 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002100 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002101
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002102 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002103 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002104 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002105
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002106 /* we lock on new_sk; but lockdep sees the lock on sk */
2107 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002108
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002109 /*
2110 * Reject any stray messages received by new socket
2111 * before the socket lock was taken (very, very unlikely)
2112 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002113 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002115 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002116 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002117 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002118
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002119 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002120 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002121 new_tsock->conn_type = msg_nametype(msg);
2122 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002124
2125 /*
2126 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2127 * Respond to 'SYN+' by queuing it on new socket.
2128 */
2129 if (!msg_data_sz(msg)) {
2130 struct msghdr m = {NULL,};
2131
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002132 tsk_advance_rx_queue(sk);
Ying Xue39a02952015-03-02 15:37:47 +08002133 __tipc_send_stream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002134 } else {
2135 __skb_dequeue(&sk->sk_receive_queue);
2136 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002137 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002138 }
2139 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002141 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142 return res;
2143}
2144
2145/**
Ying Xue247f0f32014-02-18 16:06:46 +08002146 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002148 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149 *
2150 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002151 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152 * Returns 0 on success, errno otherwise
2153 */
Ying Xue247f0f32014-02-18 16:06:46 +08002154static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002155{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002156 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002157 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002158 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002159 struct sk_buff *skb;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002160 u32 dnode = tsk_peer_node(tsk);
2161 u32 dport = tsk_peer_port(tsk);
2162 u32 onode = tipc_own_addr(net);
2163 u32 oport = tsk->portid;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002164 int res;
2165
Allan Stephense247a8f2008-03-06 15:05:38 -08002166 if (how != SHUT_RDWR)
2167 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168
Allan Stephens0c3141e2008-04-15 00:22:02 -07002169 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002170
2171 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002172 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002173 case SS_CONNECTED:
2174
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175restart:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002176 dnode = tsk_peer_node(tsk);
2177
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002178 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002179 skb = __skb_dequeue(&sk->sk_receive_queue);
2180 if (skb) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01002181 if (TIPC_SKB_CB(skb)->bytes_read) {
Ying Xuea6ca1092014-11-26 11:41:55 +08002182 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002183 goto restart;
2184 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002185 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002186 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002187 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002188 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloycda36962015-07-22 10:11:20 -04002189 0, dnode, onode, dport, oport,
2190 TIPC_CONN_SHUTDOWN);
Vegard Nossumd2fbdf72016-07-23 08:15:04 +02002191 if (skb)
2192 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002193 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002194 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002195 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002196 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002197 /* fall through */
2198
2199 case SS_DISCONNECTING:
2200
Ying Xue75031152012-10-29 09:38:15 -04002201 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002202 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002203
2204 /* Wake up anyone sleeping in poll */
2205 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002206 res = 0;
2207 break;
2208
2209 default:
2210 res = -ENOTCONN;
2211 }
2212
Allan Stephens0c3141e2008-04-15 00:22:02 -07002213 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002214 return res;
2215}
2216
Ying Xuef2f2a962015-01-09 15:27:02 +08002217static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002218{
Ying Xuef2f2a962015-01-09 15:27:02 +08002219 struct tipc_sock *tsk = (struct tipc_sock *)data;
2220 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002221 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002222 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002223 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002224
Jon Paul Maloy57289012014-08-22 18:09:09 -04002225 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002226 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002227 bh_unlock_sock(sk);
2228 goto exit;
2229 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002230 peer_port = tsk_peer_port(tsk);
2231 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002232
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002233 if (tsk->probing_state == TIPC_CONN_PROBING) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002234 if (!sock_owned_by_user(sk)) {
2235 sk->sk_socket->state = SS_DISCONNECTING;
2236 tsk->connected = 0;
2237 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2238 tsk_peer_port(tsk));
2239 sk->sk_state_change(sk);
2240 } else {
2241 /* Try again later */
2242 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2243 }
2244
Jon Paul Maloy57289012014-08-22 18:09:09 -04002245 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002246 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2247 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002248 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002249 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002250 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002251 }
2252 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002253 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002254 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002255exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002256 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002257}
2258
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002259static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002260 struct tipc_name_seq const *seq)
2261{
Ying Xuef2f98002015-01-09 15:27:05 +08002262 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002263 struct publication *publ;
2264 u32 key;
2265
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002266 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002267 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002268 key = tsk->portid + tsk->pub_count + 1;
2269 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002270 return -EADDRINUSE;
2271
Ying Xuef2f98002015-01-09 15:27:05 +08002272 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002273 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002274 if (unlikely(!publ))
2275 return -EINVAL;
2276
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002277 list_add(&publ->pport_list, &tsk->publications);
2278 tsk->pub_count++;
2279 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002280 return 0;
2281}
2282
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002283static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002284 struct tipc_name_seq const *seq)
2285{
Ying Xuef2f98002015-01-09 15:27:05 +08002286 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002287 struct publication *publ;
2288 struct publication *safe;
2289 int rc = -EINVAL;
2290
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002291 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002292 if (seq) {
2293 if (publ->scope != scope)
2294 continue;
2295 if (publ->type != seq->type)
2296 continue;
2297 if (publ->lower != seq->lower)
2298 continue;
2299 if (publ->upper != seq->upper)
2300 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002301 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002302 publ->ref, publ->key);
2303 rc = 0;
2304 break;
2305 }
Ying Xuef2f98002015-01-09 15:27:05 +08002306 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002307 publ->ref, publ->key);
2308 rc = 0;
2309 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002310 if (list_empty(&tsk->publications))
2311 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002312 return rc;
2313}
2314
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002315/* tipc_sk_reinit: set non-zero address in all existing sockets
2316 * when we go from standalone to network mode.
2317 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002318void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002319{
Ying Xuee05b31f2015-01-09 15:27:08 +08002320 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002321 const struct bucket_table *tbl;
2322 struct rhash_head *pos;
2323 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002324 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002325 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002326
Ying Xue07f6c4b2015-01-07 13:41:58 +08002327 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002328 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002329 for (i = 0; i < tbl->size; i++) {
2330 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2331 spin_lock_bh(&tsk->sk.sk_lock.slock);
2332 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002333 msg_set_prevnode(msg, tn->own_addr);
2334 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002335 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2336 }
2337 }
2338 rcu_read_unlock();
2339}
2340
Ying Xuee05b31f2015-01-09 15:27:08 +08002341static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002342{
Ying Xuee05b31f2015-01-09 15:27:08 +08002343 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002344 struct tipc_sock *tsk;
2345
2346 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002347 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002348 if (tsk)
2349 sock_hold(&tsk->sk);
2350 rcu_read_unlock();
2351
2352 return tsk;
2353}
2354
2355static int tipc_sk_insert(struct tipc_sock *tsk)
2356{
Ying Xuee05b31f2015-01-09 15:27:08 +08002357 struct sock *sk = &tsk->sk;
2358 struct net *net = sock_net(sk);
2359 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002360 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2361 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2362
2363 while (remaining--) {
2364 portid++;
2365 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2366 portid = TIPC_MIN_PORT;
2367 tsk->portid = portid;
2368 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002369 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2370 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002371 return 0;
2372 sock_put(&tsk->sk);
2373 }
2374
2375 return -1;
2376}
2377
2378static void tipc_sk_remove(struct tipc_sock *tsk)
2379{
2380 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002381 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002382
Herbert Xu6cca72892015-03-20 21:57:05 +11002383 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002384 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2385 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002386 }
2387}
2388
Herbert Xu6cca72892015-03-20 21:57:05 +11002389static const struct rhashtable_params tsk_rht_params = {
2390 .nelem_hint = 192,
2391 .head_offset = offsetof(struct tipc_sock, node),
2392 .key_offset = offsetof(struct tipc_sock, portid),
2393 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002394 .max_size = 1048576,
2395 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002396 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002397};
2398
Ying Xuee05b31f2015-01-09 15:27:08 +08002399int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002400{
Ying Xuee05b31f2015-01-09 15:27:08 +08002401 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002402
Herbert Xu6cca72892015-03-20 21:57:05 +11002403 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002404}
2405
Ying Xuee05b31f2015-01-09 15:27:08 +08002406void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002407{
Ying Xuee05b31f2015-01-09 15:27:08 +08002408 struct tipc_net *tn = net_generic(net, tipc_net_id);
2409
Ying Xue07f6c4b2015-01-07 13:41:58 +08002410 /* Wait for socket readers to complete */
2411 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002412
Ying Xuee05b31f2015-01-09 15:27:08 +08002413 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002414}
2415
2416/**
Ying Xue247f0f32014-02-18 16:06:46 +08002417 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002418 * @sock: socket structure
2419 * @lvl: option level
2420 * @opt: option identifier
2421 * @ov: pointer to new option value
2422 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002423 *
2424 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002425 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002426 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002427 * Returns 0 on success, errno otherwise
2428 */
Ying Xue247f0f32014-02-18 16:06:46 +08002429static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2430 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002431{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002432 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002433 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002434 u32 value;
2435 int res;
2436
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002437 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2438 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002439 if (lvl != SOL_TIPC)
2440 return -ENOPROTOOPT;
2441 if (ol < sizeof(value))
2442 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002443 res = get_user(value, (u32 __user *)ov);
2444 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002445 return res;
2446
Allan Stephens0c3141e2008-04-15 00:22:02 -07002447 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002448
Per Lidenb97bf3f2006-01-02 19:04:38 +01002449 switch (opt) {
2450 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002451 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002452 break;
2453 case TIPC_SRC_DROPPABLE:
2454 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002455 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002456 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457 res = -ENOPROTOOPT;
2458 break;
2459 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002460 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002461 break;
2462 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002463 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002464 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002465 break;
2466 default:
2467 res = -EINVAL;
2468 }
2469
Allan Stephens0c3141e2008-04-15 00:22:02 -07002470 release_sock(sk);
2471
Per Lidenb97bf3f2006-01-02 19:04:38 +01002472 return res;
2473}
2474
2475/**
Ying Xue247f0f32014-02-18 16:06:46 +08002476 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002477 * @sock: socket structure
2478 * @lvl: option level
2479 * @opt: option identifier
2480 * @ov: receptacle for option value
2481 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002482 *
2483 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002484 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002485 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002486 * Returns 0 on success, errno otherwise
2487 */
Ying Xue247f0f32014-02-18 16:06:46 +08002488static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2489 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002490{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002491 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002492 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002493 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002494 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002495 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002496
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002497 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2498 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002499 if (lvl != SOL_TIPC)
2500 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002501 res = get_user(len, ol);
2502 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002503 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002504
Allan Stephens0c3141e2008-04-15 00:22:02 -07002505 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002506
2507 switch (opt) {
2508 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002509 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002510 break;
2511 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002512 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002513 break;
2514 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002515 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002516 break;
2517 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002518 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002519 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002520 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002521 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002522 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002523 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002524 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002525 value = skb_queue_len(&sk->sk_receive_queue);
2526 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002527 default:
2528 res = -EINVAL;
2529 }
2530
Allan Stephens0c3141e2008-04-15 00:22:02 -07002531 release_sock(sk);
2532
Paul Gortmaker25860c32010-12-31 18:59:31 +00002533 if (res)
2534 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002535
Paul Gortmaker25860c32010-12-31 18:59:31 +00002536 if (len < sizeof(value))
2537 return -EINVAL;
2538
2539 if (copy_to_user(ov, &value, sizeof(value)))
2540 return -EFAULT;
2541
2542 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002543}
2544
Ying Xuef2f98002015-01-09 15:27:05 +08002545static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002546{
Ying Xuef2f98002015-01-09 15:27:05 +08002547 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002548 struct tipc_sioc_ln_req lnr;
2549 void __user *argp = (void __user *)arg;
2550
2551 switch (cmd) {
2552 case SIOCGETLINKNAME:
2553 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2554 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002555 if (!tipc_node_get_linkname(sock_net(sk),
2556 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002557 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2558 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2559 return -EFAULT;
2560 return 0;
2561 }
2562 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002563 default:
2564 return -ENOIOCTLCMD;
2565 }
2566}
2567
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002568/* Protocol switches for the various types of TIPC sockets */
2569
Florian Westphalbca65ea2008-02-07 18:18:01 -08002570static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002571 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002572 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002573 .release = tipc_release,
2574 .bind = tipc_bind,
2575 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002576 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002577 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002578 .getname = tipc_getname,
2579 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002580 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002581 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002582 .shutdown = tipc_shutdown,
2583 .setsockopt = tipc_setsockopt,
2584 .getsockopt = tipc_getsockopt,
2585 .sendmsg = tipc_sendmsg,
2586 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002587 .mmap = sock_no_mmap,
2588 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002589};
2590
Florian Westphalbca65ea2008-02-07 18:18:01 -08002591static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002592 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002593 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002594 .release = tipc_release,
2595 .bind = tipc_bind,
2596 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002597 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002598 .accept = tipc_accept,
2599 .getname = tipc_getname,
2600 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002601 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002602 .listen = tipc_listen,
2603 .shutdown = tipc_shutdown,
2604 .setsockopt = tipc_setsockopt,
2605 .getsockopt = tipc_getsockopt,
2606 .sendmsg = tipc_send_packet,
2607 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002608 .mmap = sock_no_mmap,
2609 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002610};
2611
Florian Westphalbca65ea2008-02-07 18:18:01 -08002612static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002613 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002614 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002615 .release = tipc_release,
2616 .bind = tipc_bind,
2617 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002618 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002619 .accept = tipc_accept,
2620 .getname = tipc_getname,
2621 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002622 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002623 .listen = tipc_listen,
2624 .shutdown = tipc_shutdown,
2625 .setsockopt = tipc_setsockopt,
2626 .getsockopt = tipc_getsockopt,
2627 .sendmsg = tipc_send_stream,
2628 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002629 .mmap = sock_no_mmap,
2630 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002631};
2632
Florian Westphalbca65ea2008-02-07 18:18:01 -08002633static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002634 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002635 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002636 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002637};
2638
2639static struct proto tipc_proto = {
2640 .name = "TIPC",
2641 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002642 .obj_size = sizeof(struct tipc_sock),
2643 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002644};
2645
2646/**
Per Liden4323add2006-01-18 00:38:21 +01002647 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002648 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002649 * Returns 0 on success, errno otherwise
2650 */
Per Liden4323add2006-01-18 00:38:21 +01002651int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002652{
2653 int res;
2654
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002655 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002656 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002657 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002658 goto out;
2659 }
2660
2661 res = sock_register(&tipc_family_ops);
2662 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002663 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002664 proto_unregister(&tipc_proto);
2665 goto out;
2666 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002667 out:
2668 return res;
2669}
2670
2671/**
Per Liden4323add2006-01-18 00:38:21 +01002672 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002673 */
Per Liden4323add2006-01-18 00:38:21 +01002674void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002675{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002676 sock_unregister(tipc_family_ops.family);
2677 proto_unregister(&tipc_proto);
2678}
Richard Alpe34b78a122014-11-20 10:29:10 +01002679
2680/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002681static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002682{
2683 u32 peer_node;
2684 u32 peer_port;
2685 struct nlattr *nest;
2686
2687 peer_node = tsk_peer_node(tsk);
2688 peer_port = tsk_peer_port(tsk);
2689
2690 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2691
2692 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2693 goto msg_full;
2694 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2695 goto msg_full;
2696
2697 if (tsk->conn_type != 0) {
2698 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2699 goto msg_full;
2700 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2701 goto msg_full;
2702 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2703 goto msg_full;
2704 }
2705 nla_nest_end(skb, nest);
2706
2707 return 0;
2708
2709msg_full:
2710 nla_nest_cancel(skb, nest);
2711
2712 return -EMSGSIZE;
2713}
2714
2715/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002716static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2717 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002718{
2719 int err;
2720 void *hdr;
2721 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002722 struct net *net = sock_net(skb->sk);
2723 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002724
2725 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002726 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002727 if (!hdr)
2728 goto msg_cancel;
2729
2730 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2731 if (!attrs)
2732 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002733 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002734 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002735 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002736 goto attr_msg_cancel;
2737
2738 if (tsk->connected) {
2739 err = __tipc_nl_add_sk_con(skb, tsk);
2740 if (err)
2741 goto attr_msg_cancel;
2742 } else if (!list_empty(&tsk->publications)) {
2743 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2744 goto attr_msg_cancel;
2745 }
2746 nla_nest_end(skb, attrs);
2747 genlmsg_end(skb, hdr);
2748
2749 return 0;
2750
2751attr_msg_cancel:
2752 nla_nest_cancel(skb, attrs);
2753genlmsg_cancel:
2754 genlmsg_cancel(skb, hdr);
2755msg_cancel:
2756 return -EMSGSIZE;
2757}
2758
2759int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2760{
2761 int err;
2762 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002763 const struct bucket_table *tbl;
2764 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002765 struct net *net = sock_net(skb->sk);
2766 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002767 u32 tbl_id = cb->args[0];
2768 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002769
Ying Xue07f6c4b2015-01-07 13:41:58 +08002770 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002771 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002772 for (; tbl_id < tbl->size; tbl_id++) {
2773 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002774 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002775 if (prev_portid && prev_portid != tsk->portid) {
2776 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2777 continue;
2778 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002779
Richard Alped6e164e2015-01-16 12:30:40 +01002780 err = __tipc_nl_add_sk(skb, cb, tsk);
2781 if (err) {
2782 prev_portid = tsk->portid;
2783 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2784 goto out;
2785 }
2786 prev_portid = 0;
2787 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002788 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002789 }
Richard Alped6e164e2015-01-16 12:30:40 +01002790out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002791 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002792 cb->args[0] = tbl_id;
2793 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002794
2795 return skb->len;
2796}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002797
2798/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002799static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2800 struct netlink_callback *cb,
2801 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002802{
2803 void *hdr;
2804 struct nlattr *attrs;
2805
2806 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002807 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002808 if (!hdr)
2809 goto msg_cancel;
2810
2811 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2812 if (!attrs)
2813 goto genlmsg_cancel;
2814
2815 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2816 goto attr_msg_cancel;
2817 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2818 goto attr_msg_cancel;
2819 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2820 goto attr_msg_cancel;
2821 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2822 goto attr_msg_cancel;
2823
2824 nla_nest_end(skb, attrs);
2825 genlmsg_end(skb, hdr);
2826
2827 return 0;
2828
2829attr_msg_cancel:
2830 nla_nest_cancel(skb, attrs);
2831genlmsg_cancel:
2832 genlmsg_cancel(skb, hdr);
2833msg_cancel:
2834 return -EMSGSIZE;
2835}
2836
2837/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002838static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2839 struct netlink_callback *cb,
2840 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002841{
2842 int err;
2843 struct publication *p;
2844
2845 if (*last_publ) {
2846 list_for_each_entry(p, &tsk->publications, pport_list) {
2847 if (p->key == *last_publ)
2848 break;
2849 }
2850 if (p->key != *last_publ) {
2851 /* We never set seq or call nl_dump_check_consistent()
2852 * this means that setting prev_seq here will cause the
2853 * consistence check to fail in the netlink callback
2854 * handler. Resulting in the last NLMSG_DONE message
2855 * having the NLM_F_DUMP_INTR flag set.
2856 */
2857 cb->prev_seq = 1;
2858 *last_publ = 0;
2859 return -EPIPE;
2860 }
2861 } else {
2862 p = list_first_entry(&tsk->publications, struct publication,
2863 pport_list);
2864 }
2865
2866 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2867 err = __tipc_nl_add_sk_publ(skb, cb, p);
2868 if (err) {
2869 *last_publ = p->key;
2870 return err;
2871 }
2872 }
2873 *last_publ = 0;
2874
2875 return 0;
2876}
2877
2878int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2879{
2880 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002881 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002882 u32 last_publ = cb->args[1];
2883 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002884 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002885 struct tipc_sock *tsk;
2886
Ying Xue07f6c4b2015-01-07 13:41:58 +08002887 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002888 struct nlattr **attrs;
2889 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2890
2891 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2892 if (err)
2893 return err;
2894
Richard Alpe45e093a2016-05-16 11:14:54 +02002895 if (!attrs[TIPC_NLA_SOCK])
2896 return -EINVAL;
2897
Richard Alpe1a1a1432014-11-20 10:29:11 +01002898 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2899 attrs[TIPC_NLA_SOCK],
2900 tipc_nl_sock_policy);
2901 if (err)
2902 return err;
2903
2904 if (!sock[TIPC_NLA_SOCK_REF])
2905 return -EINVAL;
2906
Ying Xue07f6c4b2015-01-07 13:41:58 +08002907 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002908 }
2909
2910 if (done)
2911 return 0;
2912
Ying Xuee05b31f2015-01-09 15:27:08 +08002913 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002914 if (!tsk)
2915 return -EINVAL;
2916
2917 lock_sock(&tsk->sk);
2918 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2919 if (!err)
2920 done = 1;
2921 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002922 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002923
Ying Xue07f6c4b2015-01-07 13:41:58 +08002924 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002925 cb->args[1] = last_publ;
2926 cb->args[2] = done;
2927
2928 return skb->len;
2929}