blob: 7b6a1847cf8a22059df10d4af53b6975867088b9 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05004 * Copyright (c) 2001-2007, 2012-2015, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Ying Xue07f6c4b2015-01-07 13:41:58 +080037#include <linux/rhashtable.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Jon Paul Maloyc637c102015-02-05 08:36:41 -050042#include "name_distr.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040043#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040044#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010045#include "netlink.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040046
Ying Xue07f6c4b2015-01-07 13:41:58 +080047#define SS_LISTENING -1 /* socket is listening */
48#define SS_READY -2 /* socket is connectionless */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080051#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080052#define TIPC_FWD_MSG 1
53#define TIPC_CONN_OK 0
54#define TIPC_CONN_PROBING 1
55#define TIPC_MAX_PORT 0xffffffff
56#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040057
58/**
59 * struct tipc_sock - TIPC socket structure
60 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040061 * @conn_type: TIPC type used when connection was established
62 * @conn_instance: TIPC instance used when connection was established
63 * @published: non-zero if port has one or more associated names
64 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080065 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040066 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy301bae52014-08-22 18:09:20 -040067 * @publications: list of publications for port
68 * @pub_count: total # of publications port has made during its lifetime
69 * @probing_state:
Ying Xue2f55c432015-01-09 15:27:00 +080070 * @probing_intv:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040071 * @conn_timeout: the time we can wait for an unresponded setup request
72 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
73 * @link_cong: non-zero if owner must sleep because of link congestion
74 * @sent_unacked: # messages sent by socket, and not yet acked by peer
75 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010076 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080077 * @node: hash table node
78 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040079 */
80struct tipc_sock {
81 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040082 u32 conn_type;
83 u32 conn_instance;
84 int published;
85 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080086 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040087 struct tipc_msg phdr;
88 struct list_head sock_list;
89 struct list_head publications;
90 u32 pub_count;
91 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080092 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040093 uint conn_timeout;
94 atomic_t dupl_rcvcnt;
95 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
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400299/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400300 *
301 * Handles cases where the node's network address has changed from
302 * the default of <0.0.0> to its configured setting.
303 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400304static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400305{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100306 struct sock *sk = &tsk->sk;
307 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400308 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400309 u32 orig_node;
310 u32 peer_node;
311
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100312 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400313 return false;
314
315 if (unlikely(msg_origport(msg) != peer_port))
316 return false;
317
318 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400319 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400320
321 if (likely(orig_node == peer_node))
322 return true;
323
Ying Xue34747532015-01-09 15:27:10 +0800324 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400325 return true;
326
Ying Xue34747532015-01-09 15:27:10 +0800327 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400328 return true;
329
330 return false;
331}
332
Allan Stephens0c3141e2008-04-15 00:22:02 -0700333/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400334 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 * @sock: pre-allocated socket structure
337 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800338 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900339 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700340 * This routine creates additional data structures used by the TIPC socket,
341 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 *
343 * Returns 0 on success, errno otherwise
344 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400345static int tipc_sk_create(struct net *net, struct socket *sock,
346 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500348 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700349 const struct proto_ops *ops;
350 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400352 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400353 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354
355 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 if (unlikely(protocol != 0))
357 return -EPROTONOSUPPORT;
358
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 switch (sock->type) {
360 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 ops = &stream_ops;
362 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 break;
364 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700365 ops = &packet_ops;
366 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 break;
368 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700370 ops = &msg_ops;
371 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 break;
Allan Stephens49978652006-06-25 23:47:18 -0700373 default:
Allan Stephens49978652006-06-25 23:47:18 -0700374 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 }
376
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500378 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700379 if (sk == NULL)
380 return -ENOMEM;
381
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400382 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400383 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400384 INIT_LIST_HEAD(&tsk->publications);
385 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500386 tn = net_generic(sock_net(sk), tipc_net_id);
387 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400388 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389
Allan Stephens0c3141e2008-04-15 00:22:02 -0700390 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700391 sock->ops = ops;
392 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800394 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900395 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800396 return -EINVAL;
397 }
398 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800399 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400400 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400401 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800402 sk->sk_data_ready = tipc_data_ready;
403 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800404 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400405 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
406 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700407
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400408 /* Start out with safe limits until we receive an advertised window */
409 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
410 tsk->rcv_win = tsk->snd_win;
411
Allan Stephens0c3141e2008-04-15 00:22:02 -0700412 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400413 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700414 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400415 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700416 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 return 0;
418}
419
Ying Xue07f6c4b2015-01-07 13:41:58 +0800420static void tipc_sk_callback(struct rcu_head *head)
421{
422 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
423
424 sock_put(&tsk->sk);
425}
426
Ying Xuec5fa7b32013-06-17 10:54:39 -0400427/**
Ying Xue247f0f32014-02-18 16:06:46 +0800428 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 * @sock: socket to destroy
430 *
431 * This routine cleans up any messages that are still queued on the socket.
432 * For DGRAM and RDM socket types, all queued messages are rejected.
433 * For SEQPACKET and STREAM socket types, the first message is rejected
434 * and any others are discarded. (If the first message on a STREAM socket
435 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900436 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 * NOTE: Rejected messages are not necessarily returned to the sender! They
438 * are returned or discarded according to the "destination droppable" setting
439 * specified for the message by the sender.
440 *
441 * Returns 0 on success, errno otherwise
442 */
Ying Xue247f0f32014-02-18 16:06:46 +0800443static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 struct sock *sk = sock->sk;
Sasha Levin357c4772015-01-13 12:46:41 -0500446 struct net *net;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400447 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800448 struct sk_buff *skb;
Ying Xue1ea23a22015-05-28 13:19:22 +0800449 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450
Allan Stephens0c3141e2008-04-15 00:22:02 -0700451 /*
452 * Exit if socket isn't fully initialized (occurs when a failed accept()
453 * releases a pre-allocated child socket that was never used)
454 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700455 if (sk == NULL)
456 return 0;
457
Sasha Levin357c4772015-01-13 12:46:41 -0500458 net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400459 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700460 lock_sock(sk);
461
462 /*
463 * Reject all unreceived messages, except on an active connection
464 * (which disconnects locally & sends a 'FIN+' to peer)
465 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400466 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800468 skb = __skb_dequeue(&sk->sk_receive_queue);
469 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 break;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +0100471 if (TIPC_SKB_CB(skb)->bytes_read)
Ying Xuea6ca1092014-11-26 11:41:55 +0800472 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700473 else {
474 if ((sock->state == SS_CONNECTING) ||
475 (sock->state == SS_CONNECTED)) {
476 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +0800477 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700478 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400479 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700480 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 }
482
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400483 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800484 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800485 tipc_sk_remove(tsk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100486 if (tipc_sk_connected(sk)) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500487 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +0800488 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500489 tsk_own_node(tsk), tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800490 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800491 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400492 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Ying Xuef2f98002015-01-09 15:27:05 +0800493 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400494 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495
Allan Stephens0c3141e2008-04-15 00:22:02 -0700496 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700497 sock->state = SS_DISCONNECTING;
498 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800499
500 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700501 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200503 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504}
505
506/**
Ying Xue247f0f32014-02-18 16:06:46 +0800507 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 * @sock: socket structure
509 * @uaddr: socket address describing name(s) and desired operation
510 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900511 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 * Name and name sequence binding is indicated using a positive scope value;
513 * a negative scope value unbinds the specified name. Specifying no name
514 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900515 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700517 *
518 * NOTE: This routine doesn't need to take the socket lock since it doesn't
519 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 */
Ying Xue247f0f32014-02-18 16:06:46 +0800521static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
522 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523{
Ying Xue84602762013-12-27 10:18:28 +0800524 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400526 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800527 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528
Ying Xue84602762013-12-27 10:18:28 +0800529 lock_sock(sk);
530 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400531 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800532 goto exit;
533 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900534
Ying Xue84602762013-12-27 10:18:28 +0800535 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
536 res = -EINVAL;
537 goto exit;
538 }
539 if (addr->family != AF_TIPC) {
540 res = -EAFNOSUPPORT;
541 goto exit;
542 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 if (addr->addrtype == TIPC_ADDR_NAME)
545 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800546 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
547 res = -EAFNOSUPPORT;
548 goto exit;
549 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900550
Ying Xue13a2e892013-06-17 10:54:40 -0400551 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400552 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800553 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
554 res = -EACCES;
555 goto exit;
556 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400557
Ying Xue84602762013-12-27 10:18:28 +0800558 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400559 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
560 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800561exit:
562 release_sock(sk);
563 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564}
565
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566/**
Ying Xue247f0f32014-02-18 16:06:46 +0800567 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 * @sock: socket structure
569 * @uaddr: area for returned socket address
570 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700571 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900572 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700574 *
Allan Stephens2da59912008-07-14 22:43:32 -0700575 * NOTE: This routine doesn't need to take the socket lock since it only
576 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000577 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 */
Ying Xue247f0f32014-02-18 16:06:46 +0800579static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
580 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400583 struct tipc_sock *tsk = tipc_sk(sock->sk);
Ying Xue34747532015-01-09 15:27:10 +0800584 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000586 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700587 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700588 if ((sock->state != SS_CONNECTED) &&
589 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
590 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400591 addr->addr.id.ref = tsk_peer_port(tsk);
592 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800594 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800595 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700596 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597
598 *uaddr_len = sizeof(*addr);
599 addr->addrtype = TIPC_ADDR_ID;
600 addr->family = AF_TIPC;
601 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 addr->addr.name.domain = 0;
603
Allan Stephens0c3141e2008-04-15 00:22:02 -0700604 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605}
606
607/**
Ying Xue247f0f32014-02-18 16:06:46 +0800608 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 * @file: file structure associated with the socket
610 * @sock: socket for which to calculate the poll bits
611 * @wait: ???
612 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700613 * Returns pollmask value
614 *
615 * COMMENTARY:
616 * It appears that the usual socket locking mechanisms are not useful here
617 * since the pollmask info is potentially out-of-date the moment this routine
618 * exits. TCP and other protocols seem to rely on higher level poll routines
619 * to handle any preventable race conditions, so TIPC will do the same ...
620 *
621 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700622 *
Allan Stephensf662c072010-08-17 11:00:06 +0000623 * socket state flags set
624 * ------------ ---------
625 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200626 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000627 *
628 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
629 * no write flags
630 *
631 * connected POLLIN/POLLRDNORM if data in rx queue
632 * POLLOUT if port is not congested
633 *
634 * disconnecting POLLIN/POLLRDNORM/POLLHUP
635 * no write flags
636 *
637 * listening POLLIN if SYN in rx queue
638 * no write flags
639 *
640 * ready POLLIN/POLLRDNORM if data in rx queue
641 * [connectionless] POLLOUT (since port cannot be congested)
642 *
643 * IMPORTANT: The fact that a read or write operation is indicated does NOT
644 * imply that the operation will succeed, merely that it should be performed
645 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 */
Ying Xue247f0f32014-02-18 16:06:46 +0800647static unsigned int tipc_poll(struct file *file, struct socket *sock,
648 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649{
Allan Stephens9b674e82008-03-26 16:48:21 -0700650 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400651 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000652 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700653
Ying Xuef288bef2012-08-21 11:16:57 +0800654 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700655
Allan Stephensf662c072010-08-17 11:00:06 +0000656 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200657 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500658 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200659 mask |= POLLOUT;
660 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000661 case SS_READY:
662 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400663 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000664 mask |= POLLOUT;
665 /* fall thru' */
666 case SS_CONNECTING:
667 case SS_LISTENING:
668 if (!skb_queue_empty(&sk->sk_receive_queue))
669 mask |= (POLLIN | POLLRDNORM);
670 break;
671 case SS_DISCONNECTING:
672 mask = (POLLIN | POLLRDNORM | POLLHUP);
673 break;
674 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700675
676 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677}
678
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400679/**
680 * tipc_sendmcast - send multicast message
681 * @sock: socket structure
682 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500683 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400684 * @dsz: total length of message data
685 * @timeo: timeout to wait for wakeup
686 *
687 * Called from function tipc_sendmsg(), which has done all sanity checks
688 * Returns the number of bytes sent on success, or errno
689 */
690static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500691 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400692{
693 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500694 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800695 struct net *net = sock_net(sk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500696 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100697 struct sk_buff_head pktchain;
Al Virof25dcc72014-11-28 15:52:29 -0500698 struct iov_iter save = msg->msg_iter;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400699 uint mtu;
700 int rc;
701
Parthasarathy Bhuvaragan7cf87fa2016-11-01 14:02:34 +0100702 if (!timeo && tsk->link_cong)
703 return -ELINKCONG;
704
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400705 msg_set_type(mhdr, TIPC_MCAST_MSG);
706 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
707 msg_set_destport(mhdr, 0);
708 msg_set_destnode(mhdr, 0);
709 msg_set_nametype(mhdr, seq->type);
710 msg_set_namelower(mhdr, seq->lower);
711 msg_set_nameupper(mhdr, seq->upper);
712 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
713
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100714 skb_queue_head_init(&pktchain);
715
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400716new_mtu:
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400717 mtu = tipc_bcast_get_mtu(net);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100718 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400719 if (unlikely(rc < 0))
720 return rc;
721
722 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100723 rc = tipc_bcast_xmit(net, &pktchain);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400724 if (likely(!rc))
725 return dsz;
726
727 if (rc == -ELINKCONG) {
728 tsk->link_cong = 1;
729 rc = tipc_wait_for_sndmsg(sock, &timeo);
730 if (!rc)
731 continue;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400732 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100733 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500734 if (rc == -EMSGSIZE) {
735 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400736 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500737 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400738 break;
739 } while (1);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400740 return rc;
741}
742
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500743/**
744 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
745 * @arrvq: queue with arriving messages, to be cloned after destination lookup
746 * @inputq: queue with cloned messages, delivered to socket after dest lookup
747 *
748 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400749 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500750void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
751 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400752{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500753 struct tipc_msg *msg;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500754 struct tipc_plist dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500755 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400756 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500757 struct sk_buff_head tmpq;
758 uint hsz;
759 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500760
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500761 __skb_queue_head_init(&tmpq);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500762 tipc_plist_init(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400763
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500764 skb = tipc_skb_peek(arrvq, &inputq->lock);
765 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
766 msg = buf_msg(skb);
767 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400768
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500769 if (in_own_node(net, msg_orignode(msg)))
770 scope = TIPC_NODE_SCOPE;
771
772 /* Create destination port list and message clones: */
773 tipc_nametbl_mc_translate(net,
774 msg_nametype(msg), msg_namelower(msg),
775 msg_nameupper(msg), scope, &dports);
776 portid = tipc_plist_pop(&dports);
777 for (; portid; portid = tipc_plist_pop(&dports)) {
778 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
779 if (_skb) {
780 msg_set_destport(buf_msg(_skb), portid);
781 __skb_queue_tail(&tmpq, _skb);
782 continue;
783 }
784 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400785 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500786 /* Append to inputq if not already done by other thread */
787 spin_lock_bh(&inputq->lock);
788 if (skb_peek(arrvq) == skb) {
789 skb_queue_splice_tail_init(&tmpq, inputq);
790 kfree_skb(__skb_dequeue(arrvq));
791 }
792 spin_unlock_bh(&inputq->lock);
793 __skb_queue_purge(&tmpq);
794 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400795 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500796 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400797}
798
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900799/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500800 * tipc_sk_proto_rcv - receive a connection mng protocol message
801 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400802 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500803 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400804static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
805 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500806{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400807 struct sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400808 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400809 struct tipc_msg *hdr = buf_msg(skb);
810 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400811 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400812
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500813 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400814 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500815 goto exit;
816
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400817 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500818
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400819 if (mtyp == CONN_PROBE) {
820 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400821 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
822 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400823 return;
824 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400825 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400826 tsk->snt_unacked -= msg_conn_ack(hdr);
827 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
828 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500829 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400830 sk->sk_write_space(sk);
831 } else if (mtyp != CONN_PROBE_REPLY) {
832 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500833 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500834exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400835 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500836}
837
Ying Xue3f405042014-01-17 09:50:05 +0800838static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
839{
840 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400841 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800842 DEFINE_WAIT(wait);
843 int done;
844
845 do {
846 int err = sock_error(sk);
847 if (err)
848 return err;
849 if (sock->state == SS_DISCONNECTING)
850 return -EPIPE;
851 if (!*timeo_p)
852 return -EAGAIN;
853 if (signal_pending(current))
854 return sock_intr_errno(*timeo_p);
855
856 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500857 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800858 finish_wait(sk_sleep(sk), &wait);
859 } while (!done);
860 return 0;
861}
862
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500863/**
Ying Xue247f0f32014-02-18 16:06:46 +0800864 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 * @sock: socket structure
866 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500867 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900868 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900870 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
872 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 * Returns the number of bytes sent on success, or errno otherwise
875 */
Ying Xue1b784142015-03-02 15:37:48 +0800876static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500877 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878{
Ying Xue39a02952015-03-02 15:37:47 +0800879 struct sock *sk = sock->sk;
880 int ret;
881
882 lock_sock(sk);
883 ret = __tipc_sendmsg(sock, m, dsz);
884 release_sock(sk);
885
886 return ret;
887}
888
889static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
890{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500891 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700892 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400893 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800894 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400895 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500896 u32 dnode, dport;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100897 struct sk_buff_head pktchain;
Ying Xuea6ca1092014-11-26 11:41:55 +0800898 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100899 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500900 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500901 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800902 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100903 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500905 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400906 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100907 if (unlikely(!dest)) {
Parthasarathy Bhuvaragan87227fe2016-11-01 14:02:39 +0100908 if (sock->state == SS_READY && tsk->peer.family == AF_TIPC)
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100909 dest = &tsk->peer;
Erik Hugnef2f80362015-03-19 09:02:19 +0100910 else
911 return -EDESTADDRREQ;
912 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
913 dest->family != AF_TIPC) {
914 return -EINVAL;
915 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500916 if (unlikely(sock->state != SS_READY)) {
Ying Xue39a02952015-03-02 15:37:47 +0800917 if (sock->state == SS_LISTENING)
918 return -EPIPE;
919 if (sock->state != SS_UNCONNECTED)
920 return -EISCONN;
921 if (tsk->published)
922 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700923 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400924 tsk->conn_type = dest->addr.name.name.type;
925 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700926 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100928 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800929 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500930
931 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a02952015-03-02 15:37:47 +0800932 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500933 } else if (dest->addrtype == TIPC_ADDR_NAME) {
934 u32 type = dest->addr.name.name.type;
935 u32 inst = dest->addr.name.name.instance;
936 u32 domain = dest->addr.name.domain;
937
938 dnode = domain;
939 msg_set_type(mhdr, TIPC_NAMED_MSG);
940 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
941 msg_set_nametype(mhdr, type);
942 msg_set_nameinst(mhdr, inst);
943 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800944 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500945 msg_set_destnode(mhdr, dnode);
946 msg_set_destport(mhdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800947 if (unlikely(!dport && !dnode))
948 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500949 } else if (dest->addrtype == TIPC_ADDR_ID) {
950 dnode = dest->addr.id.node;
951 msg_set_type(mhdr, TIPC_DIRECT_MSG);
952 msg_set_lookup_scope(mhdr, 0);
953 msg_set_destnode(mhdr, dnode);
954 msg_set_destport(mhdr, dest->addr.id.ref);
955 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
956 }
957
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100958 skb_queue_head_init(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500959 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500960new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800961 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100962 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500963 if (rc < 0)
Ying Xue39a02952015-03-02 15:37:47 +0800964 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500965
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900966 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100967 skb = skb_peek(&pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +0800968 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100969 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400970 if (likely(!rc)) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500971 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700972 sock->state = SS_CONNECTING;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400973 return dsz;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900974 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400975 if (rc == -ELINKCONG) {
976 tsk->link_cong = 1;
977 rc = tipc_wait_for_sndmsg(sock, &timeo);
978 if (!rc)
979 continue;
980 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100981 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500982 if (rc == -EMSGSIZE) {
983 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500984 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500985 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400986 break;
987 } while (1);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500988
989 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990}
991
Ying Xue391a6dd2014-01-17 09:50:06 +0800992static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
993{
994 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400995 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800996 DEFINE_WAIT(wait);
997 int done;
998
999 do {
1000 int err = sock_error(sk);
1001 if (err)
1002 return err;
1003 if (sock->state == SS_DISCONNECTING)
1004 return -EPIPE;
1005 else if (sock->state != SS_CONNECTED)
1006 return -ENOTCONN;
1007 if (!*timeo_p)
1008 return -EAGAIN;
1009 if (signal_pending(current))
1010 return sock_intr_errno(*timeo_p);
1011
1012 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1013 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001014 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001015 !tsk_conn_cong(tsk)) ||
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001016 !tipc_sk_connected(sk));
Ying Xue391a6dd2014-01-17 09:50:06 +08001017 finish_wait(sk_sleep(sk), &wait);
1018 } while (!done);
1019 return 0;
1020}
1021
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001022/**
Ying Xue247f0f32014-02-18 16:06:46 +08001023 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 * @sock: socket structure
1025 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001026 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029 *
1030 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001031 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 */
Ying Xue1b784142015-03-02 15:37:48 +08001033static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001035 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +08001036 int ret;
1037
1038 lock_sock(sk);
1039 ret = __tipc_send_stream(sock, m, dsz);
1040 release_sock(sk);
1041
1042 return ret;
1043}
1044
1045static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1046{
1047 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001048 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001049 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001050 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001051 struct sk_buff_head pktchain;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001052 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001053 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001054 int rc = -EINVAL;
1055 long timeo;
1056 u32 dnode;
1057 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001058 struct iov_iter save;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001059 int hlen = MIN_H_SIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001060
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001061 /* Handle implied connection establishment */
1062 if (unlikely(dest)) {
Ying Xue39a02952015-03-02 15:37:47 +08001063 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001064 hlen = msg_hdr_sz(mhdr);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001065 if (dsz && (dsz == rc))
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001066 tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001067 return rc;
1068 }
1069 if (dsz > (uint)INT_MAX)
1070 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001071
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001072 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001073 if (sock->state == SS_DISCONNECTING)
Ying Xue39a02952015-03-02 15:37:47 +08001074 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001075 else
Ying Xue39a02952015-03-02 15:37:47 +08001076 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001077 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001079 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Parthasarathy Bhuvaragan7cf87fa2016-11-01 14:02:34 +01001080 if (!timeo && tsk->link_cong)
1081 return -ELINKCONG;
1082
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001083 dnode = tsk_peer_node(tsk);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001084 skb_queue_head_init(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001085
1086next:
Al Virof25dcc72014-11-28 15:52:29 -05001087 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001088 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001089 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001090 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001091 if (unlikely(rc < 0))
Ying Xue39a02952015-03-02 15:37:47 +08001092 return rc;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001093
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001094 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001095 if (likely(!tsk_conn_cong(tsk))) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001096 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001097 if (likely(!rc)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001098 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001099 sent += send;
1100 if (sent == dsz)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001101 return dsz;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001102 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001103 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001104 if (rc == -EMSGSIZE) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001105 __skb_queue_purge(&pktchain);
Ying Xuef2f98002015-01-09 15:27:05 +08001106 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1107 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001108 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001109 goto next;
1110 }
1111 if (rc != -ELINKCONG)
1112 break;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001113
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001114 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001116 rc = tipc_wait_for_sndpkt(sock, &timeo);
1117 } while (!rc);
Ying Xue39a02952015-03-02 15:37:47 +08001118
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001119 __skb_queue_purge(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001120 return sent ? sent : rc;
1121}
1122
1123/**
1124 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001125 * @sock: socket structure
1126 * @m: message to send
1127 * @dsz: length of data to be transmitted
1128 *
1129 * Used for SOCK_SEQPACKET messages.
1130 *
1131 * Returns the number of bytes sent on success, or errno otherwise
1132 */
Ying Xue1b784142015-03-02 15:37:48 +08001133static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001134{
1135 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1136 return -EMSGSIZE;
1137
Ying Xue1b784142015-03-02 15:37:48 +08001138 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139}
1140
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001141/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001143static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001144 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145{
Ying Xue3721e9c2015-01-13 17:07:48 +08001146 struct sock *sk = &tsk->sk;
1147 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001148 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001150 msg_set_destnode(msg, peer_node);
1151 msg_set_destport(msg, peer_port);
1152 msg_set_type(msg, TIPC_CONN_MSG);
1153 msg_set_lookup_scope(msg, 0);
1154 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001155
Ying Xue2f55c432015-01-09 15:27:00 +08001156 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001157 tsk->probing_state = TIPC_CONN_OK;
Ying Xue3721e9c2015-01-13 17:07:48 +08001158 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001159 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1160 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001161 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001162 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1163 return;
1164
1165 /* Fall back to message based flow control */
1166 tsk->rcv_win = FLOWCTL_MSG_WIN;
1167 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168}
1169
1170/**
1171 * set_orig_addr - capture sender's address for received message
1172 * @m: descriptor for message info
1173 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001174 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 * Note: Address is not captured if not requested by receiver.
1176 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001177static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001179 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001181 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 addr->family = AF_TIPC;
1183 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001184 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 addr->addr.id.ref = msg_origport(msg);
1186 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001187 addr->addr.name.domain = 0; /* could leave uninitialized */
1188 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 m->msg_namelen = sizeof(struct sockaddr_tipc);
1190 }
1191}
1192
1193/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001194 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 * @m: descriptor for message info
1196 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001197 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001198 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001200 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 * Returns 0 if successful, otherwise errno
1202 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001203static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1204 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205{
1206 u32 anc_data[3];
1207 u32 err;
1208 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001209 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210 int res;
1211
1212 if (likely(m->msg_controllen == 0))
1213 return 0;
1214
1215 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 err = msg ? msg_errcode(msg) : 0;
1217 if (unlikely(err)) {
1218 anc_data[0] = err;
1219 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001220 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1221 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001223 if (anc_data[1]) {
1224 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1225 msg_data(msg));
1226 if (res)
1227 return res;
1228 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 }
1230
1231 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1233 switch (dest_type) {
1234 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001235 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 anc_data[0] = msg_nametype(msg);
1237 anc_data[1] = msg_namelower(msg);
1238 anc_data[2] = msg_namelower(msg);
1239 break;
1240 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001241 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 anc_data[0] = msg_nametype(msg);
1243 anc_data[1] = msg_namelower(msg);
1244 anc_data[2] = msg_nameupper(msg);
1245 break;
1246 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001247 has_name = (tsk->conn_type != 0);
1248 anc_data[0] = tsk->conn_type;
1249 anc_data[1] = tsk->conn_instance;
1250 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251 break;
1252 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001253 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 }
Allan Stephens2db99832010-12-31 18:59:33 +00001255 if (has_name) {
1256 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1257 if (res)
1258 return res;
1259 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260
1261 return 0;
1262}
1263
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001264static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001265{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001266 struct sock *sk = &tsk->sk;
1267 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001268 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001269 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001270 u32 peer_port = tsk_peer_port(tsk);
1271 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001272
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001273 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001274 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001275 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1276 dnode, tsk_own_node(tsk), peer_port,
1277 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001278 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001279 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001280 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001281 msg_set_conn_ack(msg, tsk->rcv_unacked);
1282 tsk->rcv_unacked = 0;
1283
1284 /* Adjust to and advertize the correct window limit */
1285 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1286 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1287 msg_set_adv_win(msg, tsk->rcv_win);
1288 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001289 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001290}
1291
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001292static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001293{
1294 struct sock *sk = sock->sk;
1295 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001296 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001297 int err;
1298
1299 for (;;) {
1300 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001301 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001302 if (sock->state == SS_DISCONNECTING) {
1303 err = -ENOTCONN;
1304 break;
1305 }
1306 release_sock(sk);
1307 timeo = schedule_timeout(timeo);
1308 lock_sock(sk);
1309 }
1310 err = 0;
1311 if (!skb_queue_empty(&sk->sk_receive_queue))
1312 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001313 err = -EAGAIN;
1314 if (!timeo)
1315 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001316 err = sock_intr_errno(timeo);
1317 if (signal_pending(current))
1318 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001319 }
1320 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001321 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001322 return err;
1323}
1324
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001325/**
Ying Xue247f0f32014-02-18 16:06:46 +08001326 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327 * @m: descriptor for message info
1328 * @buf_len: total size of user buffer area
1329 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001330 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1332 * If the complete message doesn't fit in user area, truncate it.
1333 *
1334 * Returns size of returned message data, errno otherwise
1335 */
Ying Xue1b784142015-03-02 15:37:48 +08001336static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1337 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001339 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001340 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 struct sk_buff *buf;
1342 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001343 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 unsigned int sz;
1345 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001346 int res, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 if (unlikely(!buf_len))
1350 return -EINVAL;
1351
Allan Stephens0c3141e2008-04-15 00:22:02 -07001352 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353
Allan Stephens0c3141e2008-04-15 00:22:02 -07001354 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 res = -ENOTCONN;
1356 goto exit;
1357 }
1358
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001359 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001360restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361
Allan Stephens0c3141e2008-04-15 00:22:02 -07001362 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001363 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001364 if (res)
1365 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001366
1367 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001368 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369 msg = buf_msg(buf);
1370 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001371 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 err = msg_errcode(msg);
1373
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001376 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 goto restart;
1378 }
1379
1380 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 set_orig_addr(m, msg);
1382
1383 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001384 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001385 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 goto exit;
1387
1388 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389 if (!err) {
1390 if (unlikely(buf_len < sz)) {
1391 sz = buf_len;
1392 m->msg_flags |= MSG_TRUNC;
1393 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001394 res = skb_copy_datagram_msg(buf, hlen, m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001395 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 res = sz;
1398 } else {
1399 if ((sock->state == SS_READY) ||
1400 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1401 res = 0;
1402 else
1403 res = -ECONNRESET;
1404 }
1405
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001406 if (unlikely(flags & MSG_PEEK))
1407 goto exit;
1408
1409 if (likely(sock->state != SS_READY)) {
1410 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1411 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1412 tipc_sk_send_ack(tsk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001413 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001414 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001416 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 return res;
1418}
1419
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001420/**
Ying Xue247f0f32014-02-18 16:06:46 +08001421 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 * @m: descriptor for message info
1423 * @buf_len: total size of user buffer area
1424 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001425 *
1426 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427 * will optionally wait for more; never truncates data.
1428 *
1429 * Returns size of returned message data, errno otherwise
1430 */
Ying Xue1b784142015-03-02 15:37:48 +08001431static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1432 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001434 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001435 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 struct sk_buff *buf;
1437 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001438 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 unsigned int sz;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001440 int target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001443 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444
1445 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 if (unlikely(!buf_len))
1447 return -EINVAL;
1448
Allan Stephens0c3141e2008-04-15 00:22:02 -07001449 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001451 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 res = -ENOTCONN;
1453 goto exit;
1454 }
1455
Florian Westphal3720d402010-08-17 11:00:04 +00001456 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001457 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001458
Allan Stephens0c3141e2008-04-15 00:22:02 -07001459restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001460 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001461 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001462 if (res)
1463 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001464
1465 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001466 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 msg = buf_msg(buf);
1468 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001469 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 err = msg_errcode(msg);
1471
1472 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001474 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 goto restart;
1476 }
1477
1478 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 if (sz_copied == 0) {
1480 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001481 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001482 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483 goto exit;
1484 }
1485
1486 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487 if (!err) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001488 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1489 u32 needed;
1490 int sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491
Allan Stephens0232fd02011-02-21 09:45:40 -05001492 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493 needed = (buf_len - sz_copied);
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001494 sz_to_copy = min(sz, needed);
Allan Stephens0232fd02011-02-21 09:45:40 -05001495
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001496 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001497 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001499
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500 sz_copied += sz_to_copy;
1501
1502 if (sz_to_copy < sz) {
1503 if (!(flags & MSG_PEEK))
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001504 TIPC_SKB_CB(buf)->bytes_read =
1505 offset + sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506 goto exit;
1507 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508 } else {
1509 if (sz_copied != 0)
1510 goto exit; /* can't add error msg to valid data */
1511
1512 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1513 res = 0;
1514 else
1515 res = -ECONNRESET;
1516 }
1517
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001518 if (unlikely(flags & MSG_PEEK))
1519 goto exit;
1520
1521 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1522 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1523 tipc_sk_send_ack(tsk);
1524 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525
1526 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001527 if ((sz_copied < buf_len) && /* didn't get all requested data */
1528 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001529 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001530 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001531 goto restart;
1532
1533exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001534 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001535 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001536}
1537
1538/**
Ying Xuef288bef2012-08-21 11:16:57 +08001539 * tipc_write_space - wake up thread if port congestion is released
1540 * @sk: socket
1541 */
1542static void tipc_write_space(struct sock *sk)
1543{
1544 struct socket_wq *wq;
1545
1546 rcu_read_lock();
1547 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001548 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001549 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1550 POLLWRNORM | POLLWRBAND);
1551 rcu_read_unlock();
1552}
1553
1554/**
1555 * tipc_data_ready - wake up threads to indicate messages have been received
1556 * @sk: socket
1557 * @len: the length of messages
1558 */
David S. Miller676d2362014-04-11 16:15:36 -04001559static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001560{
1561 struct socket_wq *wq;
1562
1563 rcu_read_lock();
1564 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001565 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001566 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1567 POLLRDNORM | POLLRDBAND);
1568 rcu_read_unlock();
1569}
1570
Ying Xuef4195d12015-11-22 15:46:05 +08001571static void tipc_sock_destruct(struct sock *sk)
1572{
1573 __skb_queue_purge(&sk->sk_receive_queue);
1574}
1575
Ying Xuef288bef2012-08-21 11:16:57 +08001576/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001577 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001578 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001579 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001580 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001581 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001582 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001583static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001584{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001585 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001586 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001587 struct socket *sock = sk->sk_socket;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001588 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001589
Jon Paul Maloycda36962015-07-22 10:11:20 -04001590 if (unlikely(msg_mcast(hdr)))
1591 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001592
1593 switch ((int)sock->state) {
1594 case SS_CONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001595
Ying Xue7e6c1312012-11-29 18:39:14 -05001596 /* Accept only connection-based messages sent by peer */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001597 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1598 return false;
1599
1600 if (unlikely(msg_errcode(hdr))) {
1601 sock->state = SS_DISCONNECTING;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001602 /* Let timer expire on it's own */
1603 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1604 tsk->portid);
Parthasarathy Bhuvaragan4891d8f2016-11-01 14:02:35 +01001605 sk->sk_state_change(sk);
Ying Xue7e6c1312012-11-29 18:39:14 -05001606 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001607 return true;
1608
Ying Xue7e6c1312012-11-29 18:39:14 -05001609 case SS_CONNECTING:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001610
Ying Xue7e6c1312012-11-29 18:39:14 -05001611 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001612 if (unlikely(!msg_connected(hdr)))
1613 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001614
Jon Paul Maloycda36962015-07-22 10:11:20 -04001615 if (unlikely(msg_errcode(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001616 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001617 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001618 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001619 }
1620
Jon Paul Maloycda36962015-07-22 10:11:20 -04001621 if (unlikely(!msg_isdata(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001622 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001623 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001624 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001625 }
1626
Jon Paul Maloycda36962015-07-22 10:11:20 -04001627 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1628 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001629 sock->state = SS_CONNECTED;
1630
Jon Paul Maloycda36962015-07-22 10:11:20 -04001631 /* If 'ACK+' message, add to socket receive queue */
1632 if (msg_data_sz(hdr))
1633 return true;
1634
1635 /* If empty 'ACK-' message, wake up sleeping connect() */
1636 if (waitqueue_active(sk_sleep(sk)))
1637 wake_up_interruptible(sk_sleep(sk));
1638
1639 /* 'ACK-' message is neither accepted nor rejected: */
1640 msg_set_dest_droppable(hdr, 1);
1641 return false;
1642
Ying Xue7e6c1312012-11-29 18:39:14 -05001643 case SS_LISTENING:
1644 case SS_UNCONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001645
Ying Xue7e6c1312012-11-29 18:39:14 -05001646 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001647 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1648 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001649 break;
1650 case SS_DISCONNECTING:
1651 break;
1652 default:
1653 pr_err("Unknown socket state %u\n", sock->state);
1654 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001655 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001656}
1657
1658/**
Ying Xueaba79f32013-01-20 23:30:09 +01001659 * rcvbuf_limit - get proper overload limit of socket receive queue
1660 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001661 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001662 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001663 * For connection oriented messages, irrespective of importance,
1664 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001665 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001666 * For connectionless messages, queue limits are based on message
1667 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001668 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001669 * TIPC_LOW_IMPORTANCE (2 MB)
1670 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1671 * TIPC_HIGH_IMPORTANCE (8 MB)
1672 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001673 *
1674 * Returns overload limit according to corresponding message importance
1675 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001676static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001677{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001678 struct tipc_sock *tsk = tipc_sk(sk);
1679 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001680
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001681 if (unlikely(!msg_connected(hdr)))
1682 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001683
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001684 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1685 return sk->sk_rcvbuf;
1686
1687 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001688}
1689
1690/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001691 * filter_rcv - validate incoming message
1692 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001693 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001694 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001695 * Enqueues message on receive queue if acceptable; optionally handles
1696 * disconnect indication for a connected socket.
1697 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001698 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001699 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001700 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001702static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1703 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001705 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001706 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001707 struct tipc_msg *hdr = buf_msg(skb);
1708 unsigned int limit = rcvbuf_limit(sk, skb);
1709 int err = TIPC_OK;
1710 int usr = msg_user(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711
Jon Paul Maloycda36962015-07-22 10:11:20 -04001712 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001713 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001714 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001715 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001716
Jon Paul Maloycda36962015-07-22 10:11:20 -04001717 if (unlikely(usr == SOCK_WAKEUP)) {
1718 kfree_skb(skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001719 tsk->link_cong = 0;
1720 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001721 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001722 }
1723
Jon Paul Maloycda36962015-07-22 10:11:20 -04001724 /* Drop if illegal message type */
1725 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1726 kfree_skb(skb);
1727 return false;
1728 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001729
Jon Paul Maloycda36962015-07-22 10:11:20 -04001730 /* Reject if wrong message type for current socket state */
1731 if (unlikely(sock->state == SS_READY)) {
1732 if (msg_connected(hdr)) {
1733 err = TIPC_ERR_NO_PORT;
1734 goto reject;
1735 }
1736 } else if (unlikely(!filter_connect(tsk, skb))) {
1737 err = TIPC_ERR_NO_PORT;
1738 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 }
1740
1741 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001742 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1743 err = TIPC_ERR_OVERLOAD;
1744 goto reject;
1745 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746
Ying Xueaba79f32013-01-20 23:30:09 +01001747 /* Enqueue message */
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001748 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001749 __skb_queue_tail(&sk->sk_receive_queue, skb);
1750 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751
David S. Miller676d2362014-04-11 16:15:36 -04001752 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001753 return true;
1754
1755reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001756 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1757 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001758 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759}
1760
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001761/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001762 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001763 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001764 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001765 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001766 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001767 *
1768 * Returns 0
1769 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001770static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001771{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001772 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001773 struct sk_buff_head xmitq;
1774 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001775
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001776 __skb_queue_head_init(&xmitq);
1777
1778 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001779 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001780 return 0;
1781 }
1782
1783 if (skb_queue_empty(&xmitq))
1784 return 0;
1785
1786 /* Send response/rejected message */
1787 skb = __skb_dequeue(&xmitq);
1788 dnode = msg_destnode(buf_msg(skb));
1789 selector = msg_origport(buf_msg(skb));
1790 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001791 return 0;
1792}
1793
1794/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001795 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1796 * inputq and try adding them to socket or backlog queue
1797 * @inputq: list of incoming buffers with potentially different destinations
1798 * @sk: socket where the buffers should be enqueued
1799 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001800 *
1801 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001802 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001803static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001804 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001805{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001806 unsigned long time_limit = jiffies + 2;
1807 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001808 unsigned int lim;
1809 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001810 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001811
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001812 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001813 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001814 return;
1815
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001816 skb = tipc_skb_dequeue(inputq, dport);
1817 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001818 return;
1819
1820 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001821 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001822 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001823 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001824 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001825
1826 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001827 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001828 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001829 atomic_set(dcnt, 0);
1830 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1831 if (likely(!sk_add_backlog(sk, skb, lim)))
1832 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001833
1834 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001835 onode = tipc_own_addr(sock_net(sk));
1836 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1837 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001838 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001839 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001840}
1841
1842/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001843 * tipc_sk_rcv - handle a chain of incoming buffers
1844 * @inputq: buffer list containing the buffers
1845 * Consumes all buffers in list until inputq is empty
1846 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001847 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001848void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001849{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001850 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001851 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001852 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001853 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001854 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001855 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001856
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001857 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001858 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001859 dport = tipc_skb_peek_port(inputq, dport);
1860 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001861
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001862 if (likely(tsk)) {
1863 sk = &tsk->sk;
1864 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001865 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001866 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001867 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001868 /* Send pending response/rejected messages, if any */
1869 while ((skb = __skb_dequeue(&xmitq))) {
1870 dnode = msg_destnode(buf_msg(skb));
1871 tipc_node_xmit_skb(net, skb, dnode, dport);
1872 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001873 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001874 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001875 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001876
1877 /* No destination socket => dequeue skb if still there */
1878 skb = tipc_skb_dequeue(inputq, dport);
1879 if (!skb)
1880 return;
1881
1882 /* Try secondary lookup if unresolved named message */
1883 err = TIPC_ERR_NO_PORT;
1884 if (tipc_msg_lookup_dest(net, skb, &err))
1885 goto xmit;
1886
1887 /* Prepare for message rejection */
1888 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001889 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001890xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001891 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001892 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001893 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001894}
1895
Ying Xue78eb3a52014-01-17 09:50:03 +08001896static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1897{
1898 struct sock *sk = sock->sk;
1899 DEFINE_WAIT(wait);
1900 int done;
1901
1902 do {
1903 int err = sock_error(sk);
1904 if (err)
1905 return err;
1906 if (!*timeo_p)
1907 return -ETIMEDOUT;
1908 if (signal_pending(current))
1909 return sock_intr_errno(*timeo_p);
1910
1911 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1912 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1913 finish_wait(sk_sleep(sk), &wait);
1914 } while (!done);
1915 return 0;
1916}
1917
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918/**
Ying Xue247f0f32014-02-18 16:06:46 +08001919 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 * @sock: socket structure
1921 * @dest: socket address for destination port
1922 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001923 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924 *
1925 * Returns 0 on success, errno otherwise
1926 */
Ying Xue247f0f32014-02-18 16:06:46 +08001927static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1928 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001930 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001931 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001932 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1933 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001934 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Ying Xue78eb3a52014-01-17 09:50:03 +08001935 socket_state previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001936 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937
Allan Stephens0c3141e2008-04-15 00:22:02 -07001938 lock_sock(sk);
1939
Erik Hugnef2f80362015-03-19 09:02:19 +01001940 /* DGRAM/RDM connect(), just save the destaddr */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001941 if (sock->state == SS_READY) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001942 if (dst->family == AF_UNSPEC) {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001943 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
Sasha Levin610600c2015-03-23 15:30:00 -04001944 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1945 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001946 } else {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001947 memcpy(&tsk->peer, dest, destlen);
Erik Hugnef2f80362015-03-19 09:02:19 +01001948 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001949 goto exit;
1950 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951
Allan Stephensb89741a2008-04-15 00:20:37 -07001952 /*
1953 * Reject connection attempt using multicast address
1954 *
1955 * Note: send_msg() validates the rest of the address fields,
1956 * so there's no need to do it here
1957 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001958 if (dst->addrtype == TIPC_ADDR_MCAST) {
1959 res = -EINVAL;
1960 goto exit;
1961 }
1962
Ying Xue78eb3a52014-01-17 09:50:03 +08001963 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001964 switch (sock->state) {
1965 case SS_UNCONNECTED:
1966 /* Send a 'SYN-' to destination */
1967 m.msg_name = dest;
1968 m.msg_namelen = destlen;
1969
1970 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1971 * indicate send_msg() is never blocked.
1972 */
1973 if (!timeout)
1974 m.msg_flags = MSG_DONTWAIT;
1975
Ying Xue39a02952015-03-02 15:37:47 +08001976 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001977 if ((res < 0) && (res != -EWOULDBLOCK))
1978 goto exit;
1979
1980 /* Just entered SS_CONNECTING state; the only
1981 * difference is that return value in non-blocking
1982 * case is EINPROGRESS, rather than EALREADY.
1983 */
1984 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001985 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001986 if (previous == SS_CONNECTING)
1987 res = -EALREADY;
1988 if (!timeout)
1989 goto exit;
1990 timeout = msecs_to_jiffies(timeout);
1991 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1992 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001993 break;
1994 case SS_CONNECTED:
1995 res = -EISCONN;
1996 break;
1997 default:
1998 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001999 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07002000 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07002001exit:
2002 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002003 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004}
2005
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002006/**
Ying Xue247f0f32014-02-18 16:06:46 +08002007 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01002008 * @sock: socket structure
2009 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002010 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011 * Returns 0 on success, errno otherwise
2012 */
Ying Xue247f0f32014-02-18 16:06:46 +08002013static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002014{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002015 struct sock *sk = sock->sk;
2016 int res;
2017
2018 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002019
Ying Xue245f3d32011-07-06 06:01:13 -04002020 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002021 res = -EINVAL;
2022 else {
2023 sock->state = SS_LISTENING;
2024 res = 0;
2025 }
2026
2027 release_sock(sk);
2028 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002029}
2030
Ying Xue6398e232014-01-17 09:50:04 +08002031static int tipc_wait_for_accept(struct socket *sock, long timeo)
2032{
2033 struct sock *sk = sock->sk;
2034 DEFINE_WAIT(wait);
2035 int err;
2036
2037 /* True wake-one mechanism for incoming connections: only
2038 * one process gets woken up, not the 'whole herd'.
2039 * Since we do not 'race & poll' for established sockets
2040 * anymore, the common case will execute the loop only once.
2041 */
2042 for (;;) {
2043 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2044 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002045 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002046 release_sock(sk);
2047 timeo = schedule_timeout(timeo);
2048 lock_sock(sk);
2049 }
2050 err = 0;
2051 if (!skb_queue_empty(&sk->sk_receive_queue))
2052 break;
2053 err = -EINVAL;
2054 if (sock->state != SS_LISTENING)
2055 break;
Ying Xue6398e232014-01-17 09:50:04 +08002056 err = -EAGAIN;
2057 if (!timeo)
2058 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002059 err = sock_intr_errno(timeo);
2060 if (signal_pending(current))
2061 break;
Ying Xue6398e232014-01-17 09:50:04 +08002062 }
2063 finish_wait(sk_sleep(sk), &wait);
2064 return err;
2065}
2066
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002067/**
Ying Xue247f0f32014-02-18 16:06:46 +08002068 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069 * @sock: listening socket
2070 * @newsock: new socket that is to be connected
2071 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002072 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073 * Returns 0 on success, errno otherwise
2074 */
Ying Xue247f0f32014-02-18 16:06:46 +08002075static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002077 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002079 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002080 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002081 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002082 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083
Allan Stephens0c3141e2008-04-15 00:22:02 -07002084 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002085
Allan Stephens0c3141e2008-04-15 00:22:02 -07002086 if (sock->state != SS_LISTENING) {
2087 res = -EINVAL;
2088 goto exit;
2089 }
Ying Xue6398e232014-01-17 09:50:04 +08002090 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2091 res = tipc_wait_for_accept(sock, timeo);
2092 if (res)
2093 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002094
2095 buf = skb_peek(&sk->sk_receive_queue);
2096
Parthasarathy Bhuvaragancb5da842016-11-01 14:02:36 +01002097 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002098 if (res)
2099 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002100 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002101
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002102 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002103 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002104 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002105
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002106 /* we lock on new_sk; but lockdep sees the lock on sk */
2107 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002108
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002109 /*
2110 * Reject any stray messages received by new socket
2111 * before the socket lock was taken (very, very unlikely)
2112 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002113 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002115 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002116 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002117 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002118
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002119 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002120 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002121 new_tsock->conn_type = msg_nametype(msg);
2122 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002124
2125 /*
2126 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2127 * Respond to 'SYN+' by queuing it on new socket.
2128 */
2129 if (!msg_data_sz(msg)) {
2130 struct msghdr m = {NULL,};
2131
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002132 tsk_advance_rx_queue(sk);
Ying Xue39a02952015-03-02 15:37:47 +08002133 __tipc_send_stream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002134 } else {
2135 __skb_dequeue(&sk->sk_receive_queue);
2136 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002137 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002138 }
2139 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002141 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142 return res;
2143}
2144
2145/**
Ying Xue247f0f32014-02-18 16:06:46 +08002146 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002148 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149 *
2150 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002151 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152 * Returns 0 on success, errno otherwise
2153 */
Ying Xue247f0f32014-02-18 16:06:46 +08002154static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002155{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002156 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002157 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002158 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002159 struct sk_buff *skb;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002160 u32 dnode = tsk_peer_node(tsk);
2161 u32 dport = tsk_peer_port(tsk);
2162 u32 onode = tipc_own_addr(net);
2163 u32 oport = tsk->portid;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002164 int res;
2165
Allan Stephense247a8f2008-03-06 15:05:38 -08002166 if (how != SHUT_RDWR)
2167 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168
Allan Stephens0c3141e2008-04-15 00:22:02 -07002169 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002170
2171 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002172 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002173 case SS_CONNECTED:
2174
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175restart:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002176 dnode = tsk_peer_node(tsk);
2177
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002178 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002179 skb = __skb_dequeue(&sk->sk_receive_queue);
2180 if (skb) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01002181 if (TIPC_SKB_CB(skb)->bytes_read) {
Ying Xuea6ca1092014-11-26 11:41:55 +08002182 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002183 goto restart;
2184 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002185 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002186 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002187 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002188 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloycda36962015-07-22 10:11:20 -04002189 0, dnode, onode, dport, oport,
2190 TIPC_CONN_SHUTDOWN);
Vegard Nossumd2fbdf72016-07-23 08:15:04 +02002191 if (skb)
2192 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002193 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07002194 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002195 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196 /* fall through */
2197
2198 case SS_DISCONNECTING:
2199
Ying Xue75031152012-10-29 09:38:15 -04002200 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002201 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002202
2203 /* Wake up anyone sleeping in poll */
2204 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002205 res = 0;
2206 break;
2207
2208 default:
2209 res = -ENOTCONN;
2210 }
2211
Allan Stephens0c3141e2008-04-15 00:22:02 -07002212 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002213 return res;
2214}
2215
Ying Xuef2f2a962015-01-09 15:27:02 +08002216static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002217{
Ying Xuef2f2a962015-01-09 15:27:02 +08002218 struct tipc_sock *tsk = (struct tipc_sock *)data;
2219 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002220 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002221 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002222 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002223
Jon Paul Maloy57289012014-08-22 18:09:09 -04002224 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002225 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002226 bh_unlock_sock(sk);
2227 goto exit;
2228 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002229 peer_port = tsk_peer_port(tsk);
2230 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002231
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002232 if (tsk->probing_state == TIPC_CONN_PROBING) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002233 if (!sock_owned_by_user(sk)) {
2234 sk->sk_socket->state = SS_DISCONNECTING;
Erik Hugneb3be5e32015-06-09 17:27:12 +02002235 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2236 tsk_peer_port(tsk));
2237 sk->sk_state_change(sk);
2238 } else {
2239 /* Try again later */
2240 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2241 }
2242
Jon Paul Maloy57289012014-08-22 18:09:09 -04002243 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002244 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2245 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002246 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002247 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002248 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002249 }
2250 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002251 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002252 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002253exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002254 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002255}
2256
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002257static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002258 struct tipc_name_seq const *seq)
2259{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002260 struct sock *sk = &tsk->sk;
2261 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002262 struct publication *publ;
2263 u32 key;
2264
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002265 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002266 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002267 key = tsk->portid + tsk->pub_count + 1;
2268 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002269 return -EADDRINUSE;
2270
Ying Xuef2f98002015-01-09 15:27:05 +08002271 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002272 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002273 if (unlikely(!publ))
2274 return -EINVAL;
2275
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002276 list_add(&publ->pport_list, &tsk->publications);
2277 tsk->pub_count++;
2278 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002279 return 0;
2280}
2281
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002282static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002283 struct tipc_name_seq const *seq)
2284{
Ying Xuef2f98002015-01-09 15:27:05 +08002285 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002286 struct publication *publ;
2287 struct publication *safe;
2288 int rc = -EINVAL;
2289
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002290 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002291 if (seq) {
2292 if (publ->scope != scope)
2293 continue;
2294 if (publ->type != seq->type)
2295 continue;
2296 if (publ->lower != seq->lower)
2297 continue;
2298 if (publ->upper != seq->upper)
2299 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002300 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002301 publ->ref, publ->key);
2302 rc = 0;
2303 break;
2304 }
Ying Xuef2f98002015-01-09 15:27:05 +08002305 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002306 publ->ref, publ->key);
2307 rc = 0;
2308 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002309 if (list_empty(&tsk->publications))
2310 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002311 return rc;
2312}
2313
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002314/* tipc_sk_reinit: set non-zero address in all existing sockets
2315 * when we go from standalone to network mode.
2316 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002317void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002318{
Ying Xuee05b31f2015-01-09 15:27:08 +08002319 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002320 const struct bucket_table *tbl;
2321 struct rhash_head *pos;
2322 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002323 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002324 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002325
Ying Xue07f6c4b2015-01-07 13:41:58 +08002326 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002327 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002328 for (i = 0; i < tbl->size; i++) {
2329 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2330 spin_lock_bh(&tsk->sk.sk_lock.slock);
2331 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002332 msg_set_prevnode(msg, tn->own_addr);
2333 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002334 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2335 }
2336 }
2337 rcu_read_unlock();
2338}
2339
Ying Xuee05b31f2015-01-09 15:27:08 +08002340static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002341{
Ying Xuee05b31f2015-01-09 15:27:08 +08002342 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002343 struct tipc_sock *tsk;
2344
2345 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002346 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002347 if (tsk)
2348 sock_hold(&tsk->sk);
2349 rcu_read_unlock();
2350
2351 return tsk;
2352}
2353
2354static int tipc_sk_insert(struct tipc_sock *tsk)
2355{
Ying Xuee05b31f2015-01-09 15:27:08 +08002356 struct sock *sk = &tsk->sk;
2357 struct net *net = sock_net(sk);
2358 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002359 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2360 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2361
2362 while (remaining--) {
2363 portid++;
2364 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2365 portid = TIPC_MIN_PORT;
2366 tsk->portid = portid;
2367 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002368 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2369 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002370 return 0;
2371 sock_put(&tsk->sk);
2372 }
2373
2374 return -1;
2375}
2376
2377static void tipc_sk_remove(struct tipc_sock *tsk)
2378{
2379 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002380 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002381
Herbert Xu6cca72892015-03-20 21:57:05 +11002382 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002383 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2384 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002385 }
2386}
2387
Herbert Xu6cca72892015-03-20 21:57:05 +11002388static const struct rhashtable_params tsk_rht_params = {
2389 .nelem_hint = 192,
2390 .head_offset = offsetof(struct tipc_sock, node),
2391 .key_offset = offsetof(struct tipc_sock, portid),
2392 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002393 .max_size = 1048576,
2394 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002395 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002396};
2397
Ying Xuee05b31f2015-01-09 15:27:08 +08002398int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002399{
Ying Xuee05b31f2015-01-09 15:27:08 +08002400 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002401
Herbert Xu6cca72892015-03-20 21:57:05 +11002402 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002403}
2404
Ying Xuee05b31f2015-01-09 15:27:08 +08002405void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002406{
Ying Xuee05b31f2015-01-09 15:27:08 +08002407 struct tipc_net *tn = net_generic(net, tipc_net_id);
2408
Ying Xue07f6c4b2015-01-07 13:41:58 +08002409 /* Wait for socket readers to complete */
2410 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002411
Ying Xuee05b31f2015-01-09 15:27:08 +08002412 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002413}
2414
2415/**
Ying Xue247f0f32014-02-18 16:06:46 +08002416 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002417 * @sock: socket structure
2418 * @lvl: option level
2419 * @opt: option identifier
2420 * @ov: pointer to new option value
2421 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002422 *
2423 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002424 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002425 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002426 * Returns 0 on success, errno otherwise
2427 */
Ying Xue247f0f32014-02-18 16:06:46 +08002428static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2429 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002430{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002431 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002432 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002433 u32 value;
2434 int res;
2435
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002436 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2437 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002438 if (lvl != SOL_TIPC)
2439 return -ENOPROTOOPT;
2440 if (ol < sizeof(value))
2441 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002442 res = get_user(value, (u32 __user *)ov);
2443 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002444 return res;
2445
Allan Stephens0c3141e2008-04-15 00:22:02 -07002446 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002447
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448 switch (opt) {
2449 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002450 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002451 break;
2452 case TIPC_SRC_DROPPABLE:
2453 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002454 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002455 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002456 res = -ENOPROTOOPT;
2457 break;
2458 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002459 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002460 break;
2461 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002462 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002463 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464 break;
2465 default:
2466 res = -EINVAL;
2467 }
2468
Allan Stephens0c3141e2008-04-15 00:22:02 -07002469 release_sock(sk);
2470
Per Lidenb97bf3f2006-01-02 19:04:38 +01002471 return res;
2472}
2473
2474/**
Ying Xue247f0f32014-02-18 16:06:46 +08002475 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002476 * @sock: socket structure
2477 * @lvl: option level
2478 * @opt: option identifier
2479 * @ov: receptacle for option value
2480 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002481 *
2482 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002483 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002484 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002485 * Returns 0 on success, errno otherwise
2486 */
Ying Xue247f0f32014-02-18 16:06:46 +08002487static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2488 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002489{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002490 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002491 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002492 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002493 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002494 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002495
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002496 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2497 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002498 if (lvl != SOL_TIPC)
2499 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002500 res = get_user(len, ol);
2501 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002502 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002503
Allan Stephens0c3141e2008-04-15 00:22:02 -07002504 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002505
2506 switch (opt) {
2507 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002508 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002509 break;
2510 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002511 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002512 break;
2513 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002514 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002515 break;
2516 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002517 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002518 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002519 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002520 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002521 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002522 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002523 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002524 value = skb_queue_len(&sk->sk_receive_queue);
2525 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002526 default:
2527 res = -EINVAL;
2528 }
2529
Allan Stephens0c3141e2008-04-15 00:22:02 -07002530 release_sock(sk);
2531
Paul Gortmaker25860c32010-12-31 18:59:31 +00002532 if (res)
2533 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002534
Paul Gortmaker25860c32010-12-31 18:59:31 +00002535 if (len < sizeof(value))
2536 return -EINVAL;
2537
2538 if (copy_to_user(ov, &value, sizeof(value)))
2539 return -EFAULT;
2540
2541 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002542}
2543
Ying Xuef2f98002015-01-09 15:27:05 +08002544static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002545{
Ying Xuef2f98002015-01-09 15:27:05 +08002546 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002547 struct tipc_sioc_ln_req lnr;
2548 void __user *argp = (void __user *)arg;
2549
2550 switch (cmd) {
2551 case SIOCGETLINKNAME:
2552 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2553 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002554 if (!tipc_node_get_linkname(sock_net(sk),
2555 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002556 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2557 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2558 return -EFAULT;
2559 return 0;
2560 }
2561 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002562 default:
2563 return -ENOIOCTLCMD;
2564 }
2565}
2566
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002567/* Protocol switches for the various types of TIPC sockets */
2568
Florian Westphalbca65ea2008-02-07 18:18:01 -08002569static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002570 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002571 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002572 .release = tipc_release,
2573 .bind = tipc_bind,
2574 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002575 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002576 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002577 .getname = tipc_getname,
2578 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002579 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002580 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002581 .shutdown = tipc_shutdown,
2582 .setsockopt = tipc_setsockopt,
2583 .getsockopt = tipc_getsockopt,
2584 .sendmsg = tipc_sendmsg,
2585 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002586 .mmap = sock_no_mmap,
2587 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002588};
2589
Florian Westphalbca65ea2008-02-07 18:18:01 -08002590static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002591 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002592 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002593 .release = tipc_release,
2594 .bind = tipc_bind,
2595 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002596 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002597 .accept = tipc_accept,
2598 .getname = tipc_getname,
2599 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002600 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002601 .listen = tipc_listen,
2602 .shutdown = tipc_shutdown,
2603 .setsockopt = tipc_setsockopt,
2604 .getsockopt = tipc_getsockopt,
2605 .sendmsg = tipc_send_packet,
2606 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002607 .mmap = sock_no_mmap,
2608 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002609};
2610
Florian Westphalbca65ea2008-02-07 18:18:01 -08002611static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002612 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002613 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002614 .release = tipc_release,
2615 .bind = tipc_bind,
2616 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002617 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002618 .accept = tipc_accept,
2619 .getname = tipc_getname,
2620 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002621 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002622 .listen = tipc_listen,
2623 .shutdown = tipc_shutdown,
2624 .setsockopt = tipc_setsockopt,
2625 .getsockopt = tipc_getsockopt,
2626 .sendmsg = tipc_send_stream,
2627 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002628 .mmap = sock_no_mmap,
2629 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002630};
2631
Florian Westphalbca65ea2008-02-07 18:18:01 -08002632static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002633 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002634 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002635 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002636};
2637
2638static struct proto tipc_proto = {
2639 .name = "TIPC",
2640 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002641 .obj_size = sizeof(struct tipc_sock),
2642 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002643};
2644
2645/**
Per Liden4323add2006-01-18 00:38:21 +01002646 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002647 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002648 * Returns 0 on success, errno otherwise
2649 */
Per Liden4323add2006-01-18 00:38:21 +01002650int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002651{
2652 int res;
2653
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002654 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002655 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002656 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002657 goto out;
2658 }
2659
2660 res = sock_register(&tipc_family_ops);
2661 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002662 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002663 proto_unregister(&tipc_proto);
2664 goto out;
2665 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002666 out:
2667 return res;
2668}
2669
2670/**
Per Liden4323add2006-01-18 00:38:21 +01002671 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002672 */
Per Liden4323add2006-01-18 00:38:21 +01002673void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002674{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002675 sock_unregister(tipc_family_ops.family);
2676 proto_unregister(&tipc_proto);
2677}
Richard Alpe34b78a122014-11-20 10:29:10 +01002678
2679/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002680static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002681{
2682 u32 peer_node;
2683 u32 peer_port;
2684 struct nlattr *nest;
2685
2686 peer_node = tsk_peer_node(tsk);
2687 peer_port = tsk_peer_port(tsk);
2688
2689 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2690
2691 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2692 goto msg_full;
2693 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2694 goto msg_full;
2695
2696 if (tsk->conn_type != 0) {
2697 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2698 goto msg_full;
2699 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2700 goto msg_full;
2701 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2702 goto msg_full;
2703 }
2704 nla_nest_end(skb, nest);
2705
2706 return 0;
2707
2708msg_full:
2709 nla_nest_cancel(skb, nest);
2710
2711 return -EMSGSIZE;
2712}
2713
2714/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002715static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2716 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002717{
2718 int err;
2719 void *hdr;
2720 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002721 struct net *net = sock_net(skb->sk);
2722 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002723 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002724
2725 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002726 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002727 if (!hdr)
2728 goto msg_cancel;
2729
2730 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2731 if (!attrs)
2732 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002733 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002734 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002735 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002736 goto attr_msg_cancel;
2737
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002738 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002739 err = __tipc_nl_add_sk_con(skb, tsk);
2740 if (err)
2741 goto attr_msg_cancel;
2742 } else if (!list_empty(&tsk->publications)) {
2743 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2744 goto attr_msg_cancel;
2745 }
2746 nla_nest_end(skb, attrs);
2747 genlmsg_end(skb, hdr);
2748
2749 return 0;
2750
2751attr_msg_cancel:
2752 nla_nest_cancel(skb, attrs);
2753genlmsg_cancel:
2754 genlmsg_cancel(skb, hdr);
2755msg_cancel:
2756 return -EMSGSIZE;
2757}
2758
2759int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2760{
2761 int err;
2762 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002763 const struct bucket_table *tbl;
2764 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002765 struct net *net = sock_net(skb->sk);
2766 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002767 u32 tbl_id = cb->args[0];
2768 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002769
Ying Xue07f6c4b2015-01-07 13:41:58 +08002770 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002771 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002772 for (; tbl_id < tbl->size; tbl_id++) {
2773 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002774 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002775 if (prev_portid && prev_portid != tsk->portid) {
2776 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2777 continue;
2778 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002779
Richard Alped6e164e2015-01-16 12:30:40 +01002780 err = __tipc_nl_add_sk(skb, cb, tsk);
2781 if (err) {
2782 prev_portid = tsk->portid;
2783 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2784 goto out;
2785 }
2786 prev_portid = 0;
2787 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002788 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002789 }
Richard Alped6e164e2015-01-16 12:30:40 +01002790out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002791 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002792 cb->args[0] = tbl_id;
2793 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002794
2795 return skb->len;
2796}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002797
2798/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002799static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2800 struct netlink_callback *cb,
2801 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002802{
2803 void *hdr;
2804 struct nlattr *attrs;
2805
2806 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002807 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002808 if (!hdr)
2809 goto msg_cancel;
2810
2811 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2812 if (!attrs)
2813 goto genlmsg_cancel;
2814
2815 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2816 goto attr_msg_cancel;
2817 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2818 goto attr_msg_cancel;
2819 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2820 goto attr_msg_cancel;
2821 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2822 goto attr_msg_cancel;
2823
2824 nla_nest_end(skb, attrs);
2825 genlmsg_end(skb, hdr);
2826
2827 return 0;
2828
2829attr_msg_cancel:
2830 nla_nest_cancel(skb, attrs);
2831genlmsg_cancel:
2832 genlmsg_cancel(skb, hdr);
2833msg_cancel:
2834 return -EMSGSIZE;
2835}
2836
2837/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002838static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2839 struct netlink_callback *cb,
2840 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002841{
2842 int err;
2843 struct publication *p;
2844
2845 if (*last_publ) {
2846 list_for_each_entry(p, &tsk->publications, pport_list) {
2847 if (p->key == *last_publ)
2848 break;
2849 }
2850 if (p->key != *last_publ) {
2851 /* We never set seq or call nl_dump_check_consistent()
2852 * this means that setting prev_seq here will cause the
2853 * consistence check to fail in the netlink callback
2854 * handler. Resulting in the last NLMSG_DONE message
2855 * having the NLM_F_DUMP_INTR flag set.
2856 */
2857 cb->prev_seq = 1;
2858 *last_publ = 0;
2859 return -EPIPE;
2860 }
2861 } else {
2862 p = list_first_entry(&tsk->publications, struct publication,
2863 pport_list);
2864 }
2865
2866 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2867 err = __tipc_nl_add_sk_publ(skb, cb, p);
2868 if (err) {
2869 *last_publ = p->key;
2870 return err;
2871 }
2872 }
2873 *last_publ = 0;
2874
2875 return 0;
2876}
2877
2878int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2879{
2880 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002881 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002882 u32 last_publ = cb->args[1];
2883 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002884 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002885 struct tipc_sock *tsk;
2886
Ying Xue07f6c4b2015-01-07 13:41:58 +08002887 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002888 struct nlattr **attrs;
2889 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2890
2891 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2892 if (err)
2893 return err;
2894
Richard Alpe45e093a2016-05-16 11:14:54 +02002895 if (!attrs[TIPC_NLA_SOCK])
2896 return -EINVAL;
2897
Richard Alpe1a1a1432014-11-20 10:29:11 +01002898 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2899 attrs[TIPC_NLA_SOCK],
2900 tipc_nl_sock_policy);
2901 if (err)
2902 return err;
2903
2904 if (!sock[TIPC_NLA_SOCK_REF])
2905 return -EINVAL;
2906
Ying Xue07f6c4b2015-01-07 13:41:58 +08002907 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002908 }
2909
2910 if (done)
2911 return 0;
2912
Ying Xuee05b31f2015-01-09 15:27:08 +08002913 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002914 if (!tsk)
2915 return -EINVAL;
2916
2917 lock_sock(&tsk->sk);
2918 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2919 if (!err)
2920 done = 1;
2921 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002922 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002923
Ying Xue07f6c4b2015-01-07 13:41:58 +08002924 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002925 cb->args[1] = last_publ;
2926 cb->args[2] = done;
2927
2928 return skb->len;
2929}