blob: 3b8df510a80c3a77cb5dc3a87728b9c378cdf3ca [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010038#include <linux/sched/signal.h>
39
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020042#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050043#include "link.h"
Jon Paul Maloyc637c102015-02-05 08:36:41 -050044#include "name_distr.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040045#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040046#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010047#include "netlink.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040048
Ying Xue07f6c4b2015-01-07 13:41:58 +080049#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080050#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080051#define TIPC_FWD_MSG 1
Ying Xue07f6c4b2015-01-07 13:41:58 +080052#define TIPC_MAX_PORT 0xffffffff
53#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040054
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010055enum {
56 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010057 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010058 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010059 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +010060 TIPC_CONNECTING = TCP_SYN_SENT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010061};
62
Jon Paul Maloy301bae52014-08-22 18:09:20 -040063/**
64 * struct tipc_sock - TIPC socket structure
65 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040066 * @conn_type: TIPC type used when connection was established
67 * @conn_instance: TIPC instance used when connection was established
68 * @published: non-zero if port has one or more associated names
69 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080070 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040071 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy365ad352017-01-03 10:55:11 -050072 * #cong_links: list of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073 * @publications: list of publications for port
Jon Paul Maloy365ad352017-01-03 10:55:11 -050074 * @blocking_link: address of the congested link we are currently sleeping on
Jon Paul Maloy301bae52014-08-22 18:09:20 -040075 * @pub_count: total # of publications port has made during its lifetime
76 * @probing_state:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040077 * @conn_timeout: the time we can wait for an unresponded setup request
78 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
Jon Paul Maloy365ad352017-01-03 10:55:11 -050079 * @cong_link_cnt: number of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040080 * @sent_unacked: # messages sent by socket, and not yet acked by peer
81 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010082 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080083 * @node: hash table node
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -050084 * @mc_method: cookie for use between socket and broadcast layer
Ying Xue07f6c4b2015-01-07 13:41:58 +080085 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040086 */
87struct tipc_sock {
88 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040089 u32 conn_type;
90 u32 conn_instance;
91 int published;
92 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080093 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040094 struct tipc_msg phdr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -050095 struct list_head cong_links;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 struct list_head publications;
97 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040098 uint conn_timeout;
99 atomic_t dupl_rcvcnt;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100100 bool probe_unacked;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500101 u16 cong_link_cnt;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400102 u16 snt_unacked;
103 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400104 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400105 u16 rcv_unacked;
106 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100107 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800108 struct rhash_head node;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500109 struct tipc_mc_method mc_method;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800110 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400111};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400113static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400114static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800115static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800116static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800117static int tipc_release(struct socket *sock);
David Howellscdfbabf2017-03-09 08:09:05 +0000118static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
119 bool kern);
Ying Xuef2f2a962015-01-09 15:27:02 +0800120static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400121static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400122 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400123static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400124 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800125static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800126static int tipc_sk_insert(struct tipc_sock *tsk);
127static void tipc_sk_remove(struct tipc_sock *tsk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500128static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
Ying Xue39a02952015-03-02 15:37:47 +0800129static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130
Florian Westphalbca65ea2008-02-07 18:18:01 -0800131static const struct proto_ops packet_ops;
132static const struct proto_ops stream_ops;
133static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100135static const struct rhashtable_params tsk_rht_params;
136
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500137static u32 tsk_own_node(struct tipc_sock *tsk)
138{
139 return msg_prevnode(&tsk->phdr);
140}
141
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400142static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400143{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400144 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400145}
146
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400147static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400148{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400149 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400150}
151
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400152static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400153{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400154 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400155}
156
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400157static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400158{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400159 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400160}
161
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400162static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400163{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400164 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400165}
166
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400167static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400168{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400169 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400170}
171
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400172static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400173{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400174 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400175}
176
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400177static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400178{
179 if (imp > TIPC_CRITICAL_IMPORTANCE)
180 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400181 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400182 return 0;
183}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400184
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400185static struct tipc_sock *tipc_sk(const struct sock *sk)
186{
187 return container_of(sk, struct tipc_sock, sk);
188}
189
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400190static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400191{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500192 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400193}
194
195/* tsk_blocks(): translate a buffer size in bytes to number of
196 * advertisable blocks, taking into account the ratio truesize(len)/len
197 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
198 */
199static u16 tsk_adv_blocks(int len)
200{
201 return len / FLOWCTL_BLK_SZ / 4;
202}
203
204/* tsk_inc(): increment counter for sent or received data
205 * - If block based flow control is not supported by peer we
206 * fall back to message based ditto, incrementing the counter
207 */
208static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
209{
210 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
211 return ((msglen / FLOWCTL_BLK_SZ) + 1);
212 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400213}
214
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400216 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 *
218 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400220static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400222 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223}
224
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400225/* tipc_sk_respond() : send response message back to sender
226 */
227static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
228{
229 u32 selector;
230 u32 dnode;
231 u32 onode = tipc_own_addr(sock_net(sk));
232
233 if (!tipc_msg_reverse(onode, &skb, err))
234 return;
235
236 dnode = msg_destnode(buf_msg(skb));
237 selector = msg_origport(buf_msg(skb));
238 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
239}
240
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400242 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700243 *
244 * Caller must hold socket lock
245 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400246static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700247{
Ying Xuea6ca1092014-11-26 11:41:55 +0800248 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400250 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
251 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700252}
253
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100254static bool tipc_sk_connected(struct sock *sk)
255{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100256 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100257}
258
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100259/* tipc_sk_type_connectionless - check if the socket is datagram socket
260 * @sk: socket
261 *
262 * Returns true if connection less, false otherwise
263 */
264static bool tipc_sk_type_connectionless(struct sock *sk)
265{
266 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
267}
268
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400269/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400270 *
271 * Handles cases where the node's network address has changed from
272 * the default of <0.0.0> to its configured setting.
273 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400274static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400275{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100276 struct sock *sk = &tsk->sk;
277 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400278 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400279 u32 orig_node;
280 u32 peer_node;
281
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100282 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400283 return false;
284
285 if (unlikely(msg_origport(msg) != peer_port))
286 return false;
287
288 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400289 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400290
291 if (likely(orig_node == peer_node))
292 return true;
293
Ying Xue34747532015-01-09 15:27:10 +0800294 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400295 return true;
296
Ying Xue34747532015-01-09 15:27:10 +0800297 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400298 return true;
299
300 return false;
301}
302
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100303/* tipc_set_sk_state - set the sk_state of the socket
304 * @sk: socket
305 *
306 * Caller must hold socket lock
307 *
308 * Returns 0 on success, errno otherwise
309 */
310static int tipc_set_sk_state(struct sock *sk, int state)
311{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100312 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100313 int res = -EINVAL;
314
315 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100316 case TIPC_OPEN:
317 res = 0;
318 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100319 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100320 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100321 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100322 res = 0;
323 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100324 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100325 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100326 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100327 res = 0;
328 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100329 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100330 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100331 oldsk_state == TIPC_ESTABLISHED)
332 res = 0;
333 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100334 }
335
336 if (!res)
337 sk->sk_state = state;
338
339 return res;
340}
341
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500342static int tipc_sk_sock_err(struct socket *sock, long *timeout)
343{
344 struct sock *sk = sock->sk;
345 int err = sock_error(sk);
346 int typ = sock->type;
347
348 if (err)
349 return err;
350 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
351 if (sk->sk_state == TIPC_DISCONNECTING)
352 return -EPIPE;
353 else if (!tipc_sk_connected(sk))
354 return -ENOTCONN;
355 }
356 if (!*timeout)
357 return -EAGAIN;
358 if (signal_pending(current))
359 return sock_intr_errno(*timeout);
360
361 return 0;
362}
363
364#define tipc_wait_for_cond(sock_, timeout_, condition_) \
365({ \
366 int rc_ = 0; \
367 int done_ = 0; \
368 \
369 while (!(condition_) && !done_) { \
370 struct sock *sk_ = sock->sk; \
371 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
372 \
373 rc_ = tipc_sk_sock_err(sock_, timeout_); \
374 if (rc_) \
375 break; \
376 prepare_to_wait(sk_sleep(sk_), &wait_, \
377 TASK_INTERRUPTIBLE); \
378 done_ = sk_wait_event(sk_, timeout_, \
379 (condition_), &wait_); \
380 remove_wait_queue(sk_sleep(sk_), &wait_); \
381 } \
382 rc_; \
383})
384
Allan Stephens0c3141e2008-04-15 00:22:02 -0700385/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400386 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700387 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 * @sock: pre-allocated socket structure
389 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800390 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900391 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700392 * This routine creates additional data structures used by the TIPC socket,
393 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 *
395 * Returns 0 on success, errno otherwise
396 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400397static int tipc_sk_create(struct net *net, struct socket *sock,
398 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500400 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700401 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400403 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400404 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700405
406 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 if (unlikely(protocol != 0))
408 return -EPROTONOSUPPORT;
409
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 switch (sock->type) {
411 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700412 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 break;
414 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700415 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 break;
417 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700419 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 break;
Allan Stephens49978652006-06-25 23:47:18 -0700421 default:
Allan Stephens49978652006-06-25 23:47:18 -0700422 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 }
424
Allan Stephens0c3141e2008-04-15 00:22:02 -0700425 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500426 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700427 if (sk == NULL)
428 return -ENOMEM;
429
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400430 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400431 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400432 INIT_LIST_HEAD(&tsk->publications);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500433 INIT_LIST_HEAD(&tsk->cong_links);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400434 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500435 tn = net_generic(sock_net(sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436
Allan Stephens0c3141e2008-04-15 00:22:02 -0700437 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700438 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100440 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800441 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900442 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800443 return -EINVAL;
444 }
Herbert Xu40f9f432017-02-11 19:26:46 +0800445
446 /* Ensure tsk is visible before we read own_addr. */
447 smp_mb();
448
449 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
450 NAMED_H_SIZE, 0);
451
Ying Xue07f6c4b2015-01-07 13:41:58 +0800452 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800453 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100454 sk->sk_shutdown = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400455 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400456 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800457 sk->sk_data_ready = tipc_data_ready;
458 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800459 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400460 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
461 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700462
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400463 /* Start out with safe limits until we receive an advertised window */
464 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
465 tsk->rcv_win = tsk->snd_win;
466
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100467 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400468 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700469 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400470 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700471 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100472
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473 return 0;
474}
475
Ying Xue07f6c4b2015-01-07 13:41:58 +0800476static void tipc_sk_callback(struct rcu_head *head)
477{
478 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
479
480 sock_put(&tsk->sk);
481}
482
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100483/* Caller should hold socket lock for the socket. */
484static void __tipc_shutdown(struct socket *sock, int error)
485{
486 struct sock *sk = sock->sk;
487 struct tipc_sock *tsk = tipc_sk(sk);
488 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500489 long timeout = CONN_TIMEOUT_DEFAULT;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100490 u32 dnode = tsk_peer_node(tsk);
491 struct sk_buff *skb;
492
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500493 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
494 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
495 !tsk_conn_cong(tsk)));
496
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100497 /* Reject all unreceived messages, except on an active connection
498 * (which disconnects locally & sends a 'FIN+' to peer).
499 */
500 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
501 if (TIPC_SKB_CB(skb)->bytes_read) {
502 kfree_skb(skb);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500503 continue;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100504 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500505 if (!tipc_sk_type_connectionless(sk) &&
506 sk->sk_state != TIPC_DISCONNECTING) {
507 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
508 tipc_node_remove_conn(net, dnode, tsk->portid);
509 }
510 tipc_sk_respond(sk, skb, error);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100511 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500512
513 if (tipc_sk_type_connectionless(sk))
514 return;
515
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100516 if (sk->sk_state != TIPC_DISCONNECTING) {
517 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
518 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
519 tsk_own_node(tsk), tsk_peer_port(tsk),
520 tsk->portid, error);
521 if (skb)
522 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500523 tipc_node_remove_conn(net, dnode, tsk->portid);
524 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100525 }
526}
527
Ying Xuec5fa7b32013-06-17 10:54:39 -0400528/**
Ying Xue247f0f32014-02-18 16:06:46 +0800529 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 * @sock: socket to destroy
531 *
532 * This routine cleans up any messages that are still queued on the socket.
533 * For DGRAM and RDM socket types, all queued messages are rejected.
534 * For SEQPACKET and STREAM socket types, the first message is rejected
535 * and any others are discarded. (If the first message on a STREAM socket
536 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900537 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 * NOTE: Rejected messages are not necessarily returned to the sender! They
539 * are returned or discarded according to the "destination droppable" setting
540 * specified for the message by the sender.
541 *
542 * Returns 0 on success, errno otherwise
543 */
Ying Xue247f0f32014-02-18 16:06:46 +0800544static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400547 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548
Allan Stephens0c3141e2008-04-15 00:22:02 -0700549 /*
550 * Exit if socket isn't fully initialized (occurs when a failed accept()
551 * releases a pre-allocated child socket that was never used)
552 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700553 if (sk == NULL)
554 return 0;
555
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400556 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700557 lock_sock(sk);
558
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100559 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
560 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400561 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800562 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800563 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564
Allan Stephens0c3141e2008-04-15 00:22:02 -0700565 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700566 release_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500567 u32_list_purge(&tsk->cong_links);
568 tsk->cong_link_cnt = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800569 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700570 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200572 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573}
574
575/**
Ying Xue247f0f32014-02-18 16:06:46 +0800576 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 * @sock: socket structure
578 * @uaddr: socket address describing name(s) and desired operation
579 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900580 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 * Name and name sequence binding is indicated using a positive scope value;
582 * a negative scope value unbinds the specified name. Specifying no name
583 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900584 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700586 *
587 * NOTE: This routine doesn't need to take the socket lock since it doesn't
588 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 */
Ying Xue247f0f32014-02-18 16:06:46 +0800590static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
591 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592{
Ying Xue84602762013-12-27 10:18:28 +0800593 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400595 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800596 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597
Ying Xue84602762013-12-27 10:18:28 +0800598 lock_sock(sk);
599 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400600 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800601 goto exit;
602 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603
Ying Xue84602762013-12-27 10:18:28 +0800604 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
605 res = -EINVAL;
606 goto exit;
607 }
608 if (addr->family != AF_TIPC) {
609 res = -EAFNOSUPPORT;
610 goto exit;
611 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 if (addr->addrtype == TIPC_ADDR_NAME)
614 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800615 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
616 res = -EAFNOSUPPORT;
617 goto exit;
618 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900619
Ying Xue13a2e892013-06-17 10:54:40 -0400620 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400621 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800622 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
623 res = -EACCES;
624 goto exit;
625 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400626
Ying Xue84602762013-12-27 10:18:28 +0800627 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400628 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
629 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800630exit:
631 release_sock(sk);
632 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633}
634
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900635/**
Ying Xue247f0f32014-02-18 16:06:46 +0800636 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 * @sock: socket structure
638 * @uaddr: area for returned socket address
639 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700640 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700643 *
Allan Stephens2da59912008-07-14 22:43:32 -0700644 * NOTE: This routine doesn't need to take the socket lock since it only
645 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000646 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 */
Ying Xue247f0f32014-02-18 16:06:46 +0800648static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
649 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100652 struct sock *sk = sock->sk;
653 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +0800654 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000656 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700657 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100658 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100659 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700660 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400661 addr->addr.id.ref = tsk_peer_port(tsk);
662 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800664 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800665 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700666 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667
668 *uaddr_len = sizeof(*addr);
669 addr->addrtype = TIPC_ADDR_ID;
670 addr->family = AF_TIPC;
671 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 addr->addr.name.domain = 0;
673
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675}
676
677/**
Ying Xue247f0f32014-02-18 16:06:46 +0800678 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 * @file: file structure associated with the socket
680 * @sock: socket for which to calculate the poll bits
681 * @wait: ???
682 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700683 * Returns pollmask value
684 *
685 * COMMENTARY:
686 * It appears that the usual socket locking mechanisms are not useful here
687 * since the pollmask info is potentially out-of-date the moment this routine
688 * exits. TCP and other protocols seem to rely on higher level poll routines
689 * to handle any preventable race conditions, so TIPC will do the same ...
690 *
Allan Stephensf662c072010-08-17 11:00:06 +0000691 * IMPORTANT: The fact that a read or write operation is indicated does NOT
692 * imply that the operation will succeed, merely that it should be performed
693 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694 */
Ying Xue247f0f32014-02-18 16:06:46 +0800695static unsigned int tipc_poll(struct file *file, struct socket *sock,
696 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697{
Allan Stephens9b674e82008-03-26 16:48:21 -0700698 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400699 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000700 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700701
Ying Xuef288bef2012-08-21 11:16:57 +0800702 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700703
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100704 if (sk->sk_shutdown & RCV_SHUTDOWN)
705 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
706 if (sk->sk_shutdown == SHUTDOWN_MASK)
707 mask |= POLLHUP;
708
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100709 switch (sk->sk_state) {
710 case TIPC_ESTABLISHED:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500711 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000712 mask |= POLLOUT;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100713 /* fall thru' */
714 case TIPC_LISTEN:
715 case TIPC_CONNECTING:
Allan Stephensf662c072010-08-17 11:00:06 +0000716 if (!skb_queue_empty(&sk->sk_receive_queue))
717 mask |= (POLLIN | POLLRDNORM);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100718 break;
719 case TIPC_OPEN:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500720 if (!tsk->cong_link_cnt)
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100721 mask |= POLLOUT;
722 if (tipc_sk_type_connectionless(sk) &&
723 (!skb_queue_empty(&sk->sk_receive_queue)))
724 mask |= (POLLIN | POLLRDNORM);
725 break;
726 case TIPC_DISCONNECTING:
727 mask = (POLLIN | POLLRDNORM | POLLHUP);
728 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000729 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700730
731 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732}
733
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400734/**
735 * tipc_sendmcast - send multicast message
736 * @sock: socket structure
737 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500738 * @msg: message to send
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500739 * @dlen: length of data to send
740 * @timeout: timeout to wait for wakeup
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400741 *
742 * Called from function tipc_sendmsg(), which has done all sanity checks
743 * Returns the number of bytes sent on success, or errno
744 */
745static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500746 struct msghdr *msg, size_t dlen, long timeout)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400747{
748 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500749 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500750 struct tipc_msg *hdr = &tsk->phdr;
Ying Xuef2f98002015-01-09 15:27:05 +0800751 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500752 int mtu = tipc_bcast_get_mtu(net);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500753 struct tipc_mc_method *method = &tsk->mc_method;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500754 u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500755 struct sk_buff_head pkts;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500756 struct tipc_nlist dsts;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400757 int rc;
758
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500759 /* Block or return if any destination link is congested */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500760 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
761 if (unlikely(rc))
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400762 return rc;
763
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500764 /* Lookup destination nodes */
765 tipc_nlist_init(&dsts, tipc_own_addr(net));
766 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
767 seq->upper, domain, &dsts);
768 if (!dsts.local && !dsts.remote)
769 return -EHOSTUNREACH;
770
771 /* Build message header */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500772 msg_set_type(hdr, TIPC_MCAST_MSG);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500773 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500774 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
775 msg_set_destport(hdr, 0);
776 msg_set_destnode(hdr, 0);
777 msg_set_nametype(hdr, seq->type);
778 msg_set_namelower(hdr, seq->lower);
779 msg_set_nameupper(hdr, seq->upper);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400780
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500781 /* Build message as chain of buffers */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500782 skb_queue_head_init(&pkts);
783 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500784
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500785 /* Send message if build was successful */
786 if (unlikely(rc == dlen))
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500787 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500788 &tsk->cong_link_cnt);
789
790 tipc_nlist_purge(&dsts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500791
792 return rc ? rc : dlen;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400793}
794
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500795/**
796 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
797 * @arrvq: queue with arriving messages, to be cloned after destination lookup
798 * @inputq: queue with cloned messages, delivered to socket after dest lookup
799 *
800 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400801 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500802void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
803 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400804{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500805 struct tipc_msg *msg;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500806 struct list_head dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500807 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400808 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500809 struct sk_buff_head tmpq;
810 uint hsz;
811 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500812
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500813 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500814 INIT_LIST_HEAD(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400815
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500816 skb = tipc_skb_peek(arrvq, &inputq->lock);
817 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
818 msg = buf_msg(skb);
819 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400820
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500821 if (in_own_node(net, msg_orignode(msg)))
822 scope = TIPC_NODE_SCOPE;
823
824 /* Create destination port list and message clones: */
825 tipc_nametbl_mc_translate(net,
826 msg_nametype(msg), msg_namelower(msg),
827 msg_nameupper(msg), scope, &dports);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500828 portid = u32_pop(&dports);
829 for (; portid; portid = u32_pop(&dports)) {
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500830 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
831 if (_skb) {
832 msg_set_destport(buf_msg(_skb), portid);
833 __skb_queue_tail(&tmpq, _skb);
834 continue;
835 }
836 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400837 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500838 /* Append to inputq if not already done by other thread */
839 spin_lock_bh(&inputq->lock);
840 if (skb_peek(arrvq) == skb) {
841 skb_queue_splice_tail_init(&tmpq, inputq);
842 kfree_skb(__skb_dequeue(arrvq));
843 }
844 spin_unlock_bh(&inputq->lock);
845 __skb_queue_purge(&tmpq);
846 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400847 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500848 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400849}
850
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900851/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500852 * tipc_sk_proto_rcv - receive a connection mng protocol message
853 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400854 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500855 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400856static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
857 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500858{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400859 struct sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400860 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400861 struct tipc_msg *hdr = buf_msg(skb);
862 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400863 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400864
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500865 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400866 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500867 goto exit;
868
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100869 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500870
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400871 if (mtyp == CONN_PROBE) {
872 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400873 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
874 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400875 return;
876 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400877 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400878 tsk->snt_unacked -= msg_conn_ack(hdr);
879 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
880 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500881 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400882 sk->sk_write_space(sk);
883 } else if (mtyp != CONN_PROBE_REPLY) {
884 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500885 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500886exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400887 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500888}
889
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500890/**
Ying Xue247f0f32014-02-18 16:06:46 +0800891 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 * @sock: socket structure
893 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500894 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900895 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900897 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
899 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900900 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 * Returns the number of bytes sent on success, or errno otherwise
902 */
Ying Xue1b784142015-03-02 15:37:48 +0800903static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500904 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905{
Ying Xue39a02952015-03-02 15:37:47 +0800906 struct sock *sk = sock->sk;
907 int ret;
908
909 lock_sock(sk);
910 ret = __tipc_sendmsg(sock, m, dsz);
911 release_sock(sk);
912
913 return ret;
914}
915
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500916static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +0800917{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700918 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800919 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500920 struct tipc_sock *tsk = tipc_sk(sk);
921 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
922 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
923 struct list_head *clinks = &tsk->cong_links;
924 bool syn = !tipc_sk_type_connectionless(sk);
925 struct tipc_msg *hdr = &tsk->phdr;
Erik Hugnef2f80362015-03-19 09:02:19 +0100926 struct tipc_name_seq *seq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500927 struct sk_buff_head pkts;
928 u32 type, inst, domain;
929 u32 dnode, dport;
930 int mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500932 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400933 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500934
Erik Hugnef2f80362015-03-19 09:02:19 +0100935 if (unlikely(!dest)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500936 dest = &tsk->peer;
937 if (!syn || dest->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +0100938 return -EDESTADDRREQ;
Erik Hugnef2f80362015-03-19 09:02:19 +0100939 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500940
941 if (unlikely(m->msg_namelen < sizeof(*dest)))
942 return -EINVAL;
943
944 if (unlikely(dest->family != AF_TIPC))
945 return -EINVAL;
946
947 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100948 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a02952015-03-02 15:37:47 +0800949 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100950 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a02952015-03-02 15:37:47 +0800951 return -EISCONN;
952 if (tsk->published)
953 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700954 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400955 tsk->conn_type = dest->addr.name.name.type;
956 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700957 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500959
Erik Hugnef2f80362015-03-19 09:02:19 +0100960 seq = &dest->addr.nameseq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500961 if (dest->addrtype == TIPC_ADDR_MCAST)
962 return tipc_sendmcast(sock, seq, m, dlen, timeout);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500963
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500964 if (dest->addrtype == TIPC_ADDR_NAME) {
965 type = dest->addr.name.name.type;
966 inst = dest->addr.name.name.instance;
967 domain = dest->addr.name.domain;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500968 dnode = domain;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500969 msg_set_type(hdr, TIPC_NAMED_MSG);
970 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
971 msg_set_nametype(hdr, type);
972 msg_set_nameinst(hdr, inst);
973 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800974 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500975 msg_set_destnode(hdr, dnode);
976 msg_set_destport(hdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800977 if (unlikely(!dport && !dnode))
978 return -EHOSTUNREACH;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500979
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500980 } else if (dest->addrtype == TIPC_ADDR_ID) {
981 dnode = dest->addr.id.node;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500982 msg_set_type(hdr, TIPC_DIRECT_MSG);
983 msg_set_lookup_scope(hdr, 0);
984 msg_set_destnode(hdr, dnode);
985 msg_set_destport(hdr, dest->addr.id.ref);
986 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500987 }
988
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500989 /* Block or return if destination link is congested */
990 rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode));
991 if (unlikely(rc))
Ying Xue39a02952015-03-02 15:37:47 +0800992 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500993
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500994 skb_queue_head_init(&pkts);
995 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
996 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
997 if (unlikely(rc != dlen))
998 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500999
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001000 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1001 if (unlikely(rc == -ELINKCONG)) {
1002 u32_push(clinks, dnode);
1003 tsk->cong_link_cnt++;
1004 rc = 0;
1005 }
1006
1007 if (unlikely(syn && !rc))
1008 tipc_set_sk_state(sk, TIPC_CONNECTING);
1009
1010 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011}
1012
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001013/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001014 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 * @sock: socket structure
1016 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001017 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001018 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001020 *
1021 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001022 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001024static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001026 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +08001027 int ret;
1028
1029 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001030 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a02952015-03-02 15:37:47 +08001031 release_sock(sk);
1032
1033 return ret;
1034}
1035
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001036static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +08001037{
1038 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001039 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001040 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1041 struct tipc_sock *tsk = tipc_sk(sk);
1042 struct tipc_msg *hdr = &tsk->phdr;
1043 struct net *net = sock_net(sk);
1044 struct sk_buff_head pkts;
1045 u32 dnode = tsk_peer_node(tsk);
1046 int send, sent = 0;
1047 int rc = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001048
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001049 skb_queue_head_init(&pkts);
1050
1051 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001052 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001053
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001054 /* Handle implicit connection setup */
1055 if (unlikely(dest)) {
1056 rc = __tipc_sendmsg(sock, m, dlen);
1057 if (dlen && (dlen == rc))
1058 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1059 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001060 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001062 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001063 rc = tipc_wait_for_cond(sock, &timeout,
1064 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001065 !tsk_conn_cong(tsk) &&
1066 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001067 if (unlikely(rc))
1068 break;
Ying Xue39a02952015-03-02 15:37:47 +08001069
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001070 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1071 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1072 if (unlikely(rc != send))
1073 break;
1074
1075 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1076 if (unlikely(rc == -ELINKCONG)) {
1077 tsk->cong_link_cnt = 1;
1078 rc = 0;
1079 }
1080 if (likely(!rc)) {
1081 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1082 sent += send;
1083 }
1084 } while (sent < dlen && !rc);
1085
Parthasarathy Bhuvaragan3364d612017-04-24 15:00:42 +02001086 return sent ? sent : rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001087}
1088
1089/**
1090 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001091 * @sock: socket structure
1092 * @m: message to send
1093 * @dsz: length of data to be transmitted
1094 *
1095 * Used for SOCK_SEQPACKET messages.
1096 *
1097 * Returns the number of bytes sent on success, or errno otherwise
1098 */
Ying Xue1b784142015-03-02 15:37:48 +08001099static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001100{
1101 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1102 return -EMSGSIZE;
1103
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001104 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105}
1106
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001107/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001109static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001110 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111{
Ying Xue3721e9c2015-01-13 17:07:48 +08001112 struct sock *sk = &tsk->sk;
1113 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001114 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001116 msg_set_destnode(msg, peer_node);
1117 msg_set_destport(msg, peer_port);
1118 msg_set_type(msg, TIPC_CONN_MSG);
1119 msg_set_lookup_scope(msg, 0);
1120 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001121
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01001122 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001123 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001124 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1125 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001126 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001127 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1128 return;
1129
1130 /* Fall back to message based flow control */
1131 tsk->rcv_win = FLOWCTL_MSG_WIN;
1132 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133}
1134
1135/**
1136 * set_orig_addr - capture sender's address for received message
1137 * @m: descriptor for message info
1138 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001139 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 * Note: Address is not captured if not requested by receiver.
1141 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001142static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001144 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001146 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 addr->family = AF_TIPC;
1148 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001149 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 addr->addr.id.ref = msg_origport(msg);
1151 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001152 addr->addr.name.domain = 0; /* could leave uninitialized */
1153 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 m->msg_namelen = sizeof(struct sockaddr_tipc);
1155 }
1156}
1157
1158/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001159 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 * @m: descriptor for message info
1161 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001162 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001163 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001165 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 * Returns 0 if successful, otherwise errno
1167 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001168static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1169 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170{
1171 u32 anc_data[3];
1172 u32 err;
1173 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001174 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 int res;
1176
1177 if (likely(m->msg_controllen == 0))
1178 return 0;
1179
1180 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 err = msg ? msg_errcode(msg) : 0;
1182 if (unlikely(err)) {
1183 anc_data[0] = err;
1184 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001185 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1186 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001188 if (anc_data[1]) {
1189 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1190 msg_data(msg));
1191 if (res)
1192 return res;
1193 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 }
1195
1196 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1198 switch (dest_type) {
1199 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001200 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 anc_data[0] = msg_nametype(msg);
1202 anc_data[1] = msg_namelower(msg);
1203 anc_data[2] = msg_namelower(msg);
1204 break;
1205 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001206 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 anc_data[0] = msg_nametype(msg);
1208 anc_data[1] = msg_namelower(msg);
1209 anc_data[2] = msg_nameupper(msg);
1210 break;
1211 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001212 has_name = (tsk->conn_type != 0);
1213 anc_data[0] = tsk->conn_type;
1214 anc_data[1] = tsk->conn_instance;
1215 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 break;
1217 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001218 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 }
Allan Stephens2db99832010-12-31 18:59:33 +00001220 if (has_name) {
1221 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1222 if (res)
1223 return res;
1224 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225
1226 return 0;
1227}
1228
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001229static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001230{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001231 struct sock *sk = &tsk->sk;
1232 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001233 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001234 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001235 u32 peer_port = tsk_peer_port(tsk);
1236 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001237
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001238 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001239 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001240 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1241 dnode, tsk_own_node(tsk), peer_port,
1242 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001243 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001244 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001245 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001246 msg_set_conn_ack(msg, tsk->rcv_unacked);
1247 tsk->rcv_unacked = 0;
1248
1249 /* Adjust to and advertize the correct window limit */
1250 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1251 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1252 msg_set_adv_win(msg, tsk->rcv_win);
1253 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001254 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001255}
1256
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001257static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001258{
1259 struct sock *sk = sock->sk;
1260 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001261 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001262 int err;
1263
1264 for (;;) {
1265 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001266 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001267 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001268 err = -ENOTCONN;
1269 break;
1270 }
1271 release_sock(sk);
1272 timeo = schedule_timeout(timeo);
1273 lock_sock(sk);
1274 }
1275 err = 0;
1276 if (!skb_queue_empty(&sk->sk_receive_queue))
1277 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001278 err = -EAGAIN;
1279 if (!timeo)
1280 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001281 err = sock_intr_errno(timeo);
1282 if (signal_pending(current))
1283 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001284 }
1285 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001286 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001287 return err;
1288}
1289
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001290/**
Ying Xue247f0f32014-02-18 16:06:46 +08001291 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 * @m: descriptor for message info
1293 * @buf_len: total size of user buffer area
1294 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001295 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1297 * If the complete message doesn't fit in user area, truncate it.
1298 *
1299 * Returns size of returned message data, errno otherwise
1300 */
Ying Xue1b784142015-03-02 15:37:48 +08001301static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1302 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001304 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001305 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 struct sk_buff *buf;
1307 struct tipc_msg *msg;
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001308 bool is_connectionless = tipc_sk_type_connectionless(sk);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001309 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 unsigned int sz;
1311 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001312 int res, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313
Allan Stephens0c3141e2008-04-15 00:22:02 -07001314 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 if (unlikely(!buf_len))
1316 return -EINVAL;
1317
Allan Stephens0c3141e2008-04-15 00:22:02 -07001318 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001320 if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321 res = -ENOTCONN;
1322 goto exit;
1323 }
1324
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001325 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001326restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327
Allan Stephens0c3141e2008-04-15 00:22:02 -07001328 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001329 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001330 if (res)
1331 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001332
1333 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001334 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 msg = buf_msg(buf);
1336 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001337 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 err = msg_errcode(msg);
1339
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001342 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 goto restart;
1344 }
1345
1346 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 set_orig_addr(m, msg);
1348
1349 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001350 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001351 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 goto exit;
1353
1354 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 if (!err) {
1356 if (unlikely(buf_len < sz)) {
1357 sz = buf_len;
1358 m->msg_flags |= MSG_TRUNC;
1359 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001360 res = skb_copy_datagram_msg(buf, hlen, m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001361 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 res = sz;
1364 } else {
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001365 if (is_connectionless || err == TIPC_CONN_SHUTDOWN ||
1366 m->msg_control)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 res = 0;
1368 else
1369 res = -ECONNRESET;
1370 }
1371
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001372 if (unlikely(flags & MSG_PEEK))
1373 goto exit;
1374
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001375 if (likely(!is_connectionless)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001376 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1377 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1378 tipc_sk_send_ack(tsk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001379 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001380 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001382 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 return res;
1384}
1385
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001386/**
Ying Xue247f0f32014-02-18 16:06:46 +08001387 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 * @m: descriptor for message info
1389 * @buf_len: total size of user buffer area
1390 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001391 *
1392 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 * will optionally wait for more; never truncates data.
1394 *
1395 * Returns size of returned message data, errno otherwise
1396 */
Ying Xue1b784142015-03-02 15:37:48 +08001397static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1398 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001400 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001401 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 struct sk_buff *buf;
1403 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001404 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405 unsigned int sz;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001406 int target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001409 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410
1411 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412 if (unlikely(!buf_len))
1413 return -EINVAL;
1414
Allan Stephens0c3141e2008-04-15 00:22:02 -07001415 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001417 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 res = -ENOTCONN;
1419 goto exit;
1420 }
1421
Florian Westphal3720d402010-08-17 11:00:04 +00001422 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001423 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001424
Allan Stephens0c3141e2008-04-15 00:22:02 -07001425restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001426 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001427 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001428 if (res)
1429 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001430
1431 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001432 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 msg = buf_msg(buf);
1434 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001435 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 err = msg_errcode(msg);
1437
1438 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001440 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441 goto restart;
1442 }
1443
1444 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445 if (sz_copied == 0) {
1446 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001447 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001448 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 goto exit;
1450 }
1451
1452 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453 if (!err) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001454 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1455 u32 needed;
1456 int sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457
Allan Stephens0232fd02011-02-21 09:45:40 -05001458 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 needed = (buf_len - sz_copied);
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001460 sz_to_copy = min(sz, needed);
Allan Stephens0232fd02011-02-21 09:45:40 -05001461
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001462 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001463 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001465
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 sz_copied += sz_to_copy;
1467
1468 if (sz_to_copy < sz) {
1469 if (!(flags & MSG_PEEK))
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001470 TIPC_SKB_CB(buf)->bytes_read =
1471 offset + sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472 goto exit;
1473 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474 } else {
1475 if (sz_copied != 0)
1476 goto exit; /* can't add error msg to valid data */
1477
1478 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1479 res = 0;
1480 else
1481 res = -ECONNRESET;
1482 }
1483
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001484 if (unlikely(flags & MSG_PEEK))
1485 goto exit;
1486
Parthasarathy Bhuvaragan05ff8372017-04-24 15:00:43 +02001487 tsk->rcv_unacked += tsk_inc(tsk, hlen + msg_data_sz(msg));
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001488 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1489 tipc_sk_send_ack(tsk);
1490 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491
1492 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001493 if ((sz_copied < buf_len) && /* didn't get all requested data */
1494 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001495 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001496 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 goto restart;
1498
1499exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001500 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001501 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502}
1503
1504/**
Ying Xuef288bef2012-08-21 11:16:57 +08001505 * tipc_write_space - wake up thread if port congestion is released
1506 * @sk: socket
1507 */
1508static void tipc_write_space(struct sock *sk)
1509{
1510 struct socket_wq *wq;
1511
1512 rcu_read_lock();
1513 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001514 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001515 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1516 POLLWRNORM | POLLWRBAND);
1517 rcu_read_unlock();
1518}
1519
1520/**
1521 * tipc_data_ready - wake up threads to indicate messages have been received
1522 * @sk: socket
1523 * @len: the length of messages
1524 */
David S. Miller676d2362014-04-11 16:15:36 -04001525static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001526{
1527 struct socket_wq *wq;
1528
1529 rcu_read_lock();
1530 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001531 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001532 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1533 POLLRDNORM | POLLRDBAND);
1534 rcu_read_unlock();
1535}
1536
Ying Xuef4195d12015-11-22 15:46:05 +08001537static void tipc_sock_destruct(struct sock *sk)
1538{
1539 __skb_queue_purge(&sk->sk_receive_queue);
1540}
1541
Ying Xuef288bef2012-08-21 11:16:57 +08001542/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001543 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001544 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001545 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001546 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001547 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001548 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001549static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001550{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001551 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001552 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001553 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001554
Jon Paul Maloycda36962015-07-22 10:11:20 -04001555 if (unlikely(msg_mcast(hdr)))
1556 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001557
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001558 switch (sk->sk_state) {
1559 case TIPC_CONNECTING:
Ying Xue7e6c1312012-11-29 18:39:14 -05001560 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001561 if (unlikely(!msg_connected(hdr)))
1562 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001563
Jon Paul Maloycda36962015-07-22 10:11:20 -04001564 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001565 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugne2c8d8512013-08-28 09:29:58 +02001566 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001567 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001568 }
1569
Jon Paul Maloycda36962015-07-22 10:11:20 -04001570 if (unlikely(!msg_isdata(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001571 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001572 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001573 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001574 }
1575
Jon Paul Maloycda36962015-07-22 10:11:20 -04001576 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1577 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001578
Jon Paul Maloycda36962015-07-22 10:11:20 -04001579 /* If 'ACK+' message, add to socket receive queue */
1580 if (msg_data_sz(hdr))
1581 return true;
1582
1583 /* If empty 'ACK-' message, wake up sleeping connect() */
Parthasarathy Bhuvaragan42b531d2017-04-26 10:05:00 +02001584 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001585
1586 /* 'ACK-' message is neither accepted nor rejected: */
1587 msg_set_dest_droppable(hdr, 1);
1588 return false;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001589
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001590 case TIPC_OPEN:
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001591 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001592 break;
1593 case TIPC_LISTEN:
Ying Xue7e6c1312012-11-29 18:39:14 -05001594 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001595 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1596 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001597 break;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001598 case TIPC_ESTABLISHED:
1599 /* Accept only connection-based messages sent by peer */
1600 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1601 return false;
1602
1603 if (unlikely(msg_errcode(hdr))) {
1604 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1605 /* Let timer expire on it's own */
1606 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1607 tsk->portid);
1608 sk->sk_state_change(sk);
1609 }
1610 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001611 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001612 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05001613 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001614
Jon Paul Maloycda36962015-07-22 10:11:20 -04001615 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001616}
1617
1618/**
Ying Xueaba79f32013-01-20 23:30:09 +01001619 * rcvbuf_limit - get proper overload limit of socket receive queue
1620 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001621 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001622 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001623 * For connection oriented messages, irrespective of importance,
1624 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001625 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001626 * For connectionless messages, queue limits are based on message
1627 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001628 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001629 * TIPC_LOW_IMPORTANCE (2 MB)
1630 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1631 * TIPC_HIGH_IMPORTANCE (8 MB)
1632 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001633 *
1634 * Returns overload limit according to corresponding message importance
1635 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001636static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001637{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001638 struct tipc_sock *tsk = tipc_sk(sk);
1639 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001640
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001641 if (unlikely(!msg_connected(hdr)))
1642 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001643
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001644 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1645 return sk->sk_rcvbuf;
1646
1647 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001648}
1649
1650/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001651 * filter_rcv - validate incoming message
1652 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001653 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001654 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001655 * Enqueues message on receive queue if acceptable; optionally handles
1656 * disconnect indication for a connected socket.
1657 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001658 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001659 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001660 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001662static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1663 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001664{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001665 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001666 struct tipc_msg *hdr = buf_msg(skb);
1667 unsigned int limit = rcvbuf_limit(sk, skb);
1668 int err = TIPC_OK;
1669 int usr = msg_user(hdr);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001670 u32 onode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671
Jon Paul Maloycda36962015-07-22 10:11:20 -04001672 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001673 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001674 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001675 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001676
Jon Paul Maloycda36962015-07-22 10:11:20 -04001677 if (unlikely(usr == SOCK_WAKEUP)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001678 onode = msg_orignode(hdr);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001679 kfree_skb(skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001680 u32_del(&tsk->cong_links, onode);
1681 tsk->cong_link_cnt--;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001682 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001683 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001684 }
1685
Jon Paul Maloycda36962015-07-22 10:11:20 -04001686 /* Drop if illegal message type */
1687 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1688 kfree_skb(skb);
1689 return false;
1690 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001691
Jon Paul Maloycda36962015-07-22 10:11:20 -04001692 /* Reject if wrong message type for current socket state */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001693 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001694 if (msg_connected(hdr)) {
1695 err = TIPC_ERR_NO_PORT;
1696 goto reject;
1697 }
1698 } else if (unlikely(!filter_connect(tsk, skb))) {
1699 err = TIPC_ERR_NO_PORT;
1700 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 }
1702
1703 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001704 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1705 err = TIPC_ERR_OVERLOAD;
1706 goto reject;
1707 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708
Ying Xueaba79f32013-01-20 23:30:09 +01001709 /* Enqueue message */
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001710 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001711 __skb_queue_tail(&sk->sk_receive_queue, skb);
1712 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001713
David S. Miller676d2362014-04-11 16:15:36 -04001714 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001715 return true;
1716
1717reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001718 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1719 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001720 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721}
1722
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001723/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001724 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001726 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001727 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001728 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001729 *
1730 * Returns 0
1731 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001732static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001733{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001734 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001735 struct sk_buff_head xmitq;
1736 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001737
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001738 __skb_queue_head_init(&xmitq);
1739
1740 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001741 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001742 return 0;
1743 }
1744
1745 if (skb_queue_empty(&xmitq))
1746 return 0;
1747
1748 /* Send response/rejected message */
1749 skb = __skb_dequeue(&xmitq);
1750 dnode = msg_destnode(buf_msg(skb));
1751 selector = msg_origport(buf_msg(skb));
1752 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001753 return 0;
1754}
1755
1756/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001757 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1758 * inputq and try adding them to socket or backlog queue
1759 * @inputq: list of incoming buffers with potentially different destinations
1760 * @sk: socket where the buffers should be enqueued
1761 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001762 *
1763 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001764 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001765static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001766 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001767{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001768 unsigned long time_limit = jiffies + 2;
1769 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001770 unsigned int lim;
1771 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001772 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001773
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001774 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001775 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001776 return;
1777
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001778 skb = tipc_skb_dequeue(inputq, dport);
1779 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001780 return;
1781
1782 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001783 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001784 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001785 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001786 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001787
1788 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001789 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001790 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001791 atomic_set(dcnt, 0);
1792 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1793 if (likely(!sk_add_backlog(sk, skb, lim)))
1794 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001795
1796 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001797 onode = tipc_own_addr(sock_net(sk));
1798 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1799 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001800 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001801 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001802}
1803
1804/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001805 * tipc_sk_rcv - handle a chain of incoming buffers
1806 * @inputq: buffer list containing the buffers
1807 * Consumes all buffers in list until inputq is empty
1808 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001809 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001810void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001811{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001812 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001813 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001814 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001815 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001816 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001817 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001818
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001819 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001820 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001821 dport = tipc_skb_peek_port(inputq, dport);
1822 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001823
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001824 if (likely(tsk)) {
1825 sk = &tsk->sk;
1826 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001827 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001828 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001829 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001830 /* Send pending response/rejected messages, if any */
1831 while ((skb = __skb_dequeue(&xmitq))) {
1832 dnode = msg_destnode(buf_msg(skb));
1833 tipc_node_xmit_skb(net, skb, dnode, dport);
1834 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001835 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001836 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001837 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001838
1839 /* No destination socket => dequeue skb if still there */
1840 skb = tipc_skb_dequeue(inputq, dport);
1841 if (!skb)
1842 return;
1843
1844 /* Try secondary lookup if unresolved named message */
1845 err = TIPC_ERR_NO_PORT;
1846 if (tipc_msg_lookup_dest(net, skb, &err))
1847 goto xmit;
1848
1849 /* Prepare for message rejection */
1850 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001851 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001852xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001853 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001854 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001855 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001856}
1857
Ying Xue78eb3a52014-01-17 09:50:03 +08001858static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1859{
WANG Congd9dc8b02016-11-11 10:20:50 -08001860 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08001861 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08001862 int done;
1863
1864 do {
1865 int err = sock_error(sk);
1866 if (err)
1867 return err;
1868 if (!*timeo_p)
1869 return -ETIMEDOUT;
1870 if (signal_pending(current))
1871 return sock_intr_errno(*timeo_p);
1872
WANG Congd9dc8b02016-11-11 10:20:50 -08001873 add_wait_queue(sk_sleep(sk), &wait);
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001874 done = sk_wait_event(sk, timeo_p,
WANG Congd9dc8b02016-11-11 10:20:50 -08001875 sk->sk_state != TIPC_CONNECTING, &wait);
1876 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08001877 } while (!done);
1878 return 0;
1879}
1880
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881/**
Ying Xue247f0f32014-02-18 16:06:46 +08001882 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883 * @sock: socket structure
1884 * @dest: socket address for destination port
1885 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001886 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887 *
1888 * Returns 0 on success, errno otherwise
1889 */
Ying Xue247f0f32014-02-18 16:06:46 +08001890static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1891 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001893 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001894 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001895 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1896 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001897 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001898 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001899 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900
Allan Stephens0c3141e2008-04-15 00:22:02 -07001901 lock_sock(sk);
1902
Erik Hugnef2f80362015-03-19 09:02:19 +01001903 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001904 if (tipc_sk_type_connectionless(sk)) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001905 if (dst->family == AF_UNSPEC) {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001906 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
Sasha Levin610600c2015-03-23 15:30:00 -04001907 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1908 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001909 } else {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001910 memcpy(&tsk->peer, dest, destlen);
Erik Hugnef2f80362015-03-19 09:02:19 +01001911 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001912 goto exit;
1913 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914
Allan Stephensb89741a2008-04-15 00:20:37 -07001915 /*
1916 * Reject connection attempt using multicast address
1917 *
1918 * Note: send_msg() validates the rest of the address fields,
1919 * so there's no need to do it here
1920 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001921 if (dst->addrtype == TIPC_ADDR_MCAST) {
1922 res = -EINVAL;
1923 goto exit;
1924 }
1925
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001926 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001927
1928 switch (sk->sk_state) {
1929 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05001930 /* Send a 'SYN-' to destination */
1931 m.msg_name = dest;
1932 m.msg_namelen = destlen;
1933
1934 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1935 * indicate send_msg() is never blocked.
1936 */
1937 if (!timeout)
1938 m.msg_flags = MSG_DONTWAIT;
1939
Ying Xue39a02952015-03-02 15:37:47 +08001940 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001941 if ((res < 0) && (res != -EWOULDBLOCK))
1942 goto exit;
1943
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001944 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05001945 * difference is that return value in non-blocking
1946 * case is EINPROGRESS, rather than EALREADY.
1947 */
1948 res = -EINPROGRESS;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001949 /* fall thru' */
1950 case TIPC_CONNECTING:
1951 if (!timeout) {
1952 if (previous == TIPC_CONNECTING)
1953 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08001954 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001955 }
Ying Xue78eb3a52014-01-17 09:50:03 +08001956 timeout = msecs_to_jiffies(timeout);
1957 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1958 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001959 break;
1960 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001961 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001962 break;
1963 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001964 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001965 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001966
Allan Stephens0c3141e2008-04-15 00:22:02 -07001967exit:
1968 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001969 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970}
1971
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001972/**
Ying Xue247f0f32014-02-18 16:06:46 +08001973 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974 * @sock: socket structure
1975 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001976 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977 * Returns 0 on success, errno otherwise
1978 */
Ying Xue247f0f32014-02-18 16:06:46 +08001979static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001980{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001981 struct sock *sk = sock->sk;
1982 int res;
1983
1984 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001985 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001986 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001987
Allan Stephens0c3141e2008-04-15 00:22:02 -07001988 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989}
1990
Ying Xue6398e232014-01-17 09:50:04 +08001991static int tipc_wait_for_accept(struct socket *sock, long timeo)
1992{
1993 struct sock *sk = sock->sk;
1994 DEFINE_WAIT(wait);
1995 int err;
1996
1997 /* True wake-one mechanism for incoming connections: only
1998 * one process gets woken up, not the 'whole herd'.
1999 * Since we do not 'race & poll' for established sockets
2000 * anymore, the common case will execute the loop only once.
2001 */
2002 for (;;) {
2003 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2004 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002005 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002006 release_sock(sk);
2007 timeo = schedule_timeout(timeo);
2008 lock_sock(sk);
2009 }
2010 err = 0;
2011 if (!skb_queue_empty(&sk->sk_receive_queue))
2012 break;
Ying Xue6398e232014-01-17 09:50:04 +08002013 err = -EAGAIN;
2014 if (!timeo)
2015 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002016 err = sock_intr_errno(timeo);
2017 if (signal_pending(current))
2018 break;
Ying Xue6398e232014-01-17 09:50:04 +08002019 }
2020 finish_wait(sk_sleep(sk), &wait);
2021 return err;
2022}
2023
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002024/**
Ying Xue247f0f32014-02-18 16:06:46 +08002025 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002026 * @sock: listening socket
2027 * @newsock: new socket that is to be connected
2028 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002029 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030 * Returns 0 on success, errno otherwise
2031 */
David Howellscdfbabf2017-03-09 08:09:05 +00002032static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2033 bool kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002035 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002037 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002038 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002039 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002040 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002041
Allan Stephens0c3141e2008-04-15 00:22:02 -07002042 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002043
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002044 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002045 res = -EINVAL;
2046 goto exit;
2047 }
Ying Xue6398e232014-01-17 09:50:04 +08002048 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2049 res = tipc_wait_for_accept(sock, timeo);
2050 if (res)
2051 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002052
2053 buf = skb_peek(&sk->sk_receive_queue);
2054
David Howellscdfbabf2017-03-09 08:09:05 +00002055 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002056 if (res)
2057 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002058 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002059
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002060 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002061 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002062 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002063
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002064 /* we lock on new_sk; but lockdep sees the lock on sk */
2065 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002066
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002067 /*
2068 * Reject any stray messages received by new socket
2069 * before the socket lock was taken (very, very unlikely)
2070 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002071 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002073 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002074 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002076 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002077 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002078 new_tsock->conn_type = msg_nametype(msg);
2079 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002081
2082 /*
2083 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2084 * Respond to 'SYN+' by queuing it on new socket.
2085 */
2086 if (!msg_data_sz(msg)) {
2087 struct msghdr m = {NULL,};
2088
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002089 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002090 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002091 } else {
2092 __skb_dequeue(&sk->sk_receive_queue);
2093 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002094 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002095 }
2096 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002097exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002098 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002099 return res;
2100}
2101
2102/**
Ying Xue247f0f32014-02-18 16:06:46 +08002103 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002105 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 *
2107 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002108 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002109 * Returns 0 on success, errno otherwise
2110 */
Ying Xue247f0f32014-02-18 16:06:46 +08002111static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002112{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002113 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114 int res;
2115
Allan Stephense247a8f2008-03-06 15:05:38 -08002116 if (how != SHUT_RDWR)
2117 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002118
Allan Stephens0c3141e2008-04-15 00:22:02 -07002119 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002120
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002121 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2122 sk->sk_shutdown = SEND_SHUTDOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002124 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002125 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002126 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002127
2128 /* Wake up anyone sleeping in poll */
2129 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002130 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002131 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002132 res = -ENOTCONN;
2133 }
2134
Allan Stephens0c3141e2008-04-15 00:22:02 -07002135 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002136 return res;
2137}
2138
Ying Xuef2f2a962015-01-09 15:27:02 +08002139static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002140{
Ying Xuef2f2a962015-01-09 15:27:02 +08002141 struct tipc_sock *tsk = (struct tipc_sock *)data;
2142 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002143 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002144 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002145 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002146
Jon Paul Maloy57289012014-08-22 18:09:09 -04002147 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002148 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002149 bh_unlock_sock(sk);
2150 goto exit;
2151 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002152 peer_port = tsk_peer_port(tsk);
2153 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002154
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002155 if (tsk->probe_unacked) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002156 if (!sock_owned_by_user(sk)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002157 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugneb3be5e32015-06-09 17:27:12 +02002158 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2159 tsk_peer_port(tsk));
2160 sk->sk_state_change(sk);
2161 } else {
2162 /* Try again later */
2163 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2164 }
2165
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002166 bh_unlock_sock(sk);
2167 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002168 }
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002169
2170 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2171 INT_H_SIZE, 0, peer_node, own_node,
2172 peer_port, tsk->portid, TIPC_OK);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002173 tsk->probe_unacked = true;
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002174 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002175 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002176 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002177 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002178exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002179 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002180}
2181
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002182static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002183 struct tipc_name_seq const *seq)
2184{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002185 struct sock *sk = &tsk->sk;
2186 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002187 struct publication *publ;
2188 u32 key;
2189
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002190 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002191 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002192 key = tsk->portid + tsk->pub_count + 1;
2193 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002194 return -EADDRINUSE;
2195
Ying Xuef2f98002015-01-09 15:27:05 +08002196 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002197 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002198 if (unlikely(!publ))
2199 return -EINVAL;
2200
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002201 list_add(&publ->pport_list, &tsk->publications);
2202 tsk->pub_count++;
2203 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002204 return 0;
2205}
2206
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002207static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002208 struct tipc_name_seq const *seq)
2209{
Ying Xuef2f98002015-01-09 15:27:05 +08002210 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002211 struct publication *publ;
2212 struct publication *safe;
2213 int rc = -EINVAL;
2214
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002215 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002216 if (seq) {
2217 if (publ->scope != scope)
2218 continue;
2219 if (publ->type != seq->type)
2220 continue;
2221 if (publ->lower != seq->lower)
2222 continue;
2223 if (publ->upper != seq->upper)
2224 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002225 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002226 publ->ref, publ->key);
2227 rc = 0;
2228 break;
2229 }
Ying Xuef2f98002015-01-09 15:27:05 +08002230 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002231 publ->ref, publ->key);
2232 rc = 0;
2233 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002234 if (list_empty(&tsk->publications))
2235 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002236 return rc;
2237}
2238
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002239/* tipc_sk_reinit: set non-zero address in all existing sockets
2240 * when we go from standalone to network mode.
2241 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002242void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002243{
Ying Xuee05b31f2015-01-09 15:27:08 +08002244 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002245 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002246 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002247 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002248
Herbert Xu40f9f432017-02-11 19:26:46 +08002249 rhashtable_walk_enter(&tn->sk_rht, &iter);
2250
2251 do {
2252 tsk = ERR_PTR(rhashtable_walk_start(&iter));
2253 if (tsk)
2254 continue;
2255
2256 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002257 spin_lock_bh(&tsk->sk.sk_lock.slock);
2258 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002259 msg_set_prevnode(msg, tn->own_addr);
2260 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002261 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2262 }
Herbert Xu40f9f432017-02-11 19:26:46 +08002263
2264 rhashtable_walk_stop(&iter);
2265 } while (tsk == ERR_PTR(-EAGAIN));
Ying Xue07f6c4b2015-01-07 13:41:58 +08002266}
2267
Ying Xuee05b31f2015-01-09 15:27:08 +08002268static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002269{
Ying Xuee05b31f2015-01-09 15:27:08 +08002270 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002271 struct tipc_sock *tsk;
2272
2273 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002274 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002275 if (tsk)
2276 sock_hold(&tsk->sk);
2277 rcu_read_unlock();
2278
2279 return tsk;
2280}
2281
2282static int tipc_sk_insert(struct tipc_sock *tsk)
2283{
Ying Xuee05b31f2015-01-09 15:27:08 +08002284 struct sock *sk = &tsk->sk;
2285 struct net *net = sock_net(sk);
2286 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002287 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2288 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2289
2290 while (remaining--) {
2291 portid++;
2292 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2293 portid = TIPC_MIN_PORT;
2294 tsk->portid = portid;
2295 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002296 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2297 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002298 return 0;
2299 sock_put(&tsk->sk);
2300 }
2301
2302 return -1;
2303}
2304
2305static void tipc_sk_remove(struct tipc_sock *tsk)
2306{
2307 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002308 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002309
Herbert Xu6cca72892015-03-20 21:57:05 +11002310 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002311 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2312 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002313 }
2314}
2315
Herbert Xu6cca72892015-03-20 21:57:05 +11002316static const struct rhashtable_params tsk_rht_params = {
2317 .nelem_hint = 192,
2318 .head_offset = offsetof(struct tipc_sock, node),
2319 .key_offset = offsetof(struct tipc_sock, portid),
2320 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002321 .max_size = 1048576,
2322 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002323 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002324};
2325
Ying Xuee05b31f2015-01-09 15:27:08 +08002326int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002327{
Ying Xuee05b31f2015-01-09 15:27:08 +08002328 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002329
Herbert Xu6cca72892015-03-20 21:57:05 +11002330 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002331}
2332
Ying Xuee05b31f2015-01-09 15:27:08 +08002333void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002334{
Ying Xuee05b31f2015-01-09 15:27:08 +08002335 struct tipc_net *tn = net_generic(net, tipc_net_id);
2336
Ying Xue07f6c4b2015-01-07 13:41:58 +08002337 /* Wait for socket readers to complete */
2338 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002339
Ying Xuee05b31f2015-01-09 15:27:08 +08002340 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002341}
2342
2343/**
Ying Xue247f0f32014-02-18 16:06:46 +08002344 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002345 * @sock: socket structure
2346 * @lvl: option level
2347 * @opt: option identifier
2348 * @ov: pointer to new option value
2349 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002350 *
2351 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002352 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002353 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002354 * Returns 0 on success, errno otherwise
2355 */
Ying Xue247f0f32014-02-18 16:06:46 +08002356static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2357 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002358{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002359 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002360 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002361 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03002362 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002364 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2365 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002366 if (lvl != SOL_TIPC)
2367 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002368
2369 switch (opt) {
2370 case TIPC_IMPORTANCE:
2371 case TIPC_SRC_DROPPABLE:
2372 case TIPC_DEST_DROPPABLE:
2373 case TIPC_CONN_TIMEOUT:
2374 if (ol < sizeof(value))
2375 return -EINVAL;
2376 res = get_user(value, (u32 __user *)ov);
2377 if (res)
2378 return res;
2379 break;
2380 default:
2381 if (ov || ol)
2382 return -EINVAL;
2383 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002384
Allan Stephens0c3141e2008-04-15 00:22:02 -07002385 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002386
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387 switch (opt) {
2388 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002389 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002390 break;
2391 case TIPC_SRC_DROPPABLE:
2392 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002393 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002394 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002395 res = -ENOPROTOOPT;
2396 break;
2397 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002398 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002399 break;
2400 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002401 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002402 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002403 case TIPC_MCAST_BROADCAST:
2404 tsk->mc_method.rcast = false;
2405 tsk->mc_method.mandatory = true;
2406 break;
2407 case TIPC_MCAST_REPLICAST:
2408 tsk->mc_method.rcast = true;
2409 tsk->mc_method.mandatory = true;
2410 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002411 default:
2412 res = -EINVAL;
2413 }
2414
Allan Stephens0c3141e2008-04-15 00:22:02 -07002415 release_sock(sk);
2416
Per Lidenb97bf3f2006-01-02 19:04:38 +01002417 return res;
2418}
2419
2420/**
Ying Xue247f0f32014-02-18 16:06:46 +08002421 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002422 * @sock: socket structure
2423 * @lvl: option level
2424 * @opt: option identifier
2425 * @ov: receptacle for option value
2426 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002427 *
2428 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002429 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002430 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002431 * Returns 0 on success, errno otherwise
2432 */
Ying Xue247f0f32014-02-18 16:06:46 +08002433static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2434 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002435{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002436 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002437 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002438 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002439 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002440 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002441
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002442 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2443 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002444 if (lvl != SOL_TIPC)
2445 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002446 res = get_user(len, ol);
2447 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002448 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002449
Allan Stephens0c3141e2008-04-15 00:22:02 -07002450 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002451
2452 switch (opt) {
2453 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002454 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002455 break;
2456 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002457 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002458 break;
2459 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002460 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002461 break;
2462 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002463 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002464 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002465 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002466 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002467 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002468 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002469 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002470 value = skb_queue_len(&sk->sk_receive_queue);
2471 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002472 default:
2473 res = -EINVAL;
2474 }
2475
Allan Stephens0c3141e2008-04-15 00:22:02 -07002476 release_sock(sk);
2477
Paul Gortmaker25860c32010-12-31 18:59:31 +00002478 if (res)
2479 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002480
Paul Gortmaker25860c32010-12-31 18:59:31 +00002481 if (len < sizeof(value))
2482 return -EINVAL;
2483
2484 if (copy_to_user(ov, &value, sizeof(value)))
2485 return -EFAULT;
2486
2487 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002488}
2489
Ying Xuef2f98002015-01-09 15:27:05 +08002490static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002491{
Ying Xuef2f98002015-01-09 15:27:05 +08002492 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002493 struct tipc_sioc_ln_req lnr;
2494 void __user *argp = (void __user *)arg;
2495
2496 switch (cmd) {
2497 case SIOCGETLINKNAME:
2498 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2499 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002500 if (!tipc_node_get_linkname(sock_net(sk),
2501 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002502 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2503 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2504 return -EFAULT;
2505 return 0;
2506 }
2507 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002508 default:
2509 return -ENOIOCTLCMD;
2510 }
2511}
2512
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002513/* Protocol switches for the various types of TIPC sockets */
2514
Florian Westphalbca65ea2008-02-07 18:18:01 -08002515static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002516 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002517 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002518 .release = tipc_release,
2519 .bind = tipc_bind,
2520 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002521 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002522 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002523 .getname = tipc_getname,
2524 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002525 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002526 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002527 .shutdown = tipc_shutdown,
2528 .setsockopt = tipc_setsockopt,
2529 .getsockopt = tipc_getsockopt,
2530 .sendmsg = tipc_sendmsg,
2531 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002532 .mmap = sock_no_mmap,
2533 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002534};
2535
Florian Westphalbca65ea2008-02-07 18:18:01 -08002536static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002537 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002538 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002539 .release = tipc_release,
2540 .bind = tipc_bind,
2541 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002542 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002543 .accept = tipc_accept,
2544 .getname = tipc_getname,
2545 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002546 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002547 .listen = tipc_listen,
2548 .shutdown = tipc_shutdown,
2549 .setsockopt = tipc_setsockopt,
2550 .getsockopt = tipc_getsockopt,
2551 .sendmsg = tipc_send_packet,
2552 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002553 .mmap = sock_no_mmap,
2554 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002555};
2556
Florian Westphalbca65ea2008-02-07 18:18:01 -08002557static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002558 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002559 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002560 .release = tipc_release,
2561 .bind = tipc_bind,
2562 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002563 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002564 .accept = tipc_accept,
2565 .getname = tipc_getname,
2566 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002567 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002568 .listen = tipc_listen,
2569 .shutdown = tipc_shutdown,
2570 .setsockopt = tipc_setsockopt,
2571 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002572 .sendmsg = tipc_sendstream,
Ying Xue247f0f32014-02-18 16:06:46 +08002573 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002574 .mmap = sock_no_mmap,
2575 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002576};
2577
Florian Westphalbca65ea2008-02-07 18:18:01 -08002578static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002579 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002580 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002581 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002582};
2583
2584static struct proto tipc_proto = {
2585 .name = "TIPC",
2586 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002587 .obj_size = sizeof(struct tipc_sock),
2588 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002589};
2590
2591/**
Per Liden4323add2006-01-18 00:38:21 +01002592 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002593 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002594 * Returns 0 on success, errno otherwise
2595 */
Per Liden4323add2006-01-18 00:38:21 +01002596int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002597{
2598 int res;
2599
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002600 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002601 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002602 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002603 goto out;
2604 }
2605
2606 res = sock_register(&tipc_family_ops);
2607 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002608 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002609 proto_unregister(&tipc_proto);
2610 goto out;
2611 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002612 out:
2613 return res;
2614}
2615
2616/**
Per Liden4323add2006-01-18 00:38:21 +01002617 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002618 */
Per Liden4323add2006-01-18 00:38:21 +01002619void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002620{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002621 sock_unregister(tipc_family_ops.family);
2622 proto_unregister(&tipc_proto);
2623}
Richard Alpe34b78a122014-11-20 10:29:10 +01002624
2625/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002626static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002627{
2628 u32 peer_node;
2629 u32 peer_port;
2630 struct nlattr *nest;
2631
2632 peer_node = tsk_peer_node(tsk);
2633 peer_port = tsk_peer_port(tsk);
2634
2635 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2636
2637 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2638 goto msg_full;
2639 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2640 goto msg_full;
2641
2642 if (tsk->conn_type != 0) {
2643 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2644 goto msg_full;
2645 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2646 goto msg_full;
2647 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2648 goto msg_full;
2649 }
2650 nla_nest_end(skb, nest);
2651
2652 return 0;
2653
2654msg_full:
2655 nla_nest_cancel(skb, nest);
2656
2657 return -EMSGSIZE;
2658}
2659
2660/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002661static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2662 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002663{
2664 int err;
2665 void *hdr;
2666 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002667 struct net *net = sock_net(skb->sk);
2668 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002669 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002670
2671 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002672 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002673 if (!hdr)
2674 goto msg_cancel;
2675
2676 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2677 if (!attrs)
2678 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002679 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002680 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002681 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002682 goto attr_msg_cancel;
2683
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002684 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002685 err = __tipc_nl_add_sk_con(skb, tsk);
2686 if (err)
2687 goto attr_msg_cancel;
2688 } else if (!list_empty(&tsk->publications)) {
2689 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2690 goto attr_msg_cancel;
2691 }
2692 nla_nest_end(skb, attrs);
2693 genlmsg_end(skb, hdr);
2694
2695 return 0;
2696
2697attr_msg_cancel:
2698 nla_nest_cancel(skb, attrs);
2699genlmsg_cancel:
2700 genlmsg_cancel(skb, hdr);
2701msg_cancel:
2702 return -EMSGSIZE;
2703}
2704
2705int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2706{
2707 int err;
2708 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002709 const struct bucket_table *tbl;
2710 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002711 struct net *net = sock_net(skb->sk);
2712 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002713 u32 tbl_id = cb->args[0];
2714 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002715
Ying Xue07f6c4b2015-01-07 13:41:58 +08002716 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002717 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002718 for (; tbl_id < tbl->size; tbl_id++) {
2719 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002720 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002721 if (prev_portid && prev_portid != tsk->portid) {
2722 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2723 continue;
2724 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002725
Richard Alped6e164e2015-01-16 12:30:40 +01002726 err = __tipc_nl_add_sk(skb, cb, tsk);
2727 if (err) {
2728 prev_portid = tsk->portid;
2729 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2730 goto out;
2731 }
2732 prev_portid = 0;
2733 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002734 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002735 }
Richard Alped6e164e2015-01-16 12:30:40 +01002736out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002737 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002738 cb->args[0] = tbl_id;
2739 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002740
2741 return skb->len;
2742}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002743
2744/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002745static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2746 struct netlink_callback *cb,
2747 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002748{
2749 void *hdr;
2750 struct nlattr *attrs;
2751
2752 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002753 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002754 if (!hdr)
2755 goto msg_cancel;
2756
2757 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2758 if (!attrs)
2759 goto genlmsg_cancel;
2760
2761 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2762 goto attr_msg_cancel;
2763 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2764 goto attr_msg_cancel;
2765 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2766 goto attr_msg_cancel;
2767 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2768 goto attr_msg_cancel;
2769
2770 nla_nest_end(skb, attrs);
2771 genlmsg_end(skb, hdr);
2772
2773 return 0;
2774
2775attr_msg_cancel:
2776 nla_nest_cancel(skb, attrs);
2777genlmsg_cancel:
2778 genlmsg_cancel(skb, hdr);
2779msg_cancel:
2780 return -EMSGSIZE;
2781}
2782
2783/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002784static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2785 struct netlink_callback *cb,
2786 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002787{
2788 int err;
2789 struct publication *p;
2790
2791 if (*last_publ) {
2792 list_for_each_entry(p, &tsk->publications, pport_list) {
2793 if (p->key == *last_publ)
2794 break;
2795 }
2796 if (p->key != *last_publ) {
2797 /* We never set seq or call nl_dump_check_consistent()
2798 * this means that setting prev_seq here will cause the
2799 * consistence check to fail in the netlink callback
2800 * handler. Resulting in the last NLMSG_DONE message
2801 * having the NLM_F_DUMP_INTR flag set.
2802 */
2803 cb->prev_seq = 1;
2804 *last_publ = 0;
2805 return -EPIPE;
2806 }
2807 } else {
2808 p = list_first_entry(&tsk->publications, struct publication,
2809 pport_list);
2810 }
2811
2812 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2813 err = __tipc_nl_add_sk_publ(skb, cb, p);
2814 if (err) {
2815 *last_publ = p->key;
2816 return err;
2817 }
2818 }
2819 *last_publ = 0;
2820
2821 return 0;
2822}
2823
2824int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2825{
2826 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002827 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002828 u32 last_publ = cb->args[1];
2829 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002830 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002831 struct tipc_sock *tsk;
2832
Ying Xue07f6c4b2015-01-07 13:41:58 +08002833 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002834 struct nlattr **attrs;
2835 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2836
2837 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2838 if (err)
2839 return err;
2840
Richard Alpe45e093a2016-05-16 11:14:54 +02002841 if (!attrs[TIPC_NLA_SOCK])
2842 return -EINVAL;
2843
Richard Alpe1a1a1432014-11-20 10:29:11 +01002844 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2845 attrs[TIPC_NLA_SOCK],
2846 tipc_nl_sock_policy);
2847 if (err)
2848 return err;
2849
2850 if (!sock[TIPC_NLA_SOCK_REF])
2851 return -EINVAL;
2852
Ying Xue07f6c4b2015-01-07 13:41:58 +08002853 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002854 }
2855
2856 if (done)
2857 return 0;
2858
Ying Xuee05b31f2015-01-09 15:27:08 +08002859 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002860 if (!tsk)
2861 return -EINVAL;
2862
2863 lock_sock(&tsk->sk);
2864 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2865 if (!err)
2866 done = 1;
2867 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002868 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002869
Ying Xue07f6c4b2015-01-07 13:41:58 +08002870 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002871 cb->args[1] = last_publ;
2872 cb->args[2] = done;
2873
2874 return skb->len;
2875}