blob: d37a9401e182f599532ee4d02b647e1ecaac4e20 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05004 * Copyright (c) 2001-2007, 2012-2015, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Ying Xue07f6c4b2015-01-07 13:41:58 +080037#include <linux/rhashtable.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Jon Paul Maloyc637c102015-02-05 08:36:41 -050042#include "name_distr.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040043#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040044#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010045#include "netlink.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040046
Ying Xue07f6c4b2015-01-07 13:41:58 +080047#define SS_LISTENING -1 /* socket is listening */
48#define SS_READY -2 /* socket is connectionless */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080051#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080052#define TIPC_FWD_MSG 1
53#define TIPC_CONN_OK 0
54#define TIPC_CONN_PROBING 1
55#define TIPC_MAX_PORT 0xffffffff
56#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040057
58/**
59 * struct tipc_sock - TIPC socket structure
60 * @sk: socket - interacts with 'port' and with user via the socket API
61 * @connected: non-zero if port is currently connected to a peer port
62 * @conn_type: TIPC type used when connection was established
63 * @conn_instance: TIPC instance used when connection was established
64 * @published: non-zero if port has one or more associated names
65 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080066 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040067 * @phdr: preformatted message header used when sending messages
68 * @port_list: adjacent ports in TIPC's global list of ports
69 * @publications: list of publications for port
70 * @pub_count: total # of publications port has made during its lifetime
71 * @probing_state:
Ying Xue2f55c432015-01-09 15:27:00 +080072 * @probing_intv:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073 * @conn_timeout: the time we can wait for an unresponded setup request
74 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
75 * @link_cong: non-zero if owner must sleep because of link congestion
76 * @sent_unacked: # messages sent by socket, and not yet acked by peer
77 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Erik Hugnef2f80362015-03-19 09:02:19 +010078 * @remote: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080079 * @node: hash table node
80 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040081 */
82struct tipc_sock {
83 struct sock sk;
84 int connected;
85 u32 conn_type;
86 u32 conn_instance;
87 int published;
88 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080089 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040090 struct tipc_msg phdr;
91 struct list_head sock_list;
92 struct list_head publications;
93 u32 pub_count;
94 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080095 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 uint conn_timeout;
97 atomic_t dupl_rcvcnt;
98 bool link_cong;
99 uint sent_unacked;
100 uint rcv_unacked;
Erik Hugnef2f80362015-03-19 09:02:19 +0100101 struct sockaddr_tipc remote;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800102 struct rhash_head node;
103 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400104};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400106static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400107static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800108static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800109static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800110static int tipc_release(struct socket *sock);
111static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400112static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Ying Xuef2f2a962015-01-09 15:27:02 +0800113static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400115 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400116static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400117 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800118static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800119static int tipc_sk_insert(struct tipc_sock *tsk);
120static void tipc_sk_remove(struct tipc_sock *tsk);
Ying Xue39a0295f2015-03-02 15:37:47 +0800121static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
122 size_t dsz);
123static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124
Florian Westphalbca65ea2008-02-07 18:18:01 -0800125static const struct proto_ops packet_ops;
126static const struct proto_ops stream_ops;
127static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128static struct proto tipc_proto;
129
Herbert Xu6cca72892015-03-20 21:57:05 +1100130static const struct rhashtable_params tsk_rht_params;
131
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900132/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133 * Revised TIPC socket locking policy:
134 *
135 * Most socket operations take the standard socket lock when they start
136 * and hold it until they finish (or until they need to sleep). Acquiring
137 * this lock grants the owner exclusive access to the fields of the socket
138 * data structures, with the exception of the backlog queue. A few socket
139 * operations can be done without taking the socket lock because they only
140 * read socket information that never changes during the life of the socket.
141 *
142 * Socket operations may acquire the lock for the associated TIPC port if they
143 * need to perform an operation on the port. If any routine needs to acquire
144 * both the socket lock and the port lock it must take the socket lock first
145 * to avoid the risk of deadlock.
146 *
147 * The dispatcher handling incoming messages cannot grab the socket lock in
148 * the standard fashion, since invoked it runs at the BH level and cannot block.
149 * Instead, it checks to see if the socket lock is currently owned by someone,
150 * and either handles the message itself or adds it to the socket's backlog
151 * queue; in the latter case the queued message is processed once the process
152 * owning the socket lock releases it.
153 *
154 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
155 * the problem of a blocked socket operation preventing any other operations
156 * from occurring. However, applications must be careful if they have
157 * multiple threads trying to send (or receive) on the same socket, as these
158 * operations might interfere with each other. For example, doing a connect
159 * and a receive at the same time might allow the receive to consume the
160 * ACK message meant for the connect. While additional work could be done
161 * to try and overcome this, it doesn't seem to be worthwhile at the present.
162 *
163 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
164 * that another operation that must be performed in a non-blocking manner is
165 * not delayed for very long because the lock has already been taken.
166 *
167 * NOTE: This code assumes that certain fields of a port/socket pair are
168 * constant over its lifetime; such fields can be examined without taking
169 * the socket lock and/or port lock, and do not need to be re-read even
170 * after resuming processing after waiting. These fields include:
171 * - socket type
172 * - pointer to socket sk structure (aka tipc_sock structure)
173 * - pointer to port structure
174 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500177static u32 tsk_own_node(struct tipc_sock *tsk)
178{
179 return msg_prevnode(&tsk->phdr);
180}
181
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400182static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400183{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400184 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400185}
186
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400187static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400188{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400190}
191
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400192static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400193{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400194 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400195}
196
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400197static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400198{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400199 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400200}
201
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400202static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400203{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400204 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400205}
206
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400208{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400209 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400210}
211
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400212static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400213{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400214 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400215}
216
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400217static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400218{
219 if (imp > TIPC_CRITICAL_IMPORTANCE)
220 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400221 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400222 return 0;
223}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400224
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400225static struct tipc_sock *tipc_sk(const struct sock *sk)
226{
227 return container_of(sk, struct tipc_sock, sk);
228}
229
230static int tsk_conn_cong(struct tipc_sock *tsk)
231{
232 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
233}
234
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400236 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700237 *
238 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400240static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400242 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243}
244
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400245/* tipc_sk_respond() : send response message back to sender
246 */
247static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
248{
249 u32 selector;
250 u32 dnode;
251 u32 onode = tipc_own_addr(sock_net(sk));
252
253 if (!tipc_msg_reverse(onode, &skb, err))
254 return;
255
256 dnode = msg_destnode(buf_msg(skb));
257 selector = msg_origport(buf_msg(skb));
258 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
259}
260
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400262 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700263 *
264 * Caller must hold socket lock
265 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400266static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700267{
Ying Xuea6ca1092014-11-26 11:41:55 +0800268 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700269
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400270 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
271 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700272}
273
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400274/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400275 *
276 * Handles cases where the node's network address has changed from
277 * the default of <0.0.0> to its configured setting.
278 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400279static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400280{
Ying Xue34747532015-01-09 15:27:10 +0800281 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400282 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400283 u32 orig_node;
284 u32 peer_node;
285
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400286 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400287 return false;
288
289 if (unlikely(msg_origport(msg) != peer_port))
290 return false;
291
292 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400293 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400294
295 if (likely(orig_node == peer_node))
296 return true;
297
Ying Xue34747532015-01-09 15:27:10 +0800298 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400299 return true;
300
Ying Xue34747532015-01-09 15:27:10 +0800301 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400302 return true;
303
304 return false;
305}
306
Allan Stephens0c3141e2008-04-15 00:22:02 -0700307/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400308 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700309 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 * @sock: pre-allocated socket structure
311 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800312 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900313 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 * This routine creates additional data structures used by the TIPC socket,
315 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 *
317 * Returns 0 on success, errno otherwise
318 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400319static int tipc_sk_create(struct net *net, struct socket *sock,
320 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500322 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 const struct proto_ops *ops;
324 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400326 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400327 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700328
329 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 if (unlikely(protocol != 0))
331 return -EPROTONOSUPPORT;
332
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 switch (sock->type) {
334 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 ops = &stream_ops;
336 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 break;
338 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 ops = &packet_ops;
340 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 break;
342 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700344 ops = &msg_ops;
345 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 break;
Allan Stephens49978652006-06-25 23:47:18 -0700347 default:
Allan Stephens49978652006-06-25 23:47:18 -0700348 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 }
350
Allan Stephens0c3141e2008-04-15 00:22:02 -0700351 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500352 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 if (sk == NULL)
354 return -ENOMEM;
355
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400356 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400357 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400358 INIT_LIST_HEAD(&tsk->publications);
359 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500360 tn = net_generic(sock_net(sk), tipc_net_id);
361 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400362 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363
Allan Stephens0c3141e2008-04-15 00:22:02 -0700364 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700365 sock->ops = ops;
366 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800368 if (tipc_sk_insert(tsk)) {
369 pr_warn("Socket create failed; port numbrer exhausted\n");
370 return -EINVAL;
371 }
372 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800373 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400374 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400375 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800376 sk->sk_data_ready = tipc_data_ready;
377 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800378 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400379 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500380 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400381 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700382
Allan Stephens0c3141e2008-04-15 00:22:02 -0700383 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400384 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700385 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400386 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700387 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 return 0;
389}
390
Ying Xue07f6c4b2015-01-07 13:41:58 +0800391static void tipc_sk_callback(struct rcu_head *head)
392{
393 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
394
395 sock_put(&tsk->sk);
396}
397
Ying Xuec5fa7b32013-06-17 10:54:39 -0400398/**
Ying Xue247f0f32014-02-18 16:06:46 +0800399 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 * @sock: socket to destroy
401 *
402 * This routine cleans up any messages that are still queued on the socket.
403 * For DGRAM and RDM socket types, all queued messages are rejected.
404 * For SEQPACKET and STREAM socket types, the first message is rejected
405 * and any others are discarded. (If the first message on a STREAM socket
406 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900407 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 * NOTE: Rejected messages are not necessarily returned to the sender! They
409 * are returned or discarded according to the "destination droppable" setting
410 * specified for the message by the sender.
411 *
412 * Returns 0 on success, errno otherwise
413 */
Ying Xue247f0f32014-02-18 16:06:46 +0800414static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 struct sock *sk = sock->sk;
Sasha Levin357c4772015-01-13 12:46:41 -0500417 struct net *net;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400418 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800419 struct sk_buff *skb;
Ying Xue1ea23a22015-05-28 13:19:22 +0800420 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421
Allan Stephens0c3141e2008-04-15 00:22:02 -0700422 /*
423 * Exit if socket isn't fully initialized (occurs when a failed accept()
424 * releases a pre-allocated child socket that was never used)
425 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700426 if (sk == NULL)
427 return 0;
428
Sasha Levin357c4772015-01-13 12:46:41 -0500429 net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400430 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700431 lock_sock(sk);
432
433 /*
434 * Reject all unreceived messages, except on an active connection
435 * (which disconnects locally & sends a 'FIN+' to peer)
436 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400437 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800439 skb = __skb_dequeue(&sk->sk_receive_queue);
440 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 break;
Ying Xuea6ca1092014-11-26 11:41:55 +0800442 if (TIPC_SKB_CB(skb)->handle != NULL)
443 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700444 else {
445 if ((sock->state == SS_CONNECTING) ||
446 (sock->state == SS_CONNECTED)) {
447 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400448 tsk->connected = 0;
Ying Xuef2f98002015-01-09 15:27:05 +0800449 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700450 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400451 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700452 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 }
454
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400455 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800456 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800457 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400458 if (tsk->connected) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500459 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +0800460 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500461 tsk_own_node(tsk), tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800462 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800463 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400464 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Ying Xuef2f98002015-01-09 15:27:05 +0800465 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400466 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700469 sock->state = SS_DISCONNECTING;
470 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800471
472 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700473 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200475 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476}
477
478/**
Ying Xue247f0f32014-02-18 16:06:46 +0800479 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 * @sock: socket structure
481 * @uaddr: socket address describing name(s) and desired operation
482 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900483 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484 * Name and name sequence binding is indicated using a positive scope value;
485 * a negative scope value unbinds the specified name. Specifying no name
486 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900487 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700489 *
490 * NOTE: This routine doesn't need to take the socket lock since it doesn't
491 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 */
Ying Xue247f0f32014-02-18 16:06:46 +0800493static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
494 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495{
Ying Xue84602762013-12-27 10:18:28 +0800496 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400498 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800499 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500
Ying Xue84602762013-12-27 10:18:28 +0800501 lock_sock(sk);
502 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400503 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800504 goto exit;
505 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900506
Ying Xue84602762013-12-27 10:18:28 +0800507 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
508 res = -EINVAL;
509 goto exit;
510 }
511 if (addr->family != AF_TIPC) {
512 res = -EAFNOSUPPORT;
513 goto exit;
514 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 if (addr->addrtype == TIPC_ADDR_NAME)
517 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800518 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
519 res = -EAFNOSUPPORT;
520 goto exit;
521 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900522
Ying Xue13a2e892013-06-17 10:54:40 -0400523 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400524 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800525 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
526 res = -EACCES;
527 goto exit;
528 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400529
Ying Xue84602762013-12-27 10:18:28 +0800530 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400531 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
532 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800533exit:
534 release_sock(sk);
535 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536}
537
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900538/**
Ying Xue247f0f32014-02-18 16:06:46 +0800539 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 * @sock: socket structure
541 * @uaddr: area for returned socket address
542 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700543 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900544 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700546 *
Allan Stephens2da59912008-07-14 22:43:32 -0700547 * NOTE: This routine doesn't need to take the socket lock since it only
548 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000549 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 */
Ying Xue247f0f32014-02-18 16:06:46 +0800551static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
552 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400555 struct tipc_sock *tsk = tipc_sk(sock->sk);
Ying Xue34747532015-01-09 15:27:10 +0800556 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000558 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700559 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700560 if ((sock->state != SS_CONNECTED) &&
561 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
562 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400563 addr->addr.id.ref = tsk_peer_port(tsk);
564 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700565 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800566 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800567 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700568 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569
570 *uaddr_len = sizeof(*addr);
571 addr->addrtype = TIPC_ADDR_ID;
572 addr->family = AF_TIPC;
573 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 addr->addr.name.domain = 0;
575
Allan Stephens0c3141e2008-04-15 00:22:02 -0700576 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577}
578
579/**
Ying Xue247f0f32014-02-18 16:06:46 +0800580 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 * @file: file structure associated with the socket
582 * @sock: socket for which to calculate the poll bits
583 * @wait: ???
584 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700585 * Returns pollmask value
586 *
587 * COMMENTARY:
588 * It appears that the usual socket locking mechanisms are not useful here
589 * since the pollmask info is potentially out-of-date the moment this routine
590 * exits. TCP and other protocols seem to rely on higher level poll routines
591 * to handle any preventable race conditions, so TIPC will do the same ...
592 *
593 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700594 *
Allan Stephensf662c072010-08-17 11:00:06 +0000595 * socket state flags set
596 * ------------ ---------
597 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200598 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000599 *
600 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
601 * no write flags
602 *
603 * connected POLLIN/POLLRDNORM if data in rx queue
604 * POLLOUT if port is not congested
605 *
606 * disconnecting POLLIN/POLLRDNORM/POLLHUP
607 * no write flags
608 *
609 * listening POLLIN if SYN in rx queue
610 * no write flags
611 *
612 * ready POLLIN/POLLRDNORM if data in rx queue
613 * [connectionless] POLLOUT (since port cannot be congested)
614 *
615 * IMPORTANT: The fact that a read or write operation is indicated does NOT
616 * imply that the operation will succeed, merely that it should be performed
617 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 */
Ying Xue247f0f32014-02-18 16:06:46 +0800619static unsigned int tipc_poll(struct file *file, struct socket *sock,
620 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621{
Allan Stephens9b674e82008-03-26 16:48:21 -0700622 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400623 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000624 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700625
Ying Xuef288bef2012-08-21 11:16:57 +0800626 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700627
Allan Stephensf662c072010-08-17 11:00:06 +0000628 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200629 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500630 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200631 mask |= POLLOUT;
632 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000633 case SS_READY:
634 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400635 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000636 mask |= POLLOUT;
637 /* fall thru' */
638 case SS_CONNECTING:
639 case SS_LISTENING:
640 if (!skb_queue_empty(&sk->sk_receive_queue))
641 mask |= (POLLIN | POLLRDNORM);
642 break;
643 case SS_DISCONNECTING:
644 mask = (POLLIN | POLLRDNORM | POLLHUP);
645 break;
646 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700647
648 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649}
650
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400651/**
652 * tipc_sendmcast - send multicast message
653 * @sock: socket structure
654 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500655 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400656 * @dsz: total length of message data
657 * @timeo: timeout to wait for wakeup
658 *
659 * Called from function tipc_sendmsg(), which has done all sanity checks
660 * Returns the number of bytes sent on success, or errno
661 */
662static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500663 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400664{
665 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500666 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800667 struct net *net = sock_net(sk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500668 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100669 struct sk_buff_head pktchain;
Al Virof25dcc72014-11-28 15:52:29 -0500670 struct iov_iter save = msg->msg_iter;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400671 uint mtu;
672 int rc;
673
674 msg_set_type(mhdr, TIPC_MCAST_MSG);
675 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
676 msg_set_destport(mhdr, 0);
677 msg_set_destnode(mhdr, 0);
678 msg_set_nametype(mhdr, seq->type);
679 msg_set_namelower(mhdr, seq->lower);
680 msg_set_nameupper(mhdr, seq->upper);
681 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
682
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100683 skb_queue_head_init(&pktchain);
684
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400685new_mtu:
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400686 mtu = tipc_bcast_get_mtu(net);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100687 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400688 if (unlikely(rc < 0))
689 return rc;
690
691 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100692 rc = tipc_bcast_xmit(net, &pktchain);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400693 if (likely(!rc))
694 return dsz;
695
696 if (rc == -ELINKCONG) {
697 tsk->link_cong = 1;
698 rc = tipc_wait_for_sndmsg(sock, &timeo);
699 if (!rc)
700 continue;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400701 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100702 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500703 if (rc == -EMSGSIZE) {
704 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400705 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500706 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400707 break;
708 } while (1);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400709 return rc;
710}
711
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500712/**
713 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
714 * @arrvq: queue with arriving messages, to be cloned after destination lookup
715 * @inputq: queue with cloned messages, delivered to socket after dest lookup
716 *
717 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400718 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500719void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
720 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400721{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500722 struct tipc_msg *msg;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500723 struct tipc_plist dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500724 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400725 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500726 struct sk_buff_head tmpq;
727 uint hsz;
728 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500729
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500730 __skb_queue_head_init(&tmpq);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500731 tipc_plist_init(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400732
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500733 skb = tipc_skb_peek(arrvq, &inputq->lock);
734 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
735 msg = buf_msg(skb);
736 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400737
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500738 if (in_own_node(net, msg_orignode(msg)))
739 scope = TIPC_NODE_SCOPE;
740
741 /* Create destination port list and message clones: */
742 tipc_nametbl_mc_translate(net,
743 msg_nametype(msg), msg_namelower(msg),
744 msg_nameupper(msg), scope, &dports);
745 portid = tipc_plist_pop(&dports);
746 for (; portid; portid = tipc_plist_pop(&dports)) {
747 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
748 if (_skb) {
749 msg_set_destport(buf_msg(_skb), portid);
750 __skb_queue_tail(&tmpq, _skb);
751 continue;
752 }
753 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400754 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500755 /* Append to inputq if not already done by other thread */
756 spin_lock_bh(&inputq->lock);
757 if (skb_peek(arrvq) == skb) {
758 skb_queue_splice_tail_init(&tmpq, inputq);
759 kfree_skb(__skb_dequeue(arrvq));
760 }
761 spin_unlock_bh(&inputq->lock);
762 __skb_queue_purge(&tmpq);
763 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400764 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500765 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400766}
767
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900768/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500769 * tipc_sk_proto_rcv - receive a connection mng protocol message
770 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400771 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500772 */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400773static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500774{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400775 struct sock *sk = &tsk->sk;
776 struct tipc_msg *hdr = buf_msg(skb);
777 int mtyp = msg_type(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500778 int conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400779
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500780 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400781 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500782 goto exit;
783
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400784 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500785
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400786 if (mtyp == CONN_PROBE) {
787 msg_set_type(hdr, CONN_PROBE_REPLY);
788 tipc_sk_respond(sk, skb, TIPC_OK);
789 return;
790 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400791 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400792 tsk->sent_unacked -= msg_msgcnt(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500793 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400794 sk->sk_write_space(sk);
795 } else if (mtyp != CONN_PROBE_REPLY) {
796 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500797 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500798exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400799 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500800}
801
Ying Xue3f405042014-01-17 09:50:05 +0800802static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
803{
804 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400805 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800806 DEFINE_WAIT(wait);
807 int done;
808
809 do {
810 int err = sock_error(sk);
811 if (err)
812 return err;
813 if (sock->state == SS_DISCONNECTING)
814 return -EPIPE;
815 if (!*timeo_p)
816 return -EAGAIN;
817 if (signal_pending(current))
818 return sock_intr_errno(*timeo_p);
819
820 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500821 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800822 finish_wait(sk_sleep(sk), &wait);
823 } while (!done);
824 return 0;
825}
826
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500827/**
Ying Xue247f0f32014-02-18 16:06:46 +0800828 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 * @sock: socket structure
830 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500831 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900832 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900834 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
836 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900837 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 * Returns the number of bytes sent on success, or errno otherwise
839 */
Ying Xue1b784142015-03-02 15:37:48 +0800840static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500841 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842{
Ying Xue39a0295f2015-03-02 15:37:47 +0800843 struct sock *sk = sock->sk;
844 int ret;
845
846 lock_sock(sk);
847 ret = __tipc_sendmsg(sock, m, dsz);
848 release_sock(sk);
849
850 return ret;
851}
852
853static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
854{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500855 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700856 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400857 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800858 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400859 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500860 u32 dnode, dport;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100861 struct sk_buff_head pktchain;
Ying Xuea6ca1092014-11-26 11:41:55 +0800862 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100863 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500864 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500865 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800866 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100867 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500869 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400870 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100871 if (unlikely(!dest)) {
872 if (tsk->connected && sock->state == SS_READY)
873 dest = &tsk->remote;
874 else
875 return -EDESTADDRREQ;
876 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
877 dest->family != AF_TIPC) {
878 return -EINVAL;
879 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500880 if (unlikely(sock->state != SS_READY)) {
Ying Xue39a0295f2015-03-02 15:37:47 +0800881 if (sock->state == SS_LISTENING)
882 return -EPIPE;
883 if (sock->state != SS_UNCONNECTED)
884 return -EISCONN;
885 if (tsk->published)
886 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700887 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400888 tsk->conn_type = dest->addr.name.name.type;
889 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700890 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100892 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800893 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500894
895 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a0295f2015-03-02 15:37:47 +0800896 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500897 } else if (dest->addrtype == TIPC_ADDR_NAME) {
898 u32 type = dest->addr.name.name.type;
899 u32 inst = dest->addr.name.name.instance;
900 u32 domain = dest->addr.name.domain;
901
902 dnode = domain;
903 msg_set_type(mhdr, TIPC_NAMED_MSG);
904 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
905 msg_set_nametype(mhdr, type);
906 msg_set_nameinst(mhdr, inst);
907 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800908 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500909 msg_set_destnode(mhdr, dnode);
910 msg_set_destport(mhdr, dport);
Ying Xue39a0295f2015-03-02 15:37:47 +0800911 if (unlikely(!dport && !dnode))
912 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500913 } else if (dest->addrtype == TIPC_ADDR_ID) {
914 dnode = dest->addr.id.node;
915 msg_set_type(mhdr, TIPC_DIRECT_MSG);
916 msg_set_lookup_scope(mhdr, 0);
917 msg_set_destnode(mhdr, dnode);
918 msg_set_destport(mhdr, dest->addr.id.ref);
919 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
920 }
921
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100922 skb_queue_head_init(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500923 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500924new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800925 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100926 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500927 if (rc < 0)
Ying Xue39a0295f2015-03-02 15:37:47 +0800928 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500929
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900930 do {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100931 skb = skb_peek(&pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +0800932 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100933 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400934 if (likely(!rc)) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500935 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700936 sock->state = SS_CONNECTING;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400937 return dsz;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900938 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400939 if (rc == -ELINKCONG) {
940 tsk->link_cong = 1;
941 rc = tipc_wait_for_sndmsg(sock, &timeo);
942 if (!rc)
943 continue;
944 }
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +0100945 __skb_queue_purge(&pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500946 if (rc == -EMSGSIZE) {
947 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500948 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500949 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400950 break;
951 } while (1);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500952
953 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954}
955
Ying Xue391a6dd2014-01-17 09:50:06 +0800956static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
957{
958 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400959 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800960 DEFINE_WAIT(wait);
961 int done;
962
963 do {
964 int err = sock_error(sk);
965 if (err)
966 return err;
967 if (sock->state == SS_DISCONNECTING)
968 return -EPIPE;
969 else if (sock->state != SS_CONNECTED)
970 return -ENOTCONN;
971 if (!*timeo_p)
972 return -EAGAIN;
973 if (signal_pending(current))
974 return sock_intr_errno(*timeo_p);
975
976 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
977 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500978 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400979 !tsk_conn_cong(tsk)) ||
980 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800981 finish_wait(sk_sleep(sk), &wait);
982 } while (!done);
983 return 0;
984}
985
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900986/**
Ying Xue247f0f32014-02-18 16:06:46 +0800987 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 * @sock: socket structure
989 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500990 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900991 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900993 *
994 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700995 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 */
Ying Xue1b784142015-03-02 15:37:48 +0800997static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100998{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700999 struct sock *sk = sock->sk;
Ying Xue39a0295f2015-03-02 15:37:47 +08001000 int ret;
1001
1002 lock_sock(sk);
1003 ret = __tipc_send_stream(sock, m, dsz);
1004 release_sock(sk);
1005
1006 return ret;
1007}
1008
1009static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1010{
1011 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001012 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001013 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001014 struct tipc_msg *mhdr = &tsk->phdr;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001015 struct sk_buff_head pktchain;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001016 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001017 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001018 int rc = -EINVAL;
1019 long timeo;
1020 u32 dnode;
1021 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001022 struct iov_iter save;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001023
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001024 /* Handle implied connection establishment */
1025 if (unlikely(dest)) {
Ying Xue39a0295f2015-03-02 15:37:47 +08001026 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001027 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001028 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001029 return rc;
1030 }
1031 if (dsz > (uint)INT_MAX)
1032 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001033
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001034 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001035 if (sock->state == SS_DISCONNECTING)
Ying Xue39a0295f2015-03-02 15:37:47 +08001036 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001037 else
Ying Xue39a0295f2015-03-02 15:37:47 +08001038 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001039 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001041 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001042 dnode = tsk_peer_node(tsk);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001043 skb_queue_head_init(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001044
1045next:
Al Virof25dcc72014-11-28 15:52:29 -05001046 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001047 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001048 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001049 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001050 if (unlikely(rc < 0))
Ying Xue39a0295f2015-03-02 15:37:47 +08001051 return rc;
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001052
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001053 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001054 if (likely(!tsk_conn_cong(tsk))) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001055 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001056 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001057 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001058 sent += send;
1059 if (sent == dsz)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001060 return dsz;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001061 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001062 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001063 if (rc == -EMSGSIZE) {
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001064 __skb_queue_purge(&pktchain);
Ying Xuef2f98002015-01-09 15:27:05 +08001065 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1066 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001067 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001068 goto next;
1069 }
1070 if (rc != -ELINKCONG)
1071 break;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001072
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001073 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001075 rc = tipc_wait_for_sndpkt(sock, &timeo);
1076 } while (!rc);
Ying Xue39a0295f2015-03-02 15:37:47 +08001077
Parthasarathy Bhuvaraganf214fc42016-03-01 11:07:09 +01001078 __skb_queue_purge(&pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001079 return sent ? sent : rc;
1080}
1081
1082/**
1083 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001084 * @sock: socket structure
1085 * @m: message to send
1086 * @dsz: length of data to be transmitted
1087 *
1088 * Used for SOCK_SEQPACKET messages.
1089 *
1090 * Returns the number of bytes sent on success, or errno otherwise
1091 */
Ying Xue1b784142015-03-02 15:37:48 +08001092static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001093{
1094 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1095 return -EMSGSIZE;
1096
Ying Xue1b784142015-03-02 15:37:48 +08001097 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098}
1099
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001100/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001102static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001103 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104{
Ying Xue3721e9c2015-01-13 17:07:48 +08001105 struct sock *sk = &tsk->sk;
1106 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001107 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001109 msg_set_destnode(msg, peer_node);
1110 msg_set_destport(msg, peer_port);
1111 msg_set_type(msg, TIPC_CONN_MSG);
1112 msg_set_lookup_scope(msg, 0);
1113 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001114
Ying Xue2f55c432015-01-09 15:27:00 +08001115 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001116 tsk->probing_state = TIPC_CONN_OK;
1117 tsk->connected = 1;
Ying Xue3721e9c2015-01-13 17:07:48 +08001118 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001119 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1120 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121}
1122
1123/**
1124 * set_orig_addr - capture sender's address for received message
1125 * @m: descriptor for message info
1126 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001127 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128 * Note: Address is not captured if not requested by receiver.
1129 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001130static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001132 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001134 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 addr->family = AF_TIPC;
1136 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001137 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 addr->addr.id.ref = msg_origport(msg);
1139 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001140 addr->addr.name.domain = 0; /* could leave uninitialized */
1141 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 m->msg_namelen = sizeof(struct sockaddr_tipc);
1143 }
1144}
1145
1146/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001147 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 * @m: descriptor for message info
1149 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001150 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001151 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001153 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 * Returns 0 if successful, otherwise errno
1155 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001156static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1157 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158{
1159 u32 anc_data[3];
1160 u32 err;
1161 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001162 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 int res;
1164
1165 if (likely(m->msg_controllen == 0))
1166 return 0;
1167
1168 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 err = msg ? msg_errcode(msg) : 0;
1170 if (unlikely(err)) {
1171 anc_data[0] = err;
1172 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001173 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1174 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001176 if (anc_data[1]) {
1177 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1178 msg_data(msg));
1179 if (res)
1180 return res;
1181 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 }
1183
1184 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1186 switch (dest_type) {
1187 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001188 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 anc_data[0] = msg_nametype(msg);
1190 anc_data[1] = msg_namelower(msg);
1191 anc_data[2] = msg_namelower(msg);
1192 break;
1193 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001194 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 anc_data[0] = msg_nametype(msg);
1196 anc_data[1] = msg_namelower(msg);
1197 anc_data[2] = msg_nameupper(msg);
1198 break;
1199 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001200 has_name = (tsk->conn_type != 0);
1201 anc_data[0] = tsk->conn_type;
1202 anc_data[1] = tsk->conn_instance;
1203 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 break;
1205 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001206 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 }
Allan Stephens2db99832010-12-31 18:59:33 +00001208 if (has_name) {
1209 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1210 if (res)
1211 return res;
1212 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213
1214 return 0;
1215}
1216
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001217static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001218{
Ying Xuef2f98002015-01-09 15:27:05 +08001219 struct net *net = sock_net(&tsk->sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001220 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001221 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001222 u32 peer_port = tsk_peer_port(tsk);
1223 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001224
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001225 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001226 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001227 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1228 dnode, tsk_own_node(tsk), peer_port,
1229 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001230 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001231 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001232 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001233 msg_set_msgcnt(msg, ack);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001234 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001235}
1236
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001237static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001238{
1239 struct sock *sk = sock->sk;
1240 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001241 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001242 int err;
1243
1244 for (;;) {
1245 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001246 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001247 if (sock->state == SS_DISCONNECTING) {
1248 err = -ENOTCONN;
1249 break;
1250 }
1251 release_sock(sk);
1252 timeo = schedule_timeout(timeo);
1253 lock_sock(sk);
1254 }
1255 err = 0;
1256 if (!skb_queue_empty(&sk->sk_receive_queue))
1257 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001258 err = -EAGAIN;
1259 if (!timeo)
1260 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001261 err = sock_intr_errno(timeo);
1262 if (signal_pending(current))
1263 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001264 }
1265 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001266 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001267 return err;
1268}
1269
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001270/**
Ying Xue247f0f32014-02-18 16:06:46 +08001271 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 * @m: descriptor for message info
1273 * @buf_len: total size of user buffer area
1274 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001275 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1277 * If the complete message doesn't fit in user area, truncate it.
1278 *
1279 * Returns size of returned message data, errno otherwise
1280 */
Ying Xue1b784142015-03-02 15:37:48 +08001281static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1282 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001284 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001285 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286 struct sk_buff *buf;
1287 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001288 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289 unsigned int sz;
1290 u32 err;
1291 int res;
1292
Allan Stephens0c3141e2008-04-15 00:22:02 -07001293 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001294 if (unlikely(!buf_len))
1295 return -EINVAL;
1296
Allan Stephens0c3141e2008-04-15 00:22:02 -07001297 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298
Allan Stephens0c3141e2008-04-15 00:22:02 -07001299 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 res = -ENOTCONN;
1301 goto exit;
1302 }
1303
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001304 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001305restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306
Allan Stephens0c3141e2008-04-15 00:22:02 -07001307 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001308 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001309 if (res)
1310 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001311
1312 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001313 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 msg = buf_msg(buf);
1315 sz = msg_data_sz(msg);
1316 err = msg_errcode(msg);
1317
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001320 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321 goto restart;
1322 }
1323
1324 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 set_orig_addr(m, msg);
1326
1327 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001328 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001329 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330 goto exit;
1331
1332 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 if (!err) {
1334 if (unlikely(buf_len < sz)) {
1335 sz = buf_len;
1336 m->msg_flags |= MSG_TRUNC;
1337 }
David S. Miller51f3d022014-11-05 16:46:40 -05001338 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001339 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 res = sz;
1342 } else {
1343 if ((sock->state == SS_READY) ||
1344 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1345 res = 0;
1346 else
1347 res = -ECONNRESET;
1348 }
1349
1350 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens990098062008-04-15 00:06:12 -07001352 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001353 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001354 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001355 tsk->rcv_unacked = 0;
1356 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001357 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001358 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001360 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 return res;
1362}
1363
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001364/**
Ying Xue247f0f32014-02-18 16:06:46 +08001365 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 * @m: descriptor for message info
1367 * @buf_len: total size of user buffer area
1368 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001369 *
1370 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 * will optionally wait for more; never truncates data.
1372 *
1373 * Returns size of returned message data, errno otherwise
1374 */
Ying Xue1b784142015-03-02 15:37:48 +08001375static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1376 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001378 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001379 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 struct sk_buff *buf;
1381 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001382 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001384 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001387 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388
1389 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 if (unlikely(!buf_len))
1391 return -EINVAL;
1392
Allan Stephens0c3141e2008-04-15 00:22:02 -07001393 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001395 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 res = -ENOTCONN;
1397 goto exit;
1398 }
1399
Florian Westphal3720d402010-08-17 11:00:04 +00001400 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001401 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001402
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001404 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001405 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001406 if (res)
1407 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001408
1409 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001410 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001411 msg = buf_msg(buf);
1412 sz = msg_data_sz(msg);
1413 err = msg_errcode(msg);
1414
1415 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001417 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 goto restart;
1419 }
1420
1421 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 if (sz_copied == 0) {
1423 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001424 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001425 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 goto exit;
1427 }
1428
1429 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001431 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432
Allan Stephens0232fd02011-02-21 09:45:40 -05001433 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 needed = (buf_len - sz_copied);
1435 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001436
David S. Miller51f3d022014-11-05 16:46:40 -05001437 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1438 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001439 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001441
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 sz_copied += sz_to_copy;
1443
1444 if (sz_to_copy < sz) {
1445 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001446 TIPC_SKB_CB(buf)->handle =
1447 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 goto exit;
1449 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450 } else {
1451 if (sz_copied != 0)
1452 goto exit; /* can't add error msg to valid data */
1453
1454 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1455 res = 0;
1456 else
1457 res = -ECONNRESET;
1458 }
1459
1460 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001462 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001463 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001464 tsk->rcv_unacked = 0;
1465 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001466 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001467 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468
1469 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001470 if ((sz_copied < buf_len) && /* didn't get all requested data */
1471 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001472 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001473 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1474 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 goto restart;
1476
1477exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001478 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001479 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480}
1481
1482/**
Ying Xuef288bef2012-08-21 11:16:57 +08001483 * tipc_write_space - wake up thread if port congestion is released
1484 * @sk: socket
1485 */
1486static void tipc_write_space(struct sock *sk)
1487{
1488 struct socket_wq *wq;
1489
1490 rcu_read_lock();
1491 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001492 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001493 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1494 POLLWRNORM | POLLWRBAND);
1495 rcu_read_unlock();
1496}
1497
1498/**
1499 * tipc_data_ready - wake up threads to indicate messages have been received
1500 * @sk: socket
1501 * @len: the length of messages
1502 */
David S. Miller676d2362014-04-11 16:15:36 -04001503static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001504{
1505 struct socket_wq *wq;
1506
1507 rcu_read_lock();
1508 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001509 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001510 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1511 POLLRDNORM | POLLRDBAND);
1512 rcu_read_unlock();
1513}
1514
Ying Xuef4195d12015-11-22 15:46:05 +08001515static void tipc_sock_destruct(struct sock *sk)
1516{
1517 __skb_queue_purge(&sk->sk_receive_queue);
1518}
1519
Ying Xuef288bef2012-08-21 11:16:57 +08001520/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001521 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001522 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001523 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001524 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001525 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001526 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001527static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001528{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001529 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001530 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001531 struct socket *sock = sk->sk_socket;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001532 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001533
Jon Paul Maloycda36962015-07-22 10:11:20 -04001534 if (unlikely(msg_mcast(hdr)))
1535 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001536
1537 switch ((int)sock->state) {
1538 case SS_CONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001539
Ying Xue7e6c1312012-11-29 18:39:14 -05001540 /* Accept only connection-based messages sent by peer */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001541 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1542 return false;
1543
1544 if (unlikely(msg_errcode(hdr))) {
1545 sock->state = SS_DISCONNECTING;
1546 tsk->connected = 0;
1547 /* Let timer expire on it's own */
1548 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1549 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001550 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001551 return true;
1552
Ying Xue7e6c1312012-11-29 18:39:14 -05001553 case SS_CONNECTING:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001554
Ying Xue7e6c1312012-11-29 18:39:14 -05001555 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001556 if (unlikely(!msg_connected(hdr)))
1557 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001558
Jon Paul Maloycda36962015-07-22 10:11:20 -04001559 if (unlikely(msg_errcode(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001560 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001561 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001562 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001563 }
1564
Jon Paul Maloycda36962015-07-22 10:11:20 -04001565 if (unlikely(!msg_isdata(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001566 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001567 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001568 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001569 }
1570
Jon Paul Maloycda36962015-07-22 10:11:20 -04001571 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1572 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001573 sock->state = SS_CONNECTED;
1574
Jon Paul Maloycda36962015-07-22 10:11:20 -04001575 /* If 'ACK+' message, add to socket receive queue */
1576 if (msg_data_sz(hdr))
1577 return true;
1578
1579 /* If empty 'ACK-' message, wake up sleeping connect() */
1580 if (waitqueue_active(sk_sleep(sk)))
1581 wake_up_interruptible(sk_sleep(sk));
1582
1583 /* 'ACK-' message is neither accepted nor rejected: */
1584 msg_set_dest_droppable(hdr, 1);
1585 return false;
1586
Ying Xue7e6c1312012-11-29 18:39:14 -05001587 case SS_LISTENING:
1588 case SS_UNCONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001589
Ying Xue7e6c1312012-11-29 18:39:14 -05001590 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001591 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1592 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001593 break;
1594 case SS_DISCONNECTING:
1595 break;
1596 default:
1597 pr_err("Unknown socket state %u\n", sock->state);
1598 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001599 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001600}
1601
1602/**
Ying Xueaba79f32013-01-20 23:30:09 +01001603 * rcvbuf_limit - get proper overload limit of socket receive queue
1604 * @sk: socket
1605 * @buf: message
1606 *
1607 * For all connection oriented messages, irrespective of importance,
1608 * the default overload value (i.e. 67MB) is set as limit.
1609 *
1610 * For all connectionless messages, by default new queue limits are
1611 * as belows:
1612 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001613 * TIPC_LOW_IMPORTANCE (4 MB)
1614 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1615 * TIPC_HIGH_IMPORTANCE (16 MB)
1616 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001617 *
1618 * Returns overload limit according to corresponding message importance
1619 */
1620static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1621{
1622 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001623
1624 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001625 return sysctl_tipc_rmem[2];
1626
1627 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1628 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001629}
1630
1631/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001632 * filter_rcv - validate incoming message
1633 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001634 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001635 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001636 * Enqueues message on receive queue if acceptable; optionally handles
1637 * disconnect indication for a connected socket.
1638 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001639 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001640 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001641 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001643static bool filter_rcv(struct sock *sk, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001644{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001645 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001646 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001647 struct tipc_msg *hdr = buf_msg(skb);
1648 unsigned int limit = rcvbuf_limit(sk, skb);
1649 int err = TIPC_OK;
1650 int usr = msg_user(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651
Jon Paul Maloycda36962015-07-22 10:11:20 -04001652 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
1653 tipc_sk_proto_rcv(tsk, skb);
1654 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001655 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001656
Jon Paul Maloycda36962015-07-22 10:11:20 -04001657 if (unlikely(usr == SOCK_WAKEUP)) {
1658 kfree_skb(skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001659 tsk->link_cong = 0;
1660 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001661 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001662 }
1663
Jon Paul Maloycda36962015-07-22 10:11:20 -04001664 /* Drop if illegal message type */
1665 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1666 kfree_skb(skb);
1667 return false;
1668 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001669
Jon Paul Maloycda36962015-07-22 10:11:20 -04001670 /* Reject if wrong message type for current socket state */
1671 if (unlikely(sock->state == SS_READY)) {
1672 if (msg_connected(hdr)) {
1673 err = TIPC_ERR_NO_PORT;
1674 goto reject;
1675 }
1676 } else if (unlikely(!filter_connect(tsk, skb))) {
1677 err = TIPC_ERR_NO_PORT;
1678 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 }
1680
1681 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001682 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1683 err = TIPC_ERR_OVERLOAD;
1684 goto reject;
1685 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686
Ying Xueaba79f32013-01-20 23:30:09 +01001687 /* Enqueue message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001688 TIPC_SKB_CB(skb)->handle = NULL;
1689 __skb_queue_tail(&sk->sk_receive_queue, skb);
1690 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001691
David S. Miller676d2362014-04-11 16:15:36 -04001692 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001693 return true;
1694
1695reject:
1696 tipc_sk_respond(sk, skb, err);
1697 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698}
1699
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001700/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001701 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001702 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001703 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001704 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001705 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001706 *
1707 * Returns 0
1708 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001709static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001710{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001711 unsigned int truesize = skb->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001712
Jon Paul Maloycda36962015-07-22 10:11:20 -04001713 if (likely(filter_rcv(sk, skb)))
1714 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001715 return 0;
1716}
1717
1718/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001719 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1720 * inputq and try adding them to socket or backlog queue
1721 * @inputq: list of incoming buffers with potentially different destinations
1722 * @sk: socket where the buffers should be enqueued
1723 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001724 *
1725 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001726 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001727static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1728 u32 dport)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001729{
1730 unsigned int lim;
1731 atomic_t *dcnt;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001732 struct sk_buff *skb;
1733 unsigned long time_limit = jiffies + 2;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001734
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001735 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001736 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001737 return;
1738
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001739 skb = tipc_skb_dequeue(inputq, dport);
1740 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001741 return;
1742
1743 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001744 if (!sock_owned_by_user(sk)) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001745 filter_rcv(sk, skb);
1746 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001747 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001748
1749 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001750 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001751 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001752 atomic_set(dcnt, 0);
1753 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1754 if (likely(!sk_add_backlog(sk, skb, lim)))
1755 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001756
1757 /* Overload => reject message back to sender */
1758 tipc_sk_respond(sk, skb, TIPC_ERR_OVERLOAD);
1759 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001760 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001761}
1762
1763/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001764 * tipc_sk_rcv - handle a chain of incoming buffers
1765 * @inputq: buffer list containing the buffers
1766 * Consumes all buffers in list until inputq is empty
1767 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001768 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001769void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001770{
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001771 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001772 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001773 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001774 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001775 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001776
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001777 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001778 dport = tipc_skb_peek_port(inputq, dport);
1779 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001780
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001781 if (likely(tsk)) {
1782 sk = &tsk->sk;
1783 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001784 tipc_sk_enqueue(inputq, sk, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001785 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001786 }
1787 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001788 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001789 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001790
1791 /* No destination socket => dequeue skb if still there */
1792 skb = tipc_skb_dequeue(inputq, dport);
1793 if (!skb)
1794 return;
1795
1796 /* Try secondary lookup if unresolved named message */
1797 err = TIPC_ERR_NO_PORT;
1798 if (tipc_msg_lookup_dest(net, skb, &err))
1799 goto xmit;
1800
1801 /* Prepare for message rejection */
1802 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001803 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001804xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001805 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001806 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001807 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001808}
1809
Ying Xue78eb3a52014-01-17 09:50:03 +08001810static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1811{
1812 struct sock *sk = sock->sk;
1813 DEFINE_WAIT(wait);
1814 int done;
1815
1816 do {
1817 int err = sock_error(sk);
1818 if (err)
1819 return err;
1820 if (!*timeo_p)
1821 return -ETIMEDOUT;
1822 if (signal_pending(current))
1823 return sock_intr_errno(*timeo_p);
1824
1825 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1826 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1827 finish_wait(sk_sleep(sk), &wait);
1828 } while (!done);
1829 return 0;
1830}
1831
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832/**
Ying Xue247f0f32014-02-18 16:06:46 +08001833 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 * @sock: socket structure
1835 * @dest: socket address for destination port
1836 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001837 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838 *
1839 * Returns 0 on success, errno otherwise
1840 */
Ying Xue247f0f32014-02-18 16:06:46 +08001841static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1842 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001844 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001845 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001846 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1847 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001848 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Ying Xue78eb3a52014-01-17 09:50:03 +08001849 socket_state previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001850 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851
Allan Stephens0c3141e2008-04-15 00:22:02 -07001852 lock_sock(sk);
1853
Erik Hugnef2f80362015-03-19 09:02:19 +01001854 /* DGRAM/RDM connect(), just save the destaddr */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001855 if (sock->state == SS_READY) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001856 if (dst->family == AF_UNSPEC) {
1857 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1858 tsk->connected = 0;
Sasha Levin610600c2015-03-23 15:30:00 -04001859 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1860 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001861 } else {
1862 memcpy(&tsk->remote, dest, destlen);
1863 tsk->connected = 1;
1864 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001865 goto exit;
1866 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867
Allan Stephensb89741a2008-04-15 00:20:37 -07001868 /*
1869 * Reject connection attempt using multicast address
1870 *
1871 * Note: send_msg() validates the rest of the address fields,
1872 * so there's no need to do it here
1873 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001874 if (dst->addrtype == TIPC_ADDR_MCAST) {
1875 res = -EINVAL;
1876 goto exit;
1877 }
1878
Ying Xue78eb3a52014-01-17 09:50:03 +08001879 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001880 switch (sock->state) {
1881 case SS_UNCONNECTED:
1882 /* Send a 'SYN-' to destination */
1883 m.msg_name = dest;
1884 m.msg_namelen = destlen;
1885
1886 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1887 * indicate send_msg() is never blocked.
1888 */
1889 if (!timeout)
1890 m.msg_flags = MSG_DONTWAIT;
1891
Ying Xue39a0295f2015-03-02 15:37:47 +08001892 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001893 if ((res < 0) && (res != -EWOULDBLOCK))
1894 goto exit;
1895
1896 /* Just entered SS_CONNECTING state; the only
1897 * difference is that return value in non-blocking
1898 * case is EINPROGRESS, rather than EALREADY.
1899 */
1900 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001901 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001902 if (previous == SS_CONNECTING)
1903 res = -EALREADY;
1904 if (!timeout)
1905 goto exit;
1906 timeout = msecs_to_jiffies(timeout);
1907 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1908 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001909 break;
1910 case SS_CONNECTED:
1911 res = -EISCONN;
1912 break;
1913 default:
1914 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001915 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001916 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001917exit:
1918 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001919 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920}
1921
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001922/**
Ying Xue247f0f32014-02-18 16:06:46 +08001923 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924 * @sock: socket structure
1925 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001926 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927 * Returns 0 on success, errno otherwise
1928 */
Ying Xue247f0f32014-02-18 16:06:46 +08001929static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001930{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001931 struct sock *sk = sock->sk;
1932 int res;
1933
1934 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001935
Ying Xue245f3d32011-07-06 06:01:13 -04001936 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001937 res = -EINVAL;
1938 else {
1939 sock->state = SS_LISTENING;
1940 res = 0;
1941 }
1942
1943 release_sock(sk);
1944 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945}
1946
Ying Xue6398e232014-01-17 09:50:04 +08001947static int tipc_wait_for_accept(struct socket *sock, long timeo)
1948{
1949 struct sock *sk = sock->sk;
1950 DEFINE_WAIT(wait);
1951 int err;
1952
1953 /* True wake-one mechanism for incoming connections: only
1954 * one process gets woken up, not the 'whole herd'.
1955 * Since we do not 'race & poll' for established sockets
1956 * anymore, the common case will execute the loop only once.
1957 */
1958 for (;;) {
1959 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1960 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001961 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001962 release_sock(sk);
1963 timeo = schedule_timeout(timeo);
1964 lock_sock(sk);
1965 }
1966 err = 0;
1967 if (!skb_queue_empty(&sk->sk_receive_queue))
1968 break;
1969 err = -EINVAL;
1970 if (sock->state != SS_LISTENING)
1971 break;
Ying Xue6398e232014-01-17 09:50:04 +08001972 err = -EAGAIN;
1973 if (!timeo)
1974 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001975 err = sock_intr_errno(timeo);
1976 if (signal_pending(current))
1977 break;
Ying Xue6398e232014-01-17 09:50:04 +08001978 }
1979 finish_wait(sk_sleep(sk), &wait);
1980 return err;
1981}
1982
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001983/**
Ying Xue247f0f32014-02-18 16:06:46 +08001984 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001985 * @sock: listening socket
1986 * @newsock: new socket that is to be connected
1987 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001988 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989 * Returns 0 on success, errno otherwise
1990 */
Ying Xue247f0f32014-02-18 16:06:46 +08001991static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001993 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001995 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001996 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001997 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001998 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999
Allan Stephens0c3141e2008-04-15 00:22:02 -07002000 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002001
Allan Stephens0c3141e2008-04-15 00:22:02 -07002002 if (sock->state != SS_LISTENING) {
2003 res = -EINVAL;
2004 goto exit;
2005 }
Ying Xue6398e232014-01-17 09:50:04 +08002006 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2007 res = tipc_wait_for_accept(sock, timeo);
2008 if (res)
2009 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002010
2011 buf = skb_peek(&sk->sk_receive_queue);
2012
Ying Xuec5fa7b32013-06-17 10:54:39 -04002013 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002014 if (res)
2015 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002016 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002017
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002018 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002019 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002020 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002021
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002022 /* we lock on new_sk; but lockdep sees the lock on sk */
2023 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002024
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002025 /*
2026 * Reject any stray messages received by new socket
2027 * before the socket lock was taken (very, very unlikely)
2028 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002029 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002031 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002032 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002033 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002035 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002036 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002037 new_tsock->conn_type = msg_nametype(msg);
2038 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002040
2041 /*
2042 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2043 * Respond to 'SYN+' by queuing it on new socket.
2044 */
2045 if (!msg_data_sz(msg)) {
2046 struct msghdr m = {NULL,};
2047
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002048 tsk_advance_rx_queue(sk);
Ying Xue39a0295f2015-03-02 15:37:47 +08002049 __tipc_send_stream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002050 } else {
2051 __skb_dequeue(&sk->sk_receive_queue);
2052 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002053 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002054 }
2055 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002056exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002057 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058 return res;
2059}
2060
2061/**
Ying Xue247f0f32014-02-18 16:06:46 +08002062 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002064 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002065 *
2066 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002067 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068 * Returns 0 on success, errno otherwise
2069 */
Ying Xue247f0f32014-02-18 16:06:46 +08002070static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002072 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002073 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002074 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002075 struct sk_buff *skb;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002076 u32 dnode = tsk_peer_node(tsk);
2077 u32 dport = tsk_peer_port(tsk);
2078 u32 onode = tipc_own_addr(net);
2079 u32 oport = tsk->portid;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080 int res;
2081
Allan Stephense247a8f2008-03-06 15:05:38 -08002082 if (how != SHUT_RDWR)
2083 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002084
Allan Stephens0c3141e2008-04-15 00:22:02 -07002085 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002086
2087 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002088 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089 case SS_CONNECTED:
2090
Per Lidenb97bf3f2006-01-02 19:04:38 +01002091restart:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002092 dnode = tsk_peer_node(tsk);
2093
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002094 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002095 skb = __skb_dequeue(&sk->sk_receive_queue);
2096 if (skb) {
2097 if (TIPC_SKB_CB(skb)->handle != NULL) {
2098 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002099 goto restart;
2100 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002101 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002102 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002103 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002104 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloycda36962015-07-22 10:11:20 -04002105 0, dnode, onode, dport, oport,
2106 TIPC_CONN_SHUTDOWN);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002107 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002109 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002110 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002111 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002112 /* fall through */
2113
2114 case SS_DISCONNECTING:
2115
Ying Xue75031152012-10-29 09:38:15 -04002116 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002117 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002118
2119 /* Wake up anyone sleeping in poll */
2120 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121 res = 0;
2122 break;
2123
2124 default:
2125 res = -ENOTCONN;
2126 }
2127
Allan Stephens0c3141e2008-04-15 00:22:02 -07002128 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002129 return res;
2130}
2131
Ying Xuef2f2a962015-01-09 15:27:02 +08002132static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002133{
Ying Xuef2f2a962015-01-09 15:27:02 +08002134 struct tipc_sock *tsk = (struct tipc_sock *)data;
2135 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002136 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002137 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002138 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002139
Jon Paul Maloy57289012014-08-22 18:09:09 -04002140 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002141 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002142 bh_unlock_sock(sk);
2143 goto exit;
2144 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002145 peer_port = tsk_peer_port(tsk);
2146 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002147
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002148 if (tsk->probing_state == TIPC_CONN_PROBING) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002149 if (!sock_owned_by_user(sk)) {
2150 sk->sk_socket->state = SS_DISCONNECTING;
2151 tsk->connected = 0;
2152 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2153 tsk_peer_port(tsk));
2154 sk->sk_state_change(sk);
2155 } else {
2156 /* Try again later */
2157 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2158 }
2159
Jon Paul Maloy57289012014-08-22 18:09:09 -04002160 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002161 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2162 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002163 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002164 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002165 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002166 }
2167 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002168 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002169 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002170exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002171 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002172}
2173
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002174static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002175 struct tipc_name_seq const *seq)
2176{
Ying Xuef2f98002015-01-09 15:27:05 +08002177 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002178 struct publication *publ;
2179 u32 key;
2180
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002181 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002182 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002183 key = tsk->portid + tsk->pub_count + 1;
2184 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002185 return -EADDRINUSE;
2186
Ying Xuef2f98002015-01-09 15:27:05 +08002187 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002188 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002189 if (unlikely(!publ))
2190 return -EINVAL;
2191
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002192 list_add(&publ->pport_list, &tsk->publications);
2193 tsk->pub_count++;
2194 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002195 return 0;
2196}
2197
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002198static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002199 struct tipc_name_seq const *seq)
2200{
Ying Xuef2f98002015-01-09 15:27:05 +08002201 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002202 struct publication *publ;
2203 struct publication *safe;
2204 int rc = -EINVAL;
2205
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002206 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002207 if (seq) {
2208 if (publ->scope != scope)
2209 continue;
2210 if (publ->type != seq->type)
2211 continue;
2212 if (publ->lower != seq->lower)
2213 continue;
2214 if (publ->upper != seq->upper)
2215 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002216 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002217 publ->ref, publ->key);
2218 rc = 0;
2219 break;
2220 }
Ying Xuef2f98002015-01-09 15:27:05 +08002221 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002222 publ->ref, publ->key);
2223 rc = 0;
2224 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002225 if (list_empty(&tsk->publications))
2226 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002227 return rc;
2228}
2229
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002230/* tipc_sk_reinit: set non-zero address in all existing sockets
2231 * when we go from standalone to network mode.
2232 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002233void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002234{
Ying Xuee05b31f2015-01-09 15:27:08 +08002235 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002236 const struct bucket_table *tbl;
2237 struct rhash_head *pos;
2238 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002239 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002240 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002241
Ying Xue07f6c4b2015-01-07 13:41:58 +08002242 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002243 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002244 for (i = 0; i < tbl->size; i++) {
2245 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2246 spin_lock_bh(&tsk->sk.sk_lock.slock);
2247 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002248 msg_set_prevnode(msg, tn->own_addr);
2249 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002250 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2251 }
2252 }
2253 rcu_read_unlock();
2254}
2255
Ying Xuee05b31f2015-01-09 15:27:08 +08002256static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002257{
Ying Xuee05b31f2015-01-09 15:27:08 +08002258 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002259 struct tipc_sock *tsk;
2260
2261 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002262 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002263 if (tsk)
2264 sock_hold(&tsk->sk);
2265 rcu_read_unlock();
2266
2267 return tsk;
2268}
2269
2270static int tipc_sk_insert(struct tipc_sock *tsk)
2271{
Ying Xuee05b31f2015-01-09 15:27:08 +08002272 struct sock *sk = &tsk->sk;
2273 struct net *net = sock_net(sk);
2274 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002275 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2276 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2277
2278 while (remaining--) {
2279 portid++;
2280 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2281 portid = TIPC_MIN_PORT;
2282 tsk->portid = portid;
2283 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002284 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2285 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002286 return 0;
2287 sock_put(&tsk->sk);
2288 }
2289
2290 return -1;
2291}
2292
2293static void tipc_sk_remove(struct tipc_sock *tsk)
2294{
2295 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002296 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002297
Herbert Xu6cca72892015-03-20 21:57:05 +11002298 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002299 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2300 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002301 }
2302}
2303
Herbert Xu6cca72892015-03-20 21:57:05 +11002304static const struct rhashtable_params tsk_rht_params = {
2305 .nelem_hint = 192,
2306 .head_offset = offsetof(struct tipc_sock, node),
2307 .key_offset = offsetof(struct tipc_sock, portid),
2308 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002309 .max_size = 1048576,
2310 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002311 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002312};
2313
Ying Xuee05b31f2015-01-09 15:27:08 +08002314int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002315{
Ying Xuee05b31f2015-01-09 15:27:08 +08002316 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002317
Herbert Xu6cca72892015-03-20 21:57:05 +11002318 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002319}
2320
Ying Xuee05b31f2015-01-09 15:27:08 +08002321void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002322{
Ying Xuee05b31f2015-01-09 15:27:08 +08002323 struct tipc_net *tn = net_generic(net, tipc_net_id);
2324
Ying Xue07f6c4b2015-01-07 13:41:58 +08002325 /* Wait for socket readers to complete */
2326 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002327
Ying Xuee05b31f2015-01-09 15:27:08 +08002328 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002329}
2330
2331/**
Ying Xue247f0f32014-02-18 16:06:46 +08002332 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002333 * @sock: socket structure
2334 * @lvl: option level
2335 * @opt: option identifier
2336 * @ov: pointer to new option value
2337 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002338 *
2339 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002340 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002341 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002342 * Returns 0 on success, errno otherwise
2343 */
Ying Xue247f0f32014-02-18 16:06:46 +08002344static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2345 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002346{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002347 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002348 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002349 u32 value;
2350 int res;
2351
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002352 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2353 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002354 if (lvl != SOL_TIPC)
2355 return -ENOPROTOOPT;
2356 if (ol < sizeof(value))
2357 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002358 res = get_user(value, (u32 __user *)ov);
2359 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002360 return res;
2361
Allan Stephens0c3141e2008-04-15 00:22:02 -07002362 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002363
Per Lidenb97bf3f2006-01-02 19:04:38 +01002364 switch (opt) {
2365 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002366 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002367 break;
2368 case TIPC_SRC_DROPPABLE:
2369 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002370 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002371 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002372 res = -ENOPROTOOPT;
2373 break;
2374 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002375 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002376 break;
2377 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002378 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002379 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002380 break;
2381 default:
2382 res = -EINVAL;
2383 }
2384
Allan Stephens0c3141e2008-04-15 00:22:02 -07002385 release_sock(sk);
2386
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387 return res;
2388}
2389
2390/**
Ying Xue247f0f32014-02-18 16:06:46 +08002391 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002392 * @sock: socket structure
2393 * @lvl: option level
2394 * @opt: option identifier
2395 * @ov: receptacle for option value
2396 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002397 *
2398 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002399 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002400 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002401 * Returns 0 on success, errno otherwise
2402 */
Ying Xue247f0f32014-02-18 16:06:46 +08002403static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2404 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002405{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002406 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002407 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002408 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002410 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002411
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002412 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2413 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002414 if (lvl != SOL_TIPC)
2415 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002416 res = get_user(len, ol);
2417 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002418 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002419
Allan Stephens0c3141e2008-04-15 00:22:02 -07002420 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002421
2422 switch (opt) {
2423 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002424 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002425 break;
2426 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002427 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002428 break;
2429 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002430 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002431 break;
2432 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002433 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002434 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002435 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002436 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002437 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002438 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002439 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002440 value = skb_queue_len(&sk->sk_receive_queue);
2441 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002442 default:
2443 res = -EINVAL;
2444 }
2445
Allan Stephens0c3141e2008-04-15 00:22:02 -07002446 release_sock(sk);
2447
Paul Gortmaker25860c32010-12-31 18:59:31 +00002448 if (res)
2449 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450
Paul Gortmaker25860c32010-12-31 18:59:31 +00002451 if (len < sizeof(value))
2452 return -EINVAL;
2453
2454 if (copy_to_user(ov, &value, sizeof(value)))
2455 return -EFAULT;
2456
2457 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002458}
2459
Ying Xuef2f98002015-01-09 15:27:05 +08002460static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002461{
Ying Xuef2f98002015-01-09 15:27:05 +08002462 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002463 struct tipc_sioc_ln_req lnr;
2464 void __user *argp = (void __user *)arg;
2465
2466 switch (cmd) {
2467 case SIOCGETLINKNAME:
2468 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2469 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002470 if (!tipc_node_get_linkname(sock_net(sk),
2471 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002472 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2473 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2474 return -EFAULT;
2475 return 0;
2476 }
2477 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002478 default:
2479 return -ENOIOCTLCMD;
2480 }
2481}
2482
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002483/* Protocol switches for the various types of TIPC sockets */
2484
Florian Westphalbca65ea2008-02-07 18:18:01 -08002485static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002486 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002487 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002488 .release = tipc_release,
2489 .bind = tipc_bind,
2490 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002491 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002492 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002493 .getname = tipc_getname,
2494 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002495 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002496 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002497 .shutdown = tipc_shutdown,
2498 .setsockopt = tipc_setsockopt,
2499 .getsockopt = tipc_getsockopt,
2500 .sendmsg = tipc_sendmsg,
2501 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002502 .mmap = sock_no_mmap,
2503 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002504};
2505
Florian Westphalbca65ea2008-02-07 18:18:01 -08002506static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002507 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002508 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002509 .release = tipc_release,
2510 .bind = tipc_bind,
2511 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002512 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002513 .accept = tipc_accept,
2514 .getname = tipc_getname,
2515 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002516 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002517 .listen = tipc_listen,
2518 .shutdown = tipc_shutdown,
2519 .setsockopt = tipc_setsockopt,
2520 .getsockopt = tipc_getsockopt,
2521 .sendmsg = tipc_send_packet,
2522 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002523 .mmap = sock_no_mmap,
2524 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002525};
2526
Florian Westphalbca65ea2008-02-07 18:18:01 -08002527static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002528 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002529 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002530 .release = tipc_release,
2531 .bind = tipc_bind,
2532 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002533 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002534 .accept = tipc_accept,
2535 .getname = tipc_getname,
2536 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002537 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002538 .listen = tipc_listen,
2539 .shutdown = tipc_shutdown,
2540 .setsockopt = tipc_setsockopt,
2541 .getsockopt = tipc_getsockopt,
2542 .sendmsg = tipc_send_stream,
2543 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002544 .mmap = sock_no_mmap,
2545 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002546};
2547
Florian Westphalbca65ea2008-02-07 18:18:01 -08002548static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002549 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002550 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002551 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002552};
2553
2554static struct proto tipc_proto = {
2555 .name = "TIPC",
2556 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002557 .obj_size = sizeof(struct tipc_sock),
2558 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002559};
2560
2561/**
Per Liden4323add2006-01-18 00:38:21 +01002562 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002563 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002564 * Returns 0 on success, errno otherwise
2565 */
Per Liden4323add2006-01-18 00:38:21 +01002566int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002567{
2568 int res;
2569
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002570 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002571 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002572 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002573 goto out;
2574 }
2575
2576 res = sock_register(&tipc_family_ops);
2577 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002578 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002579 proto_unregister(&tipc_proto);
2580 goto out;
2581 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002582 out:
2583 return res;
2584}
2585
2586/**
Per Liden4323add2006-01-18 00:38:21 +01002587 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002588 */
Per Liden4323add2006-01-18 00:38:21 +01002589void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002590{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002591 sock_unregister(tipc_family_ops.family);
2592 proto_unregister(&tipc_proto);
2593}
Richard Alpe34b78a122014-11-20 10:29:10 +01002594
2595/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002596static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002597{
2598 u32 peer_node;
2599 u32 peer_port;
2600 struct nlattr *nest;
2601
2602 peer_node = tsk_peer_node(tsk);
2603 peer_port = tsk_peer_port(tsk);
2604
2605 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2606
2607 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2608 goto msg_full;
2609 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2610 goto msg_full;
2611
2612 if (tsk->conn_type != 0) {
2613 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2614 goto msg_full;
2615 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2616 goto msg_full;
2617 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2618 goto msg_full;
2619 }
2620 nla_nest_end(skb, nest);
2621
2622 return 0;
2623
2624msg_full:
2625 nla_nest_cancel(skb, nest);
2626
2627 return -EMSGSIZE;
2628}
2629
2630/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002631static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2632 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002633{
2634 int err;
2635 void *hdr;
2636 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002637 struct net *net = sock_net(skb->sk);
2638 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002639
2640 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002641 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002642 if (!hdr)
2643 goto msg_cancel;
2644
2645 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2646 if (!attrs)
2647 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002648 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002649 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002650 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002651 goto attr_msg_cancel;
2652
2653 if (tsk->connected) {
2654 err = __tipc_nl_add_sk_con(skb, tsk);
2655 if (err)
2656 goto attr_msg_cancel;
2657 } else if (!list_empty(&tsk->publications)) {
2658 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2659 goto attr_msg_cancel;
2660 }
2661 nla_nest_end(skb, attrs);
2662 genlmsg_end(skb, hdr);
2663
2664 return 0;
2665
2666attr_msg_cancel:
2667 nla_nest_cancel(skb, attrs);
2668genlmsg_cancel:
2669 genlmsg_cancel(skb, hdr);
2670msg_cancel:
2671 return -EMSGSIZE;
2672}
2673
2674int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2675{
2676 int err;
2677 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002678 const struct bucket_table *tbl;
2679 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002680 struct net *net = sock_net(skb->sk);
2681 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002682 u32 tbl_id = cb->args[0];
2683 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002684
Ying Xue07f6c4b2015-01-07 13:41:58 +08002685 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002686 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002687 for (; tbl_id < tbl->size; tbl_id++) {
2688 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002689 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002690 if (prev_portid && prev_portid != tsk->portid) {
2691 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2692 continue;
2693 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002694
Richard Alped6e164e2015-01-16 12:30:40 +01002695 err = __tipc_nl_add_sk(skb, cb, tsk);
2696 if (err) {
2697 prev_portid = tsk->portid;
2698 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2699 goto out;
2700 }
2701 prev_portid = 0;
2702 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002703 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002704 }
Richard Alped6e164e2015-01-16 12:30:40 +01002705out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002706 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002707 cb->args[0] = tbl_id;
2708 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002709
2710 return skb->len;
2711}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002712
2713/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002714static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2715 struct netlink_callback *cb,
2716 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002717{
2718 void *hdr;
2719 struct nlattr *attrs;
2720
2721 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002722 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002723 if (!hdr)
2724 goto msg_cancel;
2725
2726 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2727 if (!attrs)
2728 goto genlmsg_cancel;
2729
2730 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2731 goto attr_msg_cancel;
2732 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2733 goto attr_msg_cancel;
2734 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2735 goto attr_msg_cancel;
2736 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2737 goto attr_msg_cancel;
2738
2739 nla_nest_end(skb, attrs);
2740 genlmsg_end(skb, hdr);
2741
2742 return 0;
2743
2744attr_msg_cancel:
2745 nla_nest_cancel(skb, attrs);
2746genlmsg_cancel:
2747 genlmsg_cancel(skb, hdr);
2748msg_cancel:
2749 return -EMSGSIZE;
2750}
2751
2752/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002753static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2754 struct netlink_callback *cb,
2755 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002756{
2757 int err;
2758 struct publication *p;
2759
2760 if (*last_publ) {
2761 list_for_each_entry(p, &tsk->publications, pport_list) {
2762 if (p->key == *last_publ)
2763 break;
2764 }
2765 if (p->key != *last_publ) {
2766 /* We never set seq or call nl_dump_check_consistent()
2767 * this means that setting prev_seq here will cause the
2768 * consistence check to fail in the netlink callback
2769 * handler. Resulting in the last NLMSG_DONE message
2770 * having the NLM_F_DUMP_INTR flag set.
2771 */
2772 cb->prev_seq = 1;
2773 *last_publ = 0;
2774 return -EPIPE;
2775 }
2776 } else {
2777 p = list_first_entry(&tsk->publications, struct publication,
2778 pport_list);
2779 }
2780
2781 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2782 err = __tipc_nl_add_sk_publ(skb, cb, p);
2783 if (err) {
2784 *last_publ = p->key;
2785 return err;
2786 }
2787 }
2788 *last_publ = 0;
2789
2790 return 0;
2791}
2792
2793int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2794{
2795 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002796 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002797 u32 last_publ = cb->args[1];
2798 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002799 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002800 struct tipc_sock *tsk;
2801
Ying Xue07f6c4b2015-01-07 13:41:58 +08002802 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002803 struct nlattr **attrs;
2804 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2805
2806 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2807 if (err)
2808 return err;
2809
2810 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2811 attrs[TIPC_NLA_SOCK],
2812 tipc_nl_sock_policy);
2813 if (err)
2814 return err;
2815
2816 if (!sock[TIPC_NLA_SOCK_REF])
2817 return -EINVAL;
2818
Ying Xue07f6c4b2015-01-07 13:41:58 +08002819 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002820 }
2821
2822 if (done)
2823 return 0;
2824
Ying Xuee05b31f2015-01-09 15:27:08 +08002825 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002826 if (!tsk)
2827 return -EINVAL;
2828
2829 lock_sock(&tsk->sk);
2830 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2831 if (!err)
2832 done = 1;
2833 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002834 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002835
Ying Xue07f6c4b2015-01-07 13:41:58 +08002836 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002837 cb->args[1] = last_publ;
2838 cb->args[2] = done;
2839
2840 return skb->len;
2841}