blob: fae6a55ef1b08efd78bb220f827fbdd09afad0ac [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy51b9a312016-11-19 14:47:07 -05004 * Copyright (c) 2001-2007, 2012-2016, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Ying Xue07f6c4b2015-01-07 13:41:58 +080037#include <linux/rhashtable.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Jon Paul Maloyc637c102015-02-05 08:36:41 -050042#include "name_distr.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040043#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040044#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010045#include "netlink.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040046
Ying Xue07f6c4b2015-01-07 13:41:58 +080047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080048#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080049#define TIPC_FWD_MSG 1
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define TIPC_MAX_PORT 0xffffffff
51#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040052
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010053enum {
54 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010055 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010056 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010057 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +010058 TIPC_CONNECTING = TCP_SYN_SENT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010059};
60
Jon Paul Maloy301bae52014-08-22 18:09:20 -040061/**
62 * struct tipc_sock - TIPC socket structure
63 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040064 * @conn_type: TIPC type used when connection was established
65 * @conn_instance: TIPC instance used when connection was established
66 * @published: non-zero if port has one or more associated names
67 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080068 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040069 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy301bae52014-08-22 18:09:20 -040070 * @publications: list of publications for port
71 * @pub_count: total # of publications port has made during its lifetime
72 * @probing_state:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073 * @conn_timeout: the time we can wait for an unresponded setup request
74 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
75 * @link_cong: non-zero if owner must sleep because of link congestion
76 * @sent_unacked: # messages sent by socket, and not yet acked by peer
77 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010078 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080079 * @node: hash table node
80 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040081 */
82struct tipc_sock {
83 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040084 u32 conn_type;
85 u32 conn_instance;
86 int published;
87 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080088 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040089 struct tipc_msg phdr;
90 struct list_head sock_list;
91 struct list_head publications;
92 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040093 uint conn_timeout;
94 atomic_t dupl_rcvcnt;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010095 bool probe_unacked;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 bool link_cong;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -040097 u16 snt_unacked;
98 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -040099 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400100 u16 rcv_unacked;
101 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100102 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800103 struct rhash_head node;
104 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400105};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400107static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400108static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800109static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800110static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800111static int tipc_release(struct socket *sock);
112static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
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;
Herbert Xu6cca72892015-03-20 21:57:05 +1100129static const struct rhashtable_params tsk_rht_params;
130
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500131static u32 tsk_own_node(struct tipc_sock *tsk)
132{
133 return msg_prevnode(&tsk->phdr);
134}
135
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400136static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400137{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400138 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400139}
140
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400141static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400142{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400143 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400144}
145
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400146static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400147{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400148 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400149}
150
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400151static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400152{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400153 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400154}
155
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400156static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400157{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400158 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400159}
160
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400161static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400162{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400163 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400164}
165
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400166static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400167{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400168 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400169}
170
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400171static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400172{
173 if (imp > TIPC_CRITICAL_IMPORTANCE)
174 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400175 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400176 return 0;
177}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400178
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400179static struct tipc_sock *tipc_sk(const struct sock *sk)
180{
181 return container_of(sk, struct tipc_sock, sk);
182}
183
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400184static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400185{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500186 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400187}
188
189/* tsk_blocks(): translate a buffer size in bytes to number of
190 * advertisable blocks, taking into account the ratio truesize(len)/len
191 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
192 */
193static u16 tsk_adv_blocks(int len)
194{
195 return len / FLOWCTL_BLK_SZ / 4;
196}
197
198/* tsk_inc(): increment counter for sent or received data
199 * - If block based flow control is not supported by peer we
200 * fall back to message based ditto, incrementing the counter
201 */
202static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
203{
204 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
205 return ((msglen / FLOWCTL_BLK_SZ) + 1);
206 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207}
208
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400210 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700211 *
212 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400214static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400216 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217}
218
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400219/* tipc_sk_respond() : send response message back to sender
220 */
221static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
222{
223 u32 selector;
224 u32 dnode;
225 u32 onode = tipc_own_addr(sock_net(sk));
226
227 if (!tipc_msg_reverse(onode, &skb, err))
228 return;
229
230 dnode = msg_destnode(buf_msg(skb));
231 selector = msg_origport(buf_msg(skb));
232 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
233}
234
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400236 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700237 *
238 * Caller must hold socket lock
239 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400240static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700241{
Ying Xuea6ca1092014-11-26 11:41:55 +0800242 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700243
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400244 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
245 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700246}
247
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100248static bool tipc_sk_connected(struct sock *sk)
249{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100250 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100251}
252
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100253/* tipc_sk_type_connectionless - check if the socket is datagram socket
254 * @sk: socket
255 *
256 * Returns true if connection less, false otherwise
257 */
258static bool tipc_sk_type_connectionless(struct sock *sk)
259{
260 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
261}
262
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400263/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400264 *
265 * Handles cases where the node's network address has changed from
266 * the default of <0.0.0> to its configured setting.
267 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400268static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400269{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100270 struct sock *sk = &tsk->sk;
271 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400272 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400273 u32 orig_node;
274 u32 peer_node;
275
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100276 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400277 return false;
278
279 if (unlikely(msg_origport(msg) != peer_port))
280 return false;
281
282 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400283 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400284
285 if (likely(orig_node == peer_node))
286 return true;
287
Ying Xue34747532015-01-09 15:27:10 +0800288 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400289 return true;
290
Ying Xue34747532015-01-09 15:27:10 +0800291 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400292 return true;
293
294 return false;
295}
296
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100297/* tipc_set_sk_state - set the sk_state of the socket
298 * @sk: socket
299 *
300 * Caller must hold socket lock
301 *
302 * Returns 0 on success, errno otherwise
303 */
304static int tipc_set_sk_state(struct sock *sk, int state)
305{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100306 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100307 int res = -EINVAL;
308
309 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100310 case TIPC_OPEN:
311 res = 0;
312 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100313 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100314 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100315 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100316 res = 0;
317 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100318 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100319 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100320 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100321 res = 0;
322 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100323 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100324 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100325 oldsk_state == TIPC_ESTABLISHED)
326 res = 0;
327 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100328 }
329
330 if (!res)
331 sk->sk_state = state;
332
333 return res;
334}
335
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500336static int tipc_sk_sock_err(struct socket *sock, long *timeout)
337{
338 struct sock *sk = sock->sk;
339 int err = sock_error(sk);
340 int typ = sock->type;
341
342 if (err)
343 return err;
344 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
345 if (sk->sk_state == TIPC_DISCONNECTING)
346 return -EPIPE;
347 else if (!tipc_sk_connected(sk))
348 return -ENOTCONN;
349 }
350 if (!*timeout)
351 return -EAGAIN;
352 if (signal_pending(current))
353 return sock_intr_errno(*timeout);
354
355 return 0;
356}
357
358#define tipc_wait_for_cond(sock_, timeout_, condition_) \
359({ \
360 int rc_ = 0; \
361 int done_ = 0; \
362 \
363 while (!(condition_) && !done_) { \
364 struct sock *sk_ = sock->sk; \
365 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
366 \
367 rc_ = tipc_sk_sock_err(sock_, timeout_); \
368 if (rc_) \
369 break; \
370 prepare_to_wait(sk_sleep(sk_), &wait_, \
371 TASK_INTERRUPTIBLE); \
372 done_ = sk_wait_event(sk_, timeout_, \
373 (condition_), &wait_); \
374 remove_wait_queue(sk_sleep(sk_), &wait_); \
375 } \
376 rc_; \
377})
378
Allan Stephens0c3141e2008-04-15 00:22:02 -0700379/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400380 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700381 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 * @sock: pre-allocated socket structure
383 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800384 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900385 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700386 * This routine creates additional data structures used by the TIPC socket,
387 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 *
389 * Returns 0 on success, errno otherwise
390 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400391static int tipc_sk_create(struct net *net, struct socket *sock,
392 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500394 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700395 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400397 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400398 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700399
400 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 if (unlikely(protocol != 0))
402 return -EPROTONOSUPPORT;
403
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 switch (sock->type) {
405 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700406 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 break;
408 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700409 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 break;
411 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700413 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 break;
Allan Stephens49978652006-06-25 23:47:18 -0700415 default:
Allan Stephens49978652006-06-25 23:47:18 -0700416 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 }
418
Allan Stephens0c3141e2008-04-15 00:22:02 -0700419 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500420 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700421 if (sk == NULL)
422 return -ENOMEM;
423
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400424 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400425 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400426 INIT_LIST_HEAD(&tsk->publications);
427 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500428 tn = net_generic(sock_net(sk), tipc_net_id);
429 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400430 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431
Allan Stephens0c3141e2008-04-15 00:22:02 -0700432 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700433 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100435 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800436 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900437 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800438 return -EINVAL;
439 }
440 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800441 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100442 sk->sk_shutdown = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400443 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400444 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800445 sk->sk_data_ready = tipc_data_ready;
446 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800447 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400448 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
449 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700450
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400451 /* Start out with safe limits until we receive an advertised window */
452 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
453 tsk->rcv_win = tsk->snd_win;
454
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100455 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400456 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700457 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400458 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700459 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100460
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 return 0;
462}
463
Ying Xue07f6c4b2015-01-07 13:41:58 +0800464static void tipc_sk_callback(struct rcu_head *head)
465{
466 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
467
468 sock_put(&tsk->sk);
469}
470
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100471/* Caller should hold socket lock for the socket. */
472static void __tipc_shutdown(struct socket *sock, int error)
473{
474 struct sock *sk = sock->sk;
475 struct tipc_sock *tsk = tipc_sk(sk);
476 struct net *net = sock_net(sk);
477 u32 dnode = tsk_peer_node(tsk);
478 struct sk_buff *skb;
479
480 /* Reject all unreceived messages, except on an active connection
481 * (which disconnects locally & sends a 'FIN+' to peer).
482 */
483 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
484 if (TIPC_SKB_CB(skb)->bytes_read) {
485 kfree_skb(skb);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500486 continue;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100487 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500488 if (!tipc_sk_type_connectionless(sk) &&
489 sk->sk_state != TIPC_DISCONNECTING) {
490 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
491 tipc_node_remove_conn(net, dnode, tsk->portid);
492 }
493 tipc_sk_respond(sk, skb, error);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100494 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500495
496 if (tipc_sk_type_connectionless(sk))
497 return;
498
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100499 if (sk->sk_state != TIPC_DISCONNECTING) {
500 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
501 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
502 tsk_own_node(tsk), tsk_peer_port(tsk),
503 tsk->portid, error);
504 if (skb)
505 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500506 tipc_node_remove_conn(net, dnode, tsk->portid);
507 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100508 }
509}
510
Ying Xuec5fa7b32013-06-17 10:54:39 -0400511/**
Ying Xue247f0f32014-02-18 16:06:46 +0800512 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 * @sock: socket to destroy
514 *
515 * This routine cleans up any messages that are still queued on the socket.
516 * For DGRAM and RDM socket types, all queued messages are rejected.
517 * For SEQPACKET and STREAM socket types, the first message is rejected
518 * and any others are discarded. (If the first message on a STREAM socket
519 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900520 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 * NOTE: Rejected messages are not necessarily returned to the sender! They
522 * are returned or discarded according to the "destination droppable" setting
523 * specified for the message by the sender.
524 *
525 * Returns 0 on success, errno otherwise
526 */
Ying Xue247f0f32014-02-18 16:06:46 +0800527static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400530 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531
Allan Stephens0c3141e2008-04-15 00:22:02 -0700532 /*
533 * Exit if socket isn't fully initialized (occurs when a failed accept()
534 * releases a pre-allocated child socket that was never used)
535 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700536 if (sk == NULL)
537 return 0;
538
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400539 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700540 lock_sock(sk);
541
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100542 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
543 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400544 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800545 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800546 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547
Allan Stephens0c3141e2008-04-15 00:22:02 -0700548 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700549 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800550
551 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700552 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200554 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555}
556
557/**
Ying Xue247f0f32014-02-18 16:06:46 +0800558 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 * @sock: socket structure
560 * @uaddr: socket address describing name(s) and desired operation
561 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 * Name and name sequence binding is indicated using a positive scope value;
564 * a negative scope value unbinds the specified name. Specifying no name
565 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700568 *
569 * NOTE: This routine doesn't need to take the socket lock since it doesn't
570 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 */
Ying Xue247f0f32014-02-18 16:06:46 +0800572static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
573 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574{
Ying Xue84602762013-12-27 10:18:28 +0800575 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400577 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800578 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579
Ying Xue84602762013-12-27 10:18:28 +0800580 lock_sock(sk);
581 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400582 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800583 goto exit;
584 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900585
Ying Xue84602762013-12-27 10:18:28 +0800586 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
587 res = -EINVAL;
588 goto exit;
589 }
590 if (addr->family != AF_TIPC) {
591 res = -EAFNOSUPPORT;
592 goto exit;
593 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 if (addr->addrtype == TIPC_ADDR_NAME)
596 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800597 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
598 res = -EAFNOSUPPORT;
599 goto exit;
600 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900601
Ying Xue13a2e892013-06-17 10:54:40 -0400602 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400603 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800604 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
605 res = -EACCES;
606 goto exit;
607 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400608
Ying Xue84602762013-12-27 10:18:28 +0800609 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400610 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
611 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800612exit:
613 release_sock(sk);
614 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615}
616
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900617/**
Ying Xue247f0f32014-02-18 16:06:46 +0800618 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 * @sock: socket structure
620 * @uaddr: area for returned socket address
621 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700622 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900623 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700625 *
Allan Stephens2da59912008-07-14 22:43:32 -0700626 * NOTE: This routine doesn't need to take the socket lock since it only
627 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000628 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 */
Ying Xue247f0f32014-02-18 16:06:46 +0800630static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
631 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100634 struct sock *sk = sock->sk;
635 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +0800636 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000638 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700639 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100640 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100641 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700642 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400643 addr->addr.id.ref = tsk_peer_port(tsk);
644 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700645 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800646 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800647 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700648 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649
650 *uaddr_len = sizeof(*addr);
651 addr->addrtype = TIPC_ADDR_ID;
652 addr->family = AF_TIPC;
653 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 addr->addr.name.domain = 0;
655
Allan Stephens0c3141e2008-04-15 00:22:02 -0700656 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657}
658
659/**
Ying Xue247f0f32014-02-18 16:06:46 +0800660 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 * @file: file structure associated with the socket
662 * @sock: socket for which to calculate the poll bits
663 * @wait: ???
664 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700665 * Returns pollmask value
666 *
667 * COMMENTARY:
668 * It appears that the usual socket locking mechanisms are not useful here
669 * since the pollmask info is potentially out-of-date the moment this routine
670 * exits. TCP and other protocols seem to rely on higher level poll routines
671 * to handle any preventable race conditions, so TIPC will do the same ...
672 *
Allan Stephensf662c072010-08-17 11:00:06 +0000673 * IMPORTANT: The fact that a read or write operation is indicated does NOT
674 * imply that the operation will succeed, merely that it should be performed
675 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 */
Ying Xue247f0f32014-02-18 16:06:46 +0800677static unsigned int tipc_poll(struct file *file, struct socket *sock,
678 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679{
Allan Stephens9b674e82008-03-26 16:48:21 -0700680 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400681 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000682 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700683
Ying Xuef288bef2012-08-21 11:16:57 +0800684 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700685
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100686 if (sk->sk_shutdown & RCV_SHUTDOWN)
687 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
688 if (sk->sk_shutdown == SHUTDOWN_MASK)
689 mask |= POLLHUP;
690
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100691 switch (sk->sk_state) {
692 case TIPC_ESTABLISHED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400693 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000694 mask |= POLLOUT;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100695 /* fall thru' */
696 case TIPC_LISTEN:
697 case TIPC_CONNECTING:
Allan Stephensf662c072010-08-17 11:00:06 +0000698 if (!skb_queue_empty(&sk->sk_receive_queue))
699 mask |= (POLLIN | POLLRDNORM);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100700 break;
701 case TIPC_OPEN:
702 if (!tsk->link_cong)
703 mask |= POLLOUT;
704 if (tipc_sk_type_connectionless(sk) &&
705 (!skb_queue_empty(&sk->sk_receive_queue)))
706 mask |= (POLLIN | POLLRDNORM);
707 break;
708 case TIPC_DISCONNECTING:
709 mask = (POLLIN | POLLRDNORM | POLLHUP);
710 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000711 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700712
713 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714}
715
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400716/**
717 * tipc_sendmcast - send multicast message
718 * @sock: socket structure
719 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500720 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400721 * @dsz: total length of message data
722 * @timeo: timeout to wait for wakeup
723 *
724 * Called from function tipc_sendmsg(), which has done all sanity checks
725 * Returns the number of bytes sent on success, or errno
726 */
727static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500728 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400729{
730 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500731 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800732 struct net *net = sock_net(sk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500733 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100734 struct sk_buff_head pktchain;
Al Virof25dcc72014-11-28 15:52:29 -0500735 struct iov_iter save = msg->msg_iter;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400736 uint mtu;
737 int rc;
738
Parthasarathy Bhuvaragan7cf87fa2016-11-01 14:02:34 +0100739 if (!timeo && tsk->link_cong)
740 return -ELINKCONG;
741
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400742 msg_set_type(mhdr, TIPC_MCAST_MSG);
743 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
744 msg_set_destport(mhdr, 0);
745 msg_set_destnode(mhdr, 0);
746 msg_set_nametype(mhdr, seq->type);
747 msg_set_namelower(mhdr, seq->lower);
748 msg_set_nameupper(mhdr, seq->upper);
749 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
750
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100751 skb_queue_head_init(&pktchain);
752
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400753new_mtu:
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400754 mtu = tipc_bcast_get_mtu(net);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100755 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400756 if (unlikely(rc < 0))
757 return rc;
758
759 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100760 rc = tipc_bcast_xmit(net, &pktchain);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400761 if (likely(!rc))
762 return dsz;
763
764 if (rc == -ELINKCONG) {
765 tsk->link_cong = 1;
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500766 rc = tipc_wait_for_cond(sock, &timeo, !tsk->link_cong);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400767 if (!rc)
768 continue;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400769 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100770 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500771 if (rc == -EMSGSIZE) {
772 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400773 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500774 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400775 break;
776 } while (1);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400777 return rc;
778}
779
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500780/**
781 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
782 * @arrvq: queue with arriving messages, to be cloned after destination lookup
783 * @inputq: queue with cloned messages, delivered to socket after dest lookup
784 *
785 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400786 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500787void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
788 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400789{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500790 struct tipc_msg *msg;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500791 struct list_head dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500792 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400793 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500794 struct sk_buff_head tmpq;
795 uint hsz;
796 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500797
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500798 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500799 INIT_LIST_HEAD(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400800
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500801 skb = tipc_skb_peek(arrvq, &inputq->lock);
802 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
803 msg = buf_msg(skb);
804 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400805
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500806 if (in_own_node(net, msg_orignode(msg)))
807 scope = TIPC_NODE_SCOPE;
808
809 /* Create destination port list and message clones: */
810 tipc_nametbl_mc_translate(net,
811 msg_nametype(msg), msg_namelower(msg),
812 msg_nameupper(msg), scope, &dports);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500813 portid = u32_pop(&dports);
814 for (; portid; portid = u32_pop(&dports)) {
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500815 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
816 if (_skb) {
817 msg_set_destport(buf_msg(_skb), portid);
818 __skb_queue_tail(&tmpq, _skb);
819 continue;
820 }
821 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400822 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500823 /* Append to inputq if not already done by other thread */
824 spin_lock_bh(&inputq->lock);
825 if (skb_peek(arrvq) == skb) {
826 skb_queue_splice_tail_init(&tmpq, inputq);
827 kfree_skb(__skb_dequeue(arrvq));
828 }
829 spin_unlock_bh(&inputq->lock);
830 __skb_queue_purge(&tmpq);
831 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400832 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500833 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400834}
835
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900836/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500837 * tipc_sk_proto_rcv - receive a connection mng protocol message
838 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400839 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500840 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400841static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
842 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500843{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400844 struct sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400845 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400846 struct tipc_msg *hdr = buf_msg(skb);
847 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400848 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400849
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500850 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400851 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500852 goto exit;
853
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100854 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500855
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400856 if (mtyp == CONN_PROBE) {
857 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400858 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
859 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400860 return;
861 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400862 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400863 tsk->snt_unacked -= msg_conn_ack(hdr);
864 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
865 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500866 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400867 sk->sk_write_space(sk);
868 } else if (mtyp != CONN_PROBE_REPLY) {
869 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500870 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500871exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400872 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500873}
874
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500875/**
Ying Xue247f0f32014-02-18 16:06:46 +0800876 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 * @sock: socket structure
878 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500879 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900880 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900882 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
884 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900885 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 * Returns the number of bytes sent on success, or errno otherwise
887 */
Ying Xue1b784142015-03-02 15:37:48 +0800888static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500889 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890{
Ying Xue39a02952015-03-02 15:37:47 +0800891 struct sock *sk = sock->sk;
892 int ret;
893
894 lock_sock(sk);
895 ret = __tipc_sendmsg(sock, m, dsz);
896 release_sock(sk);
897
898 return ret;
899}
900
901static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
902{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500903 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700904 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400905 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800906 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400907 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500908 u32 dnode, dport;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100909 struct sk_buff_head pktchain;
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100910 bool is_connectionless = tipc_sk_type_connectionless(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +0800911 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100912 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500913 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500914 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800915 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100916 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500918 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400919 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100920 if (unlikely(!dest)) {
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100921 if (is_connectionless && tsk->peer.family == AF_TIPC)
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100922 dest = &tsk->peer;
Erik Hugnef2f80362015-03-19 09:02:19 +0100923 else
924 return -EDESTADDRREQ;
925 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
926 dest->family != AF_TIPC) {
927 return -EINVAL;
928 }
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100929 if (!is_connectionless) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100930 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a02952015-03-02 15:37:47 +0800931 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100932 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a02952015-03-02 15:37:47 +0800933 return -EISCONN;
934 if (tsk->published)
935 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700936 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400937 tsk->conn_type = dest->addr.name.name.type;
938 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700939 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100941 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800942 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500943
944 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a02952015-03-02 15:37:47 +0800945 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500946 } else if (dest->addrtype == TIPC_ADDR_NAME) {
947 u32 type = dest->addr.name.name.type;
948 u32 inst = dest->addr.name.name.instance;
949 u32 domain = dest->addr.name.domain;
950
951 dnode = domain;
952 msg_set_type(mhdr, TIPC_NAMED_MSG);
953 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
954 msg_set_nametype(mhdr, type);
955 msg_set_nameinst(mhdr, inst);
956 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800957 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500958 msg_set_destnode(mhdr, dnode);
959 msg_set_destport(mhdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800960 if (unlikely(!dport && !dnode))
961 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500962 } else if (dest->addrtype == TIPC_ADDR_ID) {
963 dnode = dest->addr.id.node;
964 msg_set_type(mhdr, TIPC_DIRECT_MSG);
965 msg_set_lookup_scope(mhdr, 0);
966 msg_set_destnode(mhdr, dnode);
967 msg_set_destport(mhdr, dest->addr.id.ref);
968 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
969 }
970
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100971 skb_queue_head_init(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500972 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500973new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800974 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100975 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500976 if (rc < 0)
Ying Xue39a02952015-03-02 15:37:47 +0800977 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500978
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900979 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100980 skb = skb_peek(&pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +0800981 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100982 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400983 if (likely(!rc)) {
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100984 if (!is_connectionless)
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100985 tipc_set_sk_state(sk, TIPC_CONNECTING);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400986 return dsz;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900987 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400988 if (rc == -ELINKCONG) {
989 tsk->link_cong = 1;
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500990 rc = tipc_wait_for_cond(sock, &timeo, !tsk->link_cong);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400991 if (!rc)
992 continue;
993 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100994 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500995 if (rc == -EMSGSIZE) {
996 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500997 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500998 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400999 break;
1000 } while (1);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001001
1002 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003}
1004
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001005/**
Ying Xue247f0f32014-02-18 16:06:46 +08001006 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 * @sock: socket structure
1008 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001009 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001010 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001012 *
1013 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001014 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 */
Ying Xue1b784142015-03-02 15:37:48 +08001016static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001018 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +08001019 int ret;
1020
1021 lock_sock(sk);
1022 ret = __tipc_send_stream(sock, m, dsz);
1023 release_sock(sk);
1024
1025 return ret;
1026}
1027
1028static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1029{
1030 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001031 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001032 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001033 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001034 struct sk_buff_head pktchain;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001035 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001036 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001037 int rc = -EINVAL;
1038 long timeo;
1039 u32 dnode;
1040 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001041 struct iov_iter save;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001042 int hlen = MIN_H_SIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001043
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001044 /* Handle implied connection establishment */
1045 if (unlikely(dest)) {
Ying Xue39a02952015-03-02 15:37:47 +08001046 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001047 hlen = msg_hdr_sz(mhdr);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001048 if (dsz && (dsz == rc))
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001049 tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001050 return rc;
1051 }
1052 if (dsz > (uint)INT_MAX)
1053 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001054
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001055 if (unlikely(!tipc_sk_connected(sk))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001056 if (sk->sk_state == TIPC_DISCONNECTING)
Ying Xue39a02952015-03-02 15:37:47 +08001057 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001058 else
Ying Xue39a02952015-03-02 15:37:47 +08001059 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001060 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001062 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Parthasarathy Bhuvaragan7cf87fa2016-11-01 14:02:34 +01001063 if (!timeo && tsk->link_cong)
1064 return -ELINKCONG;
1065
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001066 dnode = tsk_peer_node(tsk);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001067 skb_queue_head_init(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001068
1069next:
Al Virof25dcc72014-11-28 15:52:29 -05001070 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001071 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001072 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001073 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001074 if (unlikely(rc < 0))
Ying Xue39a02952015-03-02 15:37:47 +08001075 return rc;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001076
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001077 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001078 if (likely(!tsk_conn_cong(tsk))) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001079 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001080 if (likely(!rc)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001081 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001082 sent += send;
1083 if (sent == dsz)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001084 return dsz;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001085 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001086 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001087 if (rc == -EMSGSIZE) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001088 __skb_queue_purge(&pktchain);
Ying Xuef2f98002015-01-09 15:27:05 +08001089 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1090 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001091 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001092 goto next;
1093 }
1094 if (rc != -ELINKCONG)
1095 break;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001096
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001097 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 }
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001099 rc = tipc_wait_for_cond(sock, &timeo,
1100 (!tsk->link_cong &&
1101 !tsk_conn_cong(tsk) &&
1102 tipc_sk_connected(sk)));
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001103 } while (!rc);
Ying Xue39a02952015-03-02 15:37:47 +08001104
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001105 __skb_queue_purge(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001106 return sent ? sent : rc;
1107}
1108
1109/**
1110 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001111 * @sock: socket structure
1112 * @m: message to send
1113 * @dsz: length of data to be transmitted
1114 *
1115 * Used for SOCK_SEQPACKET messages.
1116 *
1117 * Returns the number of bytes sent on success, or errno otherwise
1118 */
Ying Xue1b784142015-03-02 15:37:48 +08001119static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001120{
1121 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1122 return -EMSGSIZE;
1123
Ying Xue1b784142015-03-02 15:37:48 +08001124 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125}
1126
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001127/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001129static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001130 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131{
Ying Xue3721e9c2015-01-13 17:07:48 +08001132 struct sock *sk = &tsk->sk;
1133 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001134 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001136 msg_set_destnode(msg, peer_node);
1137 msg_set_destport(msg, peer_port);
1138 msg_set_type(msg, TIPC_CONN_MSG);
1139 msg_set_lookup_scope(msg, 0);
1140 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001141
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01001142 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001143 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001144 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1145 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001146 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001147 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1148 return;
1149
1150 /* Fall back to message based flow control */
1151 tsk->rcv_win = FLOWCTL_MSG_WIN;
1152 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153}
1154
1155/**
1156 * set_orig_addr - capture sender's address for received message
1157 * @m: descriptor for message info
1158 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001159 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 * Note: Address is not captured if not requested by receiver.
1161 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001162static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001164 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001166 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 addr->family = AF_TIPC;
1168 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001169 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 addr->addr.id.ref = msg_origport(msg);
1171 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001172 addr->addr.name.domain = 0; /* could leave uninitialized */
1173 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 m->msg_namelen = sizeof(struct sockaddr_tipc);
1175 }
1176}
1177
1178/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001179 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 * @m: descriptor for message info
1181 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001182 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001183 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001185 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 * Returns 0 if successful, otherwise errno
1187 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001188static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1189 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190{
1191 u32 anc_data[3];
1192 u32 err;
1193 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001194 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 int res;
1196
1197 if (likely(m->msg_controllen == 0))
1198 return 0;
1199
1200 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 err = msg ? msg_errcode(msg) : 0;
1202 if (unlikely(err)) {
1203 anc_data[0] = err;
1204 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001205 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1206 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001208 if (anc_data[1]) {
1209 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1210 msg_data(msg));
1211 if (res)
1212 return res;
1213 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 }
1215
1216 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1218 switch (dest_type) {
1219 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001220 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 anc_data[0] = msg_nametype(msg);
1222 anc_data[1] = msg_namelower(msg);
1223 anc_data[2] = msg_namelower(msg);
1224 break;
1225 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001226 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227 anc_data[0] = msg_nametype(msg);
1228 anc_data[1] = msg_namelower(msg);
1229 anc_data[2] = msg_nameupper(msg);
1230 break;
1231 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001232 has_name = (tsk->conn_type != 0);
1233 anc_data[0] = tsk->conn_type;
1234 anc_data[1] = tsk->conn_instance;
1235 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 break;
1237 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001238 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 }
Allan Stephens2db99832010-12-31 18:59:33 +00001240 if (has_name) {
1241 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1242 if (res)
1243 return res;
1244 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245
1246 return 0;
1247}
1248
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001249static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001250{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001251 struct sock *sk = &tsk->sk;
1252 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001253 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001254 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001255 u32 peer_port = tsk_peer_port(tsk);
1256 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001257
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001258 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001259 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001260 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1261 dnode, tsk_own_node(tsk), peer_port,
1262 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001263 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001264 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001265 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001266 msg_set_conn_ack(msg, tsk->rcv_unacked);
1267 tsk->rcv_unacked = 0;
1268
1269 /* Adjust to and advertize the correct window limit */
1270 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1271 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1272 msg_set_adv_win(msg, tsk->rcv_win);
1273 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001274 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001275}
1276
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001277static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001278{
1279 struct sock *sk = sock->sk;
1280 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001281 long timeo = *timeop;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001282 int err;
1283
1284 for (;;) {
1285 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001286 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001287 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001288 err = -ENOTCONN;
1289 break;
1290 }
1291 release_sock(sk);
1292 timeo = schedule_timeout(timeo);
1293 lock_sock(sk);
1294 }
1295 err = 0;
1296 if (!skb_queue_empty(&sk->sk_receive_queue))
1297 break;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001298 err = -EAGAIN;
1299 if (!timeo)
1300 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001301 err = sock_intr_errno(timeo);
1302 if (signal_pending(current))
1303 break;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001304 }
1305 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001306 *timeop = timeo;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001307 return err;
1308}
1309
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001310/**
Ying Xue247f0f32014-02-18 16:06:46 +08001311 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 * @m: descriptor for message info
1313 * @buf_len: total size of user buffer area
1314 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001315 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1317 * If the complete message doesn't fit in user area, truncate it.
1318 *
1319 * Returns size of returned message data, errno otherwise
1320 */
Ying Xue1b784142015-03-02 15:37:48 +08001321static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1322 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001324 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001325 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 struct sk_buff *buf;
1327 struct tipc_msg *msg;
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001328 bool is_connectionless = tipc_sk_type_connectionless(sk);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001329 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330 unsigned int sz;
1331 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001332 int res, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333
Allan Stephens0c3141e2008-04-15 00:22:02 -07001334 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 if (unlikely(!buf_len))
1336 return -EINVAL;
1337
Allan Stephens0c3141e2008-04-15 00:22:02 -07001338 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001340 if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 res = -ENOTCONN;
1342 goto exit;
1343 }
1344
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001345 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001346restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001349 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001350 if (res)
1351 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001352
1353 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001354 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 msg = buf_msg(buf);
1356 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001357 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 err = msg_errcode(msg);
1359
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001362 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 goto restart;
1364 }
1365
1366 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 set_orig_addr(m, msg);
1368
1369 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001370 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001371 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 goto exit;
1373
1374 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 if (!err) {
1376 if (unlikely(buf_len < sz)) {
1377 sz = buf_len;
1378 m->msg_flags |= MSG_TRUNC;
1379 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001380 res = skb_copy_datagram_msg(buf, hlen, m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001381 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 res = sz;
1384 } else {
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001385 if (is_connectionless || err == TIPC_CONN_SHUTDOWN ||
1386 m->msg_control)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 res = 0;
1388 else
1389 res = -ECONNRESET;
1390 }
1391
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001392 if (unlikely(flags & MSG_PEEK))
1393 goto exit;
1394
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001395 if (likely(!is_connectionless)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001396 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1397 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1398 tipc_sk_send_ack(tsk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001399 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001400 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001402 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 return res;
1404}
1405
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001406/**
Ying Xue247f0f32014-02-18 16:06:46 +08001407 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408 * @m: descriptor for message info
1409 * @buf_len: total size of user buffer area
1410 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001411 *
1412 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 * will optionally wait for more; never truncates data.
1414 *
1415 * Returns size of returned message data, errno otherwise
1416 */
Ying Xue1b784142015-03-02 15:37:48 +08001417static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1418 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001420 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001421 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 struct sk_buff *buf;
1423 struct tipc_msg *msg;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001424 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425 unsigned int sz;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001426 int target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001429 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430
1431 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432 if (unlikely(!buf_len))
1433 return -EINVAL;
1434
Allan Stephens0c3141e2008-04-15 00:22:02 -07001435 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001437 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 res = -ENOTCONN;
1439 goto exit;
1440 }
1441
Florian Westphal3720d402010-08-17 11:00:04 +00001442 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001443 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001444
Allan Stephens0c3141e2008-04-15 00:22:02 -07001445restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001446 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001447 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001448 if (res)
1449 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001450
1451 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001452 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453 msg = buf_msg(buf);
1454 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001455 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456 err = msg_errcode(msg);
1457
1458 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001460 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 goto restart;
1462 }
1463
1464 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465 if (sz_copied == 0) {
1466 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001467 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001468 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469 goto exit;
1470 }
1471
1472 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 if (!err) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001474 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1475 u32 needed;
1476 int sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477
Allan Stephens0232fd02011-02-21 09:45:40 -05001478 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 needed = (buf_len - sz_copied);
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001480 sz_to_copy = min(sz, needed);
Allan Stephens0232fd02011-02-21 09:45:40 -05001481
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001482 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001483 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001485
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486 sz_copied += sz_to_copy;
1487
1488 if (sz_to_copy < sz) {
1489 if (!(flags & MSG_PEEK))
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001490 TIPC_SKB_CB(buf)->bytes_read =
1491 offset + sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492 goto exit;
1493 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 } else {
1495 if (sz_copied != 0)
1496 goto exit; /* can't add error msg to valid data */
1497
1498 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1499 res = 0;
1500 else
1501 res = -ECONNRESET;
1502 }
1503
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001504 if (unlikely(flags & MSG_PEEK))
1505 goto exit;
1506
1507 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1508 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1509 tipc_sk_send_ack(tsk);
1510 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511
1512 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001513 if ((sz_copied < buf_len) && /* didn't get all requested data */
1514 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001515 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001516 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517 goto restart;
1518
1519exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001521 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522}
1523
1524/**
Ying Xuef288bef2012-08-21 11:16:57 +08001525 * tipc_write_space - wake up thread if port congestion is released
1526 * @sk: socket
1527 */
1528static void tipc_write_space(struct sock *sk)
1529{
1530 struct socket_wq *wq;
1531
1532 rcu_read_lock();
1533 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001534 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001535 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1536 POLLWRNORM | POLLWRBAND);
1537 rcu_read_unlock();
1538}
1539
1540/**
1541 * tipc_data_ready - wake up threads to indicate messages have been received
1542 * @sk: socket
1543 * @len: the length of messages
1544 */
David S. Miller676d2362014-04-11 16:15:36 -04001545static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001546{
1547 struct socket_wq *wq;
1548
1549 rcu_read_lock();
1550 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001551 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001552 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1553 POLLRDNORM | POLLRDBAND);
1554 rcu_read_unlock();
1555}
1556
Ying Xuef4195d12015-11-22 15:46:05 +08001557static void tipc_sock_destruct(struct sock *sk)
1558{
1559 __skb_queue_purge(&sk->sk_receive_queue);
1560}
1561
Ying Xuef288bef2012-08-21 11:16:57 +08001562/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001563 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001564 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001565 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001566 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001567 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001568 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001569static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001570{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001571 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001572 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001573 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001574
Jon Paul Maloycda36962015-07-22 10:11:20 -04001575 if (unlikely(msg_mcast(hdr)))
1576 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001577
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001578 switch (sk->sk_state) {
1579 case TIPC_CONNECTING:
Ying Xue7e6c1312012-11-29 18:39:14 -05001580 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001581 if (unlikely(!msg_connected(hdr)))
1582 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001583
Jon Paul Maloycda36962015-07-22 10:11:20 -04001584 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001585 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugne2c8d8512013-08-28 09:29:58 +02001586 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001587 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001588 }
1589
Jon Paul Maloycda36962015-07-22 10:11:20 -04001590 if (unlikely(!msg_isdata(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001591 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001592 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001593 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001594 }
1595
Jon Paul Maloycda36962015-07-22 10:11:20 -04001596 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1597 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001598
Jon Paul Maloycda36962015-07-22 10:11:20 -04001599 /* If 'ACK+' message, add to socket receive queue */
1600 if (msg_data_sz(hdr))
1601 return true;
1602
1603 /* If empty 'ACK-' message, wake up sleeping connect() */
1604 if (waitqueue_active(sk_sleep(sk)))
1605 wake_up_interruptible(sk_sleep(sk));
1606
1607 /* 'ACK-' message is neither accepted nor rejected: */
1608 msg_set_dest_droppable(hdr, 1);
1609 return false;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001610
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001611 case TIPC_OPEN:
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001612 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001613 break;
1614 case TIPC_LISTEN:
Ying Xue7e6c1312012-11-29 18:39:14 -05001615 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001616 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1617 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001618 break;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001619 case TIPC_ESTABLISHED:
1620 /* Accept only connection-based messages sent by peer */
1621 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1622 return false;
1623
1624 if (unlikely(msg_errcode(hdr))) {
1625 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1626 /* Let timer expire on it's own */
1627 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1628 tsk->portid);
1629 sk->sk_state_change(sk);
1630 }
1631 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001632 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001633 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05001634 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001635
Jon Paul Maloycda36962015-07-22 10:11:20 -04001636 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001637}
1638
1639/**
Ying Xueaba79f32013-01-20 23:30:09 +01001640 * rcvbuf_limit - get proper overload limit of socket receive queue
1641 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001642 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001643 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001644 * For connection oriented messages, irrespective of importance,
1645 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001646 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001647 * For connectionless messages, queue limits are based on message
1648 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001649 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001650 * TIPC_LOW_IMPORTANCE (2 MB)
1651 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1652 * TIPC_HIGH_IMPORTANCE (8 MB)
1653 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001654 *
1655 * Returns overload limit according to corresponding message importance
1656 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001657static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001658{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001659 struct tipc_sock *tsk = tipc_sk(sk);
1660 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001661
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001662 if (unlikely(!msg_connected(hdr)))
1663 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001664
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001665 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1666 return sk->sk_rcvbuf;
1667
1668 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001669}
1670
1671/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001672 * filter_rcv - validate incoming message
1673 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001674 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001675 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001676 * Enqueues message on receive queue if acceptable; optionally handles
1677 * disconnect indication for a connected socket.
1678 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001679 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001680 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001681 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001683static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1684 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001686 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001687 struct tipc_msg *hdr = buf_msg(skb);
1688 unsigned int limit = rcvbuf_limit(sk, skb);
1689 int err = TIPC_OK;
1690 int usr = msg_user(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691
Jon Paul Maloycda36962015-07-22 10:11:20 -04001692 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001693 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001694 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001695 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001696
Jon Paul Maloycda36962015-07-22 10:11:20 -04001697 if (unlikely(usr == SOCK_WAKEUP)) {
1698 kfree_skb(skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001699 tsk->link_cong = 0;
1700 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001701 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001702 }
1703
Jon Paul Maloycda36962015-07-22 10:11:20 -04001704 /* Drop if illegal message type */
1705 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1706 kfree_skb(skb);
1707 return false;
1708 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001709
Jon Paul Maloycda36962015-07-22 10:11:20 -04001710 /* Reject if wrong message type for current socket state */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001711 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001712 if (msg_connected(hdr)) {
1713 err = TIPC_ERR_NO_PORT;
1714 goto reject;
1715 }
1716 } else if (unlikely(!filter_connect(tsk, skb))) {
1717 err = TIPC_ERR_NO_PORT;
1718 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 }
1720
1721 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001722 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1723 err = TIPC_ERR_OVERLOAD;
1724 goto reject;
1725 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726
Ying Xueaba79f32013-01-20 23:30:09 +01001727 /* Enqueue message */
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001728 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001729 __skb_queue_tail(&sk->sk_receive_queue, skb);
1730 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731
David S. Miller676d2362014-04-11 16:15:36 -04001732 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001733 return true;
1734
1735reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001736 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1737 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001738 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739}
1740
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001741/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001742 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001743 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001744 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001745 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001746 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001747 *
1748 * Returns 0
1749 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001750static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001752 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001753 struct sk_buff_head xmitq;
1754 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001755
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001756 __skb_queue_head_init(&xmitq);
1757
1758 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001759 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001760 return 0;
1761 }
1762
1763 if (skb_queue_empty(&xmitq))
1764 return 0;
1765
1766 /* Send response/rejected message */
1767 skb = __skb_dequeue(&xmitq);
1768 dnode = msg_destnode(buf_msg(skb));
1769 selector = msg_origport(buf_msg(skb));
1770 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001771 return 0;
1772}
1773
1774/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001775 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1776 * inputq and try adding them to socket or backlog queue
1777 * @inputq: list of incoming buffers with potentially different destinations
1778 * @sk: socket where the buffers should be enqueued
1779 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001780 *
1781 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001782 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001783static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001784 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001785{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001786 unsigned long time_limit = jiffies + 2;
1787 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001788 unsigned int lim;
1789 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001790 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001791
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001792 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001793 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001794 return;
1795
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001796 skb = tipc_skb_dequeue(inputq, dport);
1797 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001798 return;
1799
1800 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001801 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001802 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001803 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001804 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001805
1806 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001807 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001808 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001809 atomic_set(dcnt, 0);
1810 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1811 if (likely(!sk_add_backlog(sk, skb, lim)))
1812 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001813
1814 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001815 onode = tipc_own_addr(sock_net(sk));
1816 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1817 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001818 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001819 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001820}
1821
1822/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001823 * tipc_sk_rcv - handle a chain of incoming buffers
1824 * @inputq: buffer list containing the buffers
1825 * Consumes all buffers in list until inputq is empty
1826 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001827 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001828void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001829{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001830 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001831 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001832 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001833 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001834 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001835 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001836
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001837 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001838 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001839 dport = tipc_skb_peek_port(inputq, dport);
1840 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001841
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001842 if (likely(tsk)) {
1843 sk = &tsk->sk;
1844 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001845 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001846 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001847 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001848 /* Send pending response/rejected messages, if any */
1849 while ((skb = __skb_dequeue(&xmitq))) {
1850 dnode = msg_destnode(buf_msg(skb));
1851 tipc_node_xmit_skb(net, skb, dnode, dport);
1852 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001853 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001854 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001855 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001856
1857 /* No destination socket => dequeue skb if still there */
1858 skb = tipc_skb_dequeue(inputq, dport);
1859 if (!skb)
1860 return;
1861
1862 /* Try secondary lookup if unresolved named message */
1863 err = TIPC_ERR_NO_PORT;
1864 if (tipc_msg_lookup_dest(net, skb, &err))
1865 goto xmit;
1866
1867 /* Prepare for message rejection */
1868 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001869 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001870xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001871 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001872 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001873 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001874}
1875
Ying Xue78eb3a52014-01-17 09:50:03 +08001876static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1877{
WANG Congd9dc8b02016-11-11 10:20:50 -08001878 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08001879 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08001880 int done;
1881
1882 do {
1883 int err = sock_error(sk);
1884 if (err)
1885 return err;
1886 if (!*timeo_p)
1887 return -ETIMEDOUT;
1888 if (signal_pending(current))
1889 return sock_intr_errno(*timeo_p);
1890
WANG Congd9dc8b02016-11-11 10:20:50 -08001891 add_wait_queue(sk_sleep(sk), &wait);
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001892 done = sk_wait_event(sk, timeo_p,
WANG Congd9dc8b02016-11-11 10:20:50 -08001893 sk->sk_state != TIPC_CONNECTING, &wait);
1894 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08001895 } while (!done);
1896 return 0;
1897}
1898
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899/**
Ying Xue247f0f32014-02-18 16:06:46 +08001900 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 * @sock: socket structure
1902 * @dest: socket address for destination port
1903 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001904 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905 *
1906 * Returns 0 on success, errno otherwise
1907 */
Ying Xue247f0f32014-02-18 16:06:46 +08001908static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1909 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001911 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001912 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001913 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1914 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001915 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001916 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001917 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918
Allan Stephens0c3141e2008-04-15 00:22:02 -07001919 lock_sock(sk);
1920
Erik Hugnef2f80362015-03-19 09:02:19 +01001921 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001922 if (tipc_sk_type_connectionless(sk)) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001923 if (dst->family == AF_UNSPEC) {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001924 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
Sasha Levin610600c2015-03-23 15:30:00 -04001925 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1926 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001927 } else {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001928 memcpy(&tsk->peer, dest, destlen);
Erik Hugnef2f80362015-03-19 09:02:19 +01001929 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001930 goto exit;
1931 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001932
Allan Stephensb89741a2008-04-15 00:20:37 -07001933 /*
1934 * Reject connection attempt using multicast address
1935 *
1936 * Note: send_msg() validates the rest of the address fields,
1937 * so there's no need to do it here
1938 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001939 if (dst->addrtype == TIPC_ADDR_MCAST) {
1940 res = -EINVAL;
1941 goto exit;
1942 }
1943
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001944 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001945
1946 switch (sk->sk_state) {
1947 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05001948 /* Send a 'SYN-' to destination */
1949 m.msg_name = dest;
1950 m.msg_namelen = destlen;
1951
1952 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1953 * indicate send_msg() is never blocked.
1954 */
1955 if (!timeout)
1956 m.msg_flags = MSG_DONTWAIT;
1957
Ying Xue39a02952015-03-02 15:37:47 +08001958 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001959 if ((res < 0) && (res != -EWOULDBLOCK))
1960 goto exit;
1961
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001962 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05001963 * difference is that return value in non-blocking
1964 * case is EINPROGRESS, rather than EALREADY.
1965 */
1966 res = -EINPROGRESS;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001967 /* fall thru' */
1968 case TIPC_CONNECTING:
1969 if (!timeout) {
1970 if (previous == TIPC_CONNECTING)
1971 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08001972 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001973 }
Ying Xue78eb3a52014-01-17 09:50:03 +08001974 timeout = msecs_to_jiffies(timeout);
1975 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1976 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001977 break;
1978 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001979 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001980 break;
1981 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001982 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001983 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001984
Allan Stephens0c3141e2008-04-15 00:22:02 -07001985exit:
1986 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001987 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988}
1989
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001990/**
Ying Xue247f0f32014-02-18 16:06:46 +08001991 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992 * @sock: socket structure
1993 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001994 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001995 * Returns 0 on success, errno otherwise
1996 */
Ying Xue247f0f32014-02-18 16:06:46 +08001997static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001998{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001999 struct sock *sk = sock->sk;
2000 int res;
2001
2002 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002003 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002004 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002005
Allan Stephens0c3141e2008-04-15 00:22:02 -07002006 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002007}
2008
Ying Xue6398e232014-01-17 09:50:04 +08002009static int tipc_wait_for_accept(struct socket *sock, long timeo)
2010{
2011 struct sock *sk = sock->sk;
2012 DEFINE_WAIT(wait);
2013 int err;
2014
2015 /* True wake-one mechanism for incoming connections: only
2016 * one process gets woken up, not the 'whole herd'.
2017 * Since we do not 'race & poll' for established sockets
2018 * anymore, the common case will execute the loop only once.
2019 */
2020 for (;;) {
2021 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2022 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002023 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002024 release_sock(sk);
2025 timeo = schedule_timeout(timeo);
2026 lock_sock(sk);
2027 }
2028 err = 0;
2029 if (!skb_queue_empty(&sk->sk_receive_queue))
2030 break;
Ying Xue6398e232014-01-17 09:50:04 +08002031 err = -EAGAIN;
2032 if (!timeo)
2033 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002034 err = sock_intr_errno(timeo);
2035 if (signal_pending(current))
2036 break;
Ying Xue6398e232014-01-17 09:50:04 +08002037 }
2038 finish_wait(sk_sleep(sk), &wait);
2039 return err;
2040}
2041
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002042/**
Ying Xue247f0f32014-02-18 16:06:46 +08002043 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044 * @sock: listening socket
2045 * @newsock: new socket that is to be connected
2046 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002047 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002048 * Returns 0 on success, errno otherwise
2049 */
Ying Xue247f0f32014-02-18 16:06:46 +08002050static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002052 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002054 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002055 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002056 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002057 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058
Allan Stephens0c3141e2008-04-15 00:22:02 -07002059 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002060
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002061 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002062 res = -EINVAL;
2063 goto exit;
2064 }
Ying Xue6398e232014-01-17 09:50:04 +08002065 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2066 res = tipc_wait_for_accept(sock, timeo);
2067 if (res)
2068 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002069
2070 buf = skb_peek(&sk->sk_receive_queue);
2071
Parthasarathy Bhuvaragancb5da842016-11-01 14:02:36 +01002072 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002073 if (res)
2074 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002075 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002076
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002077 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002078 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002079 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002080
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002081 /* we lock on new_sk; but lockdep sees the lock on sk */
2082 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002083
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002084 /*
2085 * Reject any stray messages received by new socket
2086 * before the socket lock was taken (very, very unlikely)
2087 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002088 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002090 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002091 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002093 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002094 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002095 new_tsock->conn_type = msg_nametype(msg);
2096 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002097 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002098
2099 /*
2100 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2101 * Respond to 'SYN+' by queuing it on new socket.
2102 */
2103 if (!msg_data_sz(msg)) {
2104 struct msghdr m = {NULL,};
2105
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002106 tsk_advance_rx_queue(sk);
Ying Xue39a02952015-03-02 15:37:47 +08002107 __tipc_send_stream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002108 } else {
2109 __skb_dequeue(&sk->sk_receive_queue);
2110 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002111 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002112 }
2113 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002115 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002116 return res;
2117}
2118
2119/**
Ying Xue247f0f32014-02-18 16:06:46 +08002120 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002122 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123 *
2124 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002125 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126 * Returns 0 on success, errno otherwise
2127 */
Ying Xue247f0f32014-02-18 16:06:46 +08002128static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002129{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002130 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 int res;
2132
Allan Stephense247a8f2008-03-06 15:05:38 -08002133 if (how != SHUT_RDWR)
2134 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135
Allan Stephens0c3141e2008-04-15 00:22:02 -07002136 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002137
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002138 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2139 sk->sk_shutdown = SEND_SHUTDOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002141 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002142 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002143 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002144
2145 /* Wake up anyone sleeping in poll */
2146 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002148 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149 res = -ENOTCONN;
2150 }
2151
Allan Stephens0c3141e2008-04-15 00:22:02 -07002152 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002153 return res;
2154}
2155
Ying Xuef2f2a962015-01-09 15:27:02 +08002156static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002157{
Ying Xuef2f2a962015-01-09 15:27:02 +08002158 struct tipc_sock *tsk = (struct tipc_sock *)data;
2159 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002160 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002161 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002162 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002163
Jon Paul Maloy57289012014-08-22 18:09:09 -04002164 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002165 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002166 bh_unlock_sock(sk);
2167 goto exit;
2168 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002169 peer_port = tsk_peer_port(tsk);
2170 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002171
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002172 if (tsk->probe_unacked) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002173 if (!sock_owned_by_user(sk)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002174 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugneb3be5e32015-06-09 17:27:12 +02002175 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2176 tsk_peer_port(tsk));
2177 sk->sk_state_change(sk);
2178 } else {
2179 /* Try again later */
2180 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2181 }
2182
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002183 bh_unlock_sock(sk);
2184 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002185 }
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002186
2187 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2188 INT_H_SIZE, 0, peer_node, own_node,
2189 peer_port, tsk->portid, TIPC_OK);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002190 tsk->probe_unacked = true;
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002191 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002192 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002193 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002194 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002195exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002196 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002197}
2198
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002199static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002200 struct tipc_name_seq const *seq)
2201{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002202 struct sock *sk = &tsk->sk;
2203 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002204 struct publication *publ;
2205 u32 key;
2206
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002207 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002208 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002209 key = tsk->portid + tsk->pub_count + 1;
2210 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002211 return -EADDRINUSE;
2212
Ying Xuef2f98002015-01-09 15:27:05 +08002213 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002214 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002215 if (unlikely(!publ))
2216 return -EINVAL;
2217
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002218 list_add(&publ->pport_list, &tsk->publications);
2219 tsk->pub_count++;
2220 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002221 return 0;
2222}
2223
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002224static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002225 struct tipc_name_seq const *seq)
2226{
Ying Xuef2f98002015-01-09 15:27:05 +08002227 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002228 struct publication *publ;
2229 struct publication *safe;
2230 int rc = -EINVAL;
2231
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002232 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002233 if (seq) {
2234 if (publ->scope != scope)
2235 continue;
2236 if (publ->type != seq->type)
2237 continue;
2238 if (publ->lower != seq->lower)
2239 continue;
2240 if (publ->upper != seq->upper)
2241 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002242 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002243 publ->ref, publ->key);
2244 rc = 0;
2245 break;
2246 }
Ying Xuef2f98002015-01-09 15:27:05 +08002247 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002248 publ->ref, publ->key);
2249 rc = 0;
2250 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002251 if (list_empty(&tsk->publications))
2252 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002253 return rc;
2254}
2255
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002256/* tipc_sk_reinit: set non-zero address in all existing sockets
2257 * when we go from standalone to network mode.
2258 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002259void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002260{
Ying Xuee05b31f2015-01-09 15:27:08 +08002261 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002262 const struct bucket_table *tbl;
2263 struct rhash_head *pos;
2264 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002265 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002266 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002267
Ying Xue07f6c4b2015-01-07 13:41:58 +08002268 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002269 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002270 for (i = 0; i < tbl->size; i++) {
2271 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2272 spin_lock_bh(&tsk->sk.sk_lock.slock);
2273 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002274 msg_set_prevnode(msg, tn->own_addr);
2275 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002276 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2277 }
2278 }
2279 rcu_read_unlock();
2280}
2281
Ying Xuee05b31f2015-01-09 15:27:08 +08002282static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002283{
Ying Xuee05b31f2015-01-09 15:27:08 +08002284 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002285 struct tipc_sock *tsk;
2286
2287 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002288 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002289 if (tsk)
2290 sock_hold(&tsk->sk);
2291 rcu_read_unlock();
2292
2293 return tsk;
2294}
2295
2296static int tipc_sk_insert(struct tipc_sock *tsk)
2297{
Ying Xuee05b31f2015-01-09 15:27:08 +08002298 struct sock *sk = &tsk->sk;
2299 struct net *net = sock_net(sk);
2300 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002301 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2302 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2303
2304 while (remaining--) {
2305 portid++;
2306 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2307 portid = TIPC_MIN_PORT;
2308 tsk->portid = portid;
2309 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002310 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2311 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002312 return 0;
2313 sock_put(&tsk->sk);
2314 }
2315
2316 return -1;
2317}
2318
2319static void tipc_sk_remove(struct tipc_sock *tsk)
2320{
2321 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002322 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002323
Herbert Xu6cca72892015-03-20 21:57:05 +11002324 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002325 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2326 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002327 }
2328}
2329
Herbert Xu6cca72892015-03-20 21:57:05 +11002330static const struct rhashtable_params tsk_rht_params = {
2331 .nelem_hint = 192,
2332 .head_offset = offsetof(struct tipc_sock, node),
2333 .key_offset = offsetof(struct tipc_sock, portid),
2334 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002335 .max_size = 1048576,
2336 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002337 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002338};
2339
Ying Xuee05b31f2015-01-09 15:27:08 +08002340int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002341{
Ying Xuee05b31f2015-01-09 15:27:08 +08002342 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002343
Herbert Xu6cca72892015-03-20 21:57:05 +11002344 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002345}
2346
Ying Xuee05b31f2015-01-09 15:27:08 +08002347void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002348{
Ying Xuee05b31f2015-01-09 15:27:08 +08002349 struct tipc_net *tn = net_generic(net, tipc_net_id);
2350
Ying Xue07f6c4b2015-01-07 13:41:58 +08002351 /* Wait for socket readers to complete */
2352 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002353
Ying Xuee05b31f2015-01-09 15:27:08 +08002354 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002355}
2356
2357/**
Ying Xue247f0f32014-02-18 16:06:46 +08002358 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002359 * @sock: socket structure
2360 * @lvl: option level
2361 * @opt: option identifier
2362 * @ov: pointer to new option value
2363 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002364 *
2365 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002366 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002367 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002368 * Returns 0 on success, errno otherwise
2369 */
Ying Xue247f0f32014-02-18 16:06:46 +08002370static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2371 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002372{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002373 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002374 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002375 u32 value;
2376 int res;
2377
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002378 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2379 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002380 if (lvl != SOL_TIPC)
2381 return -ENOPROTOOPT;
2382 if (ol < sizeof(value))
2383 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002384 res = get_user(value, (u32 __user *)ov);
2385 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002386 return res;
2387
Allan Stephens0c3141e2008-04-15 00:22:02 -07002388 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002389
Per Lidenb97bf3f2006-01-02 19:04:38 +01002390 switch (opt) {
2391 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002392 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002393 break;
2394 case TIPC_SRC_DROPPABLE:
2395 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002396 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002397 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002398 res = -ENOPROTOOPT;
2399 break;
2400 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002401 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002402 break;
2403 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002404 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002405 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002406 break;
2407 default:
2408 res = -EINVAL;
2409 }
2410
Allan Stephens0c3141e2008-04-15 00:22:02 -07002411 release_sock(sk);
2412
Per Lidenb97bf3f2006-01-02 19:04:38 +01002413 return res;
2414}
2415
2416/**
Ying Xue247f0f32014-02-18 16:06:46 +08002417 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002418 * @sock: socket structure
2419 * @lvl: option level
2420 * @opt: option identifier
2421 * @ov: receptacle for option value
2422 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002423 *
2424 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002425 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002426 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002427 * Returns 0 on success, errno otherwise
2428 */
Ying Xue247f0f32014-02-18 16:06:46 +08002429static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2430 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002431{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002432 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002433 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002434 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002435 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002436 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002438 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2439 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002440 if (lvl != SOL_TIPC)
2441 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002442 res = get_user(len, ol);
2443 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002444 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002445
Allan Stephens0c3141e2008-04-15 00:22:02 -07002446 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002447
2448 switch (opt) {
2449 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002450 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002451 break;
2452 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002453 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002454 break;
2455 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002456 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457 break;
2458 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002459 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002460 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002461 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002462 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002463 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002464 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002465 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002466 value = skb_queue_len(&sk->sk_receive_queue);
2467 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002468 default:
2469 res = -EINVAL;
2470 }
2471
Allan Stephens0c3141e2008-04-15 00:22:02 -07002472 release_sock(sk);
2473
Paul Gortmaker25860c32010-12-31 18:59:31 +00002474 if (res)
2475 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002476
Paul Gortmaker25860c32010-12-31 18:59:31 +00002477 if (len < sizeof(value))
2478 return -EINVAL;
2479
2480 if (copy_to_user(ov, &value, sizeof(value)))
2481 return -EFAULT;
2482
2483 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002484}
2485
Ying Xuef2f98002015-01-09 15:27:05 +08002486static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002487{
Ying Xuef2f98002015-01-09 15:27:05 +08002488 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002489 struct tipc_sioc_ln_req lnr;
2490 void __user *argp = (void __user *)arg;
2491
2492 switch (cmd) {
2493 case SIOCGETLINKNAME:
2494 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2495 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002496 if (!tipc_node_get_linkname(sock_net(sk),
2497 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002498 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2499 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2500 return -EFAULT;
2501 return 0;
2502 }
2503 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002504 default:
2505 return -ENOIOCTLCMD;
2506 }
2507}
2508
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002509/* Protocol switches for the various types of TIPC sockets */
2510
Florian Westphalbca65ea2008-02-07 18:18:01 -08002511static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002512 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002513 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002514 .release = tipc_release,
2515 .bind = tipc_bind,
2516 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002517 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002518 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002519 .getname = tipc_getname,
2520 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002521 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002522 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002523 .shutdown = tipc_shutdown,
2524 .setsockopt = tipc_setsockopt,
2525 .getsockopt = tipc_getsockopt,
2526 .sendmsg = tipc_sendmsg,
2527 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002528 .mmap = sock_no_mmap,
2529 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002530};
2531
Florian Westphalbca65ea2008-02-07 18:18:01 -08002532static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002533 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002534 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002535 .release = tipc_release,
2536 .bind = tipc_bind,
2537 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002538 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002539 .accept = tipc_accept,
2540 .getname = tipc_getname,
2541 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002542 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002543 .listen = tipc_listen,
2544 .shutdown = tipc_shutdown,
2545 .setsockopt = tipc_setsockopt,
2546 .getsockopt = tipc_getsockopt,
2547 .sendmsg = tipc_send_packet,
2548 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002549 .mmap = sock_no_mmap,
2550 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002551};
2552
Florian Westphalbca65ea2008-02-07 18:18:01 -08002553static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002554 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002555 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002556 .release = tipc_release,
2557 .bind = tipc_bind,
2558 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002559 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002560 .accept = tipc_accept,
2561 .getname = tipc_getname,
2562 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002563 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002564 .listen = tipc_listen,
2565 .shutdown = tipc_shutdown,
2566 .setsockopt = tipc_setsockopt,
2567 .getsockopt = tipc_getsockopt,
2568 .sendmsg = tipc_send_stream,
2569 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002570 .mmap = sock_no_mmap,
2571 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002572};
2573
Florian Westphalbca65ea2008-02-07 18:18:01 -08002574static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002575 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002576 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002577 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002578};
2579
2580static struct proto tipc_proto = {
2581 .name = "TIPC",
2582 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002583 .obj_size = sizeof(struct tipc_sock),
2584 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002585};
2586
2587/**
Per Liden4323add2006-01-18 00:38:21 +01002588 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002589 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002590 * Returns 0 on success, errno otherwise
2591 */
Per Liden4323add2006-01-18 00:38:21 +01002592int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002593{
2594 int res;
2595
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002596 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002597 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002598 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002599 goto out;
2600 }
2601
2602 res = sock_register(&tipc_family_ops);
2603 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002604 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002605 proto_unregister(&tipc_proto);
2606 goto out;
2607 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002608 out:
2609 return res;
2610}
2611
2612/**
Per Liden4323add2006-01-18 00:38:21 +01002613 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002614 */
Per Liden4323add2006-01-18 00:38:21 +01002615void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002616{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002617 sock_unregister(tipc_family_ops.family);
2618 proto_unregister(&tipc_proto);
2619}
Richard Alpe34b78a122014-11-20 10:29:10 +01002620
2621/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002622static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002623{
2624 u32 peer_node;
2625 u32 peer_port;
2626 struct nlattr *nest;
2627
2628 peer_node = tsk_peer_node(tsk);
2629 peer_port = tsk_peer_port(tsk);
2630
2631 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2632
2633 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2634 goto msg_full;
2635 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2636 goto msg_full;
2637
2638 if (tsk->conn_type != 0) {
2639 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2640 goto msg_full;
2641 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2642 goto msg_full;
2643 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2644 goto msg_full;
2645 }
2646 nla_nest_end(skb, nest);
2647
2648 return 0;
2649
2650msg_full:
2651 nla_nest_cancel(skb, nest);
2652
2653 return -EMSGSIZE;
2654}
2655
2656/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002657static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2658 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002659{
2660 int err;
2661 void *hdr;
2662 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002663 struct net *net = sock_net(skb->sk);
2664 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002665 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002666
2667 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002668 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002669 if (!hdr)
2670 goto msg_cancel;
2671
2672 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2673 if (!attrs)
2674 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002675 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002676 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002677 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002678 goto attr_msg_cancel;
2679
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002680 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002681 err = __tipc_nl_add_sk_con(skb, tsk);
2682 if (err)
2683 goto attr_msg_cancel;
2684 } else if (!list_empty(&tsk->publications)) {
2685 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2686 goto attr_msg_cancel;
2687 }
2688 nla_nest_end(skb, attrs);
2689 genlmsg_end(skb, hdr);
2690
2691 return 0;
2692
2693attr_msg_cancel:
2694 nla_nest_cancel(skb, attrs);
2695genlmsg_cancel:
2696 genlmsg_cancel(skb, hdr);
2697msg_cancel:
2698 return -EMSGSIZE;
2699}
2700
2701int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2702{
2703 int err;
2704 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002705 const struct bucket_table *tbl;
2706 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002707 struct net *net = sock_net(skb->sk);
2708 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002709 u32 tbl_id = cb->args[0];
2710 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002711
Ying Xue07f6c4b2015-01-07 13:41:58 +08002712 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002713 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002714 for (; tbl_id < tbl->size; tbl_id++) {
2715 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002716 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002717 if (prev_portid && prev_portid != tsk->portid) {
2718 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2719 continue;
2720 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002721
Richard Alped6e164e2015-01-16 12:30:40 +01002722 err = __tipc_nl_add_sk(skb, cb, tsk);
2723 if (err) {
2724 prev_portid = tsk->portid;
2725 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2726 goto out;
2727 }
2728 prev_portid = 0;
2729 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002730 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002731 }
Richard Alped6e164e2015-01-16 12:30:40 +01002732out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002733 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002734 cb->args[0] = tbl_id;
2735 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002736
2737 return skb->len;
2738}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002739
2740/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002741static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2742 struct netlink_callback *cb,
2743 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002744{
2745 void *hdr;
2746 struct nlattr *attrs;
2747
2748 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002749 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002750 if (!hdr)
2751 goto msg_cancel;
2752
2753 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2754 if (!attrs)
2755 goto genlmsg_cancel;
2756
2757 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2758 goto attr_msg_cancel;
2759 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2760 goto attr_msg_cancel;
2761 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2762 goto attr_msg_cancel;
2763 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2764 goto attr_msg_cancel;
2765
2766 nla_nest_end(skb, attrs);
2767 genlmsg_end(skb, hdr);
2768
2769 return 0;
2770
2771attr_msg_cancel:
2772 nla_nest_cancel(skb, attrs);
2773genlmsg_cancel:
2774 genlmsg_cancel(skb, hdr);
2775msg_cancel:
2776 return -EMSGSIZE;
2777}
2778
2779/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002780static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2781 struct netlink_callback *cb,
2782 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002783{
2784 int err;
2785 struct publication *p;
2786
2787 if (*last_publ) {
2788 list_for_each_entry(p, &tsk->publications, pport_list) {
2789 if (p->key == *last_publ)
2790 break;
2791 }
2792 if (p->key != *last_publ) {
2793 /* We never set seq or call nl_dump_check_consistent()
2794 * this means that setting prev_seq here will cause the
2795 * consistence check to fail in the netlink callback
2796 * handler. Resulting in the last NLMSG_DONE message
2797 * having the NLM_F_DUMP_INTR flag set.
2798 */
2799 cb->prev_seq = 1;
2800 *last_publ = 0;
2801 return -EPIPE;
2802 }
2803 } else {
2804 p = list_first_entry(&tsk->publications, struct publication,
2805 pport_list);
2806 }
2807
2808 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2809 err = __tipc_nl_add_sk_publ(skb, cb, p);
2810 if (err) {
2811 *last_publ = p->key;
2812 return err;
2813 }
2814 }
2815 *last_publ = 0;
2816
2817 return 0;
2818}
2819
2820int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2821{
2822 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002823 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002824 u32 last_publ = cb->args[1];
2825 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002826 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002827 struct tipc_sock *tsk;
2828
Ying Xue07f6c4b2015-01-07 13:41:58 +08002829 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002830 struct nlattr **attrs;
2831 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2832
2833 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2834 if (err)
2835 return err;
2836
Richard Alpe45e093a2016-05-16 11:14:54 +02002837 if (!attrs[TIPC_NLA_SOCK])
2838 return -EINVAL;
2839
Richard Alpe1a1a1432014-11-20 10:29:11 +01002840 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2841 attrs[TIPC_NLA_SOCK],
2842 tipc_nl_sock_policy);
2843 if (err)
2844 return err;
2845
2846 if (!sock[TIPC_NLA_SOCK_REF])
2847 return -EINVAL;
2848
Ying Xue07f6c4b2015-01-07 13:41:58 +08002849 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002850 }
2851
2852 if (done)
2853 return 0;
2854
Ying Xuee05b31f2015-01-09 15:27:08 +08002855 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002856 if (!tsk)
2857 return -EINVAL;
2858
2859 lock_sock(&tsk->sk);
2860 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2861 if (!err)
2862 done = 1;
2863 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002864 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002865
Ying Xue07f6c4b2015-01-07 13:41:58 +08002866 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002867 cb->args[1] = last_publ;
2868 cb->args[2] = done;
2869
2870 return skb->len;
2871}