blob: 6077850774454297a8efe7d8fd39800747cd3be1 [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);
Hoang Le9d8a1652021-05-14 08:23:03 +0700744 /* Decrease the skb's refcnt as increasing in the
745 * function tipc_skb_peek
746 */
747 kfree_skb(__skb_dequeue(arrvq));
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500748 }
749 spin_unlock_bh(&inputq->lock);
750 __skb_queue_purge(&tmpq);
751 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400752 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500753 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400754}
755
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900756/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500757 * tipc_sk_proto_rcv - receive a connection mng protocol message
758 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400759 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500760 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400761static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
762 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500763{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400764 struct sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400765 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400766 struct tipc_msg *hdr = buf_msg(skb);
767 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400768 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400769
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500770 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400771 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500772 goto exit;
773
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400774 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500775
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400776 if (mtyp == CONN_PROBE) {
777 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400778 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
779 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400780 return;
781 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400782 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400783 tsk->snt_unacked -= msg_conn_ack(hdr);
784 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
785 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500786 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400787 sk->sk_write_space(sk);
788 } else if (mtyp != CONN_PROBE_REPLY) {
789 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500790 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500791exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400792 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500793}
794
Ying Xue3f405042014-01-17 09:50:05 +0800795static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
796{
797 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400798 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800799 DEFINE_WAIT(wait);
800 int done;
801
802 do {
803 int err = sock_error(sk);
804 if (err)
805 return err;
806 if (sock->state == SS_DISCONNECTING)
807 return -EPIPE;
808 if (!*timeo_p)
809 return -EAGAIN;
810 if (signal_pending(current))
811 return sock_intr_errno(*timeo_p);
812
813 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500814 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800815 finish_wait(sk_sleep(sk), &wait);
816 } while (!done);
817 return 0;
818}
819
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500820/**
Ying Xue247f0f32014-02-18 16:06:46 +0800821 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 * @sock: socket structure
823 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500824 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900825 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900827 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
829 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900830 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831 * Returns the number of bytes sent on success, or errno otherwise
832 */
Ying Xue1b784142015-03-02 15:37:48 +0800833static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500834 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835{
Ying Xue39a02952015-03-02 15:37:47 +0800836 struct sock *sk = sock->sk;
837 int ret;
838
839 lock_sock(sk);
840 ret = __tipc_sendmsg(sock, m, dsz);
841 release_sock(sk);
842
843 return ret;
844}
845
846static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
847{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500848 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700849 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400850 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800851 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400852 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500853 u32 dnode, dport;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100854 struct sk_buff_head pktchain;
Ying Xuea6ca1092014-11-26 11:41:55 +0800855 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100856 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500857 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500858 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800859 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100860 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500862 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400863 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100864 if (unlikely(!dest)) {
865 if (tsk->connected && sock->state == SS_READY)
866 dest = &tsk->remote;
867 else
868 return -EDESTADDRREQ;
869 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
870 dest->family != AF_TIPC) {
871 return -EINVAL;
872 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500873 if (unlikely(sock->state != SS_READY)) {
Ying Xue39a02952015-03-02 15:37:47 +0800874 if (sock->state == SS_LISTENING)
875 return -EPIPE;
876 if (sock->state != SS_UNCONNECTED)
877 return -EISCONN;
878 if (tsk->published)
879 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700880 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400881 tsk->conn_type = dest->addr.name.name.type;
882 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700883 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100885 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800886 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500887
888 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a02952015-03-02 15:37:47 +0800889 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500890 } else if (dest->addrtype == TIPC_ADDR_NAME) {
891 u32 type = dest->addr.name.name.type;
892 u32 inst = dest->addr.name.name.instance;
893 u32 domain = dest->addr.name.domain;
894
895 dnode = domain;
896 msg_set_type(mhdr, TIPC_NAMED_MSG);
897 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
898 msg_set_nametype(mhdr, type);
899 msg_set_nameinst(mhdr, inst);
900 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800901 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500902 msg_set_destnode(mhdr, dnode);
903 msg_set_destport(mhdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800904 if (unlikely(!dport && !dnode))
905 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500906 } else if (dest->addrtype == TIPC_ADDR_ID) {
907 dnode = dest->addr.id.node;
908 msg_set_type(mhdr, TIPC_DIRECT_MSG);
909 msg_set_lookup_scope(mhdr, 0);
910 msg_set_destnode(mhdr, dnode);
911 msg_set_destport(mhdr, dest->addr.id.ref);
912 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
913 }
914
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100915 skb_queue_head_init(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500916 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500917new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800918 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100919 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500920 if (rc < 0)
Ying Xue39a02952015-03-02 15:37:47 +0800921 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500922
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900923 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100924 skb = skb_peek(&pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +0800925 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100926 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400927 if (likely(!rc)) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500928 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700929 sock->state = SS_CONNECTING;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400930 return dsz;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900931 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400932 if (rc == -ELINKCONG) {
933 tsk->link_cong = 1;
934 rc = tipc_wait_for_sndmsg(sock, &timeo);
935 if (!rc)
936 continue;
937 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100938 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500939 if (rc == -EMSGSIZE) {
940 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500941 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500942 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400943 break;
944 } while (1);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500945
946 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947}
948
Ying Xue391a6dd2014-01-17 09:50:06 +0800949static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
950{
951 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400952 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800953 DEFINE_WAIT(wait);
954 int done;
955
956 do {
957 int err = sock_error(sk);
958 if (err)
959 return err;
960 if (sock->state == SS_DISCONNECTING)
961 return -EPIPE;
962 else if (sock->state != SS_CONNECTED)
963 return -ENOTCONN;
964 if (!*timeo_p)
965 return -EAGAIN;
966 if (signal_pending(current))
967 return sock_intr_errno(*timeo_p);
968
969 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
970 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500971 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400972 !tsk_conn_cong(tsk)) ||
973 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800974 finish_wait(sk_sleep(sk), &wait);
975 } while (!done);
976 return 0;
977}
978
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900979/**
Ying Xue247f0f32014-02-18 16:06:46 +0800980 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 * @sock: socket structure
982 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500983 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900984 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900986 *
987 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700988 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 */
Ying Xue1b784142015-03-02 15:37:48 +0800990static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700992 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +0800993 int ret;
994
995 lock_sock(sk);
996 ret = __tipc_send_stream(sock, m, dsz);
997 release_sock(sk);
998
999 return ret;
1000}
1001
1002static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1003{
1004 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001005 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001006 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001007 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001008 struct sk_buff_head pktchain;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001009 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001010 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001011 int rc = -EINVAL;
1012 long timeo;
1013 u32 dnode;
1014 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001015 struct iov_iter save;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001016 int hlen = MIN_H_SIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001017
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001018 /* Handle implied connection establishment */
1019 if (unlikely(dest)) {
Ying Xue39a02952015-03-02 15:37:47 +08001020 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001021 hlen = msg_hdr_sz(mhdr);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001022 if (dsz && (dsz == rc))
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001023 tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001024 return rc;
1025 }
1026 if (dsz > (uint)INT_MAX)
1027 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001028
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001030 if (sock->state == SS_DISCONNECTING)
Ying Xue39a02952015-03-02 15:37:47 +08001031 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001032 else
Ying Xue39a02952015-03-02 15:37:47 +08001033 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001034 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001036 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001037 dnode = tsk_peer_node(tsk);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001038 skb_queue_head_init(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001039
1040next:
Al Virof25dcc72014-11-28 15:52:29 -05001041 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001042 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001043 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001044 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001045 if (unlikely(rc < 0))
Ying Xue39a02952015-03-02 15:37:47 +08001046 return rc;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001047
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001048 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001049 if (likely(!tsk_conn_cong(tsk))) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001050 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001051 if (likely(!rc)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001052 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001053 sent += send;
1054 if (sent == dsz)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001055 return dsz;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001056 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001057 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001058 if (rc == -EMSGSIZE) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001059 __skb_queue_purge(&pktchain);
Ying Xuef2f98002015-01-09 15:27:05 +08001060 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1061 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001062 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001063 goto next;
1064 }
1065 if (rc != -ELINKCONG)
1066 break;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001067
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001068 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001070 rc = tipc_wait_for_sndpkt(sock, &timeo);
1071 } while (!rc);
Ying Xue39a02952015-03-02 15:37:47 +08001072
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001073 __skb_queue_purge(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001074 return sent ? sent : rc;
1075}
1076
1077/**
1078 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001079 * @sock: socket structure
1080 * @m: message to send
1081 * @dsz: length of data to be transmitted
1082 *
1083 * Used for SOCK_SEQPACKET messages.
1084 *
1085 * Returns the number of bytes sent on success, or errno otherwise
1086 */
Ying Xue1b784142015-03-02 15:37:48 +08001087static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001088{
1089 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1090 return -EMSGSIZE;
1091
Ying Xue1b784142015-03-02 15:37:48 +08001092 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093}
1094
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001095/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001097static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001098 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099{
Ying Xue3721e9c2015-01-13 17:07:48 +08001100 struct sock *sk = &tsk->sk;
1101 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001102 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001104 msg_set_destnode(msg, peer_node);
1105 msg_set_destport(msg, peer_port);
1106 msg_set_type(msg, TIPC_CONN_MSG);
1107 msg_set_lookup_scope(msg, 0);
1108 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001109
Ying Xue2f55c432015-01-09 15:27:00 +08001110 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001111 tsk->probing_state = TIPC_CONN_OK;
1112 tsk->connected = 1;
Ying Xue3721e9c2015-01-13 17:07:48 +08001113 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001114 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1115 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001116 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001117 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1118 return;
1119
1120 /* Fall back to message based flow control */
1121 tsk->rcv_win = FLOWCTL_MSG_WIN;
1122 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123}
1124
1125/**
1126 * set_orig_addr - capture sender's address for received message
1127 * @m: descriptor for message info
1128 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001129 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 * Note: Address is not captured if not requested by receiver.
1131 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001132static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001134 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001136 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 addr->family = AF_TIPC;
1138 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001139 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 addr->addr.id.ref = msg_origport(msg);
1141 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001142 addr->addr.name.domain = 0; /* could leave uninitialized */
1143 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 m->msg_namelen = sizeof(struct sockaddr_tipc);
1145 }
1146}
1147
1148/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001149 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 * @m: descriptor for message info
1151 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001152 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001153 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001155 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 * Returns 0 if successful, otherwise errno
1157 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001158static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1159 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160{
1161 u32 anc_data[3];
1162 u32 err;
1163 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001164 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 int res;
1166
1167 if (likely(m->msg_controllen == 0))
1168 return 0;
1169
1170 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 err = msg ? msg_errcode(msg) : 0;
1172 if (unlikely(err)) {
1173 anc_data[0] = err;
1174 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001175 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1176 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001178 if (anc_data[1]) {
1179 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1180 msg_data(msg));
1181 if (res)
1182 return res;
1183 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 }
1185
1186 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1188 switch (dest_type) {
1189 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001190 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 anc_data[0] = msg_nametype(msg);
1192 anc_data[1] = msg_namelower(msg);
1193 anc_data[2] = msg_namelower(msg);
1194 break;
1195 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001196 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 anc_data[0] = msg_nametype(msg);
1198 anc_data[1] = msg_namelower(msg);
1199 anc_data[2] = msg_nameupper(msg);
1200 break;
1201 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001202 has_name = (tsk->conn_type != 0);
1203 anc_data[0] = tsk->conn_type;
1204 anc_data[1] = tsk->conn_instance;
1205 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 break;
1207 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001208 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 }
Allan Stephens2db99832010-12-31 18:59:33 +00001210 if (has_name) {
1211 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1212 if (res)
1213 return res;
1214 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215
1216 return 0;
1217}
1218
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001219static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001220{
Ying Xuef2f98002015-01-09 15:27:05 +08001221 struct net *net = sock_net(&tsk->sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001222 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001223 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001224 u32 peer_port = tsk_peer_port(tsk);
1225 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001226
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001227 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001228 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001229 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1230 dnode, tsk_own_node(tsk), peer_port,
1231 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001232 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001233 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001234 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001235 msg_set_conn_ack(msg, tsk->rcv_unacked);
1236 tsk->rcv_unacked = 0;
1237
1238 /* Adjust to and advertize the correct window limit */
1239 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1240 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1241 msg_set_adv_win(msg, tsk->rcv_win);
1242 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001243 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001244}
1245
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001246static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001247{
1248 struct sock *sk = sock->sk;
1249 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001250 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001251 int err;
1252
1253 for (;;) {
1254 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001255 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001256 if (sock->state == SS_DISCONNECTING) {
1257 err = -ENOTCONN;
1258 break;
1259 }
1260 release_sock(sk);
1261 timeo = schedule_timeout(timeo);
1262 lock_sock(sk);
1263 }
1264 err = 0;
1265 if (!skb_queue_empty(&sk->sk_receive_queue))
1266 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001267 err = -EAGAIN;
1268 if (!timeo)
1269 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001270 err = sock_intr_errno(timeo);
1271 if (signal_pending(current))
1272 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001273 }
1274 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001275 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001276 return err;
1277}
1278
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001279/**
Ying Xue247f0f32014-02-18 16:06:46 +08001280 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281 * @m: descriptor for message info
1282 * @buf_len: total size of user buffer area
1283 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001284 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1286 * If the complete message doesn't fit in user area, truncate it.
1287 *
1288 * Returns size of returned message data, errno otherwise
1289 */
Ying Xue1b784142015-03-02 15:37:48 +08001290static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1291 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001293 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001294 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 struct sk_buff *buf;
1296 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001297 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 unsigned int sz;
1299 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001300 int res, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001301
Allan Stephens0c3141e2008-04-15 00:22:02 -07001302 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 if (unlikely(!buf_len))
1304 return -EINVAL;
1305
Allan Stephens0c3141e2008-04-15 00:22:02 -07001306 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307
Allan Stephens0c3141e2008-04-15 00:22:02 -07001308 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 res = -ENOTCONN;
1310 goto exit;
1311 }
1312
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001313 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001314restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315
Allan Stephens0c3141e2008-04-15 00:22:02 -07001316 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001317 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001318 if (res)
1319 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001320
1321 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001322 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 msg = buf_msg(buf);
1324 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001325 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 err = msg_errcode(msg);
1327
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001330 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 goto restart;
1332 }
1333
1334 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 set_orig_addr(m, msg);
1336
1337 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001338 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001339 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 goto exit;
1341
1342 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 if (!err) {
1344 if (unlikely(buf_len < sz)) {
1345 sz = buf_len;
1346 m->msg_flags |= MSG_TRUNC;
1347 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001348 res = skb_copy_datagram_msg(buf, hlen, m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001349 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 res = sz;
1352 } else {
1353 if ((sock->state == SS_READY) ||
1354 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1355 res = 0;
1356 else
1357 res = -ECONNRESET;
1358 }
1359
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001360 if (unlikely(flags & MSG_PEEK))
1361 goto exit;
1362
1363 if (likely(sock->state != SS_READY)) {
1364 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1365 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1366 tipc_sk_send_ack(tsk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001367 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001368 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001370 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 return res;
1372}
1373
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001374/**
Ying Xue247f0f32014-02-18 16:06:46 +08001375 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 * @m: descriptor for message info
1377 * @buf_len: total size of user buffer area
1378 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001379 *
1380 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 * will optionally wait for more; never truncates data.
1382 *
1383 * Returns size of returned message data, errno otherwise
1384 */
Ying Xue1b784142015-03-02 15:37:48 +08001385static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1386 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001388 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001389 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 struct sk_buff *buf;
1391 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001392 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001394 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001397 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398
1399 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400 if (unlikely(!buf_len))
1401 return -EINVAL;
1402
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001405 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 res = -ENOTCONN;
1407 goto exit;
1408 }
1409
Florian Westphal3720d402010-08-17 11:00:04 +00001410 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001411 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001412
Allan Stephens0c3141e2008-04-15 00:22:02 -07001413restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001415 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001416 if (res)
1417 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001418
1419 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001420 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 msg = buf_msg(buf);
1422 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001423 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 err = msg_errcode(msg);
1425
1426 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001428 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 goto restart;
1430 }
1431
1432 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 if (sz_copied == 0) {
1434 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001435 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001436 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437 goto exit;
1438 }
1439
1440 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001442 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001443
Allan Stephens0232fd02011-02-21 09:45:40 -05001444 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445 needed = (buf_len - sz_copied);
1446 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001447
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001448 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001449 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001451
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 sz_copied += sz_to_copy;
1453
1454 if (sz_to_copy < sz) {
1455 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001456 TIPC_SKB_CB(buf)->handle =
1457 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001458 goto exit;
1459 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 } else {
1461 if (sz_copied != 0)
1462 goto exit; /* can't add error msg to valid data */
1463
1464 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1465 res = 0;
1466 else
1467 res = -ECONNRESET;
1468 }
1469
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001470 if (unlikely(flags & MSG_PEEK))
1471 goto exit;
1472
1473 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1474 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1475 tipc_sk_send_ack(tsk);
1476 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477
1478 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001479 if ((sz_copied < buf_len) && /* didn't get all requested data */
1480 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001481 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001482 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483 goto restart;
1484
1485exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001486 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001487 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488}
1489
1490/**
Ying Xuef288bef2012-08-21 11:16:57 +08001491 * tipc_write_space - wake up thread if port congestion is released
1492 * @sk: socket
1493 */
1494static void tipc_write_space(struct sock *sk)
1495{
1496 struct socket_wq *wq;
1497
1498 rcu_read_lock();
1499 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001500 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001501 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1502 POLLWRNORM | POLLWRBAND);
1503 rcu_read_unlock();
1504}
1505
1506/**
1507 * tipc_data_ready - wake up threads to indicate messages have been received
1508 * @sk: socket
1509 * @len: the length of messages
1510 */
David S. Miller676d2362014-04-11 16:15:36 -04001511static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001512{
1513 struct socket_wq *wq;
1514
1515 rcu_read_lock();
1516 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001517 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001518 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1519 POLLRDNORM | POLLRDBAND);
1520 rcu_read_unlock();
1521}
1522
Ying Xuef4195d12015-11-22 15:46:05 +08001523static void tipc_sock_destruct(struct sock *sk)
1524{
1525 __skb_queue_purge(&sk->sk_receive_queue);
1526}
1527
Ying Xuef288bef2012-08-21 11:16:57 +08001528/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001529 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001530 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001531 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001532 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001533 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001534 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001535static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001536{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001537 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001538 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001539 struct socket *sock = sk->sk_socket;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001540 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001541
Jon Paul Maloycda36962015-07-22 10:11:20 -04001542 if (unlikely(msg_mcast(hdr)))
1543 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001544
1545 switch ((int)sock->state) {
1546 case SS_CONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001547
Ying Xue7e6c1312012-11-29 18:39:14 -05001548 /* Accept only connection-based messages sent by peer */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001549 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1550 return false;
1551
1552 if (unlikely(msg_errcode(hdr))) {
1553 sock->state = SS_DISCONNECTING;
1554 tsk->connected = 0;
1555 /* Let timer expire on it's own */
1556 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1557 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001558 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001559 return true;
1560
Ying Xue7e6c1312012-11-29 18:39:14 -05001561 case SS_CONNECTING:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001562
Ying Xue7e6c1312012-11-29 18:39:14 -05001563 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001564 if (unlikely(!msg_connected(hdr)))
1565 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001566
Jon Paul Maloycda36962015-07-22 10:11:20 -04001567 if (unlikely(msg_errcode(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001568 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001569 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001570 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001571 }
1572
Jon Paul Maloycda36962015-07-22 10:11:20 -04001573 if (unlikely(!msg_isdata(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001574 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001575 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001576 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001577 }
1578
Jon Paul Maloycda36962015-07-22 10:11:20 -04001579 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1580 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001581 sock->state = SS_CONNECTED;
1582
Jon Paul Maloycda36962015-07-22 10:11:20 -04001583 /* If 'ACK+' message, add to socket receive queue */
1584 if (msg_data_sz(hdr))
1585 return true;
1586
1587 /* If empty 'ACK-' message, wake up sleeping connect() */
1588 if (waitqueue_active(sk_sleep(sk)))
1589 wake_up_interruptible(sk_sleep(sk));
1590
1591 /* 'ACK-' message is neither accepted nor rejected: */
1592 msg_set_dest_droppable(hdr, 1);
1593 return false;
1594
Ying Xue7e6c1312012-11-29 18:39:14 -05001595 case SS_LISTENING:
1596 case SS_UNCONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001597
Ying Xue7e6c1312012-11-29 18:39:14 -05001598 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001599 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1600 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001601 break;
1602 case SS_DISCONNECTING:
1603 break;
1604 default:
1605 pr_err("Unknown socket state %u\n", sock->state);
1606 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001607 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001608}
1609
1610/**
Ying Xueaba79f32013-01-20 23:30:09 +01001611 * rcvbuf_limit - get proper overload limit of socket receive queue
1612 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001613 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001614 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001615 * For connection oriented messages, irrespective of importance,
1616 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001617 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001618 * For connectionless messages, queue limits are based on message
1619 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001620 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001621 * TIPC_LOW_IMPORTANCE (2 MB)
1622 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1623 * TIPC_HIGH_IMPORTANCE (8 MB)
1624 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001625 *
1626 * Returns overload limit according to corresponding message importance
1627 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001628static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001629{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001630 struct tipc_sock *tsk = tipc_sk(sk);
1631 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001632
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001633 if (unlikely(!msg_connected(hdr)))
1634 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001635
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001636 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1637 return sk->sk_rcvbuf;
1638
1639 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001640}
1641
1642/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001643 * filter_rcv - validate incoming message
1644 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001645 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001646 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001647 * Enqueues message on receive queue if acceptable; optionally handles
1648 * disconnect indication for a connected socket.
1649 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001650 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001651 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001652 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001654static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1655 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001657 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001658 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001659 struct tipc_msg *hdr = buf_msg(skb);
1660 unsigned int limit = rcvbuf_limit(sk, skb);
1661 int err = TIPC_OK;
1662 int usr = msg_user(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663
Jon Paul Maloycda36962015-07-22 10:11:20 -04001664 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001665 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001666 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001667 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001668
Jon Paul Maloycda36962015-07-22 10:11:20 -04001669 if (unlikely(usr == SOCK_WAKEUP)) {
1670 kfree_skb(skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001671 tsk->link_cong = 0;
1672 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001673 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001674 }
1675
Jon Paul Maloycda36962015-07-22 10:11:20 -04001676 /* Drop if illegal message type */
1677 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1678 kfree_skb(skb);
1679 return false;
1680 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001681
Jon Paul Maloycda36962015-07-22 10:11:20 -04001682 /* Reject if wrong message type for current socket state */
1683 if (unlikely(sock->state == SS_READY)) {
1684 if (msg_connected(hdr)) {
1685 err = TIPC_ERR_NO_PORT;
1686 goto reject;
1687 }
1688 } else if (unlikely(!filter_connect(tsk, skb))) {
1689 err = TIPC_ERR_NO_PORT;
1690 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 }
1692
1693 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001694 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1695 err = TIPC_ERR_OVERLOAD;
1696 goto reject;
1697 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698
Ying Xueaba79f32013-01-20 23:30:09 +01001699 /* Enqueue message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001700 TIPC_SKB_CB(skb)->handle = NULL;
1701 __skb_queue_tail(&sk->sk_receive_queue, skb);
1702 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001703
David S. Miller676d2362014-04-11 16:15:36 -04001704 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001705 return true;
1706
1707reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001708 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1709 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001710 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711}
1712
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001713/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001714 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001715 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001716 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001717 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001718 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001719 *
1720 * Returns 0
1721 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001722static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001723{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001724 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001725 struct sk_buff_head xmitq;
1726 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001727
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001728 __skb_queue_head_init(&xmitq);
1729
1730 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001731 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001732 return 0;
1733 }
1734
1735 if (skb_queue_empty(&xmitq))
1736 return 0;
1737
1738 /* Send response/rejected message */
1739 skb = __skb_dequeue(&xmitq);
1740 dnode = msg_destnode(buf_msg(skb));
1741 selector = msg_origport(buf_msg(skb));
1742 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001743 return 0;
1744}
1745
1746/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001747 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1748 * inputq and try adding them to socket or backlog queue
1749 * @inputq: list of incoming buffers with potentially different destinations
1750 * @sk: socket where the buffers should be enqueued
1751 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001752 *
1753 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001754 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001755static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001756 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001757{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001758 unsigned long time_limit = jiffies + 2;
1759 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001760 unsigned int lim;
1761 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001762 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001763
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001764 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001765 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001766 return;
1767
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001768 skb = tipc_skb_dequeue(inputq, dport);
1769 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001770 return;
1771
1772 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001773 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001774 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001775 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001776 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001777
1778 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001779 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001780 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001781 atomic_set(dcnt, 0);
1782 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1783 if (likely(!sk_add_backlog(sk, skb, lim)))
1784 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001785
1786 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001787 onode = tipc_own_addr(sock_net(sk));
1788 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1789 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001790 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001791 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001792}
1793
1794/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001795 * tipc_sk_rcv - handle a chain of incoming buffers
1796 * @inputq: buffer list containing the buffers
1797 * Consumes all buffers in list until inputq is empty
1798 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001799 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001800void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001801{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001802 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001803 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001804 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001805 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001806 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001807 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001808
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001809 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001810 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001811 dport = tipc_skb_peek_port(inputq, dport);
1812 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001813
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001814 if (likely(tsk)) {
1815 sk = &tsk->sk;
1816 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001817 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001818 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001819 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001820 /* Send pending response/rejected messages, if any */
1821 while ((skb = __skb_dequeue(&xmitq))) {
1822 dnode = msg_destnode(buf_msg(skb));
1823 tipc_node_xmit_skb(net, skb, dnode, dport);
1824 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001825 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001826 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001827 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001828
1829 /* No destination socket => dequeue skb if still there */
1830 skb = tipc_skb_dequeue(inputq, dport);
1831 if (!skb)
1832 return;
1833
1834 /* Try secondary lookup if unresolved named message */
1835 err = TIPC_ERR_NO_PORT;
1836 if (tipc_msg_lookup_dest(net, skb, &err))
1837 goto xmit;
1838
1839 /* Prepare for message rejection */
1840 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001841 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001842xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001843 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001844 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001845 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001846}
1847
Ying Xue78eb3a52014-01-17 09:50:03 +08001848static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1849{
1850 struct sock *sk = sock->sk;
1851 DEFINE_WAIT(wait);
1852 int done;
1853
1854 do {
1855 int err = sock_error(sk);
1856 if (err)
1857 return err;
1858 if (!*timeo_p)
1859 return -ETIMEDOUT;
1860 if (signal_pending(current))
1861 return sock_intr_errno(*timeo_p);
1862
1863 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1864 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1865 finish_wait(sk_sleep(sk), &wait);
1866 } while (!done);
1867 return 0;
1868}
1869
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870/**
Ying Xue247f0f32014-02-18 16:06:46 +08001871 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872 * @sock: socket structure
1873 * @dest: socket address for destination port
1874 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001875 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 *
1877 * Returns 0 on success, errno otherwise
1878 */
Ying Xue247f0f32014-02-18 16:06:46 +08001879static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1880 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001882 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001883 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001884 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1885 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001886 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Ying Xue78eb3a52014-01-17 09:50:03 +08001887 socket_state previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001888 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889
Allan Stephens0c3141e2008-04-15 00:22:02 -07001890 lock_sock(sk);
1891
Erik Hugnef2f80362015-03-19 09:02:19 +01001892 /* DGRAM/RDM connect(), just save the destaddr */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001893 if (sock->state == SS_READY) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001894 if (dst->family == AF_UNSPEC) {
1895 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1896 tsk->connected = 0;
Sasha Levin610600c2015-03-23 15:30:00 -04001897 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1898 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001899 } else {
1900 memcpy(&tsk->remote, dest, destlen);
1901 tsk->connected = 1;
1902 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001903 goto exit;
1904 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905
Allan Stephensb89741a2008-04-15 00:20:37 -07001906 /*
1907 * Reject connection attempt using multicast address
1908 *
1909 * Note: send_msg() validates the rest of the address fields,
1910 * so there's no need to do it here
1911 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001912 if (dst->addrtype == TIPC_ADDR_MCAST) {
1913 res = -EINVAL;
1914 goto exit;
1915 }
1916
Ying Xue78eb3a52014-01-17 09:50:03 +08001917 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001918 switch (sock->state) {
1919 case SS_UNCONNECTED:
1920 /* Send a 'SYN-' to destination */
1921 m.msg_name = dest;
1922 m.msg_namelen = destlen;
1923
1924 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1925 * indicate send_msg() is never blocked.
1926 */
1927 if (!timeout)
1928 m.msg_flags = MSG_DONTWAIT;
1929
Ying Xue39a02952015-03-02 15:37:47 +08001930 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001931 if ((res < 0) && (res != -EWOULDBLOCK))
1932 goto exit;
1933
1934 /* Just entered SS_CONNECTING state; the only
1935 * difference is that return value in non-blocking
1936 * case is EINPROGRESS, rather than EALREADY.
1937 */
1938 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001939 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001940 if (previous == SS_CONNECTING)
1941 res = -EALREADY;
1942 if (!timeout)
1943 goto exit;
1944 timeout = msecs_to_jiffies(timeout);
1945 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1946 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001947 break;
1948 case SS_CONNECTED:
1949 res = -EISCONN;
1950 break;
1951 default:
1952 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001953 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001954 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001955exit:
1956 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001957 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958}
1959
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001960/**
Ying Xue247f0f32014-02-18 16:06:46 +08001961 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001962 * @sock: socket structure
1963 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001964 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965 * Returns 0 on success, errno otherwise
1966 */
Ying Xue247f0f32014-02-18 16:06:46 +08001967static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001969 struct sock *sk = sock->sk;
1970 int res;
1971
1972 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973
Ying Xue245f3d32011-07-06 06:01:13 -04001974 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001975 res = -EINVAL;
1976 else {
1977 sock->state = SS_LISTENING;
1978 res = 0;
1979 }
1980
1981 release_sock(sk);
1982 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983}
1984
Ying Xue6398e232014-01-17 09:50:04 +08001985static int tipc_wait_for_accept(struct socket *sock, long timeo)
1986{
1987 struct sock *sk = sock->sk;
Hoang Lea1d2c2e2021-07-23 09:25:34 +07001988 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue6398e232014-01-17 09:50:04 +08001989 int err;
1990
1991 /* True wake-one mechanism for incoming connections: only
1992 * one process gets woken up, not the 'whole herd'.
1993 * Since we do not 'race & poll' for established sockets
1994 * anymore, the common case will execute the loop only once.
1995 */
1996 for (;;) {
Ying Xuefe8e4642014-03-06 14:40:18 +01001997 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Hoang Lea1d2c2e2021-07-23 09:25:34 +07001998 add_wait_queue(sk_sleep(sk), &wait);
Ying Xue6398e232014-01-17 09:50:04 +08001999 release_sock(sk);
Hoang Lea1d2c2e2021-07-23 09:25:34 +07002000 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
Ying Xue6398e232014-01-17 09:50:04 +08002001 lock_sock(sk);
Hoang Lea1d2c2e2021-07-23 09:25:34 +07002002 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue6398e232014-01-17 09:50:04 +08002003 }
2004 err = 0;
2005 if (!skb_queue_empty(&sk->sk_receive_queue))
2006 break;
2007 err = -EINVAL;
2008 if (sock->state != SS_LISTENING)
2009 break;
Ying Xue6398e232014-01-17 09:50:04 +08002010 err = -EAGAIN;
2011 if (!timeo)
2012 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002013 err = sock_intr_errno(timeo);
2014 if (signal_pending(current))
2015 break;
Ying Xue6398e232014-01-17 09:50:04 +08002016 }
Ying Xue6398e232014-01-17 09:50:04 +08002017 return err;
2018}
2019
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002020/**
Ying Xue247f0f32014-02-18 16:06:46 +08002021 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022 * @sock: listening socket
2023 * @newsock: new socket that is to be connected
2024 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002025 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002026 * Returns 0 on success, errno otherwise
2027 */
Ying Xue247f0f32014-02-18 16:06:46 +08002028static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002029{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002030 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002032 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002033 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002034 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002035 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036
Allan Stephens0c3141e2008-04-15 00:22:02 -07002037 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002038
Allan Stephens0c3141e2008-04-15 00:22:02 -07002039 if (sock->state != SS_LISTENING) {
2040 res = -EINVAL;
2041 goto exit;
2042 }
Ying Xue6398e232014-01-17 09:50:04 +08002043 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2044 res = tipc_wait_for_accept(sock, timeo);
2045 if (res)
2046 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002047
2048 buf = skb_peek(&sk->sk_receive_queue);
2049
Ying Xuec5fa7b32013-06-17 10:54:39 -04002050 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002051 if (res)
2052 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002053 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002054
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002055 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002056 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002057 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002058
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002059 /* we lock on new_sk; but lockdep sees the lock on sk */
2060 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002061
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002062 /*
2063 * Reject any stray messages received by new socket
2064 * before the socket lock was taken (very, very unlikely)
2065 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002066 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002067
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002068 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002069 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002070 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002072 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002073 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002074 new_tsock->conn_type = msg_nametype(msg);
2075 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002077
2078 /*
2079 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2080 * Respond to 'SYN+' by queuing it on new socket.
2081 */
2082 if (!msg_data_sz(msg)) {
2083 struct msghdr m = {NULL,};
2084
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002085 tsk_advance_rx_queue(sk);
Ying Xue39a02952015-03-02 15:37:47 +08002086 __tipc_send_stream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002087 } else {
2088 __skb_dequeue(&sk->sk_receive_queue);
2089 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002090 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002091 }
2092 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002093exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002094 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002095 return res;
2096}
2097
2098/**
Ying Xue247f0f32014-02-18 16:06:46 +08002099 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002101 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002102 *
2103 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002104 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002105 * Returns 0 on success, errno otherwise
2106 */
Ying Xue247f0f32014-02-18 16:06:46 +08002107static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002109 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002110 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002111 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002112 struct sk_buff *skb;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002113 u32 dnode = tsk_peer_node(tsk);
2114 u32 dport = tsk_peer_port(tsk);
2115 u32 onode = tipc_own_addr(net);
2116 u32 oport = tsk->portid;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 int res;
2118
Allan Stephense247a8f2008-03-06 15:05:38 -08002119 if (how != SHUT_RDWR)
2120 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121
Allan Stephens0c3141e2008-04-15 00:22:02 -07002122 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123
2124 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002125 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126 case SS_CONNECTED:
2127
Per Lidenb97bf3f2006-01-02 19:04:38 +01002128restart:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002129 dnode = tsk_peer_node(tsk);
2130
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002131 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002132 skb = __skb_dequeue(&sk->sk_receive_queue);
2133 if (skb) {
2134 if (TIPC_SKB_CB(skb)->handle != NULL) {
2135 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002136 goto restart;
2137 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002138 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002139 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002140 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002141 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloycda36962015-07-22 10:11:20 -04002142 0, dnode, onode, dport, oport,
2143 TIPC_CONN_SHUTDOWN);
Vegard Nossumd2fbdf72016-07-23 08:15:04 +02002144 if (skb)
2145 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002146 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002147 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002148 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002149 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 /* fall through */
2151
2152 case SS_DISCONNECTING:
2153
Ying Xue75031152012-10-29 09:38:15 -04002154 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002155 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002156
2157 /* Wake up anyone sleeping in poll */
2158 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002159 res = 0;
2160 break;
2161
2162 default:
2163 res = -ENOTCONN;
2164 }
2165
Allan Stephens0c3141e2008-04-15 00:22:02 -07002166 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002167 return res;
2168}
2169
Ying Xuef2f2a962015-01-09 15:27:02 +08002170static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002171{
Ying Xuef2f2a962015-01-09 15:27:02 +08002172 struct tipc_sock *tsk = (struct tipc_sock *)data;
2173 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002174 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002175 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002176 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002177
Jon Paul Maloy57289012014-08-22 18:09:09 -04002178 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002179 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002180 bh_unlock_sock(sk);
2181 goto exit;
2182 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002183 peer_port = tsk_peer_port(tsk);
2184 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002185
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002186 if (tsk->probing_state == TIPC_CONN_PROBING) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002187 if (!sock_owned_by_user(sk)) {
2188 sk->sk_socket->state = SS_DISCONNECTING;
2189 tsk->connected = 0;
2190 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2191 tsk_peer_port(tsk));
2192 sk->sk_state_change(sk);
2193 } else {
2194 /* Try again later */
2195 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2196 }
2197
Jon Paul Maloy57289012014-08-22 18:09:09 -04002198 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002199 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2200 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002201 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002202 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002203 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002204 }
2205 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002206 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002207 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002208exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002209 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002210}
2211
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002212static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002213 struct tipc_name_seq const *seq)
2214{
Ying Xuef2f98002015-01-09 15:27:05 +08002215 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002216 struct publication *publ;
2217 u32 key;
2218
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002219 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002220 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002221 key = tsk->portid + tsk->pub_count + 1;
2222 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002223 return -EADDRINUSE;
2224
Ying Xuef2f98002015-01-09 15:27:05 +08002225 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002226 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002227 if (unlikely(!publ))
2228 return -EINVAL;
2229
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002230 list_add(&publ->pport_list, &tsk->publications);
2231 tsk->pub_count++;
2232 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002233 return 0;
2234}
2235
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002236static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002237 struct tipc_name_seq const *seq)
2238{
Ying Xuef2f98002015-01-09 15:27:05 +08002239 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002240 struct publication *publ;
2241 struct publication *safe;
2242 int rc = -EINVAL;
2243
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002244 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002245 if (seq) {
2246 if (publ->scope != scope)
2247 continue;
2248 if (publ->type != seq->type)
2249 continue;
2250 if (publ->lower != seq->lower)
2251 continue;
2252 if (publ->upper != seq->upper)
2253 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002254 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002255 publ->ref, publ->key);
2256 rc = 0;
2257 break;
2258 }
Ying Xuef2f98002015-01-09 15:27:05 +08002259 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002260 publ->ref, publ->key);
2261 rc = 0;
2262 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002263 if (list_empty(&tsk->publications))
2264 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002265 return rc;
2266}
2267
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002268/* tipc_sk_reinit: set non-zero address in all existing sockets
2269 * when we go from standalone to network mode.
2270 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002271void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002272{
Ying Xuee05b31f2015-01-09 15:27:08 +08002273 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu44bc7ca2017-05-23 21:53:35 -04002274 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002275 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002276 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002277
Herbert Xu44bc7ca2017-05-23 21:53:35 -04002278 rhashtable_walk_enter(&tn->sk_rht, &iter);
2279
2280 do {
2281 tsk = ERR_PTR(rhashtable_walk_start(&iter));
Bob Peterson2185dba2017-08-23 10:43:02 -04002282 if (IS_ERR(tsk))
2283 goto walk_stop;
Herbert Xu44bc7ca2017-05-23 21:53:35 -04002284
2285 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Cong Wang8dd36f12018-12-10 11:49:55 -08002286 sock_hold(&tsk->sk);
2287 rhashtable_walk_stop(&iter);
2288 lock_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002289 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002290 msg_set_prevnode(msg, tn->own_addr);
2291 msg_set_orignode(msg, tn->own_addr);
Cong Wang8dd36f12018-12-10 11:49:55 -08002292 release_sock(&tsk->sk);
2293 rhashtable_walk_start(&iter);
2294 sock_put(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002295 }
Bob Peterson2185dba2017-08-23 10:43:02 -04002296walk_stop:
Herbert Xu44bc7ca2017-05-23 21:53:35 -04002297 rhashtable_walk_stop(&iter);
2298 } while (tsk == ERR_PTR(-EAGAIN));
Ying Xue07f6c4b2015-01-07 13:41:58 +08002299}
2300
Ying Xuee05b31f2015-01-09 15:27:08 +08002301static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002302{
Ying Xuee05b31f2015-01-09 15:27:08 +08002303 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002304 struct tipc_sock *tsk;
2305
2306 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002307 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002308 if (tsk)
2309 sock_hold(&tsk->sk);
2310 rcu_read_unlock();
2311
2312 return tsk;
2313}
2314
2315static int tipc_sk_insert(struct tipc_sock *tsk)
2316{
Ying Xuee05b31f2015-01-09 15:27:08 +08002317 struct sock *sk = &tsk->sk;
2318 struct net *net = sock_net(sk);
2319 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002320 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2321 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2322
2323 while (remaining--) {
2324 portid++;
2325 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2326 portid = TIPC_MIN_PORT;
2327 tsk->portid = portid;
2328 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002329 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2330 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002331 return 0;
2332 sock_put(&tsk->sk);
2333 }
2334
2335 return -1;
2336}
2337
2338static void tipc_sk_remove(struct tipc_sock *tsk)
2339{
2340 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002341 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002342
Herbert Xu6cca72892015-03-20 21:57:05 +11002343 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002344 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2345 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002346 }
2347}
2348
Herbert Xu6cca72892015-03-20 21:57:05 +11002349static const struct rhashtable_params tsk_rht_params = {
2350 .nelem_hint = 192,
2351 .head_offset = offsetof(struct tipc_sock, node),
2352 .key_offset = offsetof(struct tipc_sock, portid),
2353 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002354 .max_size = 1048576,
2355 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002356 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002357};
2358
Ying Xuee05b31f2015-01-09 15:27:08 +08002359int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002360{
Ying Xuee05b31f2015-01-09 15:27:08 +08002361 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002362
Herbert Xu6cca72892015-03-20 21:57:05 +11002363 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002364}
2365
Ying Xuee05b31f2015-01-09 15:27:08 +08002366void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002367{
Ying Xuee05b31f2015-01-09 15:27:08 +08002368 struct tipc_net *tn = net_generic(net, tipc_net_id);
2369
Ying Xue07f6c4b2015-01-07 13:41:58 +08002370 /* Wait for socket readers to complete */
2371 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002372
Ying Xuee05b31f2015-01-09 15:27:08 +08002373 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002374}
2375
2376/**
Ying Xue247f0f32014-02-18 16:06:46 +08002377 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002378 * @sock: socket structure
2379 * @lvl: option level
2380 * @opt: option identifier
2381 * @ov: pointer to new option value
2382 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002383 *
2384 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002385 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002386 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387 * Returns 0 on success, errno otherwise
2388 */
Ying Xue247f0f32014-02-18 16:06:46 +08002389static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2390 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002391{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002392 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002393 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002394 u32 value;
2395 int res;
2396
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002397 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2398 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002399 if (lvl != SOL_TIPC)
2400 return -ENOPROTOOPT;
2401 if (ol < sizeof(value))
2402 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002403 res = get_user(value, (u32 __user *)ov);
2404 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002405 return res;
2406
Allan Stephens0c3141e2008-04-15 00:22:02 -07002407 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002408
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409 switch (opt) {
2410 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002411 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002412 break;
2413 case TIPC_SRC_DROPPABLE:
2414 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002415 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002416 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002417 res = -ENOPROTOOPT;
2418 break;
2419 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002420 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002421 break;
2422 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002423 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002424 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002425 break;
2426 default:
2427 res = -EINVAL;
2428 }
2429
Allan Stephens0c3141e2008-04-15 00:22:02 -07002430 release_sock(sk);
2431
Per Lidenb97bf3f2006-01-02 19:04:38 +01002432 return res;
2433}
2434
2435/**
Ying Xue247f0f32014-02-18 16:06:46 +08002436 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437 * @sock: socket structure
2438 * @lvl: option level
2439 * @opt: option identifier
2440 * @ov: receptacle for option value
2441 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002442 *
2443 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002444 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002445 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002446 * Returns 0 on success, errno otherwise
2447 */
Ying Xue247f0f32014-02-18 16:06:46 +08002448static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2449 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002451 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002452 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002453 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002454 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002455 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002456
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002457 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2458 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002459 if (lvl != SOL_TIPC)
2460 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002461 res = get_user(len, ol);
2462 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002463 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464
Allan Stephens0c3141e2008-04-15 00:22:02 -07002465 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002466
2467 switch (opt) {
2468 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002469 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002470 break;
2471 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002472 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002473 break;
2474 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002475 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002476 break;
2477 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002478 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002479 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002480 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002481 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002482 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002483 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002484 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002485 value = skb_queue_len(&sk->sk_receive_queue);
2486 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002487 default:
2488 res = -EINVAL;
2489 }
2490
Allan Stephens0c3141e2008-04-15 00:22:02 -07002491 release_sock(sk);
2492
Paul Gortmaker25860c32010-12-31 18:59:31 +00002493 if (res)
2494 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002495
Paul Gortmaker25860c32010-12-31 18:59:31 +00002496 if (len < sizeof(value))
2497 return -EINVAL;
2498
2499 if (copy_to_user(ov, &value, sizeof(value)))
2500 return -EFAULT;
2501
2502 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002503}
2504
Ying Xuef2f98002015-01-09 15:27:05 +08002505static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002506{
Ying Xuef2f98002015-01-09 15:27:05 +08002507 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002508 struct tipc_sioc_ln_req lnr;
2509 void __user *argp = (void __user *)arg;
2510
2511 switch (cmd) {
2512 case SIOCGETLINKNAME:
2513 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2514 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002515 if (!tipc_node_get_linkname(sock_net(sk),
2516 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002517 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2518 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2519 return -EFAULT;
2520 return 0;
2521 }
2522 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002523 default:
2524 return -ENOIOCTLCMD;
2525 }
2526}
2527
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002528/* Protocol switches for the various types of TIPC sockets */
2529
Florian Westphalbca65ea2008-02-07 18:18:01 -08002530static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002531 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002532 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002533 .release = tipc_release,
2534 .bind = tipc_bind,
2535 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002536 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002537 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002538 .getname = tipc_getname,
2539 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002540 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002541 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002542 .shutdown = tipc_shutdown,
2543 .setsockopt = tipc_setsockopt,
2544 .getsockopt = tipc_getsockopt,
2545 .sendmsg = tipc_sendmsg,
2546 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002547 .mmap = sock_no_mmap,
2548 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002549};
2550
Florian Westphalbca65ea2008-02-07 18:18:01 -08002551static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002552 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002553 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002554 .release = tipc_release,
2555 .bind = tipc_bind,
2556 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002557 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002558 .accept = tipc_accept,
2559 .getname = tipc_getname,
2560 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002561 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002562 .listen = tipc_listen,
2563 .shutdown = tipc_shutdown,
2564 .setsockopt = tipc_setsockopt,
2565 .getsockopt = tipc_getsockopt,
2566 .sendmsg = tipc_send_packet,
2567 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002568 .mmap = sock_no_mmap,
2569 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002570};
2571
Florian Westphalbca65ea2008-02-07 18:18:01 -08002572static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002573 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002574 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002575 .release = tipc_release,
2576 .bind = tipc_bind,
2577 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002578 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002579 .accept = tipc_accept,
2580 .getname = tipc_getname,
2581 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002582 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002583 .listen = tipc_listen,
2584 .shutdown = tipc_shutdown,
2585 .setsockopt = tipc_setsockopt,
2586 .getsockopt = tipc_getsockopt,
2587 .sendmsg = tipc_send_stream,
2588 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002589 .mmap = sock_no_mmap,
2590 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002591};
2592
Florian Westphalbca65ea2008-02-07 18:18:01 -08002593static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002594 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002595 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002596 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002597};
2598
2599static struct proto tipc_proto = {
2600 .name = "TIPC",
2601 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002602 .obj_size = sizeof(struct tipc_sock),
2603 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002604};
2605
2606/**
Per Liden4323add2006-01-18 00:38:21 +01002607 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002608 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002609 * Returns 0 on success, errno otherwise
2610 */
Per Liden4323add2006-01-18 00:38:21 +01002611int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002612{
2613 int res;
2614
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002615 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002616 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002617 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002618 goto out;
2619 }
2620
2621 res = sock_register(&tipc_family_ops);
2622 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002623 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002624 proto_unregister(&tipc_proto);
2625 goto out;
2626 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002627 out:
2628 return res;
2629}
2630
2631/**
Per Liden4323add2006-01-18 00:38:21 +01002632 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002633 */
Per Liden4323add2006-01-18 00:38:21 +01002634void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002635{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002636 sock_unregister(tipc_family_ops.family);
2637 proto_unregister(&tipc_proto);
2638}
Richard Alpe34b78a122014-11-20 10:29:10 +01002639
2640/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002641static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002642{
2643 u32 peer_node;
2644 u32 peer_port;
2645 struct nlattr *nest;
2646
2647 peer_node = tsk_peer_node(tsk);
2648 peer_port = tsk_peer_port(tsk);
2649
2650 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2651
2652 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2653 goto msg_full;
2654 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2655 goto msg_full;
2656
2657 if (tsk->conn_type != 0) {
2658 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2659 goto msg_full;
2660 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2661 goto msg_full;
2662 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2663 goto msg_full;
2664 }
2665 nla_nest_end(skb, nest);
2666
2667 return 0;
2668
2669msg_full:
2670 nla_nest_cancel(skb, nest);
2671
2672 return -EMSGSIZE;
2673}
2674
2675/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002676static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2677 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002678{
2679 int err;
2680 void *hdr;
2681 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002682 struct net *net = sock_net(skb->sk);
2683 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002684
2685 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002686 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002687 if (!hdr)
2688 goto msg_cancel;
2689
2690 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2691 if (!attrs)
2692 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002693 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002694 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002695 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002696 goto attr_msg_cancel;
2697
2698 if (tsk->connected) {
2699 err = __tipc_nl_add_sk_con(skb, tsk);
2700 if (err)
2701 goto attr_msg_cancel;
2702 } else if (!list_empty(&tsk->publications)) {
2703 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2704 goto attr_msg_cancel;
2705 }
2706 nla_nest_end(skb, attrs);
2707 genlmsg_end(skb, hdr);
2708
2709 return 0;
2710
2711attr_msg_cancel:
2712 nla_nest_cancel(skb, attrs);
2713genlmsg_cancel:
2714 genlmsg_cancel(skb, hdr);
2715msg_cancel:
2716 return -EMSGSIZE;
2717}
2718
2719int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2720{
2721 int err;
2722 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002723 const struct bucket_table *tbl;
2724 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002725 struct net *net = sock_net(skb->sk);
2726 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002727 u32 tbl_id = cb->args[0];
2728 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002729
Ying Xue07f6c4b2015-01-07 13:41:58 +08002730 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002731 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002732 for (; tbl_id < tbl->size; tbl_id++) {
2733 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002734 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002735 if (prev_portid && prev_portid != tsk->portid) {
2736 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2737 continue;
2738 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002739
Richard Alped6e164e2015-01-16 12:30:40 +01002740 err = __tipc_nl_add_sk(skb, cb, tsk);
2741 if (err) {
2742 prev_portid = tsk->portid;
2743 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2744 goto out;
2745 }
2746 prev_portid = 0;
2747 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002748 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002749 }
Richard Alped6e164e2015-01-16 12:30:40 +01002750out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002751 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002752 cb->args[0] = tbl_id;
2753 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002754
2755 return skb->len;
2756}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002757
2758/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002759static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2760 struct netlink_callback *cb,
2761 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002762{
2763 void *hdr;
2764 struct nlattr *attrs;
2765
2766 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002767 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002768 if (!hdr)
2769 goto msg_cancel;
2770
2771 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2772 if (!attrs)
2773 goto genlmsg_cancel;
2774
2775 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2776 goto attr_msg_cancel;
2777 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2778 goto attr_msg_cancel;
2779 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2780 goto attr_msg_cancel;
2781 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2782 goto attr_msg_cancel;
2783
2784 nla_nest_end(skb, attrs);
2785 genlmsg_end(skb, hdr);
2786
2787 return 0;
2788
2789attr_msg_cancel:
2790 nla_nest_cancel(skb, attrs);
2791genlmsg_cancel:
2792 genlmsg_cancel(skb, hdr);
2793msg_cancel:
2794 return -EMSGSIZE;
2795}
2796
2797/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002798static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2799 struct netlink_callback *cb,
2800 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002801{
2802 int err;
2803 struct publication *p;
2804
2805 if (*last_publ) {
2806 list_for_each_entry(p, &tsk->publications, pport_list) {
2807 if (p->key == *last_publ)
2808 break;
2809 }
2810 if (p->key != *last_publ) {
2811 /* We never set seq or call nl_dump_check_consistent()
2812 * this means that setting prev_seq here will cause the
2813 * consistence check to fail in the netlink callback
2814 * handler. Resulting in the last NLMSG_DONE message
2815 * having the NLM_F_DUMP_INTR flag set.
2816 */
2817 cb->prev_seq = 1;
2818 *last_publ = 0;
2819 return -EPIPE;
2820 }
2821 } else {
2822 p = list_first_entry(&tsk->publications, struct publication,
2823 pport_list);
2824 }
2825
2826 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2827 err = __tipc_nl_add_sk_publ(skb, cb, p);
2828 if (err) {
2829 *last_publ = p->key;
2830 return err;
2831 }
2832 }
2833 *last_publ = 0;
2834
2835 return 0;
2836}
2837
2838int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2839{
2840 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002841 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002842 u32 last_publ = cb->args[1];
2843 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002844 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002845 struct tipc_sock *tsk;
2846
Ying Xue07f6c4b2015-01-07 13:41:58 +08002847 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002848 struct nlattr **attrs;
2849 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2850
2851 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2852 if (err)
2853 return err;
2854
Richard Alpe45e093a2016-05-16 11:14:54 +02002855 if (!attrs[TIPC_NLA_SOCK])
2856 return -EINVAL;
2857
Richard Alpe1a1a1432014-11-20 10:29:11 +01002858 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2859 attrs[TIPC_NLA_SOCK],
2860 tipc_nl_sock_policy);
2861 if (err)
2862 return err;
2863
2864 if (!sock[TIPC_NLA_SOCK_REF])
2865 return -EINVAL;
2866
Ying Xue07f6c4b2015-01-07 13:41:58 +08002867 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002868 }
2869
2870 if (done)
2871 return 0;
2872
Ying Xuee05b31f2015-01-09 15:27:08 +08002873 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002874 if (!tsk)
2875 return -EINVAL;
2876
2877 lock_sock(&tsk->sk);
2878 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2879 if (!err)
2880 done = 1;
2881 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002882 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002883
Ying Xue07f6c4b2015-01-07 13:41:58 +08002884 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002885 cb->args[1] = last_publ;
2886 cb->args[2] = done;
2887
2888 return skb->len;
2889}