blob: 57df99ca6347ce2ce797f0fa8c8690d8bacae82a [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 SS_LISTENING -1 /* socket is listening */
48#define SS_READY -2 /* socket is connectionless */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080051#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080052#define TIPC_FWD_MSG 1
53#define TIPC_CONN_OK 0
54#define TIPC_CONN_PROBING 1
55#define TIPC_MAX_PORT 0xffffffff
56#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040057
58/**
59 * struct tipc_sock - TIPC socket structure
60 * @sk: socket - interacts with 'port' and with user via the socket API
61 * @connected: non-zero if port is currently connected to a peer port
62 * @conn_type: TIPC type used when connection was established
63 * @conn_instance: TIPC instance used when connection was established
64 * @published: non-zero if port has one or more associated names
65 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080066 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040067 * @phdr: preformatted message header used when sending messages
68 * @port_list: adjacent ports in TIPC's global list of ports
69 * @publications: list of publications for port
70 * @pub_count: total # of publications port has made during its lifetime
71 * @probing_state:
Ying Xue2f55c432015-01-09 15:27:00 +080072 * @probing_intv:
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
Erik Hugnef2f80362015-03-19 09:02:19 +010078 * @remote: '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;
84 int connected;
85 u32 conn_type;
86 u32 conn_instance;
87 int published;
88 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080089 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040090 struct tipc_msg phdr;
91 struct list_head sock_list;
92 struct list_head publications;
93 u32 pub_count;
94 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080095 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 uint conn_timeout;
97 atomic_t dupl_rcvcnt;
98 bool link_cong;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -040099 u16 snt_unacked;
100 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400101 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400102 u16 rcv_unacked;
103 u16 rcv_win;
Erik Hugnef2f80362015-03-19 09:02:19 +0100104 struct sockaddr_tipc remote;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800105 struct rhash_head node;
106 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400107};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400109static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400110static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800111static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800112static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800113static int tipc_release(struct socket *sock);
114static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400115static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Ying Xuef2f2a962015-01-09 15:27:02 +0800116static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400117static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400118 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400119static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400120 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800121static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800122static int tipc_sk_insert(struct tipc_sock *tsk);
123static void tipc_sk_remove(struct tipc_sock *tsk);
Ying Xue39a02952015-03-02 15:37:47 +0800124static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
125 size_t dsz);
126static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127
Florian Westphalbca65ea2008-02-07 18:18:01 -0800128static const struct proto_ops packet_ops;
129static const struct proto_ops stream_ops;
130static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100132static const struct rhashtable_params tsk_rht_params;
133
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500134static u32 tsk_own_node(struct tipc_sock *tsk)
135{
136 return msg_prevnode(&tsk->phdr);
137}
138
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400139static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400140{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400141 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400142}
143
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400144static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400145{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400146 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400147}
148
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400149static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400150{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400151 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400152}
153
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400154static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400155{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400156 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400157}
158
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400159static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400160{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400161 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400162}
163
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400164static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400165{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400166 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400167}
168
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400169static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400170{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400171 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400172}
173
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400174static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400175{
176 if (imp > TIPC_CRITICAL_IMPORTANCE)
177 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400178 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400179 return 0;
180}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400181
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400182static struct tipc_sock *tipc_sk(const struct sock *sk)
183{
184 return container_of(sk, struct tipc_sock, sk);
185}
186
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400187static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400188{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500189 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400190}
191
192/* tsk_blocks(): translate a buffer size in bytes to number of
193 * advertisable blocks, taking into account the ratio truesize(len)/len
194 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
195 */
196static u16 tsk_adv_blocks(int len)
197{
198 return len / FLOWCTL_BLK_SZ / 4;
199}
200
201/* tsk_inc(): increment counter for sent or received data
202 * - If block based flow control is not supported by peer we
203 * fall back to message based ditto, incrementing the counter
204 */
205static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
206{
207 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
208 return ((msglen / FLOWCTL_BLK_SZ) + 1);
209 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400210}
211
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400213 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700214 *
215 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400217static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400219 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220}
221
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400222/* tipc_sk_respond() : send response message back to sender
223 */
224static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
225{
226 u32 selector;
227 u32 dnode;
228 u32 onode = tipc_own_addr(sock_net(sk));
229
230 if (!tipc_msg_reverse(onode, &skb, err))
231 return;
232
233 dnode = msg_destnode(buf_msg(skb));
234 selector = msg_origport(buf_msg(skb));
235 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
236}
237
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400239 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700240 *
241 * Caller must hold socket lock
242 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400243static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700244{
Ying Xuea6ca1092014-11-26 11:41:55 +0800245 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700246
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400247 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
248 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249}
250
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400251/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400252 *
253 * Handles cases where the node's network address has changed from
254 * the default of <0.0.0> to its configured setting.
255 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400256static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400257{
Ying Xue34747532015-01-09 15:27:10 +0800258 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400259 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400260 u32 orig_node;
261 u32 peer_node;
262
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400263 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400264 return false;
265
266 if (unlikely(msg_origport(msg) != peer_port))
267 return false;
268
269 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400270 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400271
272 if (likely(orig_node == peer_node))
273 return true;
274
Ying Xue34747532015-01-09 15:27:10 +0800275 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400276 return true;
277
Ying Xue34747532015-01-09 15:27:10 +0800278 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400279 return true;
280
281 return false;
282}
283
Allan Stephens0c3141e2008-04-15 00:22:02 -0700284/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400285 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700286 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 * @sock: pre-allocated socket structure
288 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800289 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900290 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700291 * This routine creates additional data structures used by the TIPC socket,
292 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 *
294 * Returns 0 on success, errno otherwise
295 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400296static int tipc_sk_create(struct net *net, struct socket *sock,
297 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500299 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700300 const struct proto_ops *ops;
301 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400303 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400304 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700305
306 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 if (unlikely(protocol != 0))
308 return -EPROTONOSUPPORT;
309
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 switch (sock->type) {
311 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700312 ops = &stream_ops;
313 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 break;
315 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 ops = &packet_ops;
317 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 break;
319 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 ops = &msg_ops;
322 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 break;
Allan Stephens49978652006-06-25 23:47:18 -0700324 default:
Allan Stephens49978652006-06-25 23:47:18 -0700325 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 }
327
Allan Stephens0c3141e2008-04-15 00:22:02 -0700328 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500329 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 if (sk == NULL)
331 return -ENOMEM;
332
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400333 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400334 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400335 INIT_LIST_HEAD(&tsk->publications);
336 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500337 tn = net_generic(sock_net(sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700340 sock->ops = ops;
341 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800343 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900344 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800345 return -EINVAL;
346 }
Herbert Xu44bc7ca2017-05-23 21:53:35 -0400347
348 /* Ensure tsk is visible before we read own_addr. */
349 smp_mb();
350
351 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
352 NAMED_H_SIZE, 0);
353
Ying Xue07f6c4b2015-01-07 13:41:58 +0800354 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800355 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400356 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400357 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800358 sk->sk_data_ready = tipc_data_ready;
359 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800360 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400361 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
362 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700363
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400364 /* Start out with safe limits until we receive an advertised window */
365 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
366 tsk->rcv_win = tsk->snd_win;
367
Allan Stephens0c3141e2008-04-15 00:22:02 -0700368 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400369 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700370 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400371 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700372 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 return 0;
374}
375
Ying Xue07f6c4b2015-01-07 13:41:58 +0800376static void tipc_sk_callback(struct rcu_head *head)
377{
378 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
379
380 sock_put(&tsk->sk);
381}
382
Ying Xuec5fa7b32013-06-17 10:54:39 -0400383/**
Ying Xue247f0f32014-02-18 16:06:46 +0800384 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 * @sock: socket to destroy
386 *
387 * This routine cleans up any messages that are still queued on the socket.
388 * For DGRAM and RDM socket types, all queued messages are rejected.
389 * For SEQPACKET and STREAM socket types, the first message is rejected
390 * and any others are discarded. (If the first message on a STREAM socket
391 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900392 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 * NOTE: Rejected messages are not necessarily returned to the sender! They
394 * are returned or discarded according to the "destination droppable" setting
395 * specified for the message by the sender.
396 *
397 * Returns 0 on success, errno otherwise
398 */
Ying Xue247f0f32014-02-18 16:06:46 +0800399static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 struct sock *sk = sock->sk;
Sasha Levin357c4772015-01-13 12:46:41 -0500402 struct net *net;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400403 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800404 struct sk_buff *skb;
Ying Xue1ea23a22015-05-28 13:19:22 +0800405 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406
Allan Stephens0c3141e2008-04-15 00:22:02 -0700407 /*
408 * Exit if socket isn't fully initialized (occurs when a failed accept()
409 * releases a pre-allocated child socket that was never used)
410 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700411 if (sk == NULL)
412 return 0;
413
Sasha Levin357c4772015-01-13 12:46:41 -0500414 net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400415 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700416 lock_sock(sk);
417
418 /*
419 * Reject all unreceived messages, except on an active connection
420 * (which disconnects locally & sends a 'FIN+' to peer)
421 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400422 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800424 skb = __skb_dequeue(&sk->sk_receive_queue);
425 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 break;
Ying Xuea6ca1092014-11-26 11:41:55 +0800427 if (TIPC_SKB_CB(skb)->handle != NULL)
428 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700429 else {
430 if ((sock->state == SS_CONNECTING) ||
431 (sock->state == SS_CONNECTED)) {
432 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400433 tsk->connected = 0;
Ying Xuef2f98002015-01-09 15:27:05 +0800434 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700435 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400436 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700437 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 }
439
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400440 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800441 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800442 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400443 if (tsk->connected) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500444 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +0800445 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500446 tsk_own_node(tsk), tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800447 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800448 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400449 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Ying Xuef2f98002015-01-09 15:27:05 +0800450 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400451 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452
Allan Stephens0c3141e2008-04-15 00:22:02 -0700453 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700454 sock->state = SS_DISCONNECTING;
455 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800456
457 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700458 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200460 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461}
462
463/**
Ying Xue247f0f32014-02-18 16:06:46 +0800464 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 * @sock: socket structure
466 * @uaddr: socket address describing name(s) and desired operation
467 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900468 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 * Name and name sequence binding is indicated using a positive scope value;
470 * a negative scope value unbinds the specified name. Specifying no name
471 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900472 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700474 *
475 * NOTE: This routine doesn't need to take the socket lock since it doesn't
476 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 */
Ying Xue247f0f32014-02-18 16:06:46 +0800478static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
479 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480{
Ying Xue84602762013-12-27 10:18:28 +0800481 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400483 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800484 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485
Ying Xue84602762013-12-27 10:18:28 +0800486 lock_sock(sk);
487 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400488 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800489 goto exit;
490 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900491
Ying Xue84602762013-12-27 10:18:28 +0800492 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
493 res = -EINVAL;
494 goto exit;
495 }
496 if (addr->family != AF_TIPC) {
497 res = -EAFNOSUPPORT;
498 goto exit;
499 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 if (addr->addrtype == TIPC_ADDR_NAME)
502 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800503 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
504 res = -EAFNOSUPPORT;
505 goto exit;
506 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900507
Ying Xue13a2e892013-06-17 10:54:40 -0400508 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400509 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800510 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
511 res = -EACCES;
512 goto exit;
513 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400514
Ying Xue84602762013-12-27 10:18:28 +0800515 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400516 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
517 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800518exit:
519 release_sock(sk);
520 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521}
522
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900523/**
Ying Xue247f0f32014-02-18 16:06:46 +0800524 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 * @sock: socket structure
526 * @uaddr: area for returned socket address
527 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700528 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900529 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700531 *
Allan Stephens2da59912008-07-14 22:43:32 -0700532 * NOTE: This routine doesn't need to take the socket lock since it only
533 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000534 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 */
Ying Xue247f0f32014-02-18 16:06:46 +0800536static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
537 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400540 struct tipc_sock *tsk = tipc_sk(sock->sk);
Ying Xue34747532015-01-09 15:27:10 +0800541 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000543 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700544 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700545 if ((sock->state != SS_CONNECTED) &&
546 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
547 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400548 addr->addr.id.ref = tsk_peer_port(tsk);
549 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700550 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800551 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800552 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700553 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554
555 *uaddr_len = sizeof(*addr);
556 addr->addrtype = TIPC_ADDR_ID;
557 addr->family = AF_TIPC;
558 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 addr->addr.name.domain = 0;
560
Allan Stephens0c3141e2008-04-15 00:22:02 -0700561 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562}
563
564/**
Ying Xue247f0f32014-02-18 16:06:46 +0800565 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 * @file: file structure associated with the socket
567 * @sock: socket for which to calculate the poll bits
568 * @wait: ???
569 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700570 * Returns pollmask value
571 *
572 * COMMENTARY:
573 * It appears that the usual socket locking mechanisms are not useful here
574 * since the pollmask info is potentially out-of-date the moment this routine
575 * exits. TCP and other protocols seem to rely on higher level poll routines
576 * to handle any preventable race conditions, so TIPC will do the same ...
577 *
578 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700579 *
Allan Stephensf662c072010-08-17 11:00:06 +0000580 * socket state flags set
581 * ------------ ---------
582 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200583 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000584 *
585 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
586 * no write flags
587 *
588 * connected POLLIN/POLLRDNORM if data in rx queue
589 * POLLOUT if port is not congested
590 *
591 * disconnecting POLLIN/POLLRDNORM/POLLHUP
592 * no write flags
593 *
594 * listening POLLIN if SYN in rx queue
595 * no write flags
596 *
597 * ready POLLIN/POLLRDNORM if data in rx queue
598 * [connectionless] POLLOUT (since port cannot be congested)
599 *
600 * IMPORTANT: The fact that a read or write operation is indicated does NOT
601 * imply that the operation will succeed, merely that it should be performed
602 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 */
Ying Xue247f0f32014-02-18 16:06:46 +0800604static unsigned int tipc_poll(struct file *file, struct socket *sock,
605 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606{
Allan Stephens9b674e82008-03-26 16:48:21 -0700607 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400608 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000609 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700610
Ying Xuef288bef2012-08-21 11:16:57 +0800611 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700612
Allan Stephensf662c072010-08-17 11:00:06 +0000613 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200614 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500615 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200616 mask |= POLLOUT;
617 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000618 case SS_READY:
619 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400620 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000621 mask |= POLLOUT;
622 /* fall thru' */
623 case SS_CONNECTING:
624 case SS_LISTENING:
625 if (!skb_queue_empty(&sk->sk_receive_queue))
626 mask |= (POLLIN | POLLRDNORM);
627 break;
628 case SS_DISCONNECTING:
629 mask = (POLLIN | POLLRDNORM | POLLHUP);
630 break;
631 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700632
633 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634}
635
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400636/**
637 * tipc_sendmcast - send multicast message
638 * @sock: socket structure
639 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500640 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400641 * @dsz: total length of message data
642 * @timeo: timeout to wait for wakeup
643 *
644 * Called from function tipc_sendmsg(), which has done all sanity checks
645 * Returns the number of bytes sent on success, or errno
646 */
647static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500648 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400649{
650 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500651 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800652 struct net *net = sock_net(sk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500653 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100654 struct sk_buff_head pktchain;
Al Virof25dcc72014-11-28 15:52:29 -0500655 struct iov_iter save = msg->msg_iter;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400656 uint mtu;
657 int rc;
658
659 msg_set_type(mhdr, TIPC_MCAST_MSG);
660 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
661 msg_set_destport(mhdr, 0);
662 msg_set_destnode(mhdr, 0);
663 msg_set_nametype(mhdr, seq->type);
664 msg_set_namelower(mhdr, seq->lower);
665 msg_set_nameupper(mhdr, seq->upper);
666 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
667
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100668 skb_queue_head_init(&pktchain);
669
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400670new_mtu:
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400671 mtu = tipc_bcast_get_mtu(net);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100672 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400673 if (unlikely(rc < 0))
674 return rc;
675
676 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100677 rc = tipc_bcast_xmit(net, &pktchain);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400678 if (likely(!rc))
679 return dsz;
680
681 if (rc == -ELINKCONG) {
682 tsk->link_cong = 1;
683 rc = tipc_wait_for_sndmsg(sock, &timeo);
684 if (!rc)
685 continue;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400686 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100687 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500688 if (rc == -EMSGSIZE) {
689 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400690 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500691 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400692 break;
693 } while (1);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400694 return rc;
695}
696
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500697/**
698 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
699 * @arrvq: queue with arriving messages, to be cloned after destination lookup
700 * @inputq: queue with cloned messages, delivered to socket after dest lookup
701 *
702 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400703 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500704void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
705 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400706{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500707 struct tipc_msg *msg;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500708 struct tipc_plist dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500709 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400710 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500711 struct sk_buff_head tmpq;
712 uint hsz;
713 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500714
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500715 __skb_queue_head_init(&tmpq);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500716 tipc_plist_init(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400717
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500718 skb = tipc_skb_peek(arrvq, &inputq->lock);
719 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
720 msg = buf_msg(skb);
721 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400722
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500723 if (in_own_node(net, msg_orignode(msg)))
724 scope = TIPC_NODE_SCOPE;
725
726 /* Create destination port list and message clones: */
727 tipc_nametbl_mc_translate(net,
728 msg_nametype(msg), msg_namelower(msg),
729 msg_nameupper(msg), scope, &dports);
730 portid = tipc_plist_pop(&dports);
731 for (; portid; portid = tipc_plist_pop(&dports)) {
732 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
733 if (_skb) {
734 msg_set_destport(buf_msg(_skb), portid);
735 __skb_queue_tail(&tmpq, _skb);
736 continue;
737 }
738 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400739 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500740 /* Append to inputq if not already done by other thread */
741 spin_lock_bh(&inputq->lock);
742 if (skb_peek(arrvq) == skb) {
743 skb_queue_splice_tail_init(&tmpq, inputq);
744 kfree_skb(__skb_dequeue(arrvq));
745 }
746 spin_unlock_bh(&inputq->lock);
747 __skb_queue_purge(&tmpq);
748 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400749 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500750 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400751}
752
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900753/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500754 * tipc_sk_proto_rcv - receive a connection mng protocol message
755 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400756 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500757 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400758static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
759 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500760{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400761 struct sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400762 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400763 struct tipc_msg *hdr = buf_msg(skb);
764 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400765 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400766
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500767 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400768 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500769 goto exit;
770
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400771 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500772
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400773 if (mtyp == CONN_PROBE) {
774 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400775 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
776 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400777 return;
778 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400779 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400780 tsk->snt_unacked -= msg_conn_ack(hdr);
781 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
782 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500783 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400784 sk->sk_write_space(sk);
785 } else if (mtyp != CONN_PROBE_REPLY) {
786 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500787 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500788exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400789 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500790}
791
Ying Xue3f405042014-01-17 09:50:05 +0800792static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
793{
794 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400795 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800796 DEFINE_WAIT(wait);
797 int done;
798
799 do {
800 int err = sock_error(sk);
801 if (err)
802 return err;
803 if (sock->state == SS_DISCONNECTING)
804 return -EPIPE;
805 if (!*timeo_p)
806 return -EAGAIN;
807 if (signal_pending(current))
808 return sock_intr_errno(*timeo_p);
809
810 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500811 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800812 finish_wait(sk_sleep(sk), &wait);
813 } while (!done);
814 return 0;
815}
816
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500817/**
Ying Xue247f0f32014-02-18 16:06:46 +0800818 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 * @sock: socket structure
820 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500821 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900822 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900824 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
826 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900827 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 * Returns the number of bytes sent on success, or errno otherwise
829 */
Ying Xue1b784142015-03-02 15:37:48 +0800830static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500831 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832{
Ying Xue39a02952015-03-02 15:37:47 +0800833 struct sock *sk = sock->sk;
834 int ret;
835
836 lock_sock(sk);
837 ret = __tipc_sendmsg(sock, m, dsz);
838 release_sock(sk);
839
840 return ret;
841}
842
843static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
844{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500845 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700846 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400847 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800848 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400849 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500850 u32 dnode, dport;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100851 struct sk_buff_head pktchain;
Ying Xuea6ca1092014-11-26 11:41:55 +0800852 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100853 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500854 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500855 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800856 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100857 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500859 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400860 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100861 if (unlikely(!dest)) {
862 if (tsk->connected && sock->state == SS_READY)
863 dest = &tsk->remote;
864 else
865 return -EDESTADDRREQ;
866 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
867 dest->family != AF_TIPC) {
868 return -EINVAL;
869 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500870 if (unlikely(sock->state != SS_READY)) {
Ying Xue39a02952015-03-02 15:37:47 +0800871 if (sock->state == SS_LISTENING)
872 return -EPIPE;
873 if (sock->state != SS_UNCONNECTED)
874 return -EISCONN;
875 if (tsk->published)
876 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700877 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400878 tsk->conn_type = dest->addr.name.name.type;
879 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700880 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100882 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800883 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500884
885 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a02952015-03-02 15:37:47 +0800886 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500887 } else if (dest->addrtype == TIPC_ADDR_NAME) {
888 u32 type = dest->addr.name.name.type;
889 u32 inst = dest->addr.name.name.instance;
890 u32 domain = dest->addr.name.domain;
891
892 dnode = domain;
893 msg_set_type(mhdr, TIPC_NAMED_MSG);
894 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
895 msg_set_nametype(mhdr, type);
896 msg_set_nameinst(mhdr, inst);
897 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800898 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500899 msg_set_destnode(mhdr, dnode);
900 msg_set_destport(mhdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800901 if (unlikely(!dport && !dnode))
902 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500903 } else if (dest->addrtype == TIPC_ADDR_ID) {
904 dnode = dest->addr.id.node;
905 msg_set_type(mhdr, TIPC_DIRECT_MSG);
906 msg_set_lookup_scope(mhdr, 0);
907 msg_set_destnode(mhdr, dnode);
908 msg_set_destport(mhdr, dest->addr.id.ref);
909 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
910 }
911
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100912 skb_queue_head_init(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500913 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500914new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800915 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100916 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500917 if (rc < 0)
Ying Xue39a02952015-03-02 15:37:47 +0800918 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500919
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900920 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100921 skb = skb_peek(&pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +0800922 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100923 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400924 if (likely(!rc)) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500925 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700926 sock->state = SS_CONNECTING;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400927 return dsz;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900928 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400929 if (rc == -ELINKCONG) {
930 tsk->link_cong = 1;
931 rc = tipc_wait_for_sndmsg(sock, &timeo);
932 if (!rc)
933 continue;
934 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100935 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500936 if (rc == -EMSGSIZE) {
937 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500938 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500939 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400940 break;
941 } while (1);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500942
943 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944}
945
Ying Xue391a6dd2014-01-17 09:50:06 +0800946static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
947{
948 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400949 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800950 DEFINE_WAIT(wait);
951 int done;
952
953 do {
954 int err = sock_error(sk);
955 if (err)
956 return err;
957 if (sock->state == SS_DISCONNECTING)
958 return -EPIPE;
959 else if (sock->state != SS_CONNECTED)
960 return -ENOTCONN;
961 if (!*timeo_p)
962 return -EAGAIN;
963 if (signal_pending(current))
964 return sock_intr_errno(*timeo_p);
965
966 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
967 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500968 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400969 !tsk_conn_cong(tsk)) ||
970 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800971 finish_wait(sk_sleep(sk), &wait);
972 } while (!done);
973 return 0;
974}
975
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900976/**
Ying Xue247f0f32014-02-18 16:06:46 +0800977 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 * @sock: socket structure
979 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500980 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900981 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900983 *
984 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700985 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986 */
Ying Xue1b784142015-03-02 15:37:48 +0800987static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700989 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +0800990 int ret;
991
992 lock_sock(sk);
993 ret = __tipc_send_stream(sock, m, dsz);
994 release_sock(sk);
995
996 return ret;
997}
998
999static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1000{
1001 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001002 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001003 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001004 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001005 struct sk_buff_head pktchain;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001006 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001007 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001008 int rc = -EINVAL;
1009 long timeo;
1010 u32 dnode;
1011 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001012 struct iov_iter save;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001013 int hlen = MIN_H_SIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001014
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001015 /* Handle implied connection establishment */
1016 if (unlikely(dest)) {
Ying Xue39a02952015-03-02 15:37:47 +08001017 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001018 hlen = msg_hdr_sz(mhdr);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001019 if (dsz && (dsz == rc))
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001020 tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001021 return rc;
1022 }
1023 if (dsz > (uint)INT_MAX)
1024 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001025
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001027 if (sock->state == SS_DISCONNECTING)
Ying Xue39a02952015-03-02 15:37:47 +08001028 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001029 else
Ying Xue39a02952015-03-02 15:37:47 +08001030 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001033 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001034 dnode = tsk_peer_node(tsk);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001035 skb_queue_head_init(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001036
1037next:
Al Virof25dcc72014-11-28 15:52:29 -05001038 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001039 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001040 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001041 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001042 if (unlikely(rc < 0))
Ying Xue39a02952015-03-02 15:37:47 +08001043 return rc;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001044
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001045 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001046 if (likely(!tsk_conn_cong(tsk))) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001047 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001048 if (likely(!rc)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001049 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001050 sent += send;
1051 if (sent == dsz)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001052 return dsz;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001053 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001054 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001055 if (rc == -EMSGSIZE) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001056 __skb_queue_purge(&pktchain);
Ying Xuef2f98002015-01-09 15:27:05 +08001057 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1058 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001059 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001060 goto next;
1061 }
1062 if (rc != -ELINKCONG)
1063 break;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001064
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001065 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001067 rc = tipc_wait_for_sndpkt(sock, &timeo);
1068 } while (!rc);
Ying Xue39a02952015-03-02 15:37:47 +08001069
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001070 __skb_queue_purge(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001071 return sent ? sent : rc;
1072}
1073
1074/**
1075 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001076 * @sock: socket structure
1077 * @m: message to send
1078 * @dsz: length of data to be transmitted
1079 *
1080 * Used for SOCK_SEQPACKET messages.
1081 *
1082 * Returns the number of bytes sent on success, or errno otherwise
1083 */
Ying Xue1b784142015-03-02 15:37:48 +08001084static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001085{
1086 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1087 return -EMSGSIZE;
1088
Ying Xue1b784142015-03-02 15:37:48 +08001089 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090}
1091
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001092/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001094static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001095 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096{
Ying Xue3721e9c2015-01-13 17:07:48 +08001097 struct sock *sk = &tsk->sk;
1098 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001099 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001101 msg_set_destnode(msg, peer_node);
1102 msg_set_destport(msg, peer_port);
1103 msg_set_type(msg, TIPC_CONN_MSG);
1104 msg_set_lookup_scope(msg, 0);
1105 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001106
Ying Xue2f55c432015-01-09 15:27:00 +08001107 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001108 tsk->probing_state = TIPC_CONN_OK;
1109 tsk->connected = 1;
Ying Xue3721e9c2015-01-13 17:07:48 +08001110 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001111 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1112 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001113 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001114 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1115 return;
1116
1117 /* Fall back to message based flow control */
1118 tsk->rcv_win = FLOWCTL_MSG_WIN;
1119 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120}
1121
1122/**
1123 * set_orig_addr - capture sender's address for received message
1124 * @m: descriptor for message info
1125 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001126 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 * Note: Address is not captured if not requested by receiver.
1128 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001129static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001131 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001133 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 addr->family = AF_TIPC;
1135 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001136 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 addr->addr.id.ref = msg_origport(msg);
1138 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001139 addr->addr.name.domain = 0; /* could leave uninitialized */
1140 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 m->msg_namelen = sizeof(struct sockaddr_tipc);
1142 }
1143}
1144
1145/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001146 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 * @m: descriptor for message info
1148 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001149 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001150 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001152 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 * Returns 0 if successful, otherwise errno
1154 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001155static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1156 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157{
1158 u32 anc_data[3];
1159 u32 err;
1160 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001161 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 int res;
1163
1164 if (likely(m->msg_controllen == 0))
1165 return 0;
1166
1167 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 err = msg ? msg_errcode(msg) : 0;
1169 if (unlikely(err)) {
1170 anc_data[0] = err;
1171 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001172 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1173 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001175 if (anc_data[1]) {
1176 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1177 msg_data(msg));
1178 if (res)
1179 return res;
1180 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 }
1182
1183 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1185 switch (dest_type) {
1186 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001187 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 anc_data[0] = msg_nametype(msg);
1189 anc_data[1] = msg_namelower(msg);
1190 anc_data[2] = msg_namelower(msg);
1191 break;
1192 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001193 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 anc_data[0] = msg_nametype(msg);
1195 anc_data[1] = msg_namelower(msg);
1196 anc_data[2] = msg_nameupper(msg);
1197 break;
1198 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001199 has_name = (tsk->conn_type != 0);
1200 anc_data[0] = tsk->conn_type;
1201 anc_data[1] = tsk->conn_instance;
1202 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 break;
1204 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001205 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 }
Allan Stephens2db99832010-12-31 18:59:33 +00001207 if (has_name) {
1208 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1209 if (res)
1210 return res;
1211 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212
1213 return 0;
1214}
1215
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001216static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001217{
Ying Xuef2f98002015-01-09 15:27:05 +08001218 struct net *net = sock_net(&tsk->sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001219 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001220 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001221 u32 peer_port = tsk_peer_port(tsk);
1222 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001223
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001224 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001225 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001226 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1227 dnode, tsk_own_node(tsk), peer_port,
1228 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001229 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001230 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001231 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001232 msg_set_conn_ack(msg, tsk->rcv_unacked);
1233 tsk->rcv_unacked = 0;
1234
1235 /* Adjust to and advertize the correct window limit */
1236 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1237 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1238 msg_set_adv_win(msg, tsk->rcv_win);
1239 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001240 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001241}
1242
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001243static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001244{
1245 struct sock *sk = sock->sk;
1246 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001247 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001248 int err;
1249
1250 for (;;) {
1251 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001252 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001253 if (sock->state == SS_DISCONNECTING) {
1254 err = -ENOTCONN;
1255 break;
1256 }
1257 release_sock(sk);
1258 timeo = schedule_timeout(timeo);
1259 lock_sock(sk);
1260 }
1261 err = 0;
1262 if (!skb_queue_empty(&sk->sk_receive_queue))
1263 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001264 err = -EAGAIN;
1265 if (!timeo)
1266 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001267 err = sock_intr_errno(timeo);
1268 if (signal_pending(current))
1269 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001270 }
1271 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001272 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001273 return err;
1274}
1275
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001276/**
Ying Xue247f0f32014-02-18 16:06:46 +08001277 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 * @m: descriptor for message info
1279 * @buf_len: total size of user buffer area
1280 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001281 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1283 * If the complete message doesn't fit in user area, truncate it.
1284 *
1285 * Returns size of returned message data, errno otherwise
1286 */
Ying Xue1b784142015-03-02 15:37:48 +08001287static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1288 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001290 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001291 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 struct sk_buff *buf;
1293 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001294 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 unsigned int sz;
1296 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001297 int res, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298
Allan Stephens0c3141e2008-04-15 00:22:02 -07001299 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 if (unlikely(!buf_len))
1301 return -EINVAL;
1302
Allan Stephens0c3141e2008-04-15 00:22:02 -07001303 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304
Allan Stephens0c3141e2008-04-15 00:22:02 -07001305 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 res = -ENOTCONN;
1307 goto exit;
1308 }
1309
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001310 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001311restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312
Allan Stephens0c3141e2008-04-15 00:22:02 -07001313 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001314 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001315 if (res)
1316 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001317
1318 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001319 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 msg = buf_msg(buf);
1321 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001322 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 err = msg_errcode(msg);
1324
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001327 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 goto restart;
1329 }
1330
1331 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 set_orig_addr(m, msg);
1333
1334 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001335 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001336 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 goto exit;
1338
1339 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 if (!err) {
1341 if (unlikely(buf_len < sz)) {
1342 sz = buf_len;
1343 m->msg_flags |= MSG_TRUNC;
1344 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001345 res = skb_copy_datagram_msg(buf, hlen, m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001346 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 res = sz;
1349 } else {
1350 if ((sock->state == SS_READY) ||
1351 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1352 res = 0;
1353 else
1354 res = -ECONNRESET;
1355 }
1356
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001357 if (unlikely(flags & MSG_PEEK))
1358 goto exit;
1359
1360 if (likely(sock->state != SS_READY)) {
1361 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1362 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1363 tipc_sk_send_ack(tsk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001364 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001365 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001367 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368 return res;
1369}
1370
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001371/**
Ying Xue247f0f32014-02-18 16:06:46 +08001372 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373 * @m: descriptor for message info
1374 * @buf_len: total size of user buffer area
1375 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001376 *
1377 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 * will optionally wait for more; never truncates data.
1379 *
1380 * Returns size of returned message data, errno otherwise
1381 */
Ying Xue1b784142015-03-02 15:37:48 +08001382static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1383 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001385 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001386 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 struct sk_buff *buf;
1388 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001389 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001391 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001394 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395
1396 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 if (unlikely(!buf_len))
1398 return -EINVAL;
1399
Allan Stephens0c3141e2008-04-15 00:22:02 -07001400 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001402 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 res = -ENOTCONN;
1404 goto exit;
1405 }
1406
Florian Westphal3720d402010-08-17 11:00:04 +00001407 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001408 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001409
Allan Stephens0c3141e2008-04-15 00:22:02 -07001410restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001411 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001412 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001413 if (res)
1414 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001415
1416 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001417 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 msg = buf_msg(buf);
1419 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001420 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 err = msg_errcode(msg);
1422
1423 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001425 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 goto restart;
1427 }
1428
1429 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430 if (sz_copied == 0) {
1431 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001432 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001433 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 goto exit;
1435 }
1436
1437 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001439 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440
Allan Stephens0232fd02011-02-21 09:45:40 -05001441 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 needed = (buf_len - sz_copied);
1443 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001444
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001445 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001446 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001448
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 sz_copied += sz_to_copy;
1450
1451 if (sz_to_copy < sz) {
1452 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001453 TIPC_SKB_CB(buf)->handle =
1454 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 goto exit;
1456 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 } else {
1458 if (sz_copied != 0)
1459 goto exit; /* can't add error msg to valid data */
1460
1461 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1462 res = 0;
1463 else
1464 res = -ECONNRESET;
1465 }
1466
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001467 if (unlikely(flags & MSG_PEEK))
1468 goto exit;
1469
1470 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1471 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1472 tipc_sk_send_ack(tsk);
1473 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474
1475 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001476 if ((sz_copied < buf_len) && /* didn't get all requested data */
1477 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001478 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001479 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480 goto restart;
1481
1482exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001483 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001484 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485}
1486
1487/**
Ying Xuef288bef2012-08-21 11:16:57 +08001488 * tipc_write_space - wake up thread if port congestion is released
1489 * @sk: socket
1490 */
1491static void tipc_write_space(struct sock *sk)
1492{
1493 struct socket_wq *wq;
1494
1495 rcu_read_lock();
1496 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001497 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001498 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1499 POLLWRNORM | POLLWRBAND);
1500 rcu_read_unlock();
1501}
1502
1503/**
1504 * tipc_data_ready - wake up threads to indicate messages have been received
1505 * @sk: socket
1506 * @len: the length of messages
1507 */
David S. Miller676d2362014-04-11 16:15:36 -04001508static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001509{
1510 struct socket_wq *wq;
1511
1512 rcu_read_lock();
1513 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001514 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001515 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1516 POLLRDNORM | POLLRDBAND);
1517 rcu_read_unlock();
1518}
1519
Ying Xuef4195d12015-11-22 15:46:05 +08001520static void tipc_sock_destruct(struct sock *sk)
1521{
1522 __skb_queue_purge(&sk->sk_receive_queue);
1523}
1524
Ying Xuef288bef2012-08-21 11:16:57 +08001525/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001526 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001527 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001528 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001529 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001530 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001531 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001532static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001533{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001534 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001535 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001536 struct socket *sock = sk->sk_socket;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001537 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001538
Jon Paul Maloycda36962015-07-22 10:11:20 -04001539 if (unlikely(msg_mcast(hdr)))
1540 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001541
1542 switch ((int)sock->state) {
1543 case SS_CONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001544
Ying Xue7e6c1312012-11-29 18:39:14 -05001545 /* Accept only connection-based messages sent by peer */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001546 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1547 return false;
1548
1549 if (unlikely(msg_errcode(hdr))) {
1550 sock->state = SS_DISCONNECTING;
1551 tsk->connected = 0;
1552 /* Let timer expire on it's own */
1553 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1554 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001555 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001556 return true;
1557
Ying Xue7e6c1312012-11-29 18:39:14 -05001558 case SS_CONNECTING:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001559
Ying Xue7e6c1312012-11-29 18:39:14 -05001560 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001561 if (unlikely(!msg_connected(hdr)))
1562 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001563
Jon Paul Maloycda36962015-07-22 10:11:20 -04001564 if (unlikely(msg_errcode(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001565 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001566 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001567 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001568 }
1569
Jon Paul Maloycda36962015-07-22 10:11:20 -04001570 if (unlikely(!msg_isdata(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001571 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001572 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001573 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001574 }
1575
Jon Paul Maloycda36962015-07-22 10:11:20 -04001576 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1577 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001578 sock->state = SS_CONNECTED;
1579
Jon Paul Maloycda36962015-07-22 10:11:20 -04001580 /* If 'ACK+' message, add to socket receive queue */
1581 if (msg_data_sz(hdr))
1582 return true;
1583
1584 /* If empty 'ACK-' message, wake up sleeping connect() */
1585 if (waitqueue_active(sk_sleep(sk)))
1586 wake_up_interruptible(sk_sleep(sk));
1587
1588 /* 'ACK-' message is neither accepted nor rejected: */
1589 msg_set_dest_droppable(hdr, 1);
1590 return false;
1591
Ying Xue7e6c1312012-11-29 18:39:14 -05001592 case SS_LISTENING:
1593 case SS_UNCONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001594
Ying Xue7e6c1312012-11-29 18:39:14 -05001595 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001596 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1597 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001598 break;
1599 case SS_DISCONNECTING:
1600 break;
1601 default:
1602 pr_err("Unknown socket state %u\n", sock->state);
1603 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001604 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001605}
1606
1607/**
Ying Xueaba79f32013-01-20 23:30:09 +01001608 * rcvbuf_limit - get proper overload limit of socket receive queue
1609 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001610 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001611 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001612 * For connection oriented messages, irrespective of importance,
1613 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001614 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001615 * For connectionless messages, queue limits are based on message
1616 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001617 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001618 * TIPC_LOW_IMPORTANCE (2 MB)
1619 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1620 * TIPC_HIGH_IMPORTANCE (8 MB)
1621 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001622 *
1623 * Returns overload limit according to corresponding message importance
1624 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001625static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001626{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001627 struct tipc_sock *tsk = tipc_sk(sk);
1628 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001629
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001630 if (unlikely(!msg_connected(hdr)))
1631 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001632
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001633 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1634 return sk->sk_rcvbuf;
1635
1636 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001637}
1638
1639/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001640 * filter_rcv - validate incoming message
1641 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001642 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001643 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001644 * Enqueues message on receive queue if acceptable; optionally handles
1645 * disconnect indication for a connected socket.
1646 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001647 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001648 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001649 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001650 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001651static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1652 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001654 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001655 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001656 struct tipc_msg *hdr = buf_msg(skb);
1657 unsigned int limit = rcvbuf_limit(sk, skb);
1658 int err = TIPC_OK;
1659 int usr = msg_user(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660
Jon Paul Maloycda36962015-07-22 10:11:20 -04001661 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001662 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001663 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001664 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001665
Jon Paul Maloycda36962015-07-22 10:11:20 -04001666 if (unlikely(usr == SOCK_WAKEUP)) {
1667 kfree_skb(skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001668 tsk->link_cong = 0;
1669 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001670 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001671 }
1672
Jon Paul Maloycda36962015-07-22 10:11:20 -04001673 /* Drop if illegal message type */
1674 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1675 kfree_skb(skb);
1676 return false;
1677 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001678
Jon Paul Maloycda36962015-07-22 10:11:20 -04001679 /* Reject if wrong message type for current socket state */
1680 if (unlikely(sock->state == SS_READY)) {
1681 if (msg_connected(hdr)) {
1682 err = TIPC_ERR_NO_PORT;
1683 goto reject;
1684 }
1685 } else if (unlikely(!filter_connect(tsk, skb))) {
1686 err = TIPC_ERR_NO_PORT;
1687 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 }
1689
1690 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001691 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1692 err = TIPC_ERR_OVERLOAD;
1693 goto reject;
1694 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695
Ying Xueaba79f32013-01-20 23:30:09 +01001696 /* Enqueue message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001697 TIPC_SKB_CB(skb)->handle = NULL;
1698 __skb_queue_tail(&sk->sk_receive_queue, skb);
1699 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001700
David S. Miller676d2362014-04-11 16:15:36 -04001701 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001702 return true;
1703
1704reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001705 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1706 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001707 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708}
1709
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001710/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001711 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001712 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001713 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001714 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001715 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001716 *
1717 * Returns 0
1718 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001719static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001720{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001721 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001722 struct sk_buff_head xmitq;
1723 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001724
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001725 __skb_queue_head_init(&xmitq);
1726
1727 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001728 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001729 return 0;
1730 }
1731
1732 if (skb_queue_empty(&xmitq))
1733 return 0;
1734
1735 /* Send response/rejected message */
1736 skb = __skb_dequeue(&xmitq);
1737 dnode = msg_destnode(buf_msg(skb));
1738 selector = msg_origport(buf_msg(skb));
1739 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001740 return 0;
1741}
1742
1743/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001744 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1745 * inputq and try adding them to socket or backlog queue
1746 * @inputq: list of incoming buffers with potentially different destinations
1747 * @sk: socket where the buffers should be enqueued
1748 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001749 *
1750 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001751 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001752static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001753 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001754{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001755 unsigned long time_limit = jiffies + 2;
1756 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001757 unsigned int lim;
1758 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001759 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001760
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001761 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001762 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001763 return;
1764
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001765 skb = tipc_skb_dequeue(inputq, dport);
1766 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001767 return;
1768
1769 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001770 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001771 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001772 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001773 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001774
1775 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001776 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001777 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001778 atomic_set(dcnt, 0);
1779 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1780 if (likely(!sk_add_backlog(sk, skb, lim)))
1781 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001782
1783 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001784 onode = tipc_own_addr(sock_net(sk));
1785 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1786 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001787 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001788 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001789}
1790
1791/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001792 * tipc_sk_rcv - handle a chain of incoming buffers
1793 * @inputq: buffer list containing the buffers
1794 * Consumes all buffers in list until inputq is empty
1795 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001796 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001797void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001798{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001799 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001800 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001801 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001802 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001803 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001804 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001805
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001806 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001807 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001808 dport = tipc_skb_peek_port(inputq, dport);
1809 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001810
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001811 if (likely(tsk)) {
1812 sk = &tsk->sk;
1813 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001814 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001815 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001816 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001817 /* Send pending response/rejected messages, if any */
1818 while ((skb = __skb_dequeue(&xmitq))) {
1819 dnode = msg_destnode(buf_msg(skb));
1820 tipc_node_xmit_skb(net, skb, dnode, dport);
1821 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001822 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001823 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001824 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001825
1826 /* No destination socket => dequeue skb if still there */
1827 skb = tipc_skb_dequeue(inputq, dport);
1828 if (!skb)
1829 return;
1830
1831 /* Try secondary lookup if unresolved named message */
1832 err = TIPC_ERR_NO_PORT;
1833 if (tipc_msg_lookup_dest(net, skb, &err))
1834 goto xmit;
1835
1836 /* Prepare for message rejection */
1837 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001838 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001839xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001840 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001841 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001842 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001843}
1844
Ying Xue78eb3a52014-01-17 09:50:03 +08001845static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1846{
1847 struct sock *sk = sock->sk;
1848 DEFINE_WAIT(wait);
1849 int done;
1850
1851 do {
1852 int err = sock_error(sk);
1853 if (err)
1854 return err;
1855 if (!*timeo_p)
1856 return -ETIMEDOUT;
1857 if (signal_pending(current))
1858 return sock_intr_errno(*timeo_p);
1859
1860 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1861 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1862 finish_wait(sk_sleep(sk), &wait);
1863 } while (!done);
1864 return 0;
1865}
1866
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867/**
Ying Xue247f0f32014-02-18 16:06:46 +08001868 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869 * @sock: socket structure
1870 * @dest: socket address for destination port
1871 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001872 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873 *
1874 * Returns 0 on success, errno otherwise
1875 */
Ying Xue247f0f32014-02-18 16:06:46 +08001876static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1877 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001879 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001880 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001881 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1882 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001883 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Ying Xue78eb3a52014-01-17 09:50:03 +08001884 socket_state previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001885 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886
Allan Stephens0c3141e2008-04-15 00:22:02 -07001887 lock_sock(sk);
1888
Erik Hugnef2f80362015-03-19 09:02:19 +01001889 /* DGRAM/RDM connect(), just save the destaddr */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001890 if (sock->state == SS_READY) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001891 if (dst->family == AF_UNSPEC) {
1892 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1893 tsk->connected = 0;
Sasha Levin610600c2015-03-23 15:30:00 -04001894 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1895 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001896 } else {
1897 memcpy(&tsk->remote, dest, destlen);
1898 tsk->connected = 1;
1899 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001900 goto exit;
1901 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902
Allan Stephensb89741a2008-04-15 00:20:37 -07001903 /*
1904 * Reject connection attempt using multicast address
1905 *
1906 * Note: send_msg() validates the rest of the address fields,
1907 * so there's no need to do it here
1908 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001909 if (dst->addrtype == TIPC_ADDR_MCAST) {
1910 res = -EINVAL;
1911 goto exit;
1912 }
1913
Ying Xue78eb3a52014-01-17 09:50:03 +08001914 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001915 switch (sock->state) {
1916 case SS_UNCONNECTED:
1917 /* Send a 'SYN-' to destination */
1918 m.msg_name = dest;
1919 m.msg_namelen = destlen;
1920
1921 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1922 * indicate send_msg() is never blocked.
1923 */
1924 if (!timeout)
1925 m.msg_flags = MSG_DONTWAIT;
1926
Ying Xue39a02952015-03-02 15:37:47 +08001927 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001928 if ((res < 0) && (res != -EWOULDBLOCK))
1929 goto exit;
1930
1931 /* Just entered SS_CONNECTING state; the only
1932 * difference is that return value in non-blocking
1933 * case is EINPROGRESS, rather than EALREADY.
1934 */
1935 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001936 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001937 if (previous == SS_CONNECTING)
1938 res = -EALREADY;
1939 if (!timeout)
1940 goto exit;
1941 timeout = msecs_to_jiffies(timeout);
1942 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1943 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001944 break;
1945 case SS_CONNECTED:
1946 res = -EISCONN;
1947 break;
1948 default:
1949 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001950 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001951 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001952exit:
1953 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001954 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955}
1956
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001957/**
Ying Xue247f0f32014-02-18 16:06:46 +08001958 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959 * @sock: socket structure
1960 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001961 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001962 * Returns 0 on success, errno otherwise
1963 */
Ying Xue247f0f32014-02-18 16:06:46 +08001964static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001966 struct sock *sk = sock->sk;
1967 int res;
1968
1969 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970
Ying Xue245f3d32011-07-06 06:01:13 -04001971 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001972 res = -EINVAL;
1973 else {
1974 sock->state = SS_LISTENING;
1975 res = 0;
1976 }
1977
1978 release_sock(sk);
1979 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001980}
1981
Ying Xue6398e232014-01-17 09:50:04 +08001982static int tipc_wait_for_accept(struct socket *sock, long timeo)
1983{
1984 struct sock *sk = sock->sk;
1985 DEFINE_WAIT(wait);
1986 int err;
1987
1988 /* True wake-one mechanism for incoming connections: only
1989 * one process gets woken up, not the 'whole herd'.
1990 * Since we do not 'race & poll' for established sockets
1991 * anymore, the common case will execute the loop only once.
1992 */
1993 for (;;) {
1994 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1995 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001996 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001997 release_sock(sk);
1998 timeo = schedule_timeout(timeo);
1999 lock_sock(sk);
2000 }
2001 err = 0;
2002 if (!skb_queue_empty(&sk->sk_receive_queue))
2003 break;
2004 err = -EINVAL;
2005 if (sock->state != SS_LISTENING)
2006 break;
Ying Xue6398e232014-01-17 09:50:04 +08002007 err = -EAGAIN;
2008 if (!timeo)
2009 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002010 err = sock_intr_errno(timeo);
2011 if (signal_pending(current))
2012 break;
Ying Xue6398e232014-01-17 09:50:04 +08002013 }
2014 finish_wait(sk_sleep(sk), &wait);
2015 return err;
2016}
2017
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002018/**
Ying Xue247f0f32014-02-18 16:06:46 +08002019 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 * @sock: listening socket
2021 * @newsock: new socket that is to be connected
2022 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002023 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002024 * Returns 0 on success, errno otherwise
2025 */
Ying Xue247f0f32014-02-18 16:06:46 +08002026static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002028 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002029 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002030 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002031 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002032 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002033 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034
Allan Stephens0c3141e2008-04-15 00:22:02 -07002035 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002036
Allan Stephens0c3141e2008-04-15 00:22:02 -07002037 if (sock->state != SS_LISTENING) {
2038 res = -EINVAL;
2039 goto exit;
2040 }
Ying Xue6398e232014-01-17 09:50:04 +08002041 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2042 res = tipc_wait_for_accept(sock, timeo);
2043 if (res)
2044 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002045
2046 buf = skb_peek(&sk->sk_receive_queue);
2047
Ying Xuec5fa7b32013-06-17 10:54:39 -04002048 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002049 if (res)
2050 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002051 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002052
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002053 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002054 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002055 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002056
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002057 /* we lock on new_sk; but lockdep sees the lock on sk */
2058 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002059
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002060 /*
2061 * Reject any stray messages received by new socket
2062 * before the socket lock was taken (very, very unlikely)
2063 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002064 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002065
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002066 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002067 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002068 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002070 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002071 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002072 new_tsock->conn_type = msg_nametype(msg);
2073 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002074 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002075
2076 /*
2077 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2078 * Respond to 'SYN+' by queuing it on new socket.
2079 */
2080 if (!msg_data_sz(msg)) {
2081 struct msghdr m = {NULL,};
2082
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002083 tsk_advance_rx_queue(sk);
Ying Xue39a02952015-03-02 15:37:47 +08002084 __tipc_send_stream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002085 } else {
2086 __skb_dequeue(&sk->sk_receive_queue);
2087 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002088 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002089 }
2090 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002091exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002092 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002093 return res;
2094}
2095
2096/**
Ying Xue247f0f32014-02-18 16:06:46 +08002097 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002098 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002099 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100 *
2101 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002102 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 * Returns 0 on success, errno otherwise
2104 */
Ying Xue247f0f32014-02-18 16:06:46 +08002105static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002107 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002108 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002109 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002110 struct sk_buff *skb;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002111 u32 dnode = tsk_peer_node(tsk);
2112 u32 dport = tsk_peer_port(tsk);
2113 u32 onode = tipc_own_addr(net);
2114 u32 oport = tsk->portid;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 int res;
2116
Allan Stephense247a8f2008-03-06 15:05:38 -08002117 if (how != SHUT_RDWR)
2118 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119
Allan Stephens0c3141e2008-04-15 00:22:02 -07002120 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121
2122 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002123 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124 case SS_CONNECTED:
2125
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126restart:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002127 dnode = tsk_peer_node(tsk);
2128
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002129 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002130 skb = __skb_dequeue(&sk->sk_receive_queue);
2131 if (skb) {
2132 if (TIPC_SKB_CB(skb)->handle != NULL) {
2133 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002134 goto restart;
2135 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002136 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002137 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002138 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002139 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloycda36962015-07-22 10:11:20 -04002140 0, dnode, onode, dport, oport,
2141 TIPC_CONN_SHUTDOWN);
Vegard Nossumd2fbdf72016-07-23 08:15:04 +02002142 if (skb)
2143 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002144 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002145 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002146 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002147 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002148 /* fall through */
2149
2150 case SS_DISCONNECTING:
2151
Ying Xue75031152012-10-29 09:38:15 -04002152 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002153 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002154
2155 /* Wake up anyone sleeping in poll */
2156 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157 res = 0;
2158 break;
2159
2160 default:
2161 res = -ENOTCONN;
2162 }
2163
Allan Stephens0c3141e2008-04-15 00:22:02 -07002164 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002165 return res;
2166}
2167
Ying Xuef2f2a962015-01-09 15:27:02 +08002168static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002169{
Ying Xuef2f2a962015-01-09 15:27:02 +08002170 struct tipc_sock *tsk = (struct tipc_sock *)data;
2171 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002172 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002173 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002174 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002175
Jon Paul Maloy57289012014-08-22 18:09:09 -04002176 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002177 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002178 bh_unlock_sock(sk);
2179 goto exit;
2180 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002181 peer_port = tsk_peer_port(tsk);
2182 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002183
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002184 if (tsk->probing_state == TIPC_CONN_PROBING) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002185 if (!sock_owned_by_user(sk)) {
2186 sk->sk_socket->state = SS_DISCONNECTING;
2187 tsk->connected = 0;
2188 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2189 tsk_peer_port(tsk));
2190 sk->sk_state_change(sk);
2191 } else {
2192 /* Try again later */
2193 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2194 }
2195
Jon Paul Maloy57289012014-08-22 18:09:09 -04002196 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002197 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2198 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002199 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002200 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002201 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002202 }
2203 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002204 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002205 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002206exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002207 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002208}
2209
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002210static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002211 struct tipc_name_seq const *seq)
2212{
Ying Xuef2f98002015-01-09 15:27:05 +08002213 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002214 struct publication *publ;
2215 u32 key;
2216
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002217 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002218 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002219 key = tsk->portid + tsk->pub_count + 1;
2220 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002221 return -EADDRINUSE;
2222
Ying Xuef2f98002015-01-09 15:27:05 +08002223 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002224 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002225 if (unlikely(!publ))
2226 return -EINVAL;
2227
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002228 list_add(&publ->pport_list, &tsk->publications);
2229 tsk->pub_count++;
2230 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002231 return 0;
2232}
2233
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002234static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002235 struct tipc_name_seq const *seq)
2236{
Ying Xuef2f98002015-01-09 15:27:05 +08002237 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002238 struct publication *publ;
2239 struct publication *safe;
2240 int rc = -EINVAL;
2241
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002242 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002243 if (seq) {
2244 if (publ->scope != scope)
2245 continue;
2246 if (publ->type != seq->type)
2247 continue;
2248 if (publ->lower != seq->lower)
2249 continue;
2250 if (publ->upper != seq->upper)
2251 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002252 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002253 publ->ref, publ->key);
2254 rc = 0;
2255 break;
2256 }
Ying Xuef2f98002015-01-09 15:27:05 +08002257 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002258 publ->ref, publ->key);
2259 rc = 0;
2260 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002261 if (list_empty(&tsk->publications))
2262 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002263 return rc;
2264}
2265
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002266/* tipc_sk_reinit: set non-zero address in all existing sockets
2267 * when we go from standalone to network mode.
2268 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002269void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002270{
Ying Xuee05b31f2015-01-09 15:27:08 +08002271 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu44bc7ca2017-05-23 21:53:35 -04002272 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002273 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002274 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002275
Herbert Xu44bc7ca2017-05-23 21:53:35 -04002276 rhashtable_walk_enter(&tn->sk_rht, &iter);
2277
2278 do {
2279 tsk = ERR_PTR(rhashtable_walk_start(&iter));
Bob Peterson2185dba2017-08-23 10:43:02 -04002280 if (IS_ERR(tsk))
2281 goto walk_stop;
Herbert Xu44bc7ca2017-05-23 21:53:35 -04002282
2283 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Cong Wang8dd36f12018-12-10 11:49:55 -08002284 sock_hold(&tsk->sk);
2285 rhashtable_walk_stop(&iter);
2286 lock_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002287 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002288 msg_set_prevnode(msg, tn->own_addr);
2289 msg_set_orignode(msg, tn->own_addr);
Cong Wang8dd36f12018-12-10 11:49:55 -08002290 release_sock(&tsk->sk);
2291 rhashtable_walk_start(&iter);
2292 sock_put(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002293 }
Bob Peterson2185dba2017-08-23 10:43:02 -04002294walk_stop:
Herbert Xu44bc7ca2017-05-23 21:53:35 -04002295 rhashtable_walk_stop(&iter);
2296 } while (tsk == ERR_PTR(-EAGAIN));
Ying Xue07f6c4b2015-01-07 13:41:58 +08002297}
2298
Ying Xuee05b31f2015-01-09 15:27:08 +08002299static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002300{
Ying Xuee05b31f2015-01-09 15:27:08 +08002301 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002302 struct tipc_sock *tsk;
2303
2304 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002305 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002306 if (tsk)
2307 sock_hold(&tsk->sk);
2308 rcu_read_unlock();
2309
2310 return tsk;
2311}
2312
2313static int tipc_sk_insert(struct tipc_sock *tsk)
2314{
Ying Xuee05b31f2015-01-09 15:27:08 +08002315 struct sock *sk = &tsk->sk;
2316 struct net *net = sock_net(sk);
2317 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002318 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2319 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2320
2321 while (remaining--) {
2322 portid++;
2323 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2324 portid = TIPC_MIN_PORT;
2325 tsk->portid = portid;
2326 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002327 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2328 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002329 return 0;
2330 sock_put(&tsk->sk);
2331 }
2332
2333 return -1;
2334}
2335
2336static void tipc_sk_remove(struct tipc_sock *tsk)
2337{
2338 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002339 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002340
Herbert Xu6cca72892015-03-20 21:57:05 +11002341 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002342 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2343 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002344 }
2345}
2346
Herbert Xu6cca72892015-03-20 21:57:05 +11002347static const struct rhashtable_params tsk_rht_params = {
2348 .nelem_hint = 192,
2349 .head_offset = offsetof(struct tipc_sock, node),
2350 .key_offset = offsetof(struct tipc_sock, portid),
2351 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002352 .max_size = 1048576,
2353 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002354 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002355};
2356
Ying Xuee05b31f2015-01-09 15:27:08 +08002357int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002358{
Ying Xuee05b31f2015-01-09 15:27:08 +08002359 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002360
Herbert Xu6cca72892015-03-20 21:57:05 +11002361 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002362}
2363
Ying Xuee05b31f2015-01-09 15:27:08 +08002364void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002365{
Ying Xuee05b31f2015-01-09 15:27:08 +08002366 struct tipc_net *tn = net_generic(net, tipc_net_id);
2367
Ying Xue07f6c4b2015-01-07 13:41:58 +08002368 /* Wait for socket readers to complete */
2369 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002370
Ying Xuee05b31f2015-01-09 15:27:08 +08002371 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002372}
2373
2374/**
Ying Xue247f0f32014-02-18 16:06:46 +08002375 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002376 * @sock: socket structure
2377 * @lvl: option level
2378 * @opt: option identifier
2379 * @ov: pointer to new option value
2380 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002381 *
2382 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002383 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002384 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002385 * Returns 0 on success, errno otherwise
2386 */
Ying Xue247f0f32014-02-18 16:06:46 +08002387static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2388 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002389{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002390 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002391 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002392 u32 value;
2393 int res;
2394
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002395 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2396 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002397 if (lvl != SOL_TIPC)
2398 return -ENOPROTOOPT;
2399 if (ol < sizeof(value))
2400 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002401 res = get_user(value, (u32 __user *)ov);
2402 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002403 return res;
2404
Allan Stephens0c3141e2008-04-15 00:22:02 -07002405 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002406
Per Lidenb97bf3f2006-01-02 19:04:38 +01002407 switch (opt) {
2408 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002409 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002410 break;
2411 case TIPC_SRC_DROPPABLE:
2412 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002413 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002414 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002415 res = -ENOPROTOOPT;
2416 break;
2417 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002418 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002419 break;
2420 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002421 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002422 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002423 break;
2424 default:
2425 res = -EINVAL;
2426 }
2427
Allan Stephens0c3141e2008-04-15 00:22:02 -07002428 release_sock(sk);
2429
Per Lidenb97bf3f2006-01-02 19:04:38 +01002430 return res;
2431}
2432
2433/**
Ying Xue247f0f32014-02-18 16:06:46 +08002434 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002435 * @sock: socket structure
2436 * @lvl: option level
2437 * @opt: option identifier
2438 * @ov: receptacle for option value
2439 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002440 *
2441 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002442 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002443 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002444 * Returns 0 on success, errno otherwise
2445 */
Ying Xue247f0f32014-02-18 16:06:46 +08002446static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2447 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002449 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002450 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002451 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002452 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002453 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002454
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002455 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2456 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457 if (lvl != SOL_TIPC)
2458 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002459 res = get_user(len, ol);
2460 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002461 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002462
Allan Stephens0c3141e2008-04-15 00:22:02 -07002463 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464
2465 switch (opt) {
2466 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002467 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002468 break;
2469 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002470 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002471 break;
2472 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002473 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002474 break;
2475 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002476 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002477 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002478 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002479 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002480 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002481 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002482 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002483 value = skb_queue_len(&sk->sk_receive_queue);
2484 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002485 default:
2486 res = -EINVAL;
2487 }
2488
Allan Stephens0c3141e2008-04-15 00:22:02 -07002489 release_sock(sk);
2490
Paul Gortmaker25860c32010-12-31 18:59:31 +00002491 if (res)
2492 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002493
Paul Gortmaker25860c32010-12-31 18:59:31 +00002494 if (len < sizeof(value))
2495 return -EINVAL;
2496
2497 if (copy_to_user(ov, &value, sizeof(value)))
2498 return -EFAULT;
2499
2500 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002501}
2502
Ying Xuef2f98002015-01-09 15:27:05 +08002503static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002504{
Ying Xuef2f98002015-01-09 15:27:05 +08002505 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002506 struct tipc_sioc_ln_req lnr;
2507 void __user *argp = (void __user *)arg;
2508
2509 switch (cmd) {
2510 case SIOCGETLINKNAME:
2511 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2512 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002513 if (!tipc_node_get_linkname(sock_net(sk),
2514 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002515 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2516 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2517 return -EFAULT;
2518 return 0;
2519 }
2520 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002521 default:
2522 return -ENOIOCTLCMD;
2523 }
2524}
2525
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002526/* Protocol switches for the various types of TIPC sockets */
2527
Florian Westphalbca65ea2008-02-07 18:18:01 -08002528static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002529 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002530 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002531 .release = tipc_release,
2532 .bind = tipc_bind,
2533 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002534 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002535 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002536 .getname = tipc_getname,
2537 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002538 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002539 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002540 .shutdown = tipc_shutdown,
2541 .setsockopt = tipc_setsockopt,
2542 .getsockopt = tipc_getsockopt,
2543 .sendmsg = tipc_sendmsg,
2544 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002545 .mmap = sock_no_mmap,
2546 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002547};
2548
Florian Westphalbca65ea2008-02-07 18:18:01 -08002549static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002550 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002551 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002552 .release = tipc_release,
2553 .bind = tipc_bind,
2554 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002555 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002556 .accept = tipc_accept,
2557 .getname = tipc_getname,
2558 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002559 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002560 .listen = tipc_listen,
2561 .shutdown = tipc_shutdown,
2562 .setsockopt = tipc_setsockopt,
2563 .getsockopt = tipc_getsockopt,
2564 .sendmsg = tipc_send_packet,
2565 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002566 .mmap = sock_no_mmap,
2567 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002568};
2569
Florian Westphalbca65ea2008-02-07 18:18:01 -08002570static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002571 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002572 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002573 .release = tipc_release,
2574 .bind = tipc_bind,
2575 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002576 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002577 .accept = tipc_accept,
2578 .getname = tipc_getname,
2579 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002580 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002581 .listen = tipc_listen,
2582 .shutdown = tipc_shutdown,
2583 .setsockopt = tipc_setsockopt,
2584 .getsockopt = tipc_getsockopt,
2585 .sendmsg = tipc_send_stream,
2586 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002587 .mmap = sock_no_mmap,
2588 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002589};
2590
Florian Westphalbca65ea2008-02-07 18:18:01 -08002591static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002592 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002593 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002594 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002595};
2596
2597static struct proto tipc_proto = {
2598 .name = "TIPC",
2599 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002600 .obj_size = sizeof(struct tipc_sock),
2601 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002602};
2603
2604/**
Per Liden4323add2006-01-18 00:38:21 +01002605 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002606 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002607 * Returns 0 on success, errno otherwise
2608 */
Per Liden4323add2006-01-18 00:38:21 +01002609int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002610{
2611 int res;
2612
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002613 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002614 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002615 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002616 goto out;
2617 }
2618
2619 res = sock_register(&tipc_family_ops);
2620 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002621 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002622 proto_unregister(&tipc_proto);
2623 goto out;
2624 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002625 out:
2626 return res;
2627}
2628
2629/**
Per Liden4323add2006-01-18 00:38:21 +01002630 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002631 */
Per Liden4323add2006-01-18 00:38:21 +01002632void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002633{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002634 sock_unregister(tipc_family_ops.family);
2635 proto_unregister(&tipc_proto);
2636}
Richard Alpe34b78a122014-11-20 10:29:10 +01002637
2638/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002639static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002640{
2641 u32 peer_node;
2642 u32 peer_port;
2643 struct nlattr *nest;
2644
2645 peer_node = tsk_peer_node(tsk);
2646 peer_port = tsk_peer_port(tsk);
2647
2648 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2649
2650 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2651 goto msg_full;
2652 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2653 goto msg_full;
2654
2655 if (tsk->conn_type != 0) {
2656 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2657 goto msg_full;
2658 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2659 goto msg_full;
2660 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2661 goto msg_full;
2662 }
2663 nla_nest_end(skb, nest);
2664
2665 return 0;
2666
2667msg_full:
2668 nla_nest_cancel(skb, nest);
2669
2670 return -EMSGSIZE;
2671}
2672
2673/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002674static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2675 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002676{
2677 int err;
2678 void *hdr;
2679 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002680 struct net *net = sock_net(skb->sk);
2681 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002682
2683 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002684 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002685 if (!hdr)
2686 goto msg_cancel;
2687
2688 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2689 if (!attrs)
2690 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002691 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002692 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002693 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002694 goto attr_msg_cancel;
2695
2696 if (tsk->connected) {
2697 err = __tipc_nl_add_sk_con(skb, tsk);
2698 if (err)
2699 goto attr_msg_cancel;
2700 } else if (!list_empty(&tsk->publications)) {
2701 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2702 goto attr_msg_cancel;
2703 }
2704 nla_nest_end(skb, attrs);
2705 genlmsg_end(skb, hdr);
2706
2707 return 0;
2708
2709attr_msg_cancel:
2710 nla_nest_cancel(skb, attrs);
2711genlmsg_cancel:
2712 genlmsg_cancel(skb, hdr);
2713msg_cancel:
2714 return -EMSGSIZE;
2715}
2716
2717int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2718{
2719 int err;
2720 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002721 const struct bucket_table *tbl;
2722 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002723 struct net *net = sock_net(skb->sk);
2724 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002725 u32 tbl_id = cb->args[0];
2726 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002727
Ying Xue07f6c4b2015-01-07 13:41:58 +08002728 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002729 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002730 for (; tbl_id < tbl->size; tbl_id++) {
2731 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002732 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002733 if (prev_portid && prev_portid != tsk->portid) {
2734 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2735 continue;
2736 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002737
Richard Alped6e164e2015-01-16 12:30:40 +01002738 err = __tipc_nl_add_sk(skb, cb, tsk);
2739 if (err) {
2740 prev_portid = tsk->portid;
2741 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2742 goto out;
2743 }
2744 prev_portid = 0;
2745 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002746 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002747 }
Richard Alped6e164e2015-01-16 12:30:40 +01002748out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002749 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002750 cb->args[0] = tbl_id;
2751 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002752
2753 return skb->len;
2754}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002755
2756/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002757static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2758 struct netlink_callback *cb,
2759 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002760{
2761 void *hdr;
2762 struct nlattr *attrs;
2763
2764 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002765 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002766 if (!hdr)
2767 goto msg_cancel;
2768
2769 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2770 if (!attrs)
2771 goto genlmsg_cancel;
2772
2773 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2774 goto attr_msg_cancel;
2775 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2776 goto attr_msg_cancel;
2777 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2778 goto attr_msg_cancel;
2779 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2780 goto attr_msg_cancel;
2781
2782 nla_nest_end(skb, attrs);
2783 genlmsg_end(skb, hdr);
2784
2785 return 0;
2786
2787attr_msg_cancel:
2788 nla_nest_cancel(skb, attrs);
2789genlmsg_cancel:
2790 genlmsg_cancel(skb, hdr);
2791msg_cancel:
2792 return -EMSGSIZE;
2793}
2794
2795/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002796static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2797 struct netlink_callback *cb,
2798 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002799{
2800 int err;
2801 struct publication *p;
2802
2803 if (*last_publ) {
2804 list_for_each_entry(p, &tsk->publications, pport_list) {
2805 if (p->key == *last_publ)
2806 break;
2807 }
2808 if (p->key != *last_publ) {
2809 /* We never set seq or call nl_dump_check_consistent()
2810 * this means that setting prev_seq here will cause the
2811 * consistence check to fail in the netlink callback
2812 * handler. Resulting in the last NLMSG_DONE message
2813 * having the NLM_F_DUMP_INTR flag set.
2814 */
2815 cb->prev_seq = 1;
2816 *last_publ = 0;
2817 return -EPIPE;
2818 }
2819 } else {
2820 p = list_first_entry(&tsk->publications, struct publication,
2821 pport_list);
2822 }
2823
2824 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2825 err = __tipc_nl_add_sk_publ(skb, cb, p);
2826 if (err) {
2827 *last_publ = p->key;
2828 return err;
2829 }
2830 }
2831 *last_publ = 0;
2832
2833 return 0;
2834}
2835
2836int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2837{
2838 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002839 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002840 u32 last_publ = cb->args[1];
2841 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002842 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002843 struct tipc_sock *tsk;
2844
Ying Xue07f6c4b2015-01-07 13:41:58 +08002845 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002846 struct nlattr **attrs;
2847 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2848
2849 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2850 if (err)
2851 return err;
2852
Richard Alpe45e093a2016-05-16 11:14:54 +02002853 if (!attrs[TIPC_NLA_SOCK])
2854 return -EINVAL;
2855
Richard Alpe1a1a1432014-11-20 10:29:11 +01002856 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2857 attrs[TIPC_NLA_SOCK],
2858 tipc_nl_sock_policy);
2859 if (err)
2860 return err;
2861
2862 if (!sock[TIPC_NLA_SOCK_REF])
2863 return -EINVAL;
2864
Ying Xue07f6c4b2015-01-07 13:41:58 +08002865 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002866 }
2867
2868 if (done)
2869 return 0;
2870
Ying Xuee05b31f2015-01-09 15:27:08 +08002871 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002872 if (!tsk)
2873 return -EINVAL;
2874
2875 lock_sock(&tsk->sk);
2876 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2877 if (!err)
2878 done = 1;
2879 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002880 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002881
Ying Xue07f6c4b2015-01-07 13:41:58 +08002882 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002883 cb->args[1] = last_publ;
2884 cb->args[2] = done;
2885
2886 return skb->len;
2887}