blob: a48c0c0676cf0587bc177b3507556a8610fce2f9 [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 CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080048#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080049#define TIPC_FWD_MSG 1
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define TIPC_MAX_PORT 0xffffffff
51#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040052
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010053enum {
54 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010055 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010056 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010057 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010058};
59
Jon Paul Maloy301bae52014-08-22 18:09:20 -040060/**
61 * struct tipc_sock - TIPC socket structure
62 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040063 * @conn_type: TIPC type used when connection was established
64 * @conn_instance: TIPC instance used when connection was established
65 * @published: non-zero if port has one or more associated names
66 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080067 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040068 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy301bae52014-08-22 18:09:20 -040069 * @publications: list of publications for port
70 * @pub_count: total # of publications port has made during its lifetime
71 * @probing_state:
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;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040083 u32 conn_type;
84 u32 conn_instance;
85 int published;
86 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080087 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040088 struct tipc_msg phdr;
89 struct list_head sock_list;
90 struct list_head publications;
91 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040092 uint conn_timeout;
93 atomic_t dupl_rcvcnt;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010094 bool probe_unacked;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040095 bool link_cong;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -040096 u16 snt_unacked;
97 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -040098 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -040099 u16 rcv_unacked;
100 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100101 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800102 struct rhash_head node;
103 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400104};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400106static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400107static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800108static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800109static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800110static int tipc_release(struct socket *sock);
111static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400112static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Ying Xuef2f2a962015-01-09 15:27:02 +0800113static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400115 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400116static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400117 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800118static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800119static int tipc_sk_insert(struct tipc_sock *tsk);
120static void tipc_sk_remove(struct tipc_sock *tsk);
Ying Xue39a02952015-03-02 15:37:47 +0800121static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
122 size_t dsz);
123static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124
Florian Westphalbca65ea2008-02-07 18:18:01 -0800125static const struct proto_ops packet_ops;
126static const struct proto_ops stream_ops;
127static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128static struct proto tipc_proto;
129
Herbert Xu6cca72892015-03-20 21:57:05 +1100130static const struct rhashtable_params tsk_rht_params;
131
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900132/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133 * Revised TIPC socket locking policy:
134 *
135 * Most socket operations take the standard socket lock when they start
136 * and hold it until they finish (or until they need to sleep). Acquiring
137 * this lock grants the owner exclusive access to the fields of the socket
138 * data structures, with the exception of the backlog queue. A few socket
139 * operations can be done without taking the socket lock because they only
140 * read socket information that never changes during the life of the socket.
141 *
142 * Socket operations may acquire the lock for the associated TIPC port if they
143 * need to perform an operation on the port. If any routine needs to acquire
144 * both the socket lock and the port lock it must take the socket lock first
145 * to avoid the risk of deadlock.
146 *
147 * The dispatcher handling incoming messages cannot grab the socket lock in
148 * the standard fashion, since invoked it runs at the BH level and cannot block.
149 * Instead, it checks to see if the socket lock is currently owned by someone,
150 * and either handles the message itself or adds it to the socket's backlog
151 * queue; in the latter case the queued message is processed once the process
152 * owning the socket lock releases it.
153 *
154 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
155 * the problem of a blocked socket operation preventing any other operations
156 * from occurring. However, applications must be careful if they have
157 * multiple threads trying to send (or receive) on the same socket, as these
158 * operations might interfere with each other. For example, doing a connect
159 * and a receive at the same time might allow the receive to consume the
160 * ACK message meant for the connect. While additional work could be done
161 * to try and overcome this, it doesn't seem to be worthwhile at the present.
162 *
163 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
164 * that another operation that must be performed in a non-blocking manner is
165 * not delayed for very long because the lock has already been taken.
166 *
167 * NOTE: This code assumes that certain fields of a port/socket pair are
168 * constant over its lifetime; such fields can be examined without taking
169 * the socket lock and/or port lock, and do not need to be re-read even
170 * after resuming processing after waiting. These fields include:
171 * - socket type
172 * - pointer to socket sk structure (aka tipc_sock structure)
173 * - pointer to port structure
174 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500177static u32 tsk_own_node(struct tipc_sock *tsk)
178{
179 return msg_prevnode(&tsk->phdr);
180}
181
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400182static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400183{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400184 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400185}
186
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400187static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400188{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400190}
191
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400192static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400193{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400194 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400195}
196
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400197static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400198{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400199 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400200}
201
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400202static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400203{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400204 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400205}
206
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400208{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400209 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400210}
211
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400212static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400213{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400214 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400215}
216
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400217static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400218{
219 if (imp > TIPC_CRITICAL_IMPORTANCE)
220 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400221 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400222 return 0;
223}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400224
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400225static struct tipc_sock *tipc_sk(const struct sock *sk)
226{
227 return container_of(sk, struct tipc_sock, sk);
228}
229
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400230static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400231{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400232 return tsk->snt_unacked >= tsk->snd_win;
233}
234
235/* tsk_blocks(): translate a buffer size in bytes to number of
236 * advertisable blocks, taking into account the ratio truesize(len)/len
237 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
238 */
239static u16 tsk_adv_blocks(int len)
240{
241 return len / FLOWCTL_BLK_SZ / 4;
242}
243
244/* tsk_inc(): increment counter for sent or received data
245 * - If block based flow control is not supported by peer we
246 * fall back to message based ditto, incrementing the counter
247 */
248static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
249{
250 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
251 return ((msglen / FLOWCTL_BLK_SZ) + 1);
252 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400253}
254
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400256 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700257 *
258 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400260static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400262 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263}
264
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400265/* tipc_sk_respond() : send response message back to sender
266 */
267static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
268{
269 u32 selector;
270 u32 dnode;
271 u32 onode = tipc_own_addr(sock_net(sk));
272
273 if (!tipc_msg_reverse(onode, &skb, err))
274 return;
275
276 dnode = msg_destnode(buf_msg(skb));
277 selector = msg_origport(buf_msg(skb));
278 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
279}
280
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400282 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700283 *
284 * Caller must hold socket lock
285 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400286static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700287{
Ying Xuea6ca1092014-11-26 11:41:55 +0800288 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700289
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400290 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
291 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700292}
293
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100294static bool tipc_sk_connected(struct sock *sk)
295{
296 return sk->sk_socket->state == SS_CONNECTED;
297}
298
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100299/* tipc_sk_type_connectionless - check if the socket is datagram socket
300 * @sk: socket
301 *
302 * Returns true if connection less, false otherwise
303 */
304static bool tipc_sk_type_connectionless(struct sock *sk)
305{
306 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
307}
308
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400309/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400310 *
311 * Handles cases where the node's network address has changed from
312 * the default of <0.0.0> to its configured setting.
313 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400314static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400315{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100316 struct sock *sk = &tsk->sk;
317 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400318 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400319 u32 orig_node;
320 u32 peer_node;
321
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100322 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400323 return false;
324
325 if (unlikely(msg_origport(msg) != peer_port))
326 return false;
327
328 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400329 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400330
331 if (likely(orig_node == peer_node))
332 return true;
333
Ying Xue34747532015-01-09 15:27:10 +0800334 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400335 return true;
336
Ying Xue34747532015-01-09 15:27:10 +0800337 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400338 return true;
339
340 return false;
341}
342
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100343/* tipc_set_sk_state - set the sk_state of the socket
344 * @sk: socket
345 *
346 * Caller must hold socket lock
347 *
348 * Returns 0 on success, errno otherwise
349 */
350static int tipc_set_sk_state(struct sock *sk, int state)
351{
352 int oldstate = sk->sk_socket->state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100353 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100354 int res = -EINVAL;
355
356 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100357 case TIPC_OPEN:
358 res = 0;
359 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100360 case TIPC_LISTEN:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100361 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100362 res = 0;
363 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100364 case TIPC_ESTABLISHED:
365 if (oldstate == SS_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100366 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100367 res = 0;
368 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100369 case TIPC_DISCONNECTING:
370 if (oldstate == SS_CONNECTING ||
371 oldsk_state == TIPC_ESTABLISHED)
372 res = 0;
373 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100374 }
375
376 if (!res)
377 sk->sk_state = state;
378
379 return res;
380}
381
Allan Stephens0c3141e2008-04-15 00:22:02 -0700382/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400383 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700384 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 * @sock: pre-allocated socket structure
386 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800387 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900388 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700389 * This routine creates additional data structures used by the TIPC socket,
390 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 *
392 * Returns 0 on success, errno otherwise
393 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400394static int tipc_sk_create(struct net *net, struct socket *sock,
395 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500397 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700398 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400400 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400401 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700402
403 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 if (unlikely(protocol != 0))
405 return -EPROTONOSUPPORT;
406
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 switch (sock->type) {
408 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700409 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 break;
411 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700412 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 break;
414 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700416 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 break;
Allan Stephens49978652006-06-25 23:47:18 -0700418 default:
Allan Stephens49978652006-06-25 23:47:18 -0700419 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 }
421
Allan Stephens0c3141e2008-04-15 00:22:02 -0700422 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500423 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700424 if (sk == NULL)
425 return -ENOMEM;
426
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400427 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400428 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400429 INIT_LIST_HEAD(&tsk->publications);
430 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500431 tn = net_generic(sock_net(sk), tipc_net_id);
432 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400433 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434
Allan Stephens0c3141e2008-04-15 00:22:02 -0700435 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700436 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100438 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800439 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900440 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800441 return -EINVAL;
442 }
443 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800444 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400445 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400446 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800447 sk->sk_data_ready = tipc_data_ready;
448 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800449 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400450 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
451 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700452
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400453 /* Start out with safe limits until we receive an advertised window */
454 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
455 tsk->rcv_win = tsk->snd_win;
456
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100457 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400458 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700459 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400460 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700461 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100462
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 return 0;
464}
465
Ying Xue07f6c4b2015-01-07 13:41:58 +0800466static void tipc_sk_callback(struct rcu_head *head)
467{
468 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
469
470 sock_put(&tsk->sk);
471}
472
Ying Xuec5fa7b32013-06-17 10:54:39 -0400473/**
Ying Xue247f0f32014-02-18 16:06:46 +0800474 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 * @sock: socket to destroy
476 *
477 * This routine cleans up any messages that are still queued on the socket.
478 * For DGRAM and RDM socket types, all queued messages are rejected.
479 * For SEQPACKET and STREAM socket types, the first message is rejected
480 * and any others are discarded. (If the first message on a STREAM socket
481 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900482 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 * NOTE: Rejected messages are not necessarily returned to the sender! They
484 * are returned or discarded according to the "destination droppable" setting
485 * specified for the message by the sender.
486 *
487 * Returns 0 on success, errno otherwise
488 */
Ying Xue247f0f32014-02-18 16:06:46 +0800489static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 struct sock *sk = sock->sk;
Sasha Levin357c4772015-01-13 12:46:41 -0500492 struct net *net;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400493 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800494 struct sk_buff *skb;
Ying Xue1ea23a22015-05-28 13:19:22 +0800495 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496
Allan Stephens0c3141e2008-04-15 00:22:02 -0700497 /*
498 * Exit if socket isn't fully initialized (occurs when a failed accept()
499 * releases a pre-allocated child socket that was never used)
500 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700501 if (sk == NULL)
502 return 0;
503
Sasha Levin357c4772015-01-13 12:46:41 -0500504 net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400505 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700506 lock_sock(sk);
507
508 /*
509 * Reject all unreceived messages, except on an active connection
510 * (which disconnects locally & sends a 'FIN+' to peer)
511 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400512 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800514 skb = __skb_dequeue(&sk->sk_receive_queue);
515 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 break;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +0100517 if (TIPC_SKB_CB(skb)->bytes_read)
Ying Xuea6ca1092014-11-26 11:41:55 +0800518 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700519 else {
520 if ((sock->state == SS_CONNECTING) ||
521 (sock->state == SS_CONNECTED)) {
522 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +0800523 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700524 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400525 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700526 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 }
528
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400529 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800530 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800531 tipc_sk_remove(tsk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100532 if (tipc_sk_connected(sk)) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500533 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +0800534 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500535 tsk_own_node(tsk), tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800536 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800537 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400538 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Ying Xuef2f98002015-01-09 15:27:05 +0800539 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400540 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541
Allan Stephens0c3141e2008-04-15 00:22:02 -0700542 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700543 sock->state = SS_DISCONNECTING;
544 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800545
546 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700547 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200549 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550}
551
552/**
Ying Xue247f0f32014-02-18 16:06:46 +0800553 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 * @sock: socket structure
555 * @uaddr: socket address describing name(s) and desired operation
556 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900557 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558 * Name and name sequence binding is indicated using a positive scope value;
559 * a negative scope value unbinds the specified name. Specifying no name
560 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900561 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700563 *
564 * NOTE: This routine doesn't need to take the socket lock since it doesn't
565 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 */
Ying Xue247f0f32014-02-18 16:06:46 +0800567static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
568 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569{
Ying Xue84602762013-12-27 10:18:28 +0800570 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400572 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800573 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574
Ying Xue84602762013-12-27 10:18:28 +0800575 lock_sock(sk);
576 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400577 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800578 goto exit;
579 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900580
Ying Xue84602762013-12-27 10:18:28 +0800581 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
582 res = -EINVAL;
583 goto exit;
584 }
585 if (addr->family != AF_TIPC) {
586 res = -EAFNOSUPPORT;
587 goto exit;
588 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 if (addr->addrtype == TIPC_ADDR_NAME)
591 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800592 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
593 res = -EAFNOSUPPORT;
594 goto exit;
595 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596
Ying Xue13a2e892013-06-17 10:54:40 -0400597 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400598 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800599 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
600 res = -EACCES;
601 goto exit;
602 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400603
Ying Xue84602762013-12-27 10:18:28 +0800604 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400605 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
606 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800607exit:
608 release_sock(sk);
609 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610}
611
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612/**
Ying Xue247f0f32014-02-18 16:06:46 +0800613 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 * @sock: socket structure
615 * @uaddr: area for returned socket address
616 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700617 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900618 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700620 *
Allan Stephens2da59912008-07-14 22:43:32 -0700621 * NOTE: This routine doesn't need to take the socket lock since it only
622 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000623 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 */
Ying Xue247f0f32014-02-18 16:06:46 +0800625static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
626 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100629 struct sock *sk = sock->sk;
630 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +0800631 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000633 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700634 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700635 if ((sock->state != SS_CONNECTED) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100636 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700637 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400638 addr->addr.id.ref = tsk_peer_port(tsk);
639 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700640 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800641 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800642 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700643 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644
645 *uaddr_len = sizeof(*addr);
646 addr->addrtype = TIPC_ADDR_ID;
647 addr->family = AF_TIPC;
648 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 addr->addr.name.domain = 0;
650
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652}
653
654/**
Ying Xue247f0f32014-02-18 16:06:46 +0800655 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 * @file: file structure associated with the socket
657 * @sock: socket for which to calculate the poll bits
658 * @wait: ???
659 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700660 * Returns pollmask value
661 *
662 * COMMENTARY:
663 * It appears that the usual socket locking mechanisms are not useful here
664 * since the pollmask info is potentially out-of-date the moment this routine
665 * exits. TCP and other protocols seem to rely on higher level poll routines
666 * to handle any preventable race conditions, so TIPC will do the same ...
667 *
Allan Stephensf662c072010-08-17 11:00:06 +0000668 * IMPORTANT: The fact that a read or write operation is indicated does NOT
669 * imply that the operation will succeed, merely that it should be performed
670 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 */
Ying Xue247f0f32014-02-18 16:06:46 +0800672static unsigned int tipc_poll(struct file *file, struct socket *sock,
673 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674{
Allan Stephens9b674e82008-03-26 16:48:21 -0700675 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400676 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000677 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700678
Ying Xuef288bef2012-08-21 11:16:57 +0800679 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700680
Allan Stephensf662c072010-08-17 11:00:06 +0000681 switch ((int)sock->state) {
Allan Stephensf662c072010-08-17 11:00:06 +0000682 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400683 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000684 mask |= POLLOUT;
685 /* fall thru' */
686 case SS_CONNECTING:
Allan Stephensf662c072010-08-17 11:00:06 +0000687 if (!skb_queue_empty(&sk->sk_receive_queue))
688 mask |= (POLLIN | POLLRDNORM);
689 break;
690 case SS_DISCONNECTING:
691 mask = (POLLIN | POLLRDNORM | POLLHUP);
692 break;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100693 default:
694 switch (sk->sk_state) {
695 case TIPC_OPEN:
696 if (!tsk->link_cong)
697 mask |= POLLOUT;
698 if (tipc_sk_type_connectionless(sk) &&
699 (!skb_queue_empty(&sk->sk_receive_queue)))
700 mask |= (POLLIN | POLLRDNORM);
701 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100702 case TIPC_DISCONNECTING:
703 mask = (POLLIN | POLLRDNORM | POLLHUP);
704 break;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100705 case TIPC_LISTEN:
706 if (!skb_queue_empty(&sk->sk_receive_queue))
707 mask |= (POLLIN | POLLRDNORM);
708 break;
709 }
Allan Stephensf662c072010-08-17 11:00:06 +0000710 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700711
712 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713}
714
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400715/**
716 * tipc_sendmcast - send multicast message
717 * @sock: socket structure
718 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500719 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400720 * @dsz: total length of message data
721 * @timeo: timeout to wait for wakeup
722 *
723 * Called from function tipc_sendmsg(), which has done all sanity checks
724 * Returns the number of bytes sent on success, or errno
725 */
726static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500727 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400728{
729 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500730 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800731 struct net *net = sock_net(sk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500732 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100733 struct sk_buff_head pktchain;
Al Virof25dcc72014-11-28 15:52:29 -0500734 struct iov_iter save = msg->msg_iter;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400735 uint mtu;
736 int rc;
737
Parthasarathy Bhuvaragan7cf87fa2016-11-01 14:02:34 +0100738 if (!timeo && tsk->link_cong)
739 return -ELINKCONG;
740
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400741 msg_set_type(mhdr, TIPC_MCAST_MSG);
742 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
743 msg_set_destport(mhdr, 0);
744 msg_set_destnode(mhdr, 0);
745 msg_set_nametype(mhdr, seq->type);
746 msg_set_namelower(mhdr, seq->lower);
747 msg_set_nameupper(mhdr, seq->upper);
748 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
749
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100750 skb_queue_head_init(&pktchain);
751
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400752new_mtu:
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400753 mtu = tipc_bcast_get_mtu(net);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100754 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400755 if (unlikely(rc < 0))
756 return rc;
757
758 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100759 rc = tipc_bcast_xmit(net, &pktchain);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400760 if (likely(!rc))
761 return dsz;
762
763 if (rc == -ELINKCONG) {
764 tsk->link_cong = 1;
765 rc = tipc_wait_for_sndmsg(sock, &timeo);
766 if (!rc)
767 continue;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400768 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100769 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500770 if (rc == -EMSGSIZE) {
771 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400772 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500773 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400774 break;
775 } while (1);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400776 return rc;
777}
778
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500779/**
780 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
781 * @arrvq: queue with arriving messages, to be cloned after destination lookup
782 * @inputq: queue with cloned messages, delivered to socket after dest lookup
783 *
784 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400785 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500786void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
787 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400788{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500789 struct tipc_msg *msg;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500790 struct tipc_plist dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500791 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400792 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500793 struct sk_buff_head tmpq;
794 uint hsz;
795 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500796
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500797 __skb_queue_head_init(&tmpq);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500798 tipc_plist_init(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400799
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500800 skb = tipc_skb_peek(arrvq, &inputq->lock);
801 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
802 msg = buf_msg(skb);
803 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400804
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500805 if (in_own_node(net, msg_orignode(msg)))
806 scope = TIPC_NODE_SCOPE;
807
808 /* Create destination port list and message clones: */
809 tipc_nametbl_mc_translate(net,
810 msg_nametype(msg), msg_namelower(msg),
811 msg_nameupper(msg), scope, &dports);
812 portid = tipc_plist_pop(&dports);
813 for (; portid; portid = tipc_plist_pop(&dports)) {
814 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
815 if (_skb) {
816 msg_set_destport(buf_msg(_skb), portid);
817 __skb_queue_tail(&tmpq, _skb);
818 continue;
819 }
820 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400821 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500822 /* Append to inputq if not already done by other thread */
823 spin_lock_bh(&inputq->lock);
824 if (skb_peek(arrvq) == skb) {
825 skb_queue_splice_tail_init(&tmpq, inputq);
826 kfree_skb(__skb_dequeue(arrvq));
827 }
828 spin_unlock_bh(&inputq->lock);
829 __skb_queue_purge(&tmpq);
830 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400831 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500832 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400833}
834
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500836 * tipc_sk_proto_rcv - receive a connection mng protocol message
837 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400838 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500839 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400840static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
841 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500842{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400843 struct sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400844 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400845 struct tipc_msg *hdr = buf_msg(skb);
846 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400847 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400848
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500849 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400850 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500851 goto exit;
852
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100853 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500854
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400855 if (mtyp == CONN_PROBE) {
856 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400857 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
858 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400859 return;
860 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400861 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400862 tsk->snt_unacked -= msg_conn_ack(hdr);
863 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
864 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500865 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400866 sk->sk_write_space(sk);
867 } else if (mtyp != CONN_PROBE_REPLY) {
868 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500869 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500870exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400871 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500872}
873
Ying Xue3f405042014-01-17 09:50:05 +0800874static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
875{
876 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400877 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800878 DEFINE_WAIT(wait);
879 int done;
880
881 do {
882 int err = sock_error(sk);
883 if (err)
884 return err;
885 if (sock->state == SS_DISCONNECTING)
886 return -EPIPE;
887 if (!*timeo_p)
888 return -EAGAIN;
889 if (signal_pending(current))
890 return sock_intr_errno(*timeo_p);
891
892 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500893 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800894 finish_wait(sk_sleep(sk), &wait);
895 } while (!done);
896 return 0;
897}
898
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500899/**
Ying Xue247f0f32014-02-18 16:06:46 +0800900 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 * @sock: socket structure
902 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500903 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900904 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900906 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
908 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900909 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 * Returns the number of bytes sent on success, or errno otherwise
911 */
Ying Xue1b784142015-03-02 15:37:48 +0800912static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500913 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914{
Ying Xue39a02952015-03-02 15:37:47 +0800915 struct sock *sk = sock->sk;
916 int ret;
917
918 lock_sock(sk);
919 ret = __tipc_sendmsg(sock, m, dsz);
920 release_sock(sk);
921
922 return ret;
923}
924
925static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
926{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500927 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700928 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400929 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800930 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400931 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500932 u32 dnode, dport;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100933 struct sk_buff_head pktchain;
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100934 bool is_connectionless = tipc_sk_type_connectionless(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +0800935 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100936 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500937 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500938 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800939 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100940 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500942 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400943 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100944 if (unlikely(!dest)) {
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100945 if (is_connectionless && tsk->peer.family == AF_TIPC)
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100946 dest = &tsk->peer;
Erik Hugnef2f80362015-03-19 09:02:19 +0100947 else
948 return -EDESTADDRREQ;
949 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
950 dest->family != AF_TIPC) {
951 return -EINVAL;
952 }
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100953 if (!is_connectionless) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100954 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a02952015-03-02 15:37:47 +0800955 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100956 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a02952015-03-02 15:37:47 +0800957 return -EISCONN;
958 if (tsk->published)
959 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700960 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400961 tsk->conn_type = dest->addr.name.name.type;
962 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700963 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100965 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800966 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500967
968 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a02952015-03-02 15:37:47 +0800969 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500970 } else if (dest->addrtype == TIPC_ADDR_NAME) {
971 u32 type = dest->addr.name.name.type;
972 u32 inst = dest->addr.name.name.instance;
973 u32 domain = dest->addr.name.domain;
974
975 dnode = domain;
976 msg_set_type(mhdr, TIPC_NAMED_MSG);
977 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
978 msg_set_nametype(mhdr, type);
979 msg_set_nameinst(mhdr, inst);
980 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800981 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500982 msg_set_destnode(mhdr, dnode);
983 msg_set_destport(mhdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800984 if (unlikely(!dport && !dnode))
985 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500986 } else if (dest->addrtype == TIPC_ADDR_ID) {
987 dnode = dest->addr.id.node;
988 msg_set_type(mhdr, TIPC_DIRECT_MSG);
989 msg_set_lookup_scope(mhdr, 0);
990 msg_set_destnode(mhdr, dnode);
991 msg_set_destport(mhdr, dest->addr.id.ref);
992 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
993 }
994
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100995 skb_queue_head_init(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500996 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500997new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800998 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100999 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001000 if (rc < 0)
Ying Xue39a02952015-03-02 15:37:47 +08001001 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001002
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001003 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001004 skb = skb_peek(&pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +08001005 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001006 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001007 if (likely(!rc)) {
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001008 if (!is_connectionless)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001009 sock->state = SS_CONNECTING;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001010 return dsz;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001011 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001012 if (rc == -ELINKCONG) {
1013 tsk->link_cong = 1;
1014 rc = tipc_wait_for_sndmsg(sock, &timeo);
1015 if (!rc)
1016 continue;
1017 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001018 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -05001019 if (rc == -EMSGSIZE) {
1020 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001021 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -05001022 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001023 break;
1024 } while (1);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001025
1026 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027}
1028
Ying Xue391a6dd2014-01-17 09:50:06 +08001029static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
1030{
1031 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001032 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +08001033 DEFINE_WAIT(wait);
1034 int done;
1035
1036 do {
1037 int err = sock_error(sk);
1038 if (err)
1039 return err;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001040 if (sk->sk_state == TIPC_DISCONNECTING)
Ying Xue391a6dd2014-01-17 09:50:06 +08001041 return -EPIPE;
1042 else if (sock->state != SS_CONNECTED)
1043 return -ENOTCONN;
1044 if (!*timeo_p)
1045 return -EAGAIN;
1046 if (signal_pending(current))
1047 return sock_intr_errno(*timeo_p);
1048
1049 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1050 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001051 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001052 !tsk_conn_cong(tsk)) ||
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001053 !tipc_sk_connected(sk));
Ying Xue391a6dd2014-01-17 09:50:06 +08001054 finish_wait(sk_sleep(sk), &wait);
1055 } while (!done);
1056 return 0;
1057}
1058
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001059/**
Ying Xue247f0f32014-02-18 16:06:46 +08001060 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 * @sock: socket structure
1062 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001063 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001064 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001066 *
1067 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001068 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 */
Ying Xue1b784142015-03-02 15:37:48 +08001070static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001072 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +08001073 int ret;
1074
1075 lock_sock(sk);
1076 ret = __tipc_send_stream(sock, m, dsz);
1077 release_sock(sk);
1078
1079 return ret;
1080}
1081
1082static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1083{
1084 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001085 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001086 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001087 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001088 struct sk_buff_head pktchain;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001089 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001090 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001091 int rc = -EINVAL;
1092 long timeo;
1093 u32 dnode;
1094 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001095 struct iov_iter save;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001096 int hlen = MIN_H_SIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001097
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001098 /* Handle implied connection establishment */
1099 if (unlikely(dest)) {
Ying Xue39a02952015-03-02 15:37:47 +08001100 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001101 hlen = msg_hdr_sz(mhdr);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001102 if (dsz && (dsz == rc))
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001103 tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001104 return rc;
1105 }
1106 if (dsz > (uint)INT_MAX)
1107 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001108
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001109 if (unlikely(sock->state != SS_CONNECTED)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001110 if (sk->sk_state == TIPC_DISCONNECTING)
Ying Xue39a02952015-03-02 15:37:47 +08001111 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001112 else
Ying Xue39a02952015-03-02 15:37:47 +08001113 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001114 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001116 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Parthasarathy Bhuvaragan7cf87fa2016-11-01 14:02:34 +01001117 if (!timeo && tsk->link_cong)
1118 return -ELINKCONG;
1119
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001120 dnode = tsk_peer_node(tsk);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001121 skb_queue_head_init(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001122
1123next:
Al Virof25dcc72014-11-28 15:52:29 -05001124 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001125 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001126 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001127 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001128 if (unlikely(rc < 0))
Ying Xue39a02952015-03-02 15:37:47 +08001129 return rc;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001130
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001131 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001132 if (likely(!tsk_conn_cong(tsk))) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001133 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001134 if (likely(!rc)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001135 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001136 sent += send;
1137 if (sent == dsz)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001138 return dsz;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001139 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001140 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001141 if (rc == -EMSGSIZE) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001142 __skb_queue_purge(&pktchain);
Ying Xuef2f98002015-01-09 15:27:05 +08001143 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1144 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001145 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001146 goto next;
1147 }
1148 if (rc != -ELINKCONG)
1149 break;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001150
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001151 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001153 rc = tipc_wait_for_sndpkt(sock, &timeo);
1154 } while (!rc);
Ying Xue39a02952015-03-02 15:37:47 +08001155
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001156 __skb_queue_purge(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001157 return sent ? sent : rc;
1158}
1159
1160/**
1161 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001162 * @sock: socket structure
1163 * @m: message to send
1164 * @dsz: length of data to be transmitted
1165 *
1166 * Used for SOCK_SEQPACKET messages.
1167 *
1168 * Returns the number of bytes sent on success, or errno otherwise
1169 */
Ying Xue1b784142015-03-02 15:37:48 +08001170static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001171{
1172 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1173 return -EMSGSIZE;
1174
Ying Xue1b784142015-03-02 15:37:48 +08001175 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176}
1177
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001178/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001180static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001181 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182{
Ying Xue3721e9c2015-01-13 17:07:48 +08001183 struct sock *sk = &tsk->sk;
1184 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001185 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001187 msg_set_destnode(msg, peer_node);
1188 msg_set_destport(msg, peer_port);
1189 msg_set_type(msg, TIPC_CONN_MSG);
1190 msg_set_lookup_scope(msg, 0);
1191 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001192
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01001193 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001194 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001195 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1196 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001197 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001198 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1199 return;
1200
1201 /* Fall back to message based flow control */
1202 tsk->rcv_win = FLOWCTL_MSG_WIN;
1203 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204}
1205
1206/**
1207 * set_orig_addr - capture sender's address for received message
1208 * @m: descriptor for message info
1209 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001210 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 * Note: Address is not captured if not requested by receiver.
1212 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001213static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001215 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001217 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 addr->family = AF_TIPC;
1219 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001220 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 addr->addr.id.ref = msg_origport(msg);
1222 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001223 addr->addr.name.domain = 0; /* could leave uninitialized */
1224 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 m->msg_namelen = sizeof(struct sockaddr_tipc);
1226 }
1227}
1228
1229/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001230 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 * @m: descriptor for message info
1232 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001233 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001234 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001236 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 * Returns 0 if successful, otherwise errno
1238 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001239static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1240 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241{
1242 u32 anc_data[3];
1243 u32 err;
1244 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001245 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 int res;
1247
1248 if (likely(m->msg_controllen == 0))
1249 return 0;
1250
1251 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 err = msg ? msg_errcode(msg) : 0;
1253 if (unlikely(err)) {
1254 anc_data[0] = err;
1255 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001256 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1257 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001259 if (anc_data[1]) {
1260 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1261 msg_data(msg));
1262 if (res)
1263 return res;
1264 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265 }
1266
1267 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1269 switch (dest_type) {
1270 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001271 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 anc_data[0] = msg_nametype(msg);
1273 anc_data[1] = msg_namelower(msg);
1274 anc_data[2] = msg_namelower(msg);
1275 break;
1276 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001277 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 anc_data[0] = msg_nametype(msg);
1279 anc_data[1] = msg_namelower(msg);
1280 anc_data[2] = msg_nameupper(msg);
1281 break;
1282 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001283 has_name = (tsk->conn_type != 0);
1284 anc_data[0] = tsk->conn_type;
1285 anc_data[1] = tsk->conn_instance;
1286 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 break;
1288 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001289 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290 }
Allan Stephens2db99832010-12-31 18:59:33 +00001291 if (has_name) {
1292 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1293 if (res)
1294 return res;
1295 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296
1297 return 0;
1298}
1299
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001300static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001301{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001302 struct sock *sk = &tsk->sk;
1303 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001304 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001305 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001306 u32 peer_port = tsk_peer_port(tsk);
1307 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001308
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001309 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001310 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001311 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1312 dnode, tsk_own_node(tsk), peer_port,
1313 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001314 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001315 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001316 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001317 msg_set_conn_ack(msg, tsk->rcv_unacked);
1318 tsk->rcv_unacked = 0;
1319
1320 /* Adjust to and advertize the correct window limit */
1321 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1322 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1323 msg_set_adv_win(msg, tsk->rcv_win);
1324 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001325 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001326}
1327
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001328static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001329{
1330 struct sock *sk = sock->sk;
1331 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001332 long timeo = *timeop;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001333 int err;
1334
1335 for (;;) {
1336 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001337 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001338 if (sock->state == SS_DISCONNECTING) {
1339 err = -ENOTCONN;
1340 break;
1341 }
1342 release_sock(sk);
1343 timeo = schedule_timeout(timeo);
1344 lock_sock(sk);
1345 }
1346 err = 0;
1347 if (!skb_queue_empty(&sk->sk_receive_queue))
1348 break;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001349 err = -EAGAIN;
1350 if (!timeo)
1351 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001352 err = sock_intr_errno(timeo);
1353 if (signal_pending(current))
1354 break;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001355 }
1356 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001357 *timeop = timeo;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001358 return err;
1359}
1360
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001361/**
Ying Xue247f0f32014-02-18 16:06:46 +08001362 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 * @m: descriptor for message info
1364 * @buf_len: total size of user buffer area
1365 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001366 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1368 * If the complete message doesn't fit in user area, truncate it.
1369 *
1370 * Returns size of returned message data, errno otherwise
1371 */
Ying Xue1b784142015-03-02 15:37:48 +08001372static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1373 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001375 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001376 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 struct sk_buff *buf;
1378 struct tipc_msg *msg;
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001379 bool is_connectionless = tipc_sk_type_connectionless(sk);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001380 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 unsigned int sz;
1382 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001383 int res, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384
Allan Stephens0c3141e2008-04-15 00:22:02 -07001385 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 if (unlikely(!buf_len))
1387 return -EINVAL;
1388
Allan Stephens0c3141e2008-04-15 00:22:02 -07001389 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001391 if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 res = -ENOTCONN;
1393 goto exit;
1394 }
1395
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001396 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001397restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398
Allan Stephens0c3141e2008-04-15 00:22:02 -07001399 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001400 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001401 if (res)
1402 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403
1404 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001405 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 msg = buf_msg(buf);
1407 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001408 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409 err = msg_errcode(msg);
1410
Per Lidenb97bf3f2006-01-02 19:04:38 +01001411 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001413 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001414 goto restart;
1415 }
1416
1417 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 set_orig_addr(m, msg);
1419
1420 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001421 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001422 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423 goto exit;
1424
1425 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 if (!err) {
1427 if (unlikely(buf_len < sz)) {
1428 sz = buf_len;
1429 m->msg_flags |= MSG_TRUNC;
1430 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001431 res = skb_copy_datagram_msg(buf, hlen, m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001432 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 res = sz;
1435 } else {
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001436 if (is_connectionless || err == TIPC_CONN_SHUTDOWN ||
1437 m->msg_control)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 res = 0;
1439 else
1440 res = -ECONNRESET;
1441 }
1442
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001443 if (unlikely(flags & MSG_PEEK))
1444 goto exit;
1445
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001446 if (likely(!is_connectionless)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001447 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1448 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1449 tipc_sk_send_ack(tsk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001450 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001451 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001453 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454 return res;
1455}
1456
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001457/**
Ying Xue247f0f32014-02-18 16:06:46 +08001458 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 * @m: descriptor for message info
1460 * @buf_len: total size of user buffer area
1461 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001462 *
1463 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464 * will optionally wait for more; never truncates data.
1465 *
1466 * Returns size of returned message data, errno otherwise
1467 */
Ying Xue1b784142015-03-02 15:37:48 +08001468static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1469 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001471 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001472 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 struct sk_buff *buf;
1474 struct tipc_msg *msg;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001475 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 unsigned int sz;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001477 int target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001480 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481
1482 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483 if (unlikely(!buf_len))
1484 return -EINVAL;
1485
Allan Stephens0c3141e2008-04-15 00:22:02 -07001486 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001488 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489 res = -ENOTCONN;
1490 goto exit;
1491 }
1492
Florian Westphal3720d402010-08-17 11:00:04 +00001493 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001494 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001495
Allan Stephens0c3141e2008-04-15 00:22:02 -07001496restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001498 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001499 if (res)
1500 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001501
1502 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001503 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504 msg = buf_msg(buf);
1505 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001506 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507 err = msg_errcode(msg);
1508
1509 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001511 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512 goto restart;
1513 }
1514
1515 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 if (sz_copied == 0) {
1517 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001518 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001519 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520 goto exit;
1521 }
1522
1523 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524 if (!err) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001525 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1526 u32 needed;
1527 int sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528
Allan Stephens0232fd02011-02-21 09:45:40 -05001529 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 needed = (buf_len - sz_copied);
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001531 sz_to_copy = min(sz, needed);
Allan Stephens0232fd02011-02-21 09:45:40 -05001532
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001533 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001534 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001536
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537 sz_copied += sz_to_copy;
1538
1539 if (sz_to_copy < sz) {
1540 if (!(flags & MSG_PEEK))
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001541 TIPC_SKB_CB(buf)->bytes_read =
1542 offset + sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001543 goto exit;
1544 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001545 } else {
1546 if (sz_copied != 0)
1547 goto exit; /* can't add error msg to valid data */
1548
1549 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1550 res = 0;
1551 else
1552 res = -ECONNRESET;
1553 }
1554
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001555 if (unlikely(flags & MSG_PEEK))
1556 goto exit;
1557
1558 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1559 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1560 tipc_sk_send_ack(tsk);
1561 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562
1563 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001564 if ((sz_copied < buf_len) && /* didn't get all requested data */
1565 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001566 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001567 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568 goto restart;
1569
1570exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001571 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001572 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001573}
1574
1575/**
Ying Xuef288bef2012-08-21 11:16:57 +08001576 * tipc_write_space - wake up thread if port congestion is released
1577 * @sk: socket
1578 */
1579static void tipc_write_space(struct sock *sk)
1580{
1581 struct socket_wq *wq;
1582
1583 rcu_read_lock();
1584 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001585 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001586 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1587 POLLWRNORM | POLLWRBAND);
1588 rcu_read_unlock();
1589}
1590
1591/**
1592 * tipc_data_ready - wake up threads to indicate messages have been received
1593 * @sk: socket
1594 * @len: the length of messages
1595 */
David S. Miller676d2362014-04-11 16:15:36 -04001596static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001597{
1598 struct socket_wq *wq;
1599
1600 rcu_read_lock();
1601 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001602 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001603 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1604 POLLRDNORM | POLLRDBAND);
1605 rcu_read_unlock();
1606}
1607
Ying Xuef4195d12015-11-22 15:46:05 +08001608static void tipc_sock_destruct(struct sock *sk)
1609{
1610 __skb_queue_purge(&sk->sk_receive_queue);
1611}
1612
Ying Xuef288bef2012-08-21 11:16:57 +08001613/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001614 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001615 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001616 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001617 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001618 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001619 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001620static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001621{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001622 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001623 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001624 struct socket *sock = sk->sk_socket;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001625 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001626
Jon Paul Maloycda36962015-07-22 10:11:20 -04001627 if (unlikely(msg_mcast(hdr)))
1628 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001629
1630 switch ((int)sock->state) {
1631 case SS_CONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001632
Ying Xue7e6c1312012-11-29 18:39:14 -05001633 /* Accept only connection-based messages sent by peer */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001634 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1635 return false;
1636
1637 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001638 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001639 /* Let timer expire on it's own */
1640 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1641 tsk->portid);
Parthasarathy Bhuvaragan4891d8f2016-11-01 14:02:35 +01001642 sk->sk_state_change(sk);
Ying Xue7e6c1312012-11-29 18:39:14 -05001643 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001644 return true;
1645
Ying Xue7e6c1312012-11-29 18:39:14 -05001646 case SS_CONNECTING:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001647
Ying Xue7e6c1312012-11-29 18:39:14 -05001648 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001649 if (unlikely(!msg_connected(hdr)))
1650 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001651
Jon Paul Maloycda36962015-07-22 10:11:20 -04001652 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001653 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugne2c8d8512013-08-28 09:29:58 +02001654 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001655 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001656 }
1657
Jon Paul Maloycda36962015-07-22 10:11:20 -04001658 if (unlikely(!msg_isdata(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001659 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001660 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001661 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001662 }
1663
Jon Paul Maloycda36962015-07-22 10:11:20 -04001664 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1665 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001666 sock->state = SS_CONNECTED;
1667
Jon Paul Maloycda36962015-07-22 10:11:20 -04001668 /* If 'ACK+' message, add to socket receive queue */
1669 if (msg_data_sz(hdr))
1670 return true;
1671
1672 /* If empty 'ACK-' message, wake up sleeping connect() */
1673 if (waitqueue_active(sk_sleep(sk)))
1674 wake_up_interruptible(sk_sleep(sk));
1675
1676 /* 'ACK-' message is neither accepted nor rejected: */
1677 msg_set_dest_droppable(hdr, 1);
1678 return false;
1679
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001680 case SS_DISCONNECTING:
1681 break;
1682 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001683
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001684 switch (sk->sk_state) {
1685 case TIPC_OPEN:
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001686 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001687 break;
1688 case TIPC_LISTEN:
Ying Xue7e6c1312012-11-29 18:39:14 -05001689 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001690 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1691 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001692 break;
Ying Xue7e6c1312012-11-29 18:39:14 -05001693 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001694 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05001695 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001696
Jon Paul Maloycda36962015-07-22 10:11:20 -04001697 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001698}
1699
1700/**
Ying Xueaba79f32013-01-20 23:30:09 +01001701 * rcvbuf_limit - get proper overload limit of socket receive queue
1702 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001703 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001704 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001705 * For connection oriented messages, irrespective of importance,
1706 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001707 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001708 * For connectionless messages, queue limits are based on message
1709 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001710 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001711 * TIPC_LOW_IMPORTANCE (2 MB)
1712 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1713 * TIPC_HIGH_IMPORTANCE (8 MB)
1714 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001715 *
1716 * Returns overload limit according to corresponding message importance
1717 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001718static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001719{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001720 struct tipc_sock *tsk = tipc_sk(sk);
1721 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001722
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001723 if (unlikely(!msg_connected(hdr)))
1724 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001725
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001726 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1727 return sk->sk_rcvbuf;
1728
1729 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001730}
1731
1732/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001733 * filter_rcv - validate incoming message
1734 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001735 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001736 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001737 * Enqueues message on receive queue if acceptable; optionally handles
1738 * disconnect indication for a connected socket.
1739 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001740 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001741 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001742 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001744static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1745 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001747 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001748 struct tipc_msg *hdr = buf_msg(skb);
1749 unsigned int limit = rcvbuf_limit(sk, skb);
1750 int err = TIPC_OK;
1751 int usr = msg_user(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752
Jon Paul Maloycda36962015-07-22 10:11:20 -04001753 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001754 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001755 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001756 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001757
Jon Paul Maloycda36962015-07-22 10:11:20 -04001758 if (unlikely(usr == SOCK_WAKEUP)) {
1759 kfree_skb(skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001760 tsk->link_cong = 0;
1761 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001762 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001763 }
1764
Jon Paul Maloycda36962015-07-22 10:11:20 -04001765 /* Drop if illegal message type */
1766 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1767 kfree_skb(skb);
1768 return false;
1769 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001770
Jon Paul Maloycda36962015-07-22 10:11:20 -04001771 /* Reject if wrong message type for current socket state */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001772 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001773 if (msg_connected(hdr)) {
1774 err = TIPC_ERR_NO_PORT;
1775 goto reject;
1776 }
1777 } else if (unlikely(!filter_connect(tsk, skb))) {
1778 err = TIPC_ERR_NO_PORT;
1779 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 }
1781
1782 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001783 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1784 err = TIPC_ERR_OVERLOAD;
1785 goto reject;
1786 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787
Ying Xueaba79f32013-01-20 23:30:09 +01001788 /* Enqueue message */
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001789 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001790 __skb_queue_tail(&sk->sk_receive_queue, skb);
1791 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001792
David S. Miller676d2362014-04-11 16:15:36 -04001793 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001794 return true;
1795
1796reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001797 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1798 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001799 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800}
1801
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001802/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001803 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001804 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001805 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001806 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001807 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001808 *
1809 * Returns 0
1810 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001811static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001812{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001813 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001814 struct sk_buff_head xmitq;
1815 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001816
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001817 __skb_queue_head_init(&xmitq);
1818
1819 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001820 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001821 return 0;
1822 }
1823
1824 if (skb_queue_empty(&xmitq))
1825 return 0;
1826
1827 /* Send response/rejected message */
1828 skb = __skb_dequeue(&xmitq);
1829 dnode = msg_destnode(buf_msg(skb));
1830 selector = msg_origport(buf_msg(skb));
1831 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001832 return 0;
1833}
1834
1835/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001836 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1837 * inputq and try adding them to socket or backlog queue
1838 * @inputq: list of incoming buffers with potentially different destinations
1839 * @sk: socket where the buffers should be enqueued
1840 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001841 *
1842 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001843 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001844static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001845 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001846{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001847 unsigned long time_limit = jiffies + 2;
1848 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001849 unsigned int lim;
1850 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001851 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001852
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001853 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001854 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001855 return;
1856
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001857 skb = tipc_skb_dequeue(inputq, dport);
1858 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001859 return;
1860
1861 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001862 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001863 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001864 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001865 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001866
1867 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001868 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001869 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001870 atomic_set(dcnt, 0);
1871 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1872 if (likely(!sk_add_backlog(sk, skb, lim)))
1873 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001874
1875 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001876 onode = tipc_own_addr(sock_net(sk));
1877 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1878 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001879 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001880 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001881}
1882
1883/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001884 * tipc_sk_rcv - handle a chain of incoming buffers
1885 * @inputq: buffer list containing the buffers
1886 * Consumes all buffers in list until inputq is empty
1887 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001888 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001889void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001890{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001891 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001892 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001893 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001894 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001895 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001896 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001897
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001898 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001899 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001900 dport = tipc_skb_peek_port(inputq, dport);
1901 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001902
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001903 if (likely(tsk)) {
1904 sk = &tsk->sk;
1905 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001906 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001907 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001908 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001909 /* Send pending response/rejected messages, if any */
1910 while ((skb = __skb_dequeue(&xmitq))) {
1911 dnode = msg_destnode(buf_msg(skb));
1912 tipc_node_xmit_skb(net, skb, dnode, dport);
1913 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001914 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001915 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001916 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001917
1918 /* No destination socket => dequeue skb if still there */
1919 skb = tipc_skb_dequeue(inputq, dport);
1920 if (!skb)
1921 return;
1922
1923 /* Try secondary lookup if unresolved named message */
1924 err = TIPC_ERR_NO_PORT;
1925 if (tipc_msg_lookup_dest(net, skb, &err))
1926 goto xmit;
1927
1928 /* Prepare for message rejection */
1929 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001930 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001931xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001932 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001933 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001934 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001935}
1936
Ying Xue78eb3a52014-01-17 09:50:03 +08001937static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1938{
1939 struct sock *sk = sock->sk;
1940 DEFINE_WAIT(wait);
1941 int done;
1942
1943 do {
1944 int err = sock_error(sk);
1945 if (err)
1946 return err;
1947 if (!*timeo_p)
1948 return -ETIMEDOUT;
1949 if (signal_pending(current))
1950 return sock_intr_errno(*timeo_p);
1951
1952 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1953 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1954 finish_wait(sk_sleep(sk), &wait);
1955 } while (!done);
1956 return 0;
1957}
1958
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959/**
Ying Xue247f0f32014-02-18 16:06:46 +08001960 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001961 * @sock: socket structure
1962 * @dest: socket address for destination port
1963 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001964 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965 *
1966 * Returns 0 on success, errno otherwise
1967 */
Ying Xue247f0f32014-02-18 16:06:46 +08001968static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1969 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001971 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001972 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001973 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1974 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001975 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Ying Xue78eb3a52014-01-17 09:50:03 +08001976 socket_state previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001977 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978
Allan Stephens0c3141e2008-04-15 00:22:02 -07001979 lock_sock(sk);
1980
Erik Hugnef2f80362015-03-19 09:02:19 +01001981 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001982 if (tipc_sk_type_connectionless(sk)) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001983 if (dst->family == AF_UNSPEC) {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001984 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
Sasha Levin610600c2015-03-23 15:30:00 -04001985 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1986 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001987 } else {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001988 memcpy(&tsk->peer, dest, destlen);
Erik Hugnef2f80362015-03-19 09:02:19 +01001989 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001990 goto exit;
1991 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992
Allan Stephensb89741a2008-04-15 00:20:37 -07001993 /*
1994 * Reject connection attempt using multicast address
1995 *
1996 * Note: send_msg() validates the rest of the address fields,
1997 * so there's no need to do it here
1998 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001999 if (dst->addrtype == TIPC_ADDR_MCAST) {
2000 res = -EINVAL;
2001 goto exit;
2002 }
2003
Ying Xue78eb3a52014-01-17 09:50:03 +08002004 previous = sock->state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002005
2006 switch (sk->sk_state) {
2007 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05002008 /* Send a 'SYN-' to destination */
2009 m.msg_name = dest;
2010 m.msg_namelen = destlen;
2011
2012 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2013 * indicate send_msg() is never blocked.
2014 */
2015 if (!timeout)
2016 m.msg_flags = MSG_DONTWAIT;
2017
Ying Xue39a02952015-03-02 15:37:47 +08002018 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05002019 if ((res < 0) && (res != -EWOULDBLOCK))
2020 goto exit;
2021
2022 /* Just entered SS_CONNECTING state; the only
2023 * difference is that return value in non-blocking
2024 * case is EINPROGRESS, rather than EALREADY.
2025 */
2026 res = -EINPROGRESS;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002027 break;
2028 }
2029
2030 switch (sock->state) {
Ying Xue584d24b2012-11-29 18:51:19 -05002031 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08002032 if (previous == SS_CONNECTING)
2033 res = -EALREADY;
2034 if (!timeout)
2035 goto exit;
2036 timeout = msecs_to_jiffies(timeout);
2037 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2038 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05002039 break;
2040 case SS_CONNECTED:
2041 res = -EISCONN;
2042 break;
2043 default:
2044 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08002045 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07002046 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07002047exit:
2048 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002049 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002050}
2051
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002052/**
Ying Xue247f0f32014-02-18 16:06:46 +08002053 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01002054 * @sock: socket structure
2055 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002056 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057 * Returns 0 on success, errno otherwise
2058 */
Ying Xue247f0f32014-02-18 16:06:46 +08002059static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002060{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002061 struct sock *sk = sock->sk;
2062 int res;
2063
2064 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002065 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002066 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002067
Allan Stephens0c3141e2008-04-15 00:22:02 -07002068 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069}
2070
Ying Xue6398e232014-01-17 09:50:04 +08002071static int tipc_wait_for_accept(struct socket *sock, long timeo)
2072{
2073 struct sock *sk = sock->sk;
2074 DEFINE_WAIT(wait);
2075 int err;
2076
2077 /* True wake-one mechanism for incoming connections: only
2078 * one process gets woken up, not the 'whole herd'.
2079 * Since we do not 'race & poll' for established sockets
2080 * anymore, the common case will execute the loop only once.
2081 */
2082 for (;;) {
2083 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2084 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002085 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002086 release_sock(sk);
2087 timeo = schedule_timeout(timeo);
2088 lock_sock(sk);
2089 }
2090 err = 0;
2091 if (!skb_queue_empty(&sk->sk_receive_queue))
2092 break;
Ying Xue6398e232014-01-17 09:50:04 +08002093 err = -EAGAIN;
2094 if (!timeo)
2095 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002096 err = sock_intr_errno(timeo);
2097 if (signal_pending(current))
2098 break;
Ying Xue6398e232014-01-17 09:50:04 +08002099 }
2100 finish_wait(sk_sleep(sk), &wait);
2101 return err;
2102}
2103
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002104/**
Ying Xue247f0f32014-02-18 16:06:46 +08002105 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 * @sock: listening socket
2107 * @newsock: new socket that is to be connected
2108 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002109 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002110 * Returns 0 on success, errno otherwise
2111 */
Ying Xue247f0f32014-02-18 16:06:46 +08002112static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002114 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002116 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002117 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002118 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002119 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002120
Allan Stephens0c3141e2008-04-15 00:22:02 -07002121 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002122
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002123 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002124 res = -EINVAL;
2125 goto exit;
2126 }
Ying Xue6398e232014-01-17 09:50:04 +08002127 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2128 res = tipc_wait_for_accept(sock, timeo);
2129 if (res)
2130 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002131
2132 buf = skb_peek(&sk->sk_receive_queue);
2133
Parthasarathy Bhuvaragancb5da842016-11-01 14:02:36 +01002134 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002135 if (res)
2136 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002137 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002138
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002139 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002140 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002141 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002142
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002143 /* we lock on new_sk; but lockdep sees the lock on sk */
2144 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002145
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002146 /*
2147 * Reject any stray messages received by new socket
2148 * before the socket lock was taken (very, very unlikely)
2149 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002150 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002151
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002152 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002153 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002154 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002155
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002156 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002157 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002158 new_tsock->conn_type = msg_nametype(msg);
2159 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002160 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002161
2162 /*
2163 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2164 * Respond to 'SYN+' by queuing it on new socket.
2165 */
2166 if (!msg_data_sz(msg)) {
2167 struct msghdr m = {NULL,};
2168
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002169 tsk_advance_rx_queue(sk);
Ying Xue39a02952015-03-02 15:37:47 +08002170 __tipc_send_stream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002171 } else {
2172 __skb_dequeue(&sk->sk_receive_queue);
2173 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002174 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002175 }
2176 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002177exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002178 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002179 return res;
2180}
2181
2182/**
Ying Xue247f0f32014-02-18 16:06:46 +08002183 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002184 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002185 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002186 *
2187 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002188 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002189 * Returns 0 on success, errno otherwise
2190 */
Ying Xue247f0f32014-02-18 16:06:46 +08002191static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002192{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002193 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002194 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002195 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002196 struct sk_buff *skb;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002197 u32 dnode = tsk_peer_node(tsk);
2198 u32 dport = tsk_peer_port(tsk);
2199 u32 onode = tipc_own_addr(net);
2200 u32 oport = tsk->portid;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002201 int res;
2202
Allan Stephense247a8f2008-03-06 15:05:38 -08002203 if (how != SHUT_RDWR)
2204 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002205
Allan Stephens0c3141e2008-04-15 00:22:02 -07002206 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002207
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002208 if (sock->state == SS_CONNECTING || sock->state == SS_CONNECTED) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002209
Per Lidenb97bf3f2006-01-02 19:04:38 +01002210restart:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002211 dnode = tsk_peer_node(tsk);
2212
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002213 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002214 skb = __skb_dequeue(&sk->sk_receive_queue);
2215 if (skb) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01002216 if (TIPC_SKB_CB(skb)->bytes_read) {
Ying Xuea6ca1092014-11-26 11:41:55 +08002217 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002218 goto restart;
2219 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002220 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002221 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002222 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002223 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloycda36962015-07-22 10:11:20 -04002224 0, dnode, onode, dport, oport,
2225 TIPC_CONN_SHUTDOWN);
Vegard Nossumd2fbdf72016-07-23 08:15:04 +02002226 if (skb)
2227 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002228 }
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002229 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Ying Xuef2f98002015-01-09 15:27:05 +08002230 tipc_node_remove_conn(net, dnode, tsk->portid);
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002231 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002232
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002233 switch (sk->sk_state) {
2234 case TIPC_DISCONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002235
Ying Xue75031152012-10-29 09:38:15 -04002236 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002237 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002238
2239 /* Wake up anyone sleeping in poll */
2240 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002241 res = 0;
2242 break;
2243
2244 default:
2245 res = -ENOTCONN;
2246 }
2247
Allan Stephens0c3141e2008-04-15 00:22:02 -07002248 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002249 return res;
2250}
2251
Ying Xuef2f2a962015-01-09 15:27:02 +08002252static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002253{
Ying Xuef2f2a962015-01-09 15:27:02 +08002254 struct tipc_sock *tsk = (struct tipc_sock *)data;
2255 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002256 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002257 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002258 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002259
Jon Paul Maloy57289012014-08-22 18:09:09 -04002260 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002261 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002262 bh_unlock_sock(sk);
2263 goto exit;
2264 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002265 peer_port = tsk_peer_port(tsk);
2266 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002267
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002268 if (tsk->probe_unacked) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002269 if (!sock_owned_by_user(sk)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002270 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugneb3be5e32015-06-09 17:27:12 +02002271 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2272 tsk_peer_port(tsk));
2273 sk->sk_state_change(sk);
2274 } else {
2275 /* Try again later */
2276 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2277 }
2278
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002279 bh_unlock_sock(sk);
2280 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002281 }
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002282
2283 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2284 INT_H_SIZE, 0, peer_node, own_node,
2285 peer_port, tsk->portid, TIPC_OK);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002286 tsk->probe_unacked = true;
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002287 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002288 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002289 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002290 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002291exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002292 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002293}
2294
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002295static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002296 struct tipc_name_seq const *seq)
2297{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002298 struct sock *sk = &tsk->sk;
2299 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002300 struct publication *publ;
2301 u32 key;
2302
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002303 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002304 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002305 key = tsk->portid + tsk->pub_count + 1;
2306 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002307 return -EADDRINUSE;
2308
Ying Xuef2f98002015-01-09 15:27:05 +08002309 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002310 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002311 if (unlikely(!publ))
2312 return -EINVAL;
2313
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002314 list_add(&publ->pport_list, &tsk->publications);
2315 tsk->pub_count++;
2316 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002317 return 0;
2318}
2319
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002320static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002321 struct tipc_name_seq const *seq)
2322{
Ying Xuef2f98002015-01-09 15:27:05 +08002323 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002324 struct publication *publ;
2325 struct publication *safe;
2326 int rc = -EINVAL;
2327
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002328 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002329 if (seq) {
2330 if (publ->scope != scope)
2331 continue;
2332 if (publ->type != seq->type)
2333 continue;
2334 if (publ->lower != seq->lower)
2335 continue;
2336 if (publ->upper != seq->upper)
2337 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002338 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002339 publ->ref, publ->key);
2340 rc = 0;
2341 break;
2342 }
Ying Xuef2f98002015-01-09 15:27:05 +08002343 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002344 publ->ref, publ->key);
2345 rc = 0;
2346 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002347 if (list_empty(&tsk->publications))
2348 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002349 return rc;
2350}
2351
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002352/* tipc_sk_reinit: set non-zero address in all existing sockets
2353 * when we go from standalone to network mode.
2354 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002355void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002356{
Ying Xuee05b31f2015-01-09 15:27:08 +08002357 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002358 const struct bucket_table *tbl;
2359 struct rhash_head *pos;
2360 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002361 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002362 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002363
Ying Xue07f6c4b2015-01-07 13:41:58 +08002364 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002365 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002366 for (i = 0; i < tbl->size; i++) {
2367 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2368 spin_lock_bh(&tsk->sk.sk_lock.slock);
2369 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002370 msg_set_prevnode(msg, tn->own_addr);
2371 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002372 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2373 }
2374 }
2375 rcu_read_unlock();
2376}
2377
Ying Xuee05b31f2015-01-09 15:27:08 +08002378static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002379{
Ying Xuee05b31f2015-01-09 15:27:08 +08002380 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002381 struct tipc_sock *tsk;
2382
2383 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002384 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002385 if (tsk)
2386 sock_hold(&tsk->sk);
2387 rcu_read_unlock();
2388
2389 return tsk;
2390}
2391
2392static int tipc_sk_insert(struct tipc_sock *tsk)
2393{
Ying Xuee05b31f2015-01-09 15:27:08 +08002394 struct sock *sk = &tsk->sk;
2395 struct net *net = sock_net(sk);
2396 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002397 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2398 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2399
2400 while (remaining--) {
2401 portid++;
2402 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2403 portid = TIPC_MIN_PORT;
2404 tsk->portid = portid;
2405 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002406 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2407 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002408 return 0;
2409 sock_put(&tsk->sk);
2410 }
2411
2412 return -1;
2413}
2414
2415static void tipc_sk_remove(struct tipc_sock *tsk)
2416{
2417 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002418 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002419
Herbert Xu6cca72892015-03-20 21:57:05 +11002420 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002421 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2422 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002423 }
2424}
2425
Herbert Xu6cca72892015-03-20 21:57:05 +11002426static const struct rhashtable_params tsk_rht_params = {
2427 .nelem_hint = 192,
2428 .head_offset = offsetof(struct tipc_sock, node),
2429 .key_offset = offsetof(struct tipc_sock, portid),
2430 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002431 .max_size = 1048576,
2432 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002433 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002434};
2435
Ying Xuee05b31f2015-01-09 15:27:08 +08002436int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002437{
Ying Xuee05b31f2015-01-09 15:27:08 +08002438 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002439
Herbert Xu6cca72892015-03-20 21:57:05 +11002440 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002441}
2442
Ying Xuee05b31f2015-01-09 15:27:08 +08002443void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002444{
Ying Xuee05b31f2015-01-09 15:27:08 +08002445 struct tipc_net *tn = net_generic(net, tipc_net_id);
2446
Ying Xue07f6c4b2015-01-07 13:41:58 +08002447 /* Wait for socket readers to complete */
2448 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002449
Ying Xuee05b31f2015-01-09 15:27:08 +08002450 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002451}
2452
2453/**
Ying Xue247f0f32014-02-18 16:06:46 +08002454 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002455 * @sock: socket structure
2456 * @lvl: option level
2457 * @opt: option identifier
2458 * @ov: pointer to new option value
2459 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002460 *
2461 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002462 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002463 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464 * Returns 0 on success, errno otherwise
2465 */
Ying Xue247f0f32014-02-18 16:06:46 +08002466static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2467 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002468{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002469 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002470 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002471 u32 value;
2472 int res;
2473
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002474 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2475 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002476 if (lvl != SOL_TIPC)
2477 return -ENOPROTOOPT;
2478 if (ol < sizeof(value))
2479 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002480 res = get_user(value, (u32 __user *)ov);
2481 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002482 return res;
2483
Allan Stephens0c3141e2008-04-15 00:22:02 -07002484 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002485
Per Lidenb97bf3f2006-01-02 19:04:38 +01002486 switch (opt) {
2487 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002488 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002489 break;
2490 case TIPC_SRC_DROPPABLE:
2491 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002492 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002493 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002494 res = -ENOPROTOOPT;
2495 break;
2496 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002497 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002498 break;
2499 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002500 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002501 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002502 break;
2503 default:
2504 res = -EINVAL;
2505 }
2506
Allan Stephens0c3141e2008-04-15 00:22:02 -07002507 release_sock(sk);
2508
Per Lidenb97bf3f2006-01-02 19:04:38 +01002509 return res;
2510}
2511
2512/**
Ying Xue247f0f32014-02-18 16:06:46 +08002513 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002514 * @sock: socket structure
2515 * @lvl: option level
2516 * @opt: option identifier
2517 * @ov: receptacle for option value
2518 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002519 *
2520 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002521 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002522 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002523 * Returns 0 on success, errno otherwise
2524 */
Ying Xue247f0f32014-02-18 16:06:46 +08002525static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2526 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002527{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002528 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002529 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002530 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002531 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002532 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002533
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002534 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2535 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002536 if (lvl != SOL_TIPC)
2537 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002538 res = get_user(len, ol);
2539 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002540 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002541
Allan Stephens0c3141e2008-04-15 00:22:02 -07002542 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002543
2544 switch (opt) {
2545 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002546 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002547 break;
2548 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002549 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002550 break;
2551 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002552 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002553 break;
2554 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002555 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002556 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002557 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002558 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002559 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002560 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002561 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002562 value = skb_queue_len(&sk->sk_receive_queue);
2563 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002564 default:
2565 res = -EINVAL;
2566 }
2567
Allan Stephens0c3141e2008-04-15 00:22:02 -07002568 release_sock(sk);
2569
Paul Gortmaker25860c32010-12-31 18:59:31 +00002570 if (res)
2571 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002572
Paul Gortmaker25860c32010-12-31 18:59:31 +00002573 if (len < sizeof(value))
2574 return -EINVAL;
2575
2576 if (copy_to_user(ov, &value, sizeof(value)))
2577 return -EFAULT;
2578
2579 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002580}
2581
Ying Xuef2f98002015-01-09 15:27:05 +08002582static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002583{
Ying Xuef2f98002015-01-09 15:27:05 +08002584 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002585 struct tipc_sioc_ln_req lnr;
2586 void __user *argp = (void __user *)arg;
2587
2588 switch (cmd) {
2589 case SIOCGETLINKNAME:
2590 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2591 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002592 if (!tipc_node_get_linkname(sock_net(sk),
2593 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002594 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2595 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2596 return -EFAULT;
2597 return 0;
2598 }
2599 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002600 default:
2601 return -ENOIOCTLCMD;
2602 }
2603}
2604
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002605/* Protocol switches for the various types of TIPC sockets */
2606
Florian Westphalbca65ea2008-02-07 18:18:01 -08002607static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002608 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002609 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002610 .release = tipc_release,
2611 .bind = tipc_bind,
2612 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002613 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002614 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002615 .getname = tipc_getname,
2616 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002617 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002618 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002619 .shutdown = tipc_shutdown,
2620 .setsockopt = tipc_setsockopt,
2621 .getsockopt = tipc_getsockopt,
2622 .sendmsg = tipc_sendmsg,
2623 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002624 .mmap = sock_no_mmap,
2625 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002626};
2627
Florian Westphalbca65ea2008-02-07 18:18:01 -08002628static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002629 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002630 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002631 .release = tipc_release,
2632 .bind = tipc_bind,
2633 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002634 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002635 .accept = tipc_accept,
2636 .getname = tipc_getname,
2637 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002638 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002639 .listen = tipc_listen,
2640 .shutdown = tipc_shutdown,
2641 .setsockopt = tipc_setsockopt,
2642 .getsockopt = tipc_getsockopt,
2643 .sendmsg = tipc_send_packet,
2644 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002645 .mmap = sock_no_mmap,
2646 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002647};
2648
Florian Westphalbca65ea2008-02-07 18:18:01 -08002649static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002650 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002651 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002652 .release = tipc_release,
2653 .bind = tipc_bind,
2654 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002655 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002656 .accept = tipc_accept,
2657 .getname = tipc_getname,
2658 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002659 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002660 .listen = tipc_listen,
2661 .shutdown = tipc_shutdown,
2662 .setsockopt = tipc_setsockopt,
2663 .getsockopt = tipc_getsockopt,
2664 .sendmsg = tipc_send_stream,
2665 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002666 .mmap = sock_no_mmap,
2667 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002668};
2669
Florian Westphalbca65ea2008-02-07 18:18:01 -08002670static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002671 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002672 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002673 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002674};
2675
2676static struct proto tipc_proto = {
2677 .name = "TIPC",
2678 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002679 .obj_size = sizeof(struct tipc_sock),
2680 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002681};
2682
2683/**
Per Liden4323add2006-01-18 00:38:21 +01002684 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002685 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002686 * Returns 0 on success, errno otherwise
2687 */
Per Liden4323add2006-01-18 00:38:21 +01002688int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002689{
2690 int res;
2691
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002692 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002693 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002694 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002695 goto out;
2696 }
2697
2698 res = sock_register(&tipc_family_ops);
2699 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002700 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002701 proto_unregister(&tipc_proto);
2702 goto out;
2703 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002704 out:
2705 return res;
2706}
2707
2708/**
Per Liden4323add2006-01-18 00:38:21 +01002709 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002710 */
Per Liden4323add2006-01-18 00:38:21 +01002711void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002712{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002713 sock_unregister(tipc_family_ops.family);
2714 proto_unregister(&tipc_proto);
2715}
Richard Alpe34b78a122014-11-20 10:29:10 +01002716
2717/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002718static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002719{
2720 u32 peer_node;
2721 u32 peer_port;
2722 struct nlattr *nest;
2723
2724 peer_node = tsk_peer_node(tsk);
2725 peer_port = tsk_peer_port(tsk);
2726
2727 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2728
2729 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2730 goto msg_full;
2731 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2732 goto msg_full;
2733
2734 if (tsk->conn_type != 0) {
2735 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2736 goto msg_full;
2737 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2738 goto msg_full;
2739 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2740 goto msg_full;
2741 }
2742 nla_nest_end(skb, nest);
2743
2744 return 0;
2745
2746msg_full:
2747 nla_nest_cancel(skb, nest);
2748
2749 return -EMSGSIZE;
2750}
2751
2752/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002753static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2754 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002755{
2756 int err;
2757 void *hdr;
2758 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002759 struct net *net = sock_net(skb->sk);
2760 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002761 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002762
2763 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002764 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002765 if (!hdr)
2766 goto msg_cancel;
2767
2768 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2769 if (!attrs)
2770 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002771 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002772 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002773 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002774 goto attr_msg_cancel;
2775
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002776 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002777 err = __tipc_nl_add_sk_con(skb, tsk);
2778 if (err)
2779 goto attr_msg_cancel;
2780 } else if (!list_empty(&tsk->publications)) {
2781 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2782 goto attr_msg_cancel;
2783 }
2784 nla_nest_end(skb, attrs);
2785 genlmsg_end(skb, hdr);
2786
2787 return 0;
2788
2789attr_msg_cancel:
2790 nla_nest_cancel(skb, attrs);
2791genlmsg_cancel:
2792 genlmsg_cancel(skb, hdr);
2793msg_cancel:
2794 return -EMSGSIZE;
2795}
2796
2797int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2798{
2799 int err;
2800 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002801 const struct bucket_table *tbl;
2802 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002803 struct net *net = sock_net(skb->sk);
2804 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002805 u32 tbl_id = cb->args[0];
2806 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002807
Ying Xue07f6c4b2015-01-07 13:41:58 +08002808 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002809 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002810 for (; tbl_id < tbl->size; tbl_id++) {
2811 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002812 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002813 if (prev_portid && prev_portid != tsk->portid) {
2814 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2815 continue;
2816 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002817
Richard Alped6e164e2015-01-16 12:30:40 +01002818 err = __tipc_nl_add_sk(skb, cb, tsk);
2819 if (err) {
2820 prev_portid = tsk->portid;
2821 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2822 goto out;
2823 }
2824 prev_portid = 0;
2825 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002826 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002827 }
Richard Alped6e164e2015-01-16 12:30:40 +01002828out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002829 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002830 cb->args[0] = tbl_id;
2831 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002832
2833 return skb->len;
2834}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002835
2836/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002837static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2838 struct netlink_callback *cb,
2839 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002840{
2841 void *hdr;
2842 struct nlattr *attrs;
2843
2844 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002845 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002846 if (!hdr)
2847 goto msg_cancel;
2848
2849 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2850 if (!attrs)
2851 goto genlmsg_cancel;
2852
2853 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2854 goto attr_msg_cancel;
2855 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2856 goto attr_msg_cancel;
2857 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2858 goto attr_msg_cancel;
2859 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2860 goto attr_msg_cancel;
2861
2862 nla_nest_end(skb, attrs);
2863 genlmsg_end(skb, hdr);
2864
2865 return 0;
2866
2867attr_msg_cancel:
2868 nla_nest_cancel(skb, attrs);
2869genlmsg_cancel:
2870 genlmsg_cancel(skb, hdr);
2871msg_cancel:
2872 return -EMSGSIZE;
2873}
2874
2875/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002876static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2877 struct netlink_callback *cb,
2878 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002879{
2880 int err;
2881 struct publication *p;
2882
2883 if (*last_publ) {
2884 list_for_each_entry(p, &tsk->publications, pport_list) {
2885 if (p->key == *last_publ)
2886 break;
2887 }
2888 if (p->key != *last_publ) {
2889 /* We never set seq or call nl_dump_check_consistent()
2890 * this means that setting prev_seq here will cause the
2891 * consistence check to fail in the netlink callback
2892 * handler. Resulting in the last NLMSG_DONE message
2893 * having the NLM_F_DUMP_INTR flag set.
2894 */
2895 cb->prev_seq = 1;
2896 *last_publ = 0;
2897 return -EPIPE;
2898 }
2899 } else {
2900 p = list_first_entry(&tsk->publications, struct publication,
2901 pport_list);
2902 }
2903
2904 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2905 err = __tipc_nl_add_sk_publ(skb, cb, p);
2906 if (err) {
2907 *last_publ = p->key;
2908 return err;
2909 }
2910 }
2911 *last_publ = 0;
2912
2913 return 0;
2914}
2915
2916int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2917{
2918 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002919 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002920 u32 last_publ = cb->args[1];
2921 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002922 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002923 struct tipc_sock *tsk;
2924
Ying Xue07f6c4b2015-01-07 13:41:58 +08002925 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002926 struct nlattr **attrs;
2927 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2928
2929 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2930 if (err)
2931 return err;
2932
Richard Alpe45e093a2016-05-16 11:14:54 +02002933 if (!attrs[TIPC_NLA_SOCK])
2934 return -EINVAL;
2935
Richard Alpe1a1a1432014-11-20 10:29:11 +01002936 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2937 attrs[TIPC_NLA_SOCK],
2938 tipc_nl_sock_policy);
2939 if (err)
2940 return err;
2941
2942 if (!sock[TIPC_NLA_SOCK_REF])
2943 return -EINVAL;
2944
Ying Xue07f6c4b2015-01-07 13:41:58 +08002945 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002946 }
2947
2948 if (done)
2949 return 0;
2950
Ying Xuee05b31f2015-01-09 15:27:08 +08002951 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002952 if (!tsk)
2953 return -EINVAL;
2954
2955 lock_sock(&tsk->sk);
2956 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2957 if (!err)
2958 done = 1;
2959 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002960 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002961
Ying Xue07f6c4b2015-01-07 13:41:58 +08002962 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002963 cb->args[1] = last_publ;
2964 cb->args[2] = done;
2965
2966 return skb->len;
2967}