blob: 9074b5cede38b8edd75890b684a706d96b9f71ba [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"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040044
Ying Xue07f6c4b2015-01-07 13:41:58 +080045#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
Ying Xue07f6c4b2015-01-07 13:41:58 +080048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080049#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define TIPC_FWD_MSG 1
51#define TIPC_CONN_OK 0
52#define TIPC_CONN_PROBING 1
53#define TIPC_MAX_PORT 0xffffffff
54#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040055
56/**
57 * struct tipc_sock - TIPC socket structure
58 * @sk: socket - interacts with 'port' and with user via the socket API
59 * @connected: non-zero if port is currently connected to a peer port
60 * @conn_type: TIPC type used when connection was established
61 * @conn_instance: TIPC instance used when connection was established
62 * @published: non-zero if port has one or more associated names
63 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080064 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040065 * @phdr: preformatted message header used when sending messages
66 * @port_list: adjacent ports in TIPC's global list of ports
67 * @publications: list of publications for port
68 * @pub_count: total # of publications port has made during its lifetime
69 * @probing_state:
Ying Xue2f55c432015-01-09 15:27:00 +080070 * @probing_intv:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040071 * @conn_timeout: the time we can wait for an unresponded setup request
72 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
73 * @link_cong: non-zero if owner must sleep because of link congestion
74 * @sent_unacked: # messages sent by socket, and not yet acked by peer
75 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Erik Hugnef2f80362015-03-19 09:02:19 +010076 * @remote: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080077 * @node: hash table node
78 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040079 */
80struct tipc_sock {
81 struct sock sk;
82 int connected;
83 u32 conn_type;
84 u32 conn_instance;
85 int published;
86 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080087 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040088 struct tipc_msg phdr;
89 struct list_head sock_list;
90 struct list_head publications;
91 u32 pub_count;
92 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080093 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040094 uint conn_timeout;
95 atomic_t dupl_rcvcnt;
96 bool link_cong;
97 uint sent_unacked;
98 uint rcv_unacked;
Erik Hugnef2f80362015-03-19 09:02:19 +010099 struct sockaddr_tipc remote;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800100 struct rhash_head node;
101 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400102};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400104static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400105static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800106static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800107static int tipc_release(struct socket *sock);
108static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400109static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Ying Xuef2f2a962015-01-09 15:27:02 +0800110static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400111static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400112 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400113static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400114 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800115static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800116static int tipc_sk_insert(struct tipc_sock *tsk);
117static void tipc_sk_remove(struct tipc_sock *tsk);
Ying Xue39a02952015-03-02 15:37:47 +0800118static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
119 size_t dsz);
120static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121
Florian Westphalbca65ea2008-02-07 18:18:01 -0800122static const struct proto_ops packet_ops;
123static const struct proto_ops stream_ops;
124static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125static struct proto tipc_proto;
126
Richard Alpe1a1a1432014-11-20 10:29:11 +0100127static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
128 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
129 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
130 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
131 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
132 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
133};
134
Herbert Xu6cca72892015-03-20 21:57:05 +1100135static const struct rhashtable_params tsk_rht_params;
136
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900137/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138 * Revised TIPC socket locking policy:
139 *
140 * Most socket operations take the standard socket lock when they start
141 * and hold it until they finish (or until they need to sleep). Acquiring
142 * this lock grants the owner exclusive access to the fields of the socket
143 * data structures, with the exception of the backlog queue. A few socket
144 * operations can be done without taking the socket lock because they only
145 * read socket information that never changes during the life of the socket.
146 *
147 * Socket operations may acquire the lock for the associated TIPC port if they
148 * need to perform an operation on the port. If any routine needs to acquire
149 * both the socket lock and the port lock it must take the socket lock first
150 * to avoid the risk of deadlock.
151 *
152 * The dispatcher handling incoming messages cannot grab the socket lock in
153 * the standard fashion, since invoked it runs at the BH level and cannot block.
154 * Instead, it checks to see if the socket lock is currently owned by someone,
155 * and either handles the message itself or adds it to the socket's backlog
156 * queue; in the latter case the queued message is processed once the process
157 * owning the socket lock releases it.
158 *
159 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
160 * the problem of a blocked socket operation preventing any other operations
161 * from occurring. However, applications must be careful if they have
162 * multiple threads trying to send (or receive) on the same socket, as these
163 * operations might interfere with each other. For example, doing a connect
164 * and a receive at the same time might allow the receive to consume the
165 * ACK message meant for the connect. While additional work could be done
166 * to try and overcome this, it doesn't seem to be worthwhile at the present.
167 *
168 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
169 * that another operation that must be performed in a non-blocking manner is
170 * not delayed for very long because the lock has already been taken.
171 *
172 * NOTE: This code assumes that certain fields of a port/socket pair are
173 * constant over its lifetime; such fields can be examined without taking
174 * the socket lock and/or port lock, and do not need to be re-read even
175 * after resuming processing after waiting. These fields include:
176 * - socket type
177 * - pointer to socket sk structure (aka tipc_sock structure)
178 * - pointer to port structure
179 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500182static u32 tsk_own_node(struct tipc_sock *tsk)
183{
184 return msg_prevnode(&tsk->phdr);
185}
186
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400187static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400188{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400190}
191
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400192static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400193{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400194 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400195}
196
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400197static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400198{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400199 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400200}
201
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400202static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400203{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400204 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400205}
206
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400208{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400209 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400210}
211
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400212static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400213{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400214 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400215}
216
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400217static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400218{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400219 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400220}
221
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400222static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400223{
224 if (imp > TIPC_CRITICAL_IMPORTANCE)
225 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400226 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400227 return 0;
228}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400229
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400230static struct tipc_sock *tipc_sk(const struct sock *sk)
231{
232 return container_of(sk, struct tipc_sock, sk);
233}
234
235static int tsk_conn_cong(struct tipc_sock *tsk)
236{
237 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
238}
239
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400241 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700242 *
243 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400245static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400247 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248}
249
250/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400251 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700252 *
253 * Caller must hold socket lock
254 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400255static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700256{
Ying Xuea6ca1092014-11-26 11:41:55 +0800257 struct sk_buff *skb;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500258 u32 dnode;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500259 u32 own_node = tsk_own_node(tipc_sk(sk));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700260
Ying Xuea6ca1092014-11-26 11:41:55 +0800261 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500262 if (tipc_msg_reverse(own_node, skb, &dnode, TIPC_ERR_NO_PORT))
263 tipc_link_xmit_skb(sock_net(sk), skb, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500264 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700265}
266
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400267/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400268 *
269 * Handles cases where the node's network address has changed from
270 * the default of <0.0.0> to its configured setting.
271 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400272static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400273{
Ying Xue34747532015-01-09 15:27:10 +0800274 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400275 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400276 u32 orig_node;
277 u32 peer_node;
278
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400279 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400280 return false;
281
282 if (unlikely(msg_origport(msg) != peer_port))
283 return false;
284
285 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400286 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400287
288 if (likely(orig_node == peer_node))
289 return true;
290
Ying Xue34747532015-01-09 15:27:10 +0800291 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400292 return true;
293
Ying Xue34747532015-01-09 15:27:10 +0800294 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400295 return true;
296
297 return false;
298}
299
Allan Stephens0c3141e2008-04-15 00:22:02 -0700300/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400301 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700302 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 * @sock: pre-allocated socket structure
304 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800305 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900306 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700307 * This routine creates additional data structures used by the TIPC socket,
308 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 *
310 * Returns 0 on success, errno otherwise
311 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400312static int tipc_sk_create(struct net *net, struct socket *sock,
313 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500315 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 const struct proto_ops *ops;
317 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400319 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400320 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321
322 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 if (unlikely(protocol != 0))
324 return -EPROTONOSUPPORT;
325
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 switch (sock->type) {
327 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700328 ops = &stream_ops;
329 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 break;
331 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700332 ops = &packet_ops;
333 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 break;
335 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700337 ops = &msg_ops;
338 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 break;
Allan Stephens49978652006-06-25 23:47:18 -0700340 default:
Allan Stephens49978652006-06-25 23:47:18 -0700341 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 }
343
Allan Stephens0c3141e2008-04-15 00:22:02 -0700344 /* Allocate socket's protocol area */
Ying Xue76100a82015-03-18 09:32:57 +0800345 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700346 if (sk == NULL)
347 return -ENOMEM;
348
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400349 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400350 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400351 INIT_LIST_HEAD(&tsk->publications);
352 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500353 tn = net_generic(sock_net(sk), tipc_net_id);
354 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400355 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356
Allan Stephens0c3141e2008-04-15 00:22:02 -0700357 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 sock->ops = ops;
359 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800361 if (tipc_sk_insert(tsk)) {
362 pr_warn("Socket create failed; port numbrer exhausted\n");
363 return -EINVAL;
364 }
365 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800366 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400367 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400368 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800369 sk->sk_data_ready = tipc_data_ready;
370 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400371 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500372 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400373 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700374
Allan Stephens0c3141e2008-04-15 00:22:02 -0700375 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400376 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400378 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700379 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 return 0;
381}
382
Ying Xue07f6c4b2015-01-07 13:41:58 +0800383static void tipc_sk_callback(struct rcu_head *head)
384{
385 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
386
387 sock_put(&tsk->sk);
388}
389
Ying Xuec5fa7b32013-06-17 10:54:39 -0400390/**
Ying Xue247f0f32014-02-18 16:06:46 +0800391 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 * @sock: socket to destroy
393 *
394 * This routine cleans up any messages that are still queued on the socket.
395 * For DGRAM and RDM socket types, all queued messages are rejected.
396 * For SEQPACKET and STREAM socket types, the first message is rejected
397 * and any others are discarded. (If the first message on a STREAM socket
398 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900399 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 * NOTE: Rejected messages are not necessarily returned to the sender! They
401 * are returned or discarded according to the "destination droppable" setting
402 * specified for the message by the sender.
403 *
404 * Returns 0 on success, errno otherwise
405 */
Ying Xue247f0f32014-02-18 16:06:46 +0800406static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 struct sock *sk = sock->sk;
Sasha Levin357c4772015-01-13 12:46:41 -0500409 struct net *net;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400410 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800411 struct sk_buff *skb;
Ying Xuef2f2a962015-01-09 15:27:02 +0800412 u32 dnode, probing_state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413
Allan Stephens0c3141e2008-04-15 00:22:02 -0700414 /*
415 * Exit if socket isn't fully initialized (occurs when a failed accept()
416 * releases a pre-allocated child socket that was never used)
417 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700418 if (sk == NULL)
419 return 0;
420
Sasha Levin357c4772015-01-13 12:46:41 -0500421 net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400422 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700423 lock_sock(sk);
424
425 /*
426 * Reject all unreceived messages, except on an active connection
427 * (which disconnects locally & sends a 'FIN+' to peer)
428 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400429 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800431 skb = __skb_dequeue(&sk->sk_receive_queue);
432 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 break;
Ying Xuea6ca1092014-11-26 11:41:55 +0800434 if (TIPC_SKB_CB(skb)->handle != NULL)
435 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700436 else {
437 if ((sock->state == SS_CONNECTING) ||
438 (sock->state == SS_CONNECTED)) {
439 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400440 tsk->connected = 0;
Ying Xuef2f98002015-01-09 15:27:05 +0800441 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700442 }
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500443 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
Ying Xue34747532015-01-09 15:27:10 +0800444 TIPC_ERR_NO_PORT))
Ying Xuef2f98002015-01-09 15:27:05 +0800445 tipc_link_xmit_skb(net, skb, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700446 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 }
448
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400449 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xuef2f2a962015-01-09 15:27:02 +0800450 probing_state = tsk->probing_state;
Ying Xue3721e9c2015-01-13 17:07:48 +0800451 if (del_timer_sync(&sk->sk_timer) &&
452 probing_state != TIPC_CONN_PROBING)
Ying Xuef2f2a962015-01-09 15:27:02 +0800453 sock_put(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800454 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400455 if (tsk->connected) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500456 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +0800457 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500458 tsk_own_node(tsk), tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800459 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800460 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +0800461 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
462 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400463 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464
Allan Stephens0c3141e2008-04-15 00:22:02 -0700465 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100466 __skb_queue_purge(&sk->sk_receive_queue);
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;
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500669 struct sk_buff_head *pktchain = &sk->sk_write_queue;
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
683new_mtu:
684 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500685 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400686 if (unlikely(rc < 0))
687 return rc;
688
689 do {
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500690 rc = tipc_bclink_xmit(net, pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400691 if (likely(rc >= 0)) {
692 rc = dsz;
693 break;
694 }
Al Virof25dcc72014-11-28 15:52:29 -0500695 if (rc == -EMSGSIZE) {
696 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400697 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500698 }
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400699 if (rc != -ELINKCONG)
700 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400701 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400702 rc = tipc_wait_for_sndmsg(sock, &timeo);
703 if (rc)
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500704 __skb_queue_purge(pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400705 } while (!rc);
706 return rc;
707}
708
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500709/**
710 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
711 * @arrvq: queue with arriving messages, to be cloned after destination lookup
712 * @inputq: queue with cloned messages, delivered to socket after dest lookup
713 *
714 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400715 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500716void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
717 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400718{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500719 struct tipc_msg *msg;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500720 struct tipc_plist dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500721 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400722 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500723 struct sk_buff_head tmpq;
724 uint hsz;
725 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500726
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500727 __skb_queue_head_init(&tmpq);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500728 tipc_plist_init(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400729
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500730 skb = tipc_skb_peek(arrvq, &inputq->lock);
731 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
732 msg = buf_msg(skb);
733 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400734
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500735 if (in_own_node(net, msg_orignode(msg)))
736 scope = TIPC_NODE_SCOPE;
737
738 /* Create destination port list and message clones: */
739 tipc_nametbl_mc_translate(net,
740 msg_nametype(msg), msg_namelower(msg),
741 msg_nameupper(msg), scope, &dports);
742 portid = tipc_plist_pop(&dports);
743 for (; portid; portid = tipc_plist_pop(&dports)) {
744 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
745 if (_skb) {
746 msg_set_destport(buf_msg(_skb), portid);
747 __skb_queue_tail(&tmpq, _skb);
748 continue;
749 }
750 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400751 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500752 /* Append to inputq if not already done by other thread */
753 spin_lock_bh(&inputq->lock);
754 if (skb_peek(arrvq) == skb) {
755 skb_queue_splice_tail_init(&tmpq, inputq);
756 kfree_skb(__skb_dequeue(arrvq));
757 }
758 spin_unlock_bh(&inputq->lock);
759 __skb_queue_purge(&tmpq);
760 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400761 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500762 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400763}
764
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900765/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500766 * tipc_sk_proto_rcv - receive a connection mng protocol message
767 * @tsk: receiving socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500768 * @skb: pointer to message buffer. Set to NULL if buffer is consumed.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500769 */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500770static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff **skb)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500771{
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500772 struct tipc_msg *msg = buf_msg(*skb);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500773 int conn_cong;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500774 u32 dnode;
775 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500776 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400777 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500778 goto exit;
779
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400780 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500781
782 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400783 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500784 tsk->sent_unacked -= msg_msgcnt(msg);
785 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400786 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500787 } else if (msg_type(msg) == CONN_PROBE) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500788 if (tipc_msg_reverse(own_node, *skb, &dnode, TIPC_OK)) {
789 msg_set_type(msg, CONN_PROBE_REPLY);
790 return;
791 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500792 }
793 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
794exit:
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500795 kfree_skb(*skb);
796 *skb = NULL;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500797}
798
Ying Xue3f405042014-01-17 09:50:05 +0800799static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
800{
801 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400802 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800803 DEFINE_WAIT(wait);
804 int done;
805
806 do {
807 int err = sock_error(sk);
808 if (err)
809 return err;
810 if (sock->state == SS_DISCONNECTING)
811 return -EPIPE;
812 if (!*timeo_p)
813 return -EAGAIN;
814 if (signal_pending(current))
815 return sock_intr_errno(*timeo_p);
816
817 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500818 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800819 finish_wait(sk_sleep(sk), &wait);
820 } while (!done);
821 return 0;
822}
823
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500824/**
Ying Xue247f0f32014-02-18 16:06:46 +0800825 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 * @sock: socket structure
827 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500828 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900829 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900831 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
833 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900834 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 * Returns the number of bytes sent on success, or errno otherwise
836 */
Ying Xue1b784142015-03-02 15:37:48 +0800837static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500838 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839{
Ying Xue39a02952015-03-02 15:37:47 +0800840 struct sock *sk = sock->sk;
841 int ret;
842
843 lock_sock(sk);
844 ret = __tipc_sendmsg(sock, m, dsz);
845 release_sock(sk);
846
847 return ret;
848}
849
850static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
851{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500852 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700853 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400854 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800855 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400856 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500857 u32 dnode, dport;
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500858 struct sk_buff_head *pktchain = &sk->sk_write_queue;
Ying Xuea6ca1092014-11-26 11:41:55 +0800859 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100860 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500861 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500862 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800863 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100864 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500866 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400867 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100868 if (unlikely(!dest)) {
869 if (tsk->connected && sock->state == SS_READY)
870 dest = &tsk->remote;
871 else
872 return -EDESTADDRREQ;
873 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
874 dest->family != AF_TIPC) {
875 return -EINVAL;
876 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500877 if (unlikely(sock->state != SS_READY)) {
Ying Xue39a02952015-03-02 15:37:47 +0800878 if (sock->state == SS_LISTENING)
879 return -EPIPE;
880 if (sock->state != SS_UNCONNECTED)
881 return -EISCONN;
882 if (tsk->published)
883 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700884 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400885 tsk->conn_type = dest->addr.name.name.type;
886 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700887 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100889 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800890 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500891
892 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a02952015-03-02 15:37:47 +0800893 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500894 } else if (dest->addrtype == TIPC_ADDR_NAME) {
895 u32 type = dest->addr.name.name.type;
896 u32 inst = dest->addr.name.name.instance;
897 u32 domain = dest->addr.name.domain;
898
899 dnode = domain;
900 msg_set_type(mhdr, TIPC_NAMED_MSG);
901 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
902 msg_set_nametype(mhdr, type);
903 msg_set_nameinst(mhdr, inst);
904 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800905 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500906 msg_set_destnode(mhdr, dnode);
907 msg_set_destport(mhdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800908 if (unlikely(!dport && !dnode))
909 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500910 } else if (dest->addrtype == TIPC_ADDR_ID) {
911 dnode = dest->addr.id.node;
912 msg_set_type(mhdr, TIPC_DIRECT_MSG);
913 msg_set_lookup_scope(mhdr, 0);
914 msg_set_destnode(mhdr, dnode);
915 msg_set_destport(mhdr, dest->addr.id.ref);
916 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
917 }
918
Al Virof25dcc72014-11-28 15:52:29 -0500919 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500920new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800921 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500922 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500923 if (rc < 0)
Ying Xue39a02952015-03-02 15:37:47 +0800924 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500925
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900926 do {
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500927 skb = skb_peek(pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +0800928 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500929 rc = tipc_link_xmit(net, pktchain, dnode, tsk->portid);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500930 if (likely(rc >= 0)) {
931 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700932 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500933 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700934 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900935 }
Al Virof25dcc72014-11-28 15:52:29 -0500936 if (rc == -EMSGSIZE) {
937 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500938 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500939 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500940 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700941 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400942 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500943 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400944 if (rc)
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500945 __skb_queue_purge(pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500946 } while (!rc);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500947
948 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949}
950
Ying Xue391a6dd2014-01-17 09:50:06 +0800951static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
952{
953 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400954 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800955 DEFINE_WAIT(wait);
956 int done;
957
958 do {
959 int err = sock_error(sk);
960 if (err)
961 return err;
962 if (sock->state == SS_DISCONNECTING)
963 return -EPIPE;
964 else if (sock->state != SS_CONNECTED)
965 return -ENOTCONN;
966 if (!*timeo_p)
967 return -EAGAIN;
968 if (signal_pending(current))
969 return sock_intr_errno(*timeo_p);
970
971 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
972 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500973 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400974 !tsk_conn_cong(tsk)) ||
975 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800976 finish_wait(sk_sleep(sk), &wait);
977 } while (!done);
978 return 0;
979}
980
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900981/**
Ying Xue247f0f32014-02-18 16:06:46 +0800982 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 * @sock: socket structure
984 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500985 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900986 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900988 *
989 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700990 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 */
Ying Xue1b784142015-03-02 15:37:48 +0800992static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700994 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +0800995 int ret;
996
997 lock_sock(sk);
998 ret = __tipc_send_stream(sock, m, dsz);
999 release_sock(sk);
1000
1001 return ret;
1002}
1003
1004static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1005{
1006 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001007 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001008 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001009 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001010 struct sk_buff_head *pktchain = &sk->sk_write_queue;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001011 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001012 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001013 int rc = -EINVAL;
1014 long timeo;
1015 u32 dnode;
1016 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001017 struct iov_iter save;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001018
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001019 /* Handle implied connection establishment */
1020 if (unlikely(dest)) {
Ying Xue39a02952015-03-02 15:37:47 +08001021 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001022 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001023 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001024 return rc;
1025 }
1026 if (dsz > (uint)INT_MAX)
1027 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001028
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001030 if (sock->state == SS_DISCONNECTING)
Ying Xue39a02952015-03-02 15:37:47 +08001031 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001032 else
Ying Xue39a02952015-03-02 15:37:47 +08001033 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001034 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001036 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001037 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001038
1039next:
Al Virof25dcc72014-11-28 15:52:29 -05001040 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001041 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001042 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001043 rc = tipc_msg_build(mhdr, m, sent, send, mtu, pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001044 if (unlikely(rc < 0))
Ying Xue39a02952015-03-02 15:37:47 +08001045 return rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001046 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001047 if (likely(!tsk_conn_cong(tsk))) {
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001048 rc = tipc_link_xmit(net, pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001049 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001050 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001051 sent += send;
1052 if (sent == dsz)
1053 break;
1054 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001055 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001056 if (rc == -EMSGSIZE) {
Ying Xuef2f98002015-01-09 15:27:05 +08001057 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1058 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001059 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001060 goto next;
1061 }
1062 if (rc != -ELINKCONG)
1063 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001064 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001066 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001067 if (rc)
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001068 __skb_queue_purge(pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001069 } while (!rc);
Ying Xue39a02952015-03-02 15:37:47 +08001070
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001071 return sent ? sent : rc;
1072}
1073
1074/**
1075 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001076 * @sock: socket structure
1077 * @m: message to send
1078 * @dsz: length of data to be transmitted
1079 *
1080 * Used for SOCK_SEQPACKET messages.
1081 *
1082 * Returns the number of bytes sent on success, or errno otherwise
1083 */
Ying Xue1b784142015-03-02 15:37:48 +08001084static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001085{
1086 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1087 return -EMSGSIZE;
1088
Ying Xue1b784142015-03-02 15:37:48 +08001089 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090}
1091
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001092/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001094static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001095 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096{
Ying Xue3721e9c2015-01-13 17:07:48 +08001097 struct sock *sk = &tsk->sk;
1098 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001099 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001101 msg_set_destnode(msg, peer_node);
1102 msg_set_destport(msg, peer_port);
1103 msg_set_type(msg, TIPC_CONN_MSG);
1104 msg_set_lookup_scope(msg, 0);
1105 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001106
Ying Xue2f55c432015-01-09 15:27:00 +08001107 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001108 tsk->probing_state = TIPC_CONN_OK;
1109 tsk->connected = 1;
Ying Xue3721e9c2015-01-13 17:07:48 +08001110 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001111 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1112 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113}
1114
1115/**
1116 * set_orig_addr - capture sender's address for received message
1117 * @m: descriptor for message info
1118 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001119 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120 * Note: Address is not captured if not requested by receiver.
1121 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001122static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001124 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001126 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 addr->family = AF_TIPC;
1128 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001129 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 addr->addr.id.ref = msg_origport(msg);
1131 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001132 addr->addr.name.domain = 0; /* could leave uninitialized */
1133 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 m->msg_namelen = sizeof(struct sockaddr_tipc);
1135 }
1136}
1137
1138/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001139 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 * @m: descriptor for message info
1141 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001142 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001143 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001145 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 * Returns 0 if successful, otherwise errno
1147 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001148static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1149 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150{
1151 u32 anc_data[3];
1152 u32 err;
1153 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001154 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 int res;
1156
1157 if (likely(m->msg_controllen == 0))
1158 return 0;
1159
1160 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 err = msg ? msg_errcode(msg) : 0;
1162 if (unlikely(err)) {
1163 anc_data[0] = err;
1164 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001165 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1166 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001168 if (anc_data[1]) {
1169 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1170 msg_data(msg));
1171 if (res)
1172 return res;
1173 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 }
1175
1176 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1178 switch (dest_type) {
1179 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001180 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 anc_data[0] = msg_nametype(msg);
1182 anc_data[1] = msg_namelower(msg);
1183 anc_data[2] = msg_namelower(msg);
1184 break;
1185 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001186 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 anc_data[0] = msg_nametype(msg);
1188 anc_data[1] = msg_namelower(msg);
1189 anc_data[2] = msg_nameupper(msg);
1190 break;
1191 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001192 has_name = (tsk->conn_type != 0);
1193 anc_data[0] = tsk->conn_type;
1194 anc_data[1] = tsk->conn_instance;
1195 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 break;
1197 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001198 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 }
Allan Stephens2db99832010-12-31 18:59:33 +00001200 if (has_name) {
1201 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1202 if (res)
1203 return res;
1204 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205
1206 return 0;
1207}
1208
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001209static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001210{
Ying Xuef2f98002015-01-09 15:27:05 +08001211 struct net *net = sock_net(&tsk->sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001212 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001213 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001214 u32 peer_port = tsk_peer_port(tsk);
1215 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001216
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001217 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001218 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001219 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1220 dnode, tsk_own_node(tsk), peer_port,
1221 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001222 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001223 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001224 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001225 msg_set_msgcnt(msg, ack);
Ying Xuef2f98002015-01-09 15:27:05 +08001226 tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001227}
1228
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001229static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001230{
1231 struct sock *sk = sock->sk;
1232 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001233 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001234 int err;
1235
1236 for (;;) {
1237 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001238 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001239 if (sock->state == SS_DISCONNECTING) {
1240 err = -ENOTCONN;
1241 break;
1242 }
1243 release_sock(sk);
1244 timeo = schedule_timeout(timeo);
1245 lock_sock(sk);
1246 }
1247 err = 0;
1248 if (!skb_queue_empty(&sk->sk_receive_queue))
1249 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001250 err = -EAGAIN;
1251 if (!timeo)
1252 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001253 err = sock_intr_errno(timeo);
1254 if (signal_pending(current))
1255 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001256 }
1257 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001258 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001259 return err;
1260}
1261
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001262/**
Ying Xue247f0f32014-02-18 16:06:46 +08001263 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 * @m: descriptor for message info
1265 * @buf_len: total size of user buffer area
1266 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001267 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1269 * If the complete message doesn't fit in user area, truncate it.
1270 *
1271 * Returns size of returned message data, errno otherwise
1272 */
Ying Xue1b784142015-03-02 15:37:48 +08001273static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1274 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001276 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001277 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 struct sk_buff *buf;
1279 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001280 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281 unsigned int sz;
1282 u32 err;
1283 int res;
1284
Allan Stephens0c3141e2008-04-15 00:22:02 -07001285 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286 if (unlikely(!buf_len))
1287 return -EINVAL;
1288
Allan Stephens0c3141e2008-04-15 00:22:02 -07001289 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290
Allan Stephens0c3141e2008-04-15 00:22:02 -07001291 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 res = -ENOTCONN;
1293 goto exit;
1294 }
1295
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001296 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001297restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298
Allan Stephens0c3141e2008-04-15 00:22:02 -07001299 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001300 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001301 if (res)
1302 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001303
1304 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001305 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 msg = buf_msg(buf);
1307 sz = msg_data_sz(msg);
1308 err = msg_errcode(msg);
1309
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001312 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 goto restart;
1314 }
1315
1316 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 set_orig_addr(m, msg);
1318
1319 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001320 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001321 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 goto exit;
1323
1324 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 if (!err) {
1326 if (unlikely(buf_len < sz)) {
1327 sz = buf_len;
1328 m->msg_flags |= MSG_TRUNC;
1329 }
David S. Miller51f3d022014-11-05 16:46:40 -05001330 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001331 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 res = sz;
1334 } else {
1335 if ((sock->state == SS_READY) ||
1336 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1337 res = 0;
1338 else
1339 res = -ECONNRESET;
1340 }
1341
1342 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001344 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001345 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001346 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001347 tsk->rcv_unacked = 0;
1348 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001349 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001350 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001352 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 return res;
1354}
1355
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001356/**
Ying Xue247f0f32014-02-18 16:06:46 +08001357 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 * @m: descriptor for message info
1359 * @buf_len: total size of user buffer area
1360 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001361 *
1362 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 * will optionally wait for more; never truncates data.
1364 *
1365 * Returns size of returned message data, errno otherwise
1366 */
Ying Xue1b784142015-03-02 15:37:48 +08001367static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1368 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001370 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001371 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 struct sk_buff *buf;
1373 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001374 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001376 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001379 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380
1381 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 if (unlikely(!buf_len))
1383 return -EINVAL;
1384
Allan Stephens0c3141e2008-04-15 00:22:02 -07001385 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001387 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 res = -ENOTCONN;
1389 goto exit;
1390 }
1391
Florian Westphal3720d402010-08-17 11:00:04 +00001392 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001393 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001394
Allan Stephens0c3141e2008-04-15 00:22:02 -07001395restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001396 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001397 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001398 if (res)
1399 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001400
1401 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001402 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 msg = buf_msg(buf);
1404 sz = msg_data_sz(msg);
1405 err = msg_errcode(msg);
1406
1407 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001409 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 goto restart;
1411 }
1412
1413 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001414 if (sz_copied == 0) {
1415 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001416 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001417 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 goto exit;
1419 }
1420
1421 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001423 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424
Allan Stephens0232fd02011-02-21 09:45:40 -05001425 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 needed = (buf_len - sz_copied);
1427 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001428
David S. Miller51f3d022014-11-05 16:46:40 -05001429 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1430 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001431 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001433
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 sz_copied += sz_to_copy;
1435
1436 if (sz_to_copy < sz) {
1437 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001438 TIPC_SKB_CB(buf)->handle =
1439 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 goto exit;
1441 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 } else {
1443 if (sz_copied != 0)
1444 goto exit; /* can't add error msg to valid data */
1445
1446 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1447 res = 0;
1448 else
1449 res = -ECONNRESET;
1450 }
1451
1452 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001454 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001455 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001456 tsk->rcv_unacked = 0;
1457 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001458 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001459 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460
1461 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001462 if ((sz_copied < buf_len) && /* didn't get all requested data */
1463 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001464 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001465 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1466 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 goto restart;
1468
1469exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001470 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001471 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472}
1473
1474/**
Ying Xuef288bef2012-08-21 11:16:57 +08001475 * tipc_write_space - wake up thread if port congestion is released
1476 * @sk: socket
1477 */
1478static void tipc_write_space(struct sock *sk)
1479{
1480 struct socket_wq *wq;
1481
1482 rcu_read_lock();
1483 wq = rcu_dereference(sk->sk_wq);
1484 if (wq_has_sleeper(wq))
1485 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1486 POLLWRNORM | POLLWRBAND);
1487 rcu_read_unlock();
1488}
1489
1490/**
1491 * tipc_data_ready - wake up threads to indicate messages have been received
1492 * @sk: socket
1493 * @len: the length of messages
1494 */
David S. Miller676d2362014-04-11 16:15:36 -04001495static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001496{
1497 struct socket_wq *wq;
1498
1499 rcu_read_lock();
1500 wq = rcu_dereference(sk->sk_wq);
1501 if (wq_has_sleeper(wq))
1502 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1503 POLLRDNORM | POLLRDBAND);
1504 rcu_read_unlock();
1505}
1506
1507/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001508 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001509 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001510 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001511 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001512 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001513 */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001514static int filter_connect(struct tipc_sock *tsk, struct sk_buff **skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001515{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001516 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001517 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001518 struct socket *sock = sk->sk_socket;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001519 struct tipc_msg *msg = buf_msg(*skb);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001520 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001521
1522 if (msg_mcast(msg))
1523 return retval;
1524
1525 switch ((int)sock->state) {
1526 case SS_CONNECTED:
1527 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001528 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001529 if (unlikely(msg_errcode(msg))) {
1530 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001531 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001532 /* let timer expire on it's own */
Ying Xuef2f98002015-01-09 15:27:05 +08001533 tipc_node_remove_conn(net, tsk_peer_node(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08001534 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001535 }
1536 retval = TIPC_OK;
1537 }
1538 break;
1539 case SS_CONNECTING:
1540 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001541
1542 if (unlikely(!msg_connected(msg)))
1543 break;
1544
Ying Xue584d24b2012-11-29 18:51:19 -05001545 if (unlikely(msg_errcode(msg))) {
1546 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001547 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001548 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001549 break;
1550 }
1551
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001552 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001553 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001554 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001555 retval = TIPC_OK;
1556 break;
1557 }
1558
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001559 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1560 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001561 sock->state = SS_CONNECTED;
1562
Ying Xue584d24b2012-11-29 18:51:19 -05001563 /* If an incoming message is an 'ACK-', it should be
1564 * discarded here because it doesn't contain useful
1565 * data. In addition, we should try to wake up
1566 * connect() routine if sleeping.
1567 */
1568 if (msg_data_sz(msg) == 0) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001569 kfree_skb(*skb);
1570 *skb = NULL;
Ying Xue584d24b2012-11-29 18:51:19 -05001571 if (waitqueue_active(sk_sleep(sk)))
1572 wake_up_interruptible(sk_sleep(sk));
1573 }
1574 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001575 break;
1576 case SS_LISTENING:
1577 case SS_UNCONNECTED:
1578 /* Accept only SYN message */
1579 if (!msg_connected(msg) && !(msg_errcode(msg)))
1580 retval = TIPC_OK;
1581 break;
1582 case SS_DISCONNECTING:
1583 break;
1584 default:
1585 pr_err("Unknown socket state %u\n", sock->state);
1586 }
1587 return retval;
1588}
1589
1590/**
Ying Xueaba79f32013-01-20 23:30:09 +01001591 * rcvbuf_limit - get proper overload limit of socket receive queue
1592 * @sk: socket
1593 * @buf: message
1594 *
1595 * For all connection oriented messages, irrespective of importance,
1596 * the default overload value (i.e. 67MB) is set as limit.
1597 *
1598 * For all connectionless messages, by default new queue limits are
1599 * as belows:
1600 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001601 * TIPC_LOW_IMPORTANCE (4 MB)
1602 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1603 * TIPC_HIGH_IMPORTANCE (16 MB)
1604 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001605 *
1606 * Returns overload limit according to corresponding message importance
1607 */
1608static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1609{
1610 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001611
1612 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001613 return sysctl_tipc_rmem[2];
1614
1615 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1616 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001617}
1618
1619/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001620 * filter_rcv - validate incoming message
1621 * @sk: socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001622 * @skb: pointer to message. Set to NULL if buffer is consumed.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001623 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001624 * Enqueues message on receive queue if acceptable; optionally handles
1625 * disconnect indication for a connected socket.
1626 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001627 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001628 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001629 * Returns 0 (TIPC_OK) if message was ok, -TIPC error code if rejected
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001631static int filter_rcv(struct sock *sk, struct sk_buff **skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001633 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001634 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001635 struct tipc_msg *msg = buf_msg(*skb);
1636 unsigned int limit = rcvbuf_limit(sk, *skb);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001637 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001639 if (unlikely(msg_user(msg) == CONN_MANAGER)) {
1640 tipc_sk_proto_rcv(tsk, skb);
1641 return TIPC_OK;
1642 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001643
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001644 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001645 kfree_skb(*skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001646 tsk->link_cong = 0;
1647 sk->sk_write_space(sk);
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001648 *skb = NULL;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001649 return TIPC_OK;
1650 }
1651
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001653 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001654 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001655
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001657 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001658 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659 } else {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001660 rc = filter_connect(tsk, skb);
1661 if (rc != TIPC_OK || !*skb)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001662 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 }
1664
1665 /* Reject message if there isn't room to queue it */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001666 if (sk_rmem_alloc_get(sk) + (*skb)->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001667 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668
Ying Xueaba79f32013-01-20 23:30:09 +01001669 /* Enqueue message */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001670 TIPC_SKB_CB(*skb)->handle = NULL;
1671 __skb_queue_tail(&sk->sk_receive_queue, *skb);
1672 skb_set_owner_r(*skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001673
David S. Miller676d2362014-04-11 16:15:36 -04001674 sk->sk_data_ready(sk);
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001675 *skb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 return TIPC_OK;
1677}
1678
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001679/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001680 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001681 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001682 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001683 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001684 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 *
1686 * Returns 0
1687 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001688static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689{
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001690 int err;
1691 atomic_t *dcnt;
1692 u32 dnode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001693 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +08001694 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001695 uint truesize = skb->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001696
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001697 err = filter_rcv(sk, &skb);
1698 if (likely(!skb)) {
1699 dcnt = &tsk->dupl_rcvcnt;
1700 if (atomic_read(dcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1701 atomic_add(truesize, dcnt);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001702 return 0;
1703 }
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001704 if (!err || tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode, -err))
1705 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001706 return 0;
1707}
1708
1709/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001710 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1711 * inputq and try adding them to socket or backlog queue
1712 * @inputq: list of incoming buffers with potentially different destinations
1713 * @sk: socket where the buffers should be enqueued
1714 * @dport: port number for the socket
1715 * @_skb: returned buffer to be forwarded or rejected, if applicable
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001716 *
1717 * Caller must hold socket lock
1718 *
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001719 * Returns TIPC_OK if all buffers enqueued, otherwise -TIPC_ERR_OVERLOAD
1720 * or -TIPC_ERR_NO_PORT
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001721 */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001722static int tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1723 u32 dport, struct sk_buff **_skb)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001724{
1725 unsigned int lim;
1726 atomic_t *dcnt;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001727 int err;
1728 struct sk_buff *skb;
1729 unsigned long time_limit = jiffies + 2;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001730
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001731 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001732 if (unlikely(time_after_eq(jiffies, time_limit)))
1733 return TIPC_OK;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001734 skb = tipc_skb_dequeue(inputq, dport);
1735 if (unlikely(!skb))
1736 return TIPC_OK;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001737 if (!sock_owned_by_user(sk)) {
1738 err = filter_rcv(sk, &skb);
1739 if (likely(!skb))
1740 continue;
1741 *_skb = skb;
1742 return err;
1743 }
1744 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1745 if (sk->sk_backlog.len)
1746 atomic_set(dcnt, 0);
1747 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1748 if (likely(!sk_add_backlog(sk, skb, lim)))
1749 continue;
1750 *_skb = skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001751 return -TIPC_ERR_OVERLOAD;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001752 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001753 return TIPC_OK;
1754}
1755
1756/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001757 * tipc_sk_rcv - handle a chain of incoming buffers
1758 * @inputq: buffer list containing the buffers
1759 * Consumes all buffers in list until inputq is empty
1760 * Note: may be called in multiple threads referring to the same queue
1761 * Returns 0 if last buffer was accepted, otherwise -EHOSTUNREACH
1762 * Only node local calls check the return value, sending single-buffer queues
Allan Stephens0c3141e2008-04-15 00:22:02 -07001763 */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001764int tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001765{
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001766 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001767 int err;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001768 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001769 struct tipc_sock *tsk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001770 struct tipc_net *tn;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001771 struct sock *sk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001772
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001773 while (skb_queue_len(inputq)) {
Erik Hugne9871b272015-04-23 09:37:39 -04001774 err = -TIPC_ERR_NO_PORT;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001775 skb = NULL;
1776 dport = tipc_skb_peek_port(inputq, dport);
1777 tsk = tipc_sk_lookup(net, dport);
1778 if (likely(tsk)) {
1779 sk = &tsk->sk;
1780 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1781 err = tipc_sk_enqueue(inputq, sk, dport, &skb);
1782 spin_unlock_bh(&sk->sk_lock.slock);
1783 dport = 0;
1784 }
1785 sock_put(sk);
1786 } else {
1787 skb = tipc_skb_dequeue(inputq, dport);
1788 }
1789 if (likely(!skb))
1790 continue;
1791 if (tipc_msg_lookup_dest(net, skb, &dnode, &err))
1792 goto xmit;
1793 if (!err) {
1794 dnode = msg_destnode(buf_msg(skb));
1795 goto xmit;
1796 }
1797 tn = net_generic(net, tipc_net_id);
1798 if (!tipc_msg_reverse(tn->own_addr, skb, &dnode, -err))
1799 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001800xmit:
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001801 tipc_link_xmit_skb(net, skb, dnode, dport);
1802 }
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001803 return err ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001804}
1805
Ying Xue78eb3a52014-01-17 09:50:03 +08001806static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1807{
1808 struct sock *sk = sock->sk;
1809 DEFINE_WAIT(wait);
1810 int done;
1811
1812 do {
1813 int err = sock_error(sk);
1814 if (err)
1815 return err;
1816 if (!*timeo_p)
1817 return -ETIMEDOUT;
1818 if (signal_pending(current))
1819 return sock_intr_errno(*timeo_p);
1820
1821 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1822 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1823 finish_wait(sk_sleep(sk), &wait);
1824 } while (!done);
1825 return 0;
1826}
1827
Per Lidenb97bf3f2006-01-02 19:04:38 +01001828/**
Ying Xue247f0f32014-02-18 16:06:46 +08001829 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 * @sock: socket structure
1831 * @dest: socket address for destination port
1832 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001833 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 *
1835 * Returns 0 on success, errno otherwise
1836 */
Ying Xue247f0f32014-02-18 16:06:46 +08001837static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1838 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001840 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001841 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001842 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1843 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001844 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Ying Xue78eb3a52014-01-17 09:50:03 +08001845 socket_state previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001846 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847
Allan Stephens0c3141e2008-04-15 00:22:02 -07001848 lock_sock(sk);
1849
Erik Hugnef2f80362015-03-19 09:02:19 +01001850 /* DGRAM/RDM connect(), just save the destaddr */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001851 if (sock->state == SS_READY) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001852 if (dst->family == AF_UNSPEC) {
1853 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1854 tsk->connected = 0;
Sasha Levin610600c2015-03-23 15:30:00 -04001855 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1856 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001857 } else {
1858 memcpy(&tsk->remote, dest, destlen);
1859 tsk->connected = 1;
1860 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001861 goto exit;
1862 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863
Allan Stephensb89741a2008-04-15 00:20:37 -07001864 /*
1865 * Reject connection attempt using multicast address
1866 *
1867 * Note: send_msg() validates the rest of the address fields,
1868 * so there's no need to do it here
1869 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001870 if (dst->addrtype == TIPC_ADDR_MCAST) {
1871 res = -EINVAL;
1872 goto exit;
1873 }
1874
Ying Xue78eb3a52014-01-17 09:50:03 +08001875 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001876 switch (sock->state) {
1877 case SS_UNCONNECTED:
1878 /* Send a 'SYN-' to destination */
1879 m.msg_name = dest;
1880 m.msg_namelen = destlen;
1881
1882 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1883 * indicate send_msg() is never blocked.
1884 */
1885 if (!timeout)
1886 m.msg_flags = MSG_DONTWAIT;
1887
Ying Xue39a02952015-03-02 15:37:47 +08001888 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001889 if ((res < 0) && (res != -EWOULDBLOCK))
1890 goto exit;
1891
1892 /* Just entered SS_CONNECTING state; the only
1893 * difference is that return value in non-blocking
1894 * case is EINPROGRESS, rather than EALREADY.
1895 */
1896 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001897 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001898 if (previous == SS_CONNECTING)
1899 res = -EALREADY;
1900 if (!timeout)
1901 goto exit;
1902 timeout = msecs_to_jiffies(timeout);
1903 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1904 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001905 break;
1906 case SS_CONNECTED:
1907 res = -EISCONN;
1908 break;
1909 default:
1910 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001911 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001912 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001913exit:
1914 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001915 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916}
1917
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001918/**
Ying Xue247f0f32014-02-18 16:06:46 +08001919 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 * @sock: socket structure
1921 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001922 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923 * Returns 0 on success, errno otherwise
1924 */
Ying Xue247f0f32014-02-18 16:06:46 +08001925static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001926{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001927 struct sock *sk = sock->sk;
1928 int res;
1929
1930 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001931
Ying Xue245f3d32011-07-06 06:01:13 -04001932 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001933 res = -EINVAL;
1934 else {
1935 sock->state = SS_LISTENING;
1936 res = 0;
1937 }
1938
1939 release_sock(sk);
1940 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001941}
1942
Ying Xue6398e232014-01-17 09:50:04 +08001943static int tipc_wait_for_accept(struct socket *sock, long timeo)
1944{
1945 struct sock *sk = sock->sk;
1946 DEFINE_WAIT(wait);
1947 int err;
1948
1949 /* True wake-one mechanism for incoming connections: only
1950 * one process gets woken up, not the 'whole herd'.
1951 * Since we do not 'race & poll' for established sockets
1952 * anymore, the common case will execute the loop only once.
1953 */
1954 for (;;) {
1955 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1956 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001957 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001958 release_sock(sk);
1959 timeo = schedule_timeout(timeo);
1960 lock_sock(sk);
1961 }
1962 err = 0;
1963 if (!skb_queue_empty(&sk->sk_receive_queue))
1964 break;
1965 err = -EINVAL;
1966 if (sock->state != SS_LISTENING)
1967 break;
Ying Xue6398e232014-01-17 09:50:04 +08001968 err = -EAGAIN;
1969 if (!timeo)
1970 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001971 err = sock_intr_errno(timeo);
1972 if (signal_pending(current))
1973 break;
Ying Xue6398e232014-01-17 09:50:04 +08001974 }
1975 finish_wait(sk_sleep(sk), &wait);
1976 return err;
1977}
1978
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001979/**
Ying Xue247f0f32014-02-18 16:06:46 +08001980 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981 * @sock: listening socket
1982 * @newsock: new socket that is to be connected
1983 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001984 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001985 * Returns 0 on success, errno otherwise
1986 */
Ying Xue247f0f32014-02-18 16:06:46 +08001987static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001989 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001991 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001992 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001993 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001994 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001995
Allan Stephens0c3141e2008-04-15 00:22:02 -07001996 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001997
Allan Stephens0c3141e2008-04-15 00:22:02 -07001998 if (sock->state != SS_LISTENING) {
1999 res = -EINVAL;
2000 goto exit;
2001 }
Ying Xue6398e232014-01-17 09:50:04 +08002002 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2003 res = tipc_wait_for_accept(sock, timeo);
2004 if (res)
2005 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002006
2007 buf = skb_peek(&sk->sk_receive_queue);
2008
Ying Xuec5fa7b32013-06-17 10:54:39 -04002009 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002010 if (res)
2011 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002012
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002013 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002014 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002015 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002016
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002017 /* we lock on new_sk; but lockdep sees the lock on sk */
2018 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002019
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002020 /*
2021 * Reject any stray messages received by new socket
2022 * before the socket lock was taken (very, very unlikely)
2023 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002024 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002025
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002026 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002027 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002028 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002029
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002030 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002031 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002032 new_tsock->conn_type = msg_nametype(msg);
2033 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002035
2036 /*
2037 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2038 * Respond to 'SYN+' by queuing it on new socket.
2039 */
2040 if (!msg_data_sz(msg)) {
2041 struct msghdr m = {NULL,};
2042
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002043 tsk_advance_rx_queue(sk);
Ying Xue39a02952015-03-02 15:37:47 +08002044 __tipc_send_stream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002045 } else {
2046 __skb_dequeue(&sk->sk_receive_queue);
2047 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002048 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002049 }
2050 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002052 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053 return res;
2054}
2055
2056/**
Ying Xue247f0f32014-02-18 16:06:46 +08002057 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002059 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002060 *
2061 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002062 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063 * Returns 0 on success, errno otherwise
2064 */
Ying Xue247f0f32014-02-18 16:06:46 +08002065static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002066{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002067 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002068 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002069 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002070 struct sk_buff *skb;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002071 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072 int res;
2073
Allan Stephense247a8f2008-03-06 15:05:38 -08002074 if (how != SHUT_RDWR)
2075 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076
Allan Stephens0c3141e2008-04-15 00:22:02 -07002077 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078
2079 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002080 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081 case SS_CONNECTED:
2082
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002084 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002085 skb = __skb_dequeue(&sk->sk_receive_queue);
2086 if (skb) {
2087 if (TIPC_SKB_CB(skb)->handle != NULL) {
2088 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089 goto restart;
2090 }
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002091 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
Ying Xue34747532015-01-09 15:27:10 +08002092 TIPC_CONN_SHUTDOWN))
Ying Xuef2f98002015-01-09 15:27:05 +08002093 tipc_link_xmit_skb(net, skb, dnode,
2094 tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002095 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002096 dnode = tsk_peer_node(tsk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002097
2098 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002099 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002100 0, dnode, tsk_own_node(tsk),
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002101 tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002102 tsk->portid, TIPC_CONN_SHUTDOWN);
Ying Xuef2f98002015-01-09 15:27:05 +08002103 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002105 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002106 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002107 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108 /* fall through */
2109
2110 case SS_DISCONNECTING:
2111
Ying Xue75031152012-10-29 09:38:15 -04002112 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002113 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002114
2115 /* Wake up anyone sleeping in poll */
2116 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 res = 0;
2118 break;
2119
2120 default:
2121 res = -ENOTCONN;
2122 }
2123
Allan Stephens0c3141e2008-04-15 00:22:02 -07002124 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002125 return res;
2126}
2127
Ying Xuef2f2a962015-01-09 15:27:02 +08002128static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002129{
Ying Xuef2f2a962015-01-09 15:27:02 +08002130 struct tipc_sock *tsk = (struct tipc_sock *)data;
2131 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002132 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002133 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002134 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002135
Jon Paul Maloy57289012014-08-22 18:09:09 -04002136 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002137 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002138 bh_unlock_sock(sk);
2139 goto exit;
2140 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002141 peer_port = tsk_peer_port(tsk);
2142 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002143
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002144 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002145 /* Previous probe not answered -> self abort */
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002146 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +08002147 TIPC_CONN_MSG, SHORT_H_SIZE, 0,
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002148 own_node, peer_node, tsk->portid,
Ying Xue34747532015-01-09 15:27:10 +08002149 peer_port, TIPC_ERR_NO_PORT);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002150 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002151 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2152 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002153 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002154 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002155 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002156 }
2157 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002158 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +08002159 tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002160exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002161 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002162}
2163
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002164static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002165 struct tipc_name_seq const *seq)
2166{
Ying Xuef2f98002015-01-09 15:27:05 +08002167 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002168 struct publication *publ;
2169 u32 key;
2170
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002171 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002172 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002173 key = tsk->portid + tsk->pub_count + 1;
2174 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002175 return -EADDRINUSE;
2176
Ying Xuef2f98002015-01-09 15:27:05 +08002177 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002178 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002179 if (unlikely(!publ))
2180 return -EINVAL;
2181
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002182 list_add(&publ->pport_list, &tsk->publications);
2183 tsk->pub_count++;
2184 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002185 return 0;
2186}
2187
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002188static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002189 struct tipc_name_seq const *seq)
2190{
Ying Xuef2f98002015-01-09 15:27:05 +08002191 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002192 struct publication *publ;
2193 struct publication *safe;
2194 int rc = -EINVAL;
2195
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002196 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002197 if (seq) {
2198 if (publ->scope != scope)
2199 continue;
2200 if (publ->type != seq->type)
2201 continue;
2202 if (publ->lower != seq->lower)
2203 continue;
2204 if (publ->upper != seq->upper)
2205 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002206 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002207 publ->ref, publ->key);
2208 rc = 0;
2209 break;
2210 }
Ying Xuef2f98002015-01-09 15:27:05 +08002211 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002212 publ->ref, publ->key);
2213 rc = 0;
2214 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002215 if (list_empty(&tsk->publications))
2216 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002217 return rc;
2218}
2219
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002220/* tipc_sk_reinit: set non-zero address in all existing sockets
2221 * when we go from standalone to network mode.
2222 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002223void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002224{
Ying Xuee05b31f2015-01-09 15:27:08 +08002225 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002226 const struct bucket_table *tbl;
2227 struct rhash_head *pos;
2228 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002229 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002230 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002231
Ying Xue07f6c4b2015-01-07 13:41:58 +08002232 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002233 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002234 for (i = 0; i < tbl->size; i++) {
2235 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2236 spin_lock_bh(&tsk->sk.sk_lock.slock);
2237 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002238 msg_set_prevnode(msg, tn->own_addr);
2239 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002240 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2241 }
2242 }
2243 rcu_read_unlock();
2244}
2245
Ying Xuee05b31f2015-01-09 15:27:08 +08002246static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002247{
Ying Xuee05b31f2015-01-09 15:27:08 +08002248 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002249 struct tipc_sock *tsk;
2250
2251 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002252 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002253 if (tsk)
2254 sock_hold(&tsk->sk);
2255 rcu_read_unlock();
2256
2257 return tsk;
2258}
2259
2260static int tipc_sk_insert(struct tipc_sock *tsk)
2261{
Ying Xuee05b31f2015-01-09 15:27:08 +08002262 struct sock *sk = &tsk->sk;
2263 struct net *net = sock_net(sk);
2264 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002265 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2266 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2267
2268 while (remaining--) {
2269 portid++;
2270 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2271 portid = TIPC_MIN_PORT;
2272 tsk->portid = portid;
2273 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002274 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2275 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002276 return 0;
2277 sock_put(&tsk->sk);
2278 }
2279
2280 return -1;
2281}
2282
2283static void tipc_sk_remove(struct tipc_sock *tsk)
2284{
2285 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002286 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002287
Herbert Xu6cca72892015-03-20 21:57:05 +11002288 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002289 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2290 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002291 }
2292}
2293
Herbert Xu6cca72892015-03-20 21:57:05 +11002294static const struct rhashtable_params tsk_rht_params = {
2295 .nelem_hint = 192,
2296 .head_offset = offsetof(struct tipc_sock, node),
2297 .key_offset = offsetof(struct tipc_sock, portid),
2298 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002299 .max_size = 1048576,
2300 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002301 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002302};
2303
Ying Xuee05b31f2015-01-09 15:27:08 +08002304int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002305{
Ying Xuee05b31f2015-01-09 15:27:08 +08002306 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002307
Herbert Xu6cca72892015-03-20 21:57:05 +11002308 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002309}
2310
Ying Xuee05b31f2015-01-09 15:27:08 +08002311void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002312{
Ying Xuee05b31f2015-01-09 15:27:08 +08002313 struct tipc_net *tn = net_generic(net, tipc_net_id);
2314
Ying Xue07f6c4b2015-01-07 13:41:58 +08002315 /* Wait for socket readers to complete */
2316 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002317
Ying Xuee05b31f2015-01-09 15:27:08 +08002318 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002319}
2320
2321/**
Ying Xue247f0f32014-02-18 16:06:46 +08002322 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002323 * @sock: socket structure
2324 * @lvl: option level
2325 * @opt: option identifier
2326 * @ov: pointer to new option value
2327 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002328 *
2329 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002330 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002331 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002332 * Returns 0 on success, errno otherwise
2333 */
Ying Xue247f0f32014-02-18 16:06:46 +08002334static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2335 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002336{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002337 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002338 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002339 u32 value;
2340 int res;
2341
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002342 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2343 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002344 if (lvl != SOL_TIPC)
2345 return -ENOPROTOOPT;
2346 if (ol < sizeof(value))
2347 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002348 res = get_user(value, (u32 __user *)ov);
2349 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350 return res;
2351
Allan Stephens0c3141e2008-04-15 00:22:02 -07002352 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002353
Per Lidenb97bf3f2006-01-02 19:04:38 +01002354 switch (opt) {
2355 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002356 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002357 break;
2358 case TIPC_SRC_DROPPABLE:
2359 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002360 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002361 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362 res = -ENOPROTOOPT;
2363 break;
2364 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002365 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002366 break;
2367 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002368 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002369 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002370 break;
2371 default:
2372 res = -EINVAL;
2373 }
2374
Allan Stephens0c3141e2008-04-15 00:22:02 -07002375 release_sock(sk);
2376
Per Lidenb97bf3f2006-01-02 19:04:38 +01002377 return res;
2378}
2379
2380/**
Ying Xue247f0f32014-02-18 16:06:46 +08002381 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002382 * @sock: socket structure
2383 * @lvl: option level
2384 * @opt: option identifier
2385 * @ov: receptacle for option value
2386 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002387 *
2388 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002389 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002390 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002391 * Returns 0 on success, errno otherwise
2392 */
Ying Xue247f0f32014-02-18 16:06:46 +08002393static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2394 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002395{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002396 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002397 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002398 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002399 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002400 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002401
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002402 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2403 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002404 if (lvl != SOL_TIPC)
2405 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002406 res = get_user(len, ol);
2407 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002408 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409
Allan Stephens0c3141e2008-04-15 00:22:02 -07002410 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002411
2412 switch (opt) {
2413 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002414 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002415 break;
2416 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002417 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002418 break;
2419 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002420 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002421 break;
2422 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002423 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002424 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002425 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002426 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002427 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002428 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002429 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002430 value = skb_queue_len(&sk->sk_receive_queue);
2431 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002432 default:
2433 res = -EINVAL;
2434 }
2435
Allan Stephens0c3141e2008-04-15 00:22:02 -07002436 release_sock(sk);
2437
Paul Gortmaker25860c32010-12-31 18:59:31 +00002438 if (res)
2439 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002440
Paul Gortmaker25860c32010-12-31 18:59:31 +00002441 if (len < sizeof(value))
2442 return -EINVAL;
2443
2444 if (copy_to_user(ov, &value, sizeof(value)))
2445 return -EFAULT;
2446
2447 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448}
2449
Ying Xuef2f98002015-01-09 15:27:05 +08002450static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002451{
Ying Xuef2f98002015-01-09 15:27:05 +08002452 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002453 struct tipc_sioc_ln_req lnr;
2454 void __user *argp = (void __user *)arg;
2455
2456 switch (cmd) {
2457 case SIOCGETLINKNAME:
2458 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2459 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002460 if (!tipc_node_get_linkname(sock_net(sk),
2461 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002462 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2463 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2464 return -EFAULT;
2465 return 0;
2466 }
2467 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002468 default:
2469 return -ENOIOCTLCMD;
2470 }
2471}
2472
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002473/* Protocol switches for the various types of TIPC sockets */
2474
Florian Westphalbca65ea2008-02-07 18:18:01 -08002475static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002476 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002477 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002478 .release = tipc_release,
2479 .bind = tipc_bind,
2480 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002481 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002482 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002483 .getname = tipc_getname,
2484 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002485 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002486 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002487 .shutdown = tipc_shutdown,
2488 .setsockopt = tipc_setsockopt,
2489 .getsockopt = tipc_getsockopt,
2490 .sendmsg = tipc_sendmsg,
2491 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002492 .mmap = sock_no_mmap,
2493 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002494};
2495
Florian Westphalbca65ea2008-02-07 18:18:01 -08002496static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002497 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002498 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002499 .release = tipc_release,
2500 .bind = tipc_bind,
2501 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002502 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002503 .accept = tipc_accept,
2504 .getname = tipc_getname,
2505 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002506 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002507 .listen = tipc_listen,
2508 .shutdown = tipc_shutdown,
2509 .setsockopt = tipc_setsockopt,
2510 .getsockopt = tipc_getsockopt,
2511 .sendmsg = tipc_send_packet,
2512 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002513 .mmap = sock_no_mmap,
2514 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002515};
2516
Florian Westphalbca65ea2008-02-07 18:18:01 -08002517static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002518 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002519 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002520 .release = tipc_release,
2521 .bind = tipc_bind,
2522 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002523 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002524 .accept = tipc_accept,
2525 .getname = tipc_getname,
2526 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002527 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002528 .listen = tipc_listen,
2529 .shutdown = tipc_shutdown,
2530 .setsockopt = tipc_setsockopt,
2531 .getsockopt = tipc_getsockopt,
2532 .sendmsg = tipc_send_stream,
2533 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002534 .mmap = sock_no_mmap,
2535 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002536};
2537
Florian Westphalbca65ea2008-02-07 18:18:01 -08002538static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002539 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002540 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002541 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002542};
2543
2544static struct proto tipc_proto = {
2545 .name = "TIPC",
2546 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002547 .obj_size = sizeof(struct tipc_sock),
2548 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002549};
2550
2551/**
Per Liden4323add2006-01-18 00:38:21 +01002552 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002553 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002554 * Returns 0 on success, errno otherwise
2555 */
Per Liden4323add2006-01-18 00:38:21 +01002556int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002557{
2558 int res;
2559
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002560 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002561 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002562 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002563 goto out;
2564 }
2565
2566 res = sock_register(&tipc_family_ops);
2567 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002568 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002569 proto_unregister(&tipc_proto);
2570 goto out;
2571 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002572 out:
2573 return res;
2574}
2575
2576/**
Per Liden4323add2006-01-18 00:38:21 +01002577 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002578 */
Per Liden4323add2006-01-18 00:38:21 +01002579void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002580{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002581 sock_unregister(tipc_family_ops.family);
2582 proto_unregister(&tipc_proto);
2583}
Richard Alpe34b78a122014-11-20 10:29:10 +01002584
2585/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002586static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002587{
2588 u32 peer_node;
2589 u32 peer_port;
2590 struct nlattr *nest;
2591
2592 peer_node = tsk_peer_node(tsk);
2593 peer_port = tsk_peer_port(tsk);
2594
2595 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2596
2597 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2598 goto msg_full;
2599 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2600 goto msg_full;
2601
2602 if (tsk->conn_type != 0) {
2603 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2604 goto msg_full;
2605 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2606 goto msg_full;
2607 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2608 goto msg_full;
2609 }
2610 nla_nest_end(skb, nest);
2611
2612 return 0;
2613
2614msg_full:
2615 nla_nest_cancel(skb, nest);
2616
2617 return -EMSGSIZE;
2618}
2619
2620/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002621static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2622 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002623{
2624 int err;
2625 void *hdr;
2626 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002627 struct net *net = sock_net(skb->sk);
2628 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002629
2630 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002631 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002632 if (!hdr)
2633 goto msg_cancel;
2634
2635 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2636 if (!attrs)
2637 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002638 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002639 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002640 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002641 goto attr_msg_cancel;
2642
2643 if (tsk->connected) {
2644 err = __tipc_nl_add_sk_con(skb, tsk);
2645 if (err)
2646 goto attr_msg_cancel;
2647 } else if (!list_empty(&tsk->publications)) {
2648 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2649 goto attr_msg_cancel;
2650 }
2651 nla_nest_end(skb, attrs);
2652 genlmsg_end(skb, hdr);
2653
2654 return 0;
2655
2656attr_msg_cancel:
2657 nla_nest_cancel(skb, attrs);
2658genlmsg_cancel:
2659 genlmsg_cancel(skb, hdr);
2660msg_cancel:
2661 return -EMSGSIZE;
2662}
2663
2664int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2665{
2666 int err;
2667 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002668 const struct bucket_table *tbl;
2669 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002670 struct net *net = sock_net(skb->sk);
2671 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002672 u32 tbl_id = cb->args[0];
2673 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002674
Ying Xue07f6c4b2015-01-07 13:41:58 +08002675 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002676 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002677 for (; tbl_id < tbl->size; tbl_id++) {
2678 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002679 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002680 if (prev_portid && prev_portid != tsk->portid) {
2681 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2682 continue;
2683 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002684
Richard Alped6e164e2015-01-16 12:30:40 +01002685 err = __tipc_nl_add_sk(skb, cb, tsk);
2686 if (err) {
2687 prev_portid = tsk->portid;
2688 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2689 goto out;
2690 }
2691 prev_portid = 0;
2692 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002693 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002694 }
Richard Alped6e164e2015-01-16 12:30:40 +01002695out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002696 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002697 cb->args[0] = tbl_id;
2698 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002699
2700 return skb->len;
2701}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002702
2703/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002704static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2705 struct netlink_callback *cb,
2706 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002707{
2708 void *hdr;
2709 struct nlattr *attrs;
2710
2711 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002712 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002713 if (!hdr)
2714 goto msg_cancel;
2715
2716 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2717 if (!attrs)
2718 goto genlmsg_cancel;
2719
2720 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2721 goto attr_msg_cancel;
2722 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2723 goto attr_msg_cancel;
2724 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2725 goto attr_msg_cancel;
2726 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2727 goto attr_msg_cancel;
2728
2729 nla_nest_end(skb, attrs);
2730 genlmsg_end(skb, hdr);
2731
2732 return 0;
2733
2734attr_msg_cancel:
2735 nla_nest_cancel(skb, attrs);
2736genlmsg_cancel:
2737 genlmsg_cancel(skb, hdr);
2738msg_cancel:
2739 return -EMSGSIZE;
2740}
2741
2742/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002743static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2744 struct netlink_callback *cb,
2745 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002746{
2747 int err;
2748 struct publication *p;
2749
2750 if (*last_publ) {
2751 list_for_each_entry(p, &tsk->publications, pport_list) {
2752 if (p->key == *last_publ)
2753 break;
2754 }
2755 if (p->key != *last_publ) {
2756 /* We never set seq or call nl_dump_check_consistent()
2757 * this means that setting prev_seq here will cause the
2758 * consistence check to fail in the netlink callback
2759 * handler. Resulting in the last NLMSG_DONE message
2760 * having the NLM_F_DUMP_INTR flag set.
2761 */
2762 cb->prev_seq = 1;
2763 *last_publ = 0;
2764 return -EPIPE;
2765 }
2766 } else {
2767 p = list_first_entry(&tsk->publications, struct publication,
2768 pport_list);
2769 }
2770
2771 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2772 err = __tipc_nl_add_sk_publ(skb, cb, p);
2773 if (err) {
2774 *last_publ = p->key;
2775 return err;
2776 }
2777 }
2778 *last_publ = 0;
2779
2780 return 0;
2781}
2782
2783int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2784{
2785 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002786 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002787 u32 last_publ = cb->args[1];
2788 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002789 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002790 struct tipc_sock *tsk;
2791
Ying Xue07f6c4b2015-01-07 13:41:58 +08002792 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002793 struct nlattr **attrs;
2794 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2795
2796 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2797 if (err)
2798 return err;
2799
2800 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2801 attrs[TIPC_NLA_SOCK],
2802 tipc_nl_sock_policy);
2803 if (err)
2804 return err;
2805
2806 if (!sock[TIPC_NLA_SOCK_REF])
2807 return -EINVAL;
2808
Ying Xue07f6c4b2015-01-07 13:41:58 +08002809 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002810 }
2811
2812 if (done)
2813 return 0;
2814
Ying Xuee05b31f2015-01-09 15:27:08 +08002815 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002816 if (!tsk)
2817 return -EINVAL;
2818
2819 lock_sock(&tsk->sk);
2820 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2821 if (!err)
2822 done = 1;
2823 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002824 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002825
Ying Xue07f6c4b2015-01-07 13:41:58 +08002826 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002827 cb->args[1] = last_publ;
2828 cb->args[2] = done;
2829
2830 return skb->len;
2831}