blob: 56b8a96c2257b17b0168dbad2b695f816e87a39a [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05004 * Copyright (c) 2001-2007, 2012-2015, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Ying Xue07f6c4b2015-01-07 13:41:58 +080037#include <linux/rhashtable.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Jon Paul Maloyc637c102015-02-05 08:36:41 -050042#include "name_distr.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040043#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040044#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010045#include "netlink.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040046
Ying Xue07f6c4b2015-01-07 13:41:58 +080047#define SS_LISTENING -1 /* socket is listening */
48#define SS_READY -2 /* socket is connectionless */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080051#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080052#define TIPC_FWD_MSG 1
53#define TIPC_CONN_OK 0
54#define TIPC_CONN_PROBING 1
55#define TIPC_MAX_PORT 0xffffffff
56#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040057
58/**
59 * struct tipc_sock - TIPC socket structure
60 * @sk: socket - interacts with 'port' and with user via the socket API
61 * @connected: non-zero if port is currently connected to a peer port
62 * @conn_type: TIPC type used when connection was established
63 * @conn_instance: TIPC instance used when connection was established
64 * @published: non-zero if port has one or more associated names
65 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080066 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040067 * @phdr: preformatted message header used when sending messages
68 * @port_list: adjacent ports in TIPC's global list of ports
69 * @publications: list of publications for port
70 * @pub_count: total # of publications port has made during its lifetime
71 * @probing_state:
Ying Xue2f55c432015-01-09 15:27:00 +080072 * @probing_intv:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073 * @conn_timeout: the time we can wait for an unresponded setup request
74 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
75 * @link_cong: non-zero if owner must sleep because of link congestion
76 * @sent_unacked: # messages sent by socket, and not yet acked by peer
77 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Erik Hugnef2f80362015-03-19 09:02:19 +010078 * @remote: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080079 * @node: hash table node
80 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040081 */
82struct tipc_sock {
83 struct sock sk;
84 int connected;
85 u32 conn_type;
86 u32 conn_instance;
87 int published;
88 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080089 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040090 struct tipc_msg phdr;
91 struct list_head sock_list;
92 struct list_head publications;
93 u32 pub_count;
94 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080095 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 uint conn_timeout;
97 atomic_t dupl_rcvcnt;
98 bool link_cong;
99 uint sent_unacked;
100 uint rcv_unacked;
Erik Hugnef2f80362015-03-19 09:02:19 +0100101 struct sockaddr_tipc remote;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800102 struct rhash_head node;
103 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400104};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400106static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400107static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800108static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800109static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800110static int tipc_release(struct socket *sock);
111static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400112static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Ying Xuef2f2a962015-01-09 15:27:02 +0800113static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400115 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400116static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400117 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800118static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800119static int tipc_sk_insert(struct tipc_sock *tsk);
120static void tipc_sk_remove(struct tipc_sock *tsk);
Ying Xue39a02952015-03-02 15:37:47 +0800121static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
122 size_t dsz);
123static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124
Florian Westphalbca65ea2008-02-07 18:18:01 -0800125static const struct proto_ops packet_ops;
126static const struct proto_ops stream_ops;
127static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128static struct proto tipc_proto;
129
Herbert Xu6cca72892015-03-20 21:57:05 +1100130static const struct rhashtable_params tsk_rht_params;
131
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900132/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133 * Revised TIPC socket locking policy:
134 *
135 * Most socket operations take the standard socket lock when they start
136 * and hold it until they finish (or until they need to sleep). Acquiring
137 * this lock grants the owner exclusive access to the fields of the socket
138 * data structures, with the exception of the backlog queue. A few socket
139 * operations can be done without taking the socket lock because they only
140 * read socket information that never changes during the life of the socket.
141 *
142 * Socket operations may acquire the lock for the associated TIPC port if they
143 * need to perform an operation on the port. If any routine needs to acquire
144 * both the socket lock and the port lock it must take the socket lock first
145 * to avoid the risk of deadlock.
146 *
147 * The dispatcher handling incoming messages cannot grab the socket lock in
148 * the standard fashion, since invoked it runs at the BH level and cannot block.
149 * Instead, it checks to see if the socket lock is currently owned by someone,
150 * and either handles the message itself or adds it to the socket's backlog
151 * queue; in the latter case the queued message is processed once the process
152 * owning the socket lock releases it.
153 *
154 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
155 * the problem of a blocked socket operation preventing any other operations
156 * from occurring. However, applications must be careful if they have
157 * multiple threads trying to send (or receive) on the same socket, as these
158 * operations might interfere with each other. For example, doing a connect
159 * and a receive at the same time might allow the receive to consume the
160 * ACK message meant for the connect. While additional work could be done
161 * to try and overcome this, it doesn't seem to be worthwhile at the present.
162 *
163 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
164 * that another operation that must be performed in a non-blocking manner is
165 * not delayed for very long because the lock has already been taken.
166 *
167 * NOTE: This code assumes that certain fields of a port/socket pair are
168 * constant over its lifetime; such fields can be examined without taking
169 * the socket lock and/or port lock, and do not need to be re-read even
170 * after resuming processing after waiting. These fields include:
171 * - socket type
172 * - pointer to socket sk structure (aka tipc_sock structure)
173 * - pointer to port structure
174 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500177static u32 tsk_own_node(struct tipc_sock *tsk)
178{
179 return msg_prevnode(&tsk->phdr);
180}
181
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400182static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400183{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400184 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400185}
186
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400187static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400188{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400190}
191
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400192static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400193{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400194 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400195}
196
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400197static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400198{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400199 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400200}
201
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400202static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400203{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400204 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400205}
206
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400208{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400209 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400210}
211
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400212static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400213{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400214 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400215}
216
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400217static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400218{
219 if (imp > TIPC_CRITICAL_IMPORTANCE)
220 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400221 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400222 return 0;
223}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400224
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400225static struct tipc_sock *tipc_sk(const struct sock *sk)
226{
227 return container_of(sk, struct tipc_sock, sk);
228}
229
230static int tsk_conn_cong(struct tipc_sock *tsk)
231{
232 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
233}
234
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400236 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700237 *
238 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400240static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400242 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243}
244
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400245/* tipc_sk_respond() : send response message back to sender
246 */
247static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
248{
249 u32 selector;
250 u32 dnode;
251 u32 onode = tipc_own_addr(sock_net(sk));
252
253 if (!tipc_msg_reverse(onode, &skb, err))
254 return;
255
256 dnode = msg_destnode(buf_msg(skb));
257 selector = msg_origport(buf_msg(skb));
258 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
259}
260
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400262 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700263 *
264 * Caller must hold socket lock
265 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400266static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700267{
Ying Xuea6ca1092014-11-26 11:41:55 +0800268 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700269
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400270 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
271 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700272}
273
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400274/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400275 *
276 * Handles cases where the node's network address has changed from
277 * the default of <0.0.0> to its configured setting.
278 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400279static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400280{
Ying Xue34747532015-01-09 15:27:10 +0800281 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400282 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400283 u32 orig_node;
284 u32 peer_node;
285
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400286 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400287 return false;
288
289 if (unlikely(msg_origport(msg) != peer_port))
290 return false;
291
292 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400293 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400294
295 if (likely(orig_node == peer_node))
296 return true;
297
Ying Xue34747532015-01-09 15:27:10 +0800298 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400299 return true;
300
Ying Xue34747532015-01-09 15:27:10 +0800301 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400302 return true;
303
304 return false;
305}
306
Allan Stephens0c3141e2008-04-15 00:22:02 -0700307/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400308 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700309 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 * @sock: pre-allocated socket structure
311 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800312 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900313 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 * This routine creates additional data structures used by the TIPC socket,
315 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 *
317 * Returns 0 on success, errno otherwise
318 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400319static int tipc_sk_create(struct net *net, struct socket *sock,
320 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500322 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 const struct proto_ops *ops;
324 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400326 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400327 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700328
329 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 if (unlikely(protocol != 0))
331 return -EPROTONOSUPPORT;
332
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 switch (sock->type) {
334 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 ops = &stream_ops;
336 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 break;
338 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 ops = &packet_ops;
340 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 break;
342 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700344 ops = &msg_ops;
345 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 break;
Allan Stephens49978652006-06-25 23:47:18 -0700347 default:
Allan Stephens49978652006-06-25 23:47:18 -0700348 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 }
350
Allan Stephens0c3141e2008-04-15 00:22:02 -0700351 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500352 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 if (sk == NULL)
354 return -ENOMEM;
355
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400356 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400357 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400358 INIT_LIST_HEAD(&tsk->publications);
359 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500360 tn = net_generic(sock_net(sk), tipc_net_id);
361 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400362 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363
Allan Stephens0c3141e2008-04-15 00:22:02 -0700364 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700365 sock->ops = ops;
366 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800368 if (tipc_sk_insert(tsk)) {
369 pr_warn("Socket create failed; port numbrer exhausted\n");
370 return -EINVAL;
371 }
372 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800373 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400374 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400375 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800376 sk->sk_data_ready = tipc_data_ready;
377 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800378 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400379 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500380 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400381 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700382
Allan Stephens0c3141e2008-04-15 00:22:02 -0700383 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400384 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700385 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400386 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700387 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 return 0;
389}
390
Ying Xue07f6c4b2015-01-07 13:41:58 +0800391static void tipc_sk_callback(struct rcu_head *head)
392{
393 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
394
395 sock_put(&tsk->sk);
396}
397
Ying Xuec5fa7b32013-06-17 10:54:39 -0400398/**
Ying Xue247f0f32014-02-18 16:06:46 +0800399 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 * @sock: socket to destroy
401 *
402 * This routine cleans up any messages that are still queued on the socket.
403 * For DGRAM and RDM socket types, all queued messages are rejected.
404 * For SEQPACKET and STREAM socket types, the first message is rejected
405 * and any others are discarded. (If the first message on a STREAM socket
406 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900407 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 * NOTE: Rejected messages are not necessarily returned to the sender! They
409 * are returned or discarded according to the "destination droppable" setting
410 * specified for the message by the sender.
411 *
412 * Returns 0 on success, errno otherwise
413 */
Ying Xue247f0f32014-02-18 16:06:46 +0800414static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 struct sock *sk = sock->sk;
Sasha Levin357c4772015-01-13 12:46:41 -0500417 struct net *net;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400418 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800419 struct sk_buff *skb;
Ying Xue1ea23a22015-05-28 13:19:22 +0800420 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421
Allan Stephens0c3141e2008-04-15 00:22:02 -0700422 /*
423 * Exit if socket isn't fully initialized (occurs when a failed accept()
424 * releases a pre-allocated child socket that was never used)
425 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700426 if (sk == NULL)
427 return 0;
428
Sasha Levin357c4772015-01-13 12:46:41 -0500429 net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400430 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700431 lock_sock(sk);
432
433 /*
434 * Reject all unreceived messages, except on an active connection
435 * (which disconnects locally & sends a 'FIN+' to peer)
436 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400437 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800439 skb = __skb_dequeue(&sk->sk_receive_queue);
440 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 break;
Ying Xuea6ca1092014-11-26 11:41:55 +0800442 if (TIPC_SKB_CB(skb)->handle != NULL)
443 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700444 else {
445 if ((sock->state == SS_CONNECTING) ||
446 (sock->state == SS_CONNECTED)) {
447 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400448 tsk->connected = 0;
Ying Xuef2f98002015-01-09 15:27:05 +0800449 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700450 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400451 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700452 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 }
454
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400455 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800456 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800457 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400458 if (tsk->connected) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500459 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +0800460 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500461 tsk_own_node(tsk), tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800462 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800463 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400464 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Ying Xuef2f98002015-01-09 15:27:05 +0800465 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400466 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700469 sock->state = SS_DISCONNECTING;
470 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800471
472 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700473 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200475 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476}
477
478/**
Ying Xue247f0f32014-02-18 16:06:46 +0800479 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 * @sock: socket structure
481 * @uaddr: socket address describing name(s) and desired operation
482 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900483 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484 * Name and name sequence binding is indicated using a positive scope value;
485 * a negative scope value unbinds the specified name. Specifying no name
486 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900487 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700489 *
490 * NOTE: This routine doesn't need to take the socket lock since it doesn't
491 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 */
Ying Xue247f0f32014-02-18 16:06:46 +0800493static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
494 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495{
Ying Xue84602762013-12-27 10:18:28 +0800496 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400498 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800499 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500
Ying Xue84602762013-12-27 10:18:28 +0800501 lock_sock(sk);
502 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400503 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800504 goto exit;
505 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900506
Ying Xue84602762013-12-27 10:18:28 +0800507 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
508 res = -EINVAL;
509 goto exit;
510 }
511 if (addr->family != AF_TIPC) {
512 res = -EAFNOSUPPORT;
513 goto exit;
514 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 if (addr->addrtype == TIPC_ADDR_NAME)
517 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800518 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
519 res = -EAFNOSUPPORT;
520 goto exit;
521 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900522
Ying Xue13a2e892013-06-17 10:54:40 -0400523 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400524 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800525 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
526 res = -EACCES;
527 goto exit;
528 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400529
Ying Xue84602762013-12-27 10:18:28 +0800530 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400531 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
532 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800533exit:
534 release_sock(sk);
535 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536}
537
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900538/**
Ying Xue247f0f32014-02-18 16:06:46 +0800539 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 * @sock: socket structure
541 * @uaddr: area for returned socket address
542 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700543 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900544 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700546 *
Allan Stephens2da59912008-07-14 22:43:32 -0700547 * NOTE: This routine doesn't need to take the socket lock since it only
548 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000549 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 */
Ying Xue247f0f32014-02-18 16:06:46 +0800551static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
552 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400555 struct tipc_sock *tsk = tipc_sk(sock->sk);
Ying Xue34747532015-01-09 15:27:10 +0800556 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000558 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700559 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700560 if ((sock->state != SS_CONNECTED) &&
561 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
562 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400563 addr->addr.id.ref = tsk_peer_port(tsk);
564 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700565 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800566 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800567 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700568 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569
570 *uaddr_len = sizeof(*addr);
571 addr->addrtype = TIPC_ADDR_ID;
572 addr->family = AF_TIPC;
573 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 addr->addr.name.domain = 0;
575
Allan Stephens0c3141e2008-04-15 00:22:02 -0700576 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577}
578
579/**
Ying Xue247f0f32014-02-18 16:06:46 +0800580 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 * @file: file structure associated with the socket
582 * @sock: socket for which to calculate the poll bits
583 * @wait: ???
584 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700585 * Returns pollmask value
586 *
587 * COMMENTARY:
588 * It appears that the usual socket locking mechanisms are not useful here
589 * since the pollmask info is potentially out-of-date the moment this routine
590 * exits. TCP and other protocols seem to rely on higher level poll routines
591 * to handle any preventable race conditions, so TIPC will do the same ...
592 *
593 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700594 *
Allan Stephensf662c072010-08-17 11:00:06 +0000595 * socket state flags set
596 * ------------ ---------
597 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200598 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000599 *
600 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
601 * no write flags
602 *
603 * connected POLLIN/POLLRDNORM if data in rx queue
604 * POLLOUT if port is not congested
605 *
606 * disconnecting POLLIN/POLLRDNORM/POLLHUP
607 * no write flags
608 *
609 * listening POLLIN if SYN in rx queue
610 * no write flags
611 *
612 * ready POLLIN/POLLRDNORM if data in rx queue
613 * [connectionless] POLLOUT (since port cannot be congested)
614 *
615 * IMPORTANT: The fact that a read or write operation is indicated does NOT
616 * imply that the operation will succeed, merely that it should be performed
617 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 */
Ying Xue247f0f32014-02-18 16:06:46 +0800619static unsigned int tipc_poll(struct file *file, struct socket *sock,
620 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621{
Allan Stephens9b674e82008-03-26 16:48:21 -0700622 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400623 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000624 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700625
Ying Xuef288bef2012-08-21 11:16:57 +0800626 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700627
Allan Stephensf662c072010-08-17 11:00:06 +0000628 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200629 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500630 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200631 mask |= POLLOUT;
632 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000633 case SS_READY:
634 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400635 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000636 mask |= POLLOUT;
637 /* fall thru' */
638 case SS_CONNECTING:
639 case SS_LISTENING:
640 if (!skb_queue_empty(&sk->sk_receive_queue))
641 mask |= (POLLIN | POLLRDNORM);
642 break;
643 case SS_DISCONNECTING:
644 mask = (POLLIN | POLLRDNORM | POLLHUP);
645 break;
646 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700647
648 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649}
650
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400651/**
652 * tipc_sendmcast - send multicast message
653 * @sock: socket structure
654 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500655 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400656 * @dsz: total length of message data
657 * @timeo: timeout to wait for wakeup
658 *
659 * Called from function tipc_sendmsg(), which has done all sanity checks
660 * Returns the number of bytes sent on success, or errno
661 */
662static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500663 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400664{
665 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500666 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800667 struct net *net = sock_net(sk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500668 struct tipc_msg *mhdr = &tsk->phdr;
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:
Jon Paul Maloy959e1782015-10-22 08:51:43 -0400684 mtu = tipc_bcast_get_mtu(net);
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 Maloy6beb19a2015-10-22 08:51:33 -0400690 rc = tipc_bcast_xmit(net, pktchain);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400691 if (likely(!rc))
692 return dsz;
693
694 if (rc == -ELINKCONG) {
695 tsk->link_cong = 1;
696 rc = tipc_wait_for_sndmsg(sock, &timeo);
697 if (!rc)
698 continue;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400699 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400700 __skb_queue_purge(pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500701 if (rc == -EMSGSIZE) {
702 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400703 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500704 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400705 break;
706 } while (1);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400707 return rc;
708}
709
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500710/**
711 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
712 * @arrvq: queue with arriving messages, to be cloned after destination lookup
713 * @inputq: queue with cloned messages, delivered to socket after dest lookup
714 *
715 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400716 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500717void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
718 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400719{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500720 struct tipc_msg *msg;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500721 struct tipc_plist dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500722 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400723 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500724 struct sk_buff_head tmpq;
725 uint hsz;
726 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500727
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500728 __skb_queue_head_init(&tmpq);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500729 tipc_plist_init(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400730
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500731 skb = tipc_skb_peek(arrvq, &inputq->lock);
732 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
733 msg = buf_msg(skb);
734 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400735
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500736 if (in_own_node(net, msg_orignode(msg)))
737 scope = TIPC_NODE_SCOPE;
738
739 /* Create destination port list and message clones: */
740 tipc_nametbl_mc_translate(net,
741 msg_nametype(msg), msg_namelower(msg),
742 msg_nameupper(msg), scope, &dports);
743 portid = tipc_plist_pop(&dports);
744 for (; portid; portid = tipc_plist_pop(&dports)) {
745 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
746 if (_skb) {
747 msg_set_destport(buf_msg(_skb), portid);
748 __skb_queue_tail(&tmpq, _skb);
749 continue;
750 }
751 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400752 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500753 /* Append to inputq if not already done by other thread */
754 spin_lock_bh(&inputq->lock);
755 if (skb_peek(arrvq) == skb) {
756 skb_queue_splice_tail_init(&tmpq, inputq);
757 kfree_skb(__skb_dequeue(arrvq));
758 }
759 spin_unlock_bh(&inputq->lock);
760 __skb_queue_purge(&tmpq);
761 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400762 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500763 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400764}
765
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900766/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500767 * tipc_sk_proto_rcv - receive a connection mng protocol message
768 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400769 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500770 */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400771static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500772{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400773 struct sock *sk = &tsk->sk;
774 struct tipc_msg *hdr = buf_msg(skb);
775 int mtyp = msg_type(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500776 int conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400777
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500778 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400779 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500780 goto exit;
781
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400782 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500783
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400784 if (mtyp == CONN_PROBE) {
785 msg_set_type(hdr, CONN_PROBE_REPLY);
786 tipc_sk_respond(sk, skb, TIPC_OK);
787 return;
788 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400789 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400790 tsk->sent_unacked -= msg_msgcnt(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500791 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400792 sk->sk_write_space(sk);
793 } else if (mtyp != CONN_PROBE_REPLY) {
794 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500795 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500796exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400797 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500798}
799
Ying Xue3f405042014-01-17 09:50:05 +0800800static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
801{
802 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400803 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800804 DEFINE_WAIT(wait);
805 int done;
806
807 do {
808 int err = sock_error(sk);
809 if (err)
810 return err;
811 if (sock->state == SS_DISCONNECTING)
812 return -EPIPE;
813 if (!*timeo_p)
814 return -EAGAIN;
815 if (signal_pending(current))
816 return sock_intr_errno(*timeo_p);
817
818 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500819 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800820 finish_wait(sk_sleep(sk), &wait);
821 } while (!done);
822 return 0;
823}
824
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500825/**
Ying Xue247f0f32014-02-18 16:06:46 +0800826 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 * @sock: socket structure
828 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500829 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900830 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900832 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
834 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 * Returns the number of bytes sent on success, or errno otherwise
837 */
Ying Xue1b784142015-03-02 15:37:48 +0800838static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500839 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840{
Ying Xue39a02952015-03-02 15:37:47 +0800841 struct sock *sk = sock->sk;
842 int ret;
843
844 lock_sock(sk);
845 ret = __tipc_sendmsg(sock, m, dsz);
846 release_sock(sk);
847
848 return ret;
849}
850
851static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
852{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500853 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700854 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400855 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800856 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400857 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500858 u32 dnode, dport;
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500859 struct sk_buff_head *pktchain = &sk->sk_write_queue;
Ying Xuea6ca1092014-11-26 11:41:55 +0800860 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100861 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500862 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500863 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800864 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100865 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500867 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400868 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100869 if (unlikely(!dest)) {
870 if (tsk->connected && sock->state == SS_READY)
871 dest = &tsk->remote;
872 else
873 return -EDESTADDRREQ;
874 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
875 dest->family != AF_TIPC) {
876 return -EINVAL;
877 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500878 if (unlikely(sock->state != SS_READY)) {
Ying Xue39a02952015-03-02 15:37:47 +0800879 if (sock->state == SS_LISTENING)
880 return -EPIPE;
881 if (sock->state != SS_UNCONNECTED)
882 return -EISCONN;
883 if (tsk->published)
884 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700885 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400886 tsk->conn_type = dest->addr.name.name.type;
887 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700888 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100890 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800891 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500892
893 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a02952015-03-02 15:37:47 +0800894 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500895 } else if (dest->addrtype == TIPC_ADDR_NAME) {
896 u32 type = dest->addr.name.name.type;
897 u32 inst = dest->addr.name.name.instance;
898 u32 domain = dest->addr.name.domain;
899
900 dnode = domain;
901 msg_set_type(mhdr, TIPC_NAMED_MSG);
902 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
903 msg_set_nametype(mhdr, type);
904 msg_set_nameinst(mhdr, inst);
905 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800906 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500907 msg_set_destnode(mhdr, dnode);
908 msg_set_destport(mhdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800909 if (unlikely(!dport && !dnode))
910 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500911 } else if (dest->addrtype == TIPC_ADDR_ID) {
912 dnode = dest->addr.id.node;
913 msg_set_type(mhdr, TIPC_DIRECT_MSG);
914 msg_set_lookup_scope(mhdr, 0);
915 msg_set_destnode(mhdr, dnode);
916 msg_set_destport(mhdr, dest->addr.id.ref);
917 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
918 }
919
Al Virof25dcc72014-11-28 15:52:29 -0500920 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500921new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800922 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500923 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500924 if (rc < 0)
Ying Xue39a02952015-03-02 15:37:47 +0800925 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500926
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900927 do {
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500928 skb = skb_peek(pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +0800929 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -0400930 rc = tipc_node_xmit(net, pktchain, dnode, tsk->portid);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400931 if (likely(!rc)) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500932 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700933 sock->state = SS_CONNECTING;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400934 return dsz;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900935 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400936 if (rc == -ELINKCONG) {
937 tsk->link_cong = 1;
938 rc = tipc_wait_for_sndmsg(sock, &timeo);
939 if (!rc)
940 continue;
941 }
942 __skb_queue_purge(pktchain);
Al Virof25dcc72014-11-28 15:52:29 -0500943 if (rc == -EMSGSIZE) {
944 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500945 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500946 }
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400947 break;
948 } while (1);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500949
950 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951}
952
Ying Xue391a6dd2014-01-17 09:50:06 +0800953static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
954{
955 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400956 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800957 DEFINE_WAIT(wait);
958 int done;
959
960 do {
961 int err = sock_error(sk);
962 if (err)
963 return err;
964 if (sock->state == SS_DISCONNECTING)
965 return -EPIPE;
966 else if (sock->state != SS_CONNECTED)
967 return -ENOTCONN;
968 if (!*timeo_p)
969 return -EAGAIN;
970 if (signal_pending(current))
971 return sock_intr_errno(*timeo_p);
972
973 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
974 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500975 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400976 !tsk_conn_cong(tsk)) ||
977 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800978 finish_wait(sk_sleep(sk), &wait);
979 } while (!done);
980 return 0;
981}
982
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900983/**
Ying Xue247f0f32014-02-18 16:06:46 +0800984 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 * @sock: socket structure
986 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500987 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900988 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900990 *
991 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700992 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993 */
Ying Xue1b784142015-03-02 15:37:48 +0800994static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700996 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +0800997 int ret;
998
999 lock_sock(sk);
1000 ret = __tipc_send_stream(sock, m, dsz);
1001 release_sock(sk);
1002
1003 return ret;
1004}
1005
1006static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1007{
1008 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001009 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001010 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001011 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001012 struct sk_buff_head *pktchain = &sk->sk_write_queue;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001013 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001014 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001015 int rc = -EINVAL;
1016 long timeo;
1017 u32 dnode;
1018 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001019 struct iov_iter save;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001020
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001021 /* Handle implied connection establishment */
1022 if (unlikely(dest)) {
Ying Xue39a02952015-03-02 15:37:47 +08001023 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001024 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001025 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001026 return rc;
1027 }
1028 if (dsz > (uint)INT_MAX)
1029 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001030
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001032 if (sock->state == SS_DISCONNECTING)
Ying Xue39a02952015-03-02 15:37:47 +08001033 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001034 else
Ying Xue39a02952015-03-02 15:37:47 +08001035 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001036 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001038 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001039 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001040
1041next:
Al Virof25dcc72014-11-28 15:52:29 -05001042 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001043 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001044 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001045 rc = tipc_msg_build(mhdr, m, sent, send, mtu, pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001046 if (unlikely(rc < 0))
Ying Xue39a02952015-03-02 15:37:47 +08001047 return rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001048 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001049 if (likely(!tsk_conn_cong(tsk))) {
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001050 rc = tipc_node_xmit(net, pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001051 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001052 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001053 sent += send;
1054 if (sent == dsz)
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001055 return dsz;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001056 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001057 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001058 if (rc == -EMSGSIZE) {
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001059 __skb_queue_purge(pktchain);
Ying Xuef2f98002015-01-09 15:27:05 +08001060 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1061 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001062 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001063 goto next;
1064 }
1065 if (rc != -ELINKCONG)
1066 break;
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001067
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001068 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001070 rc = tipc_wait_for_sndpkt(sock, &timeo);
1071 } while (!rc);
Ying Xue39a02952015-03-02 15:37:47 +08001072
Jon Paul Maloy22d85c72015-07-16 16:54:23 -04001073 __skb_queue_purge(pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001074 return sent ? sent : rc;
1075}
1076
1077/**
1078 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001079 * @sock: socket structure
1080 * @m: message to send
1081 * @dsz: length of data to be transmitted
1082 *
1083 * Used for SOCK_SEQPACKET messages.
1084 *
1085 * Returns the number of bytes sent on success, or errno otherwise
1086 */
Ying Xue1b784142015-03-02 15:37:48 +08001087static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001088{
1089 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1090 return -EMSGSIZE;
1091
Ying Xue1b784142015-03-02 15:37:48 +08001092 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093}
1094
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001095/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001097static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001098 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099{
Ying Xue3721e9c2015-01-13 17:07:48 +08001100 struct sock *sk = &tsk->sk;
1101 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001102 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001104 msg_set_destnode(msg, peer_node);
1105 msg_set_destport(msg, peer_port);
1106 msg_set_type(msg, TIPC_CONN_MSG);
1107 msg_set_lookup_scope(msg, 0);
1108 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001109
Ying Xue2f55c432015-01-09 15:27:00 +08001110 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001111 tsk->probing_state = TIPC_CONN_OK;
1112 tsk->connected = 1;
Ying Xue3721e9c2015-01-13 17:07:48 +08001113 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001114 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1115 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116}
1117
1118/**
1119 * set_orig_addr - capture sender's address for received message
1120 * @m: descriptor for message info
1121 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001122 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123 * Note: Address is not captured if not requested by receiver.
1124 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001125static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001127 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001129 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 addr->family = AF_TIPC;
1131 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001132 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 addr->addr.id.ref = msg_origport(msg);
1134 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001135 addr->addr.name.domain = 0; /* could leave uninitialized */
1136 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 m->msg_namelen = sizeof(struct sockaddr_tipc);
1138 }
1139}
1140
1141/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001142 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 * @m: descriptor for message info
1144 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001145 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001146 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001148 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149 * Returns 0 if successful, otherwise errno
1150 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001151static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1152 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153{
1154 u32 anc_data[3];
1155 u32 err;
1156 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001157 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158 int res;
1159
1160 if (likely(m->msg_controllen == 0))
1161 return 0;
1162
1163 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 err = msg ? msg_errcode(msg) : 0;
1165 if (unlikely(err)) {
1166 anc_data[0] = err;
1167 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001168 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1169 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001171 if (anc_data[1]) {
1172 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1173 msg_data(msg));
1174 if (res)
1175 return res;
1176 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 }
1178
1179 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1181 switch (dest_type) {
1182 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001183 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 anc_data[0] = msg_nametype(msg);
1185 anc_data[1] = msg_namelower(msg);
1186 anc_data[2] = msg_namelower(msg);
1187 break;
1188 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001189 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 anc_data[0] = msg_nametype(msg);
1191 anc_data[1] = msg_namelower(msg);
1192 anc_data[2] = msg_nameupper(msg);
1193 break;
1194 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001195 has_name = (tsk->conn_type != 0);
1196 anc_data[0] = tsk->conn_type;
1197 anc_data[1] = tsk->conn_instance;
1198 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 break;
1200 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001201 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 }
Allan Stephens2db99832010-12-31 18:59:33 +00001203 if (has_name) {
1204 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1205 if (res)
1206 return res;
1207 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208
1209 return 0;
1210}
1211
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001212static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001213{
Ying Xuef2f98002015-01-09 15:27:05 +08001214 struct net *net = sock_net(&tsk->sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001215 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001216 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001217 u32 peer_port = tsk_peer_port(tsk);
1218 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001219
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001220 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001221 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001222 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1223 dnode, tsk_own_node(tsk), peer_port,
1224 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001225 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001226 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001227 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001228 msg_set_msgcnt(msg, ack);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001229 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001230}
1231
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001232static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001233{
1234 struct sock *sk = sock->sk;
1235 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001236 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001237 int err;
1238
1239 for (;;) {
1240 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001241 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001242 if (sock->state == SS_DISCONNECTING) {
1243 err = -ENOTCONN;
1244 break;
1245 }
1246 release_sock(sk);
1247 timeo = schedule_timeout(timeo);
1248 lock_sock(sk);
1249 }
1250 err = 0;
1251 if (!skb_queue_empty(&sk->sk_receive_queue))
1252 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001253 err = -EAGAIN;
1254 if (!timeo)
1255 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001256 err = sock_intr_errno(timeo);
1257 if (signal_pending(current))
1258 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001259 }
1260 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001261 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001262 return err;
1263}
1264
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001265/**
Ying Xue247f0f32014-02-18 16:06:46 +08001266 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001267 * @m: descriptor for message info
1268 * @buf_len: total size of user buffer area
1269 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001270 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1272 * If the complete message doesn't fit in user area, truncate it.
1273 *
1274 * Returns size of returned message data, errno otherwise
1275 */
Ying Xue1b784142015-03-02 15:37:48 +08001276static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1277 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001279 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001280 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281 struct sk_buff *buf;
1282 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001283 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001284 unsigned int sz;
1285 u32 err;
1286 int res;
1287
Allan Stephens0c3141e2008-04-15 00:22:02 -07001288 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289 if (unlikely(!buf_len))
1290 return -EINVAL;
1291
Allan Stephens0c3141e2008-04-15 00:22:02 -07001292 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293
Allan Stephens0c3141e2008-04-15 00:22:02 -07001294 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 res = -ENOTCONN;
1296 goto exit;
1297 }
1298
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001299 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001300restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001301
Allan Stephens0c3141e2008-04-15 00:22:02 -07001302 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001303 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001304 if (res)
1305 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001306
1307 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001308 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 msg = buf_msg(buf);
1310 sz = msg_data_sz(msg);
1311 err = msg_errcode(msg);
1312
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001315 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 goto restart;
1317 }
1318
1319 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 set_orig_addr(m, msg);
1321
1322 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001323 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001324 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 goto exit;
1326
1327 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 if (!err) {
1329 if (unlikely(buf_len < sz)) {
1330 sz = buf_len;
1331 m->msg_flags |= MSG_TRUNC;
1332 }
David S. Miller51f3d022014-11-05 16:46:40 -05001333 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001334 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 res = sz;
1337 } else {
1338 if ((sock->state == SS_READY) ||
1339 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1340 res = 0;
1341 else
1342 res = -ECONNRESET;
1343 }
1344
1345 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001347 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001348 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001349 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001350 tsk->rcv_unacked = 0;
1351 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001352 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001353 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001355 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 return res;
1357}
1358
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001359/**
Ying Xue247f0f32014-02-18 16:06:46 +08001360 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 * @m: descriptor for message info
1362 * @buf_len: total size of user buffer area
1363 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001364 *
1365 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 * will optionally wait for more; never truncates data.
1367 *
1368 * Returns size of returned message data, errno otherwise
1369 */
Ying Xue1b784142015-03-02 15:37:48 +08001370static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1371 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001373 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001374 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 struct sk_buff *buf;
1376 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001377 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001379 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001382 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383
1384 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 if (unlikely(!buf_len))
1386 return -EINVAL;
1387
Allan Stephens0c3141e2008-04-15 00:22:02 -07001388 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001390 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391 res = -ENOTCONN;
1392 goto exit;
1393 }
1394
Florian Westphal3720d402010-08-17 11:00:04 +00001395 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001396 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001397
Allan Stephens0c3141e2008-04-15 00:22:02 -07001398restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001399 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001400 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001401 if (res)
1402 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403
1404 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001405 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 msg = buf_msg(buf);
1407 sz = msg_data_sz(msg);
1408 err = msg_errcode(msg);
1409
1410 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001411 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001412 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 goto restart;
1414 }
1415
1416 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 if (sz_copied == 0) {
1418 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001419 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001420 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 goto exit;
1422 }
1423
1424 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001426 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427
Allan Stephens0232fd02011-02-21 09:45:40 -05001428 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 needed = (buf_len - sz_copied);
1430 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001431
David S. Miller51f3d022014-11-05 16:46:40 -05001432 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1433 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001434 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001436
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437 sz_copied += sz_to_copy;
1438
1439 if (sz_to_copy < sz) {
1440 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001441 TIPC_SKB_CB(buf)->handle =
1442 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001443 goto exit;
1444 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445 } else {
1446 if (sz_copied != 0)
1447 goto exit; /* can't add error msg to valid data */
1448
1449 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1450 res = 0;
1451 else
1452 res = -ECONNRESET;
1453 }
1454
1455 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001457 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001458 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001459 tsk->rcv_unacked = 0;
1460 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001461 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001462 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463
1464 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001465 if ((sz_copied < buf_len) && /* didn't get all requested data */
1466 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001467 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001468 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1469 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 goto restart;
1471
1472exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001473 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001474 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475}
1476
1477/**
Ying Xuef288bef2012-08-21 11:16:57 +08001478 * tipc_write_space - wake up thread if port congestion is released
1479 * @sk: socket
1480 */
1481static void tipc_write_space(struct sock *sk)
1482{
1483 struct socket_wq *wq;
1484
1485 rcu_read_lock();
1486 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001487 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001488 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1489 POLLWRNORM | POLLWRBAND);
1490 rcu_read_unlock();
1491}
1492
1493/**
1494 * tipc_data_ready - wake up threads to indicate messages have been received
1495 * @sk: socket
1496 * @len: the length of messages
1497 */
David S. Miller676d2362014-04-11 16:15:36 -04001498static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001499{
1500 struct socket_wq *wq;
1501
1502 rcu_read_lock();
1503 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001504 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001505 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1506 POLLRDNORM | POLLRDBAND);
1507 rcu_read_unlock();
1508}
1509
Ying Xuef4195d12015-11-22 15:46:05 +08001510static void tipc_sock_destruct(struct sock *sk)
1511{
1512 __skb_queue_purge(&sk->sk_receive_queue);
1513}
1514
Ying Xuef288bef2012-08-21 11:16:57 +08001515/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001516 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001517 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001518 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001519 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001520 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001521 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001522static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001523{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001524 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001525 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001526 struct socket *sock = sk->sk_socket;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001527 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001528
Jon Paul Maloycda36962015-07-22 10:11:20 -04001529 if (unlikely(msg_mcast(hdr)))
1530 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001531
1532 switch ((int)sock->state) {
1533 case SS_CONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001534
Ying Xue7e6c1312012-11-29 18:39:14 -05001535 /* Accept only connection-based messages sent by peer */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001536 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1537 return false;
1538
1539 if (unlikely(msg_errcode(hdr))) {
1540 sock->state = SS_DISCONNECTING;
1541 tsk->connected = 0;
1542 /* Let timer expire on it's own */
1543 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1544 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001545 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001546 return true;
1547
Ying Xue7e6c1312012-11-29 18:39:14 -05001548 case SS_CONNECTING:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001549
Ying Xue7e6c1312012-11-29 18:39:14 -05001550 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001551 if (unlikely(!msg_connected(hdr)))
1552 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001553
Jon Paul Maloycda36962015-07-22 10:11:20 -04001554 if (unlikely(msg_errcode(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001555 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001556 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001557 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001558 }
1559
Jon Paul Maloycda36962015-07-22 10:11:20 -04001560 if (unlikely(!msg_isdata(hdr))) {
Ying Xue584d24b2012-11-29 18:51:19 -05001561 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001562 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001563 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001564 }
1565
Jon Paul Maloycda36962015-07-22 10:11:20 -04001566 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1567 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001568 sock->state = SS_CONNECTED;
1569
Jon Paul Maloycda36962015-07-22 10:11:20 -04001570 /* If 'ACK+' message, add to socket receive queue */
1571 if (msg_data_sz(hdr))
1572 return true;
1573
1574 /* If empty 'ACK-' message, wake up sleeping connect() */
1575 if (waitqueue_active(sk_sleep(sk)))
1576 wake_up_interruptible(sk_sleep(sk));
1577
1578 /* 'ACK-' message is neither accepted nor rejected: */
1579 msg_set_dest_droppable(hdr, 1);
1580 return false;
1581
Ying Xue7e6c1312012-11-29 18:39:14 -05001582 case SS_LISTENING:
1583 case SS_UNCONNECTED:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001584
Ying Xue7e6c1312012-11-29 18:39:14 -05001585 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001586 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1587 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001588 break;
1589 case SS_DISCONNECTING:
1590 break;
1591 default:
1592 pr_err("Unknown socket state %u\n", sock->state);
1593 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001594 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001595}
1596
1597/**
Ying Xueaba79f32013-01-20 23:30:09 +01001598 * rcvbuf_limit - get proper overload limit of socket receive queue
1599 * @sk: socket
1600 * @buf: message
1601 *
1602 * For all connection oriented messages, irrespective of importance,
1603 * the default overload value (i.e. 67MB) is set as limit.
1604 *
1605 * For all connectionless messages, by default new queue limits are
1606 * as belows:
1607 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001608 * TIPC_LOW_IMPORTANCE (4 MB)
1609 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1610 * TIPC_HIGH_IMPORTANCE (16 MB)
1611 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001612 *
1613 * Returns overload limit according to corresponding message importance
1614 */
1615static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1616{
1617 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001618
1619 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001620 return sysctl_tipc_rmem[2];
1621
1622 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1623 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001624}
1625
1626/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001627 * filter_rcv - validate incoming message
1628 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001629 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001630 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001631 * Enqueues message on receive queue if acceptable; optionally handles
1632 * disconnect indication for a connected socket.
1633 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001634 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001635 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001636 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001638static bool filter_rcv(struct sock *sk, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001640 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001641 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001642 struct tipc_msg *hdr = buf_msg(skb);
1643 unsigned int limit = rcvbuf_limit(sk, skb);
1644 int err = TIPC_OK;
1645 int usr = msg_user(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001646
Jon Paul Maloycda36962015-07-22 10:11:20 -04001647 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
1648 tipc_sk_proto_rcv(tsk, skb);
1649 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001650 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001651
Jon Paul Maloycda36962015-07-22 10:11:20 -04001652 if (unlikely(usr == SOCK_WAKEUP)) {
1653 kfree_skb(skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001654 tsk->link_cong = 0;
1655 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001656 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001657 }
1658
Jon Paul Maloycda36962015-07-22 10:11:20 -04001659 /* Drop if illegal message type */
1660 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1661 kfree_skb(skb);
1662 return false;
1663 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001664
Jon Paul Maloycda36962015-07-22 10:11:20 -04001665 /* Reject if wrong message type for current socket state */
1666 if (unlikely(sock->state == SS_READY)) {
1667 if (msg_connected(hdr)) {
1668 err = TIPC_ERR_NO_PORT;
1669 goto reject;
1670 }
1671 } else if (unlikely(!filter_connect(tsk, skb))) {
1672 err = TIPC_ERR_NO_PORT;
1673 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 }
1675
1676 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001677 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1678 err = TIPC_ERR_OVERLOAD;
1679 goto reject;
1680 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681
Ying Xueaba79f32013-01-20 23:30:09 +01001682 /* Enqueue message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001683 TIPC_SKB_CB(skb)->handle = NULL;
1684 __skb_queue_tail(&sk->sk_receive_queue, skb);
1685 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001686
David S. Miller676d2362014-04-11 16:15:36 -04001687 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001688 return true;
1689
1690reject:
1691 tipc_sk_respond(sk, skb, err);
1692 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693}
1694
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001695/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001696 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001697 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001698 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001699 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001700 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001701 *
1702 * Returns 0
1703 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001704static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001705{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001706 unsigned int truesize = skb->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001707
Jon Paul Maloycda36962015-07-22 10:11:20 -04001708 if (likely(filter_rcv(sk, skb)))
1709 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001710 return 0;
1711}
1712
1713/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001714 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1715 * inputq and try adding them to socket or backlog queue
1716 * @inputq: list of incoming buffers with potentially different destinations
1717 * @sk: socket where the buffers should be enqueued
1718 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001719 *
1720 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001721 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001722static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1723 u32 dport)
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 struct sk_buff *skb;
1728 unsigned long time_limit = jiffies + 2;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001729
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001730 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001731 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001732 return;
1733
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001734 skb = tipc_skb_dequeue(inputq, dport);
1735 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001736 return;
1737
1738 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001739 if (!sock_owned_by_user(sk)) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001740 filter_rcv(sk, skb);
1741 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001742 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001743
1744 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001745 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1746 if (sk->sk_backlog.len)
1747 atomic_set(dcnt, 0);
1748 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1749 if (likely(!sk_add_backlog(sk, skb, lim)))
1750 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001751
1752 /* Overload => reject message back to sender */
1753 tipc_sk_respond(sk, skb, TIPC_ERR_OVERLOAD);
1754 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001755 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001756}
1757
1758/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001759 * tipc_sk_rcv - handle a chain of incoming buffers
1760 * @inputq: buffer list containing the buffers
1761 * Consumes all buffers in list until inputq is empty
1762 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001763 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001764void 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 Maloy9816f062014-05-14 05:39:15 -04001768 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001769 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001770 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001771
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001772 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001773 dport = tipc_skb_peek_port(inputq, dport);
1774 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001775
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001776 if (likely(tsk)) {
1777 sk = &tsk->sk;
1778 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001779 tipc_sk_enqueue(inputq, sk, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001780 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001781 }
1782 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001783 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001784 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001785
1786 /* No destination socket => dequeue skb if still there */
1787 skb = tipc_skb_dequeue(inputq, dport);
1788 if (!skb)
1789 return;
1790
1791 /* Try secondary lookup if unresolved named message */
1792 err = TIPC_ERR_NO_PORT;
1793 if (tipc_msg_lookup_dest(net, skb, &err))
1794 goto xmit;
1795
1796 /* Prepare for message rejection */
1797 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001798 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001799xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001800 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001801 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001802 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001803}
1804
Ying Xue78eb3a52014-01-17 09:50:03 +08001805static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1806{
1807 struct sock *sk = sock->sk;
1808 DEFINE_WAIT(wait);
1809 int done;
1810
1811 do {
1812 int err = sock_error(sk);
1813 if (err)
1814 return err;
1815 if (!*timeo_p)
1816 return -ETIMEDOUT;
1817 if (signal_pending(current))
1818 return sock_intr_errno(*timeo_p);
1819
1820 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1821 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1822 finish_wait(sk_sleep(sk), &wait);
1823 } while (!done);
1824 return 0;
1825}
1826
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827/**
Ying Xue247f0f32014-02-18 16:06:46 +08001828 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 * @sock: socket structure
1830 * @dest: socket address for destination port
1831 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001832 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833 *
1834 * Returns 0 on success, errno otherwise
1835 */
Ying Xue247f0f32014-02-18 16:06:46 +08001836static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1837 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001839 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001840 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001841 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1842 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001843 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Ying Xue78eb3a52014-01-17 09:50:03 +08001844 socket_state previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001845 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001846
Allan Stephens0c3141e2008-04-15 00:22:02 -07001847 lock_sock(sk);
1848
Erik Hugnef2f80362015-03-19 09:02:19 +01001849 /* DGRAM/RDM connect(), just save the destaddr */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001850 if (sock->state == SS_READY) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001851 if (dst->family == AF_UNSPEC) {
1852 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1853 tsk->connected = 0;
Sasha Levin610600c2015-03-23 15:30:00 -04001854 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1855 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001856 } else {
1857 memcpy(&tsk->remote, dest, destlen);
1858 tsk->connected = 1;
1859 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001860 goto exit;
1861 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862
Allan Stephensb89741a2008-04-15 00:20:37 -07001863 /*
1864 * Reject connection attempt using multicast address
1865 *
1866 * Note: send_msg() validates the rest of the address fields,
1867 * so there's no need to do it here
1868 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001869 if (dst->addrtype == TIPC_ADDR_MCAST) {
1870 res = -EINVAL;
1871 goto exit;
1872 }
1873
Ying Xue78eb3a52014-01-17 09:50:03 +08001874 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001875 switch (sock->state) {
1876 case SS_UNCONNECTED:
1877 /* Send a 'SYN-' to destination */
1878 m.msg_name = dest;
1879 m.msg_namelen = destlen;
1880
1881 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1882 * indicate send_msg() is never blocked.
1883 */
1884 if (!timeout)
1885 m.msg_flags = MSG_DONTWAIT;
1886
Ying Xue39a02952015-03-02 15:37:47 +08001887 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001888 if ((res < 0) && (res != -EWOULDBLOCK))
1889 goto exit;
1890
1891 /* Just entered SS_CONNECTING state; the only
1892 * difference is that return value in non-blocking
1893 * case is EINPROGRESS, rather than EALREADY.
1894 */
1895 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001896 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001897 if (previous == SS_CONNECTING)
1898 res = -EALREADY;
1899 if (!timeout)
1900 goto exit;
1901 timeout = msecs_to_jiffies(timeout);
1902 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1903 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001904 break;
1905 case SS_CONNECTED:
1906 res = -EISCONN;
1907 break;
1908 default:
1909 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001910 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001911 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001912exit:
1913 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001914 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915}
1916
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001917/**
Ying Xue247f0f32014-02-18 16:06:46 +08001918 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919 * @sock: socket structure
1920 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001921 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001922 * Returns 0 on success, errno otherwise
1923 */
Ying Xue247f0f32014-02-18 16:06:46 +08001924static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001926 struct sock *sk = sock->sk;
1927 int res;
1928
1929 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001930
Ying Xue245f3d32011-07-06 06:01:13 -04001931 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001932 res = -EINVAL;
1933 else {
1934 sock->state = SS_LISTENING;
1935 res = 0;
1936 }
1937
1938 release_sock(sk);
1939 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940}
1941
Ying Xue6398e232014-01-17 09:50:04 +08001942static int tipc_wait_for_accept(struct socket *sock, long timeo)
1943{
1944 struct sock *sk = sock->sk;
1945 DEFINE_WAIT(wait);
1946 int err;
1947
1948 /* True wake-one mechanism for incoming connections: only
1949 * one process gets woken up, not the 'whole herd'.
1950 * Since we do not 'race & poll' for established sockets
1951 * anymore, the common case will execute the loop only once.
1952 */
1953 for (;;) {
1954 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1955 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001956 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001957 release_sock(sk);
1958 timeo = schedule_timeout(timeo);
1959 lock_sock(sk);
1960 }
1961 err = 0;
1962 if (!skb_queue_empty(&sk->sk_receive_queue))
1963 break;
1964 err = -EINVAL;
1965 if (sock->state != SS_LISTENING)
1966 break;
Ying Xue6398e232014-01-17 09:50:04 +08001967 err = -EAGAIN;
1968 if (!timeo)
1969 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001970 err = sock_intr_errno(timeo);
1971 if (signal_pending(current))
1972 break;
Ying Xue6398e232014-01-17 09:50:04 +08001973 }
1974 finish_wait(sk_sleep(sk), &wait);
1975 return err;
1976}
1977
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001978/**
Ying Xue247f0f32014-02-18 16:06:46 +08001979 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001980 * @sock: listening socket
1981 * @newsock: new socket that is to be connected
1982 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001983 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001984 * Returns 0 on success, errno otherwise
1985 */
Ying Xue247f0f32014-02-18 16:06:46 +08001986static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001987{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001988 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001990 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001991 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001992 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001993 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994
Allan Stephens0c3141e2008-04-15 00:22:02 -07001995 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001996
Allan Stephens0c3141e2008-04-15 00:22:02 -07001997 if (sock->state != SS_LISTENING) {
1998 res = -EINVAL;
1999 goto exit;
2000 }
Ying Xue6398e232014-01-17 09:50:04 +08002001 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2002 res = tipc_wait_for_accept(sock, timeo);
2003 if (res)
2004 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002005
2006 buf = skb_peek(&sk->sk_receive_queue);
2007
Ying Xuec5fa7b32013-06-17 10:54:39 -04002008 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002009 if (res)
2010 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002011 security_sk_clone(sock->sk, new_sock->sk);
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 Maloycda36962015-07-22 10:11:20 -04002071 u32 dnode = tsk_peer_node(tsk);
2072 u32 dport = tsk_peer_port(tsk);
2073 u32 onode = tipc_own_addr(net);
2074 u32 oport = tsk->portid;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075 int res;
2076
Allan Stephense247a8f2008-03-06 15:05:38 -08002077 if (how != SHUT_RDWR)
2078 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002079
Allan Stephens0c3141e2008-04-15 00:22:02 -07002080 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081
2082 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002083 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002084 case SS_CONNECTED:
2085
Per Lidenb97bf3f2006-01-02 19:04:38 +01002086restart:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002087 dnode = tsk_peer_node(tsk);
2088
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002089 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002090 skb = __skb_dequeue(&sk->sk_receive_queue);
2091 if (skb) {
2092 if (TIPC_SKB_CB(skb)->handle != NULL) {
2093 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002094 goto restart;
2095 }
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04002096 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002097 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002098 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002099 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloycda36962015-07-22 10:11:20 -04002100 0, dnode, onode, dport, oport,
2101 TIPC_CONN_SHUTDOWN);
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002102 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002104 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002105 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002106 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002107 /* fall through */
2108
2109 case SS_DISCONNECTING:
2110
Ying Xue75031152012-10-29 09:38:15 -04002111 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002112 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002113
2114 /* Wake up anyone sleeping in poll */
2115 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002116 res = 0;
2117 break;
2118
2119 default:
2120 res = -ENOTCONN;
2121 }
2122
Allan Stephens0c3141e2008-04-15 00:22:02 -07002123 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124 return res;
2125}
2126
Ying Xuef2f2a962015-01-09 15:27:02 +08002127static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002128{
Ying Xuef2f2a962015-01-09 15:27:02 +08002129 struct tipc_sock *tsk = (struct tipc_sock *)data;
2130 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002131 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002132 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002133 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002134
Jon Paul Maloy57289012014-08-22 18:09:09 -04002135 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002136 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002137 bh_unlock_sock(sk);
2138 goto exit;
2139 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002140 peer_port = tsk_peer_port(tsk);
2141 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002142
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002143 if (tsk->probing_state == TIPC_CONN_PROBING) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002144 if (!sock_owned_by_user(sk)) {
2145 sk->sk_socket->state = SS_DISCONNECTING;
2146 tsk->connected = 0;
2147 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2148 tsk_peer_port(tsk));
2149 sk->sk_state_change(sk);
2150 } else {
2151 /* Try again later */
2152 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2153 }
2154
Jon Paul Maloy57289012014-08-22 18:09:09 -04002155 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002156 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2157 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002158 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002159 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002160 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002161 }
2162 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002163 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002164 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002165exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002166 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002167}
2168
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002169static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002170 struct tipc_name_seq const *seq)
2171{
Ying Xuef2f98002015-01-09 15:27:05 +08002172 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002173 struct publication *publ;
2174 u32 key;
2175
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002176 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002177 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002178 key = tsk->portid + tsk->pub_count + 1;
2179 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002180 return -EADDRINUSE;
2181
Ying Xuef2f98002015-01-09 15:27:05 +08002182 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002183 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002184 if (unlikely(!publ))
2185 return -EINVAL;
2186
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002187 list_add(&publ->pport_list, &tsk->publications);
2188 tsk->pub_count++;
2189 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002190 return 0;
2191}
2192
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002193static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002194 struct tipc_name_seq const *seq)
2195{
Ying Xuef2f98002015-01-09 15:27:05 +08002196 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002197 struct publication *publ;
2198 struct publication *safe;
2199 int rc = -EINVAL;
2200
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002201 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002202 if (seq) {
2203 if (publ->scope != scope)
2204 continue;
2205 if (publ->type != seq->type)
2206 continue;
2207 if (publ->lower != seq->lower)
2208 continue;
2209 if (publ->upper != seq->upper)
2210 break;
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 break;
2215 }
Ying Xuef2f98002015-01-09 15:27:05 +08002216 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002217 publ->ref, publ->key);
2218 rc = 0;
2219 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002220 if (list_empty(&tsk->publications))
2221 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002222 return rc;
2223}
2224
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002225/* tipc_sk_reinit: set non-zero address in all existing sockets
2226 * when we go from standalone to network mode.
2227 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002228void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002229{
Ying Xuee05b31f2015-01-09 15:27:08 +08002230 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002231 const struct bucket_table *tbl;
2232 struct rhash_head *pos;
2233 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002234 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002235 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002236
Ying Xue07f6c4b2015-01-07 13:41:58 +08002237 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002238 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002239 for (i = 0; i < tbl->size; i++) {
2240 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2241 spin_lock_bh(&tsk->sk.sk_lock.slock);
2242 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002243 msg_set_prevnode(msg, tn->own_addr);
2244 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002245 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2246 }
2247 }
2248 rcu_read_unlock();
2249}
2250
Ying Xuee05b31f2015-01-09 15:27:08 +08002251static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002252{
Ying Xuee05b31f2015-01-09 15:27:08 +08002253 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002254 struct tipc_sock *tsk;
2255
2256 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002257 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002258 if (tsk)
2259 sock_hold(&tsk->sk);
2260 rcu_read_unlock();
2261
2262 return tsk;
2263}
2264
2265static int tipc_sk_insert(struct tipc_sock *tsk)
2266{
Ying Xuee05b31f2015-01-09 15:27:08 +08002267 struct sock *sk = &tsk->sk;
2268 struct net *net = sock_net(sk);
2269 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002270 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2271 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2272
2273 while (remaining--) {
2274 portid++;
2275 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2276 portid = TIPC_MIN_PORT;
2277 tsk->portid = portid;
2278 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002279 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2280 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002281 return 0;
2282 sock_put(&tsk->sk);
2283 }
2284
2285 return -1;
2286}
2287
2288static void tipc_sk_remove(struct tipc_sock *tsk)
2289{
2290 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002291 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002292
Herbert Xu6cca72892015-03-20 21:57:05 +11002293 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002294 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2295 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002296 }
2297}
2298
Herbert Xu6cca72892015-03-20 21:57:05 +11002299static const struct rhashtable_params tsk_rht_params = {
2300 .nelem_hint = 192,
2301 .head_offset = offsetof(struct tipc_sock, node),
2302 .key_offset = offsetof(struct tipc_sock, portid),
2303 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002304 .max_size = 1048576,
2305 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002306 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002307};
2308
Ying Xuee05b31f2015-01-09 15:27:08 +08002309int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002310{
Ying Xuee05b31f2015-01-09 15:27:08 +08002311 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002312
Herbert Xu6cca72892015-03-20 21:57:05 +11002313 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002314}
2315
Ying Xuee05b31f2015-01-09 15:27:08 +08002316void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002317{
Ying Xuee05b31f2015-01-09 15:27:08 +08002318 struct tipc_net *tn = net_generic(net, tipc_net_id);
2319
Ying Xue07f6c4b2015-01-07 13:41:58 +08002320 /* Wait for socket readers to complete */
2321 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002322
Ying Xuee05b31f2015-01-09 15:27:08 +08002323 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002324}
2325
2326/**
Ying Xue247f0f32014-02-18 16:06:46 +08002327 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002328 * @sock: socket structure
2329 * @lvl: option level
2330 * @opt: option identifier
2331 * @ov: pointer to new option value
2332 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002333 *
2334 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002335 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002336 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337 * Returns 0 on success, errno otherwise
2338 */
Ying Xue247f0f32014-02-18 16:06:46 +08002339static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2340 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002341{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002342 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002343 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002344 u32 value;
2345 int res;
2346
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002347 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2348 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002349 if (lvl != SOL_TIPC)
2350 return -ENOPROTOOPT;
2351 if (ol < sizeof(value))
2352 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002353 res = get_user(value, (u32 __user *)ov);
2354 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002355 return res;
2356
Allan Stephens0c3141e2008-04-15 00:22:02 -07002357 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002358
Per Lidenb97bf3f2006-01-02 19:04:38 +01002359 switch (opt) {
2360 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002361 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362 break;
2363 case TIPC_SRC_DROPPABLE:
2364 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002365 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002366 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002367 res = -ENOPROTOOPT;
2368 break;
2369 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002370 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002371 break;
2372 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002373 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002374 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002375 break;
2376 default:
2377 res = -EINVAL;
2378 }
2379
Allan Stephens0c3141e2008-04-15 00:22:02 -07002380 release_sock(sk);
2381
Per Lidenb97bf3f2006-01-02 19:04:38 +01002382 return res;
2383}
2384
2385/**
Ying Xue247f0f32014-02-18 16:06:46 +08002386 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387 * @sock: socket structure
2388 * @lvl: option level
2389 * @opt: option identifier
2390 * @ov: receptacle for option value
2391 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002392 *
2393 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002394 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002395 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002396 * Returns 0 on success, errno otherwise
2397 */
Ying Xue247f0f32014-02-18 16:06:46 +08002398static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2399 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002400{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002401 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002402 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002403 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002404 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002405 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002406
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002407 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2408 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409 if (lvl != SOL_TIPC)
2410 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002411 res = get_user(len, ol);
2412 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002413 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002414
Allan Stephens0c3141e2008-04-15 00:22:02 -07002415 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002416
2417 switch (opt) {
2418 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002419 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002420 break;
2421 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002422 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002423 break;
2424 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002425 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002426 break;
2427 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002428 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002429 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002430 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002431 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002432 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002433 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002434 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002435 value = skb_queue_len(&sk->sk_receive_queue);
2436 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437 default:
2438 res = -EINVAL;
2439 }
2440
Allan Stephens0c3141e2008-04-15 00:22:02 -07002441 release_sock(sk);
2442
Paul Gortmaker25860c32010-12-31 18:59:31 +00002443 if (res)
2444 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002445
Paul Gortmaker25860c32010-12-31 18:59:31 +00002446 if (len < sizeof(value))
2447 return -EINVAL;
2448
2449 if (copy_to_user(ov, &value, sizeof(value)))
2450 return -EFAULT;
2451
2452 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002453}
2454
Ying Xuef2f98002015-01-09 15:27:05 +08002455static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002456{
Ying Xuef2f98002015-01-09 15:27:05 +08002457 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002458 struct tipc_sioc_ln_req lnr;
2459 void __user *argp = (void __user *)arg;
2460
2461 switch (cmd) {
2462 case SIOCGETLINKNAME:
2463 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2464 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002465 if (!tipc_node_get_linkname(sock_net(sk),
2466 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002467 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2468 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2469 return -EFAULT;
2470 return 0;
2471 }
2472 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002473 default:
2474 return -ENOIOCTLCMD;
2475 }
2476}
2477
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002478/* Protocol switches for the various types of TIPC sockets */
2479
Florian Westphalbca65ea2008-02-07 18:18:01 -08002480static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002481 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002482 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002483 .release = tipc_release,
2484 .bind = tipc_bind,
2485 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002486 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002487 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002488 .getname = tipc_getname,
2489 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002490 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002491 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002492 .shutdown = tipc_shutdown,
2493 .setsockopt = tipc_setsockopt,
2494 .getsockopt = tipc_getsockopt,
2495 .sendmsg = tipc_sendmsg,
2496 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002497 .mmap = sock_no_mmap,
2498 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002499};
2500
Florian Westphalbca65ea2008-02-07 18:18:01 -08002501static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002502 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002503 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002504 .release = tipc_release,
2505 .bind = tipc_bind,
2506 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002507 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002508 .accept = tipc_accept,
2509 .getname = tipc_getname,
2510 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002511 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002512 .listen = tipc_listen,
2513 .shutdown = tipc_shutdown,
2514 .setsockopt = tipc_setsockopt,
2515 .getsockopt = tipc_getsockopt,
2516 .sendmsg = tipc_send_packet,
2517 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002518 .mmap = sock_no_mmap,
2519 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002520};
2521
Florian Westphalbca65ea2008-02-07 18:18:01 -08002522static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002523 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002524 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002525 .release = tipc_release,
2526 .bind = tipc_bind,
2527 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002528 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002529 .accept = tipc_accept,
2530 .getname = tipc_getname,
2531 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002532 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002533 .listen = tipc_listen,
2534 .shutdown = tipc_shutdown,
2535 .setsockopt = tipc_setsockopt,
2536 .getsockopt = tipc_getsockopt,
2537 .sendmsg = tipc_send_stream,
2538 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002539 .mmap = sock_no_mmap,
2540 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002541};
2542
Florian Westphalbca65ea2008-02-07 18:18:01 -08002543static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002544 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002545 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002546 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002547};
2548
2549static struct proto tipc_proto = {
2550 .name = "TIPC",
2551 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002552 .obj_size = sizeof(struct tipc_sock),
2553 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002554};
2555
2556/**
Per Liden4323add2006-01-18 00:38:21 +01002557 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002558 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002559 * Returns 0 on success, errno otherwise
2560 */
Per Liden4323add2006-01-18 00:38:21 +01002561int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002562{
2563 int res;
2564
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002565 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002566 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002567 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002568 goto out;
2569 }
2570
2571 res = sock_register(&tipc_family_ops);
2572 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002573 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002574 proto_unregister(&tipc_proto);
2575 goto out;
2576 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002577 out:
2578 return res;
2579}
2580
2581/**
Per Liden4323add2006-01-18 00:38:21 +01002582 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002583 */
Per Liden4323add2006-01-18 00:38:21 +01002584void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002585{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002586 sock_unregister(tipc_family_ops.family);
2587 proto_unregister(&tipc_proto);
2588}
Richard Alpe34b78a122014-11-20 10:29:10 +01002589
2590/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002591static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002592{
2593 u32 peer_node;
2594 u32 peer_port;
2595 struct nlattr *nest;
2596
2597 peer_node = tsk_peer_node(tsk);
2598 peer_port = tsk_peer_port(tsk);
2599
2600 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2601
2602 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2603 goto msg_full;
2604 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2605 goto msg_full;
2606
2607 if (tsk->conn_type != 0) {
2608 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2609 goto msg_full;
2610 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2611 goto msg_full;
2612 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2613 goto msg_full;
2614 }
2615 nla_nest_end(skb, nest);
2616
2617 return 0;
2618
2619msg_full:
2620 nla_nest_cancel(skb, nest);
2621
2622 return -EMSGSIZE;
2623}
2624
2625/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002626static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2627 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002628{
2629 int err;
2630 void *hdr;
2631 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002632 struct net *net = sock_net(skb->sk);
2633 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002634
2635 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002636 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002637 if (!hdr)
2638 goto msg_cancel;
2639
2640 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2641 if (!attrs)
2642 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002643 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002644 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002645 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002646 goto attr_msg_cancel;
2647
2648 if (tsk->connected) {
2649 err = __tipc_nl_add_sk_con(skb, tsk);
2650 if (err)
2651 goto attr_msg_cancel;
2652 } else if (!list_empty(&tsk->publications)) {
2653 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2654 goto attr_msg_cancel;
2655 }
2656 nla_nest_end(skb, attrs);
2657 genlmsg_end(skb, hdr);
2658
2659 return 0;
2660
2661attr_msg_cancel:
2662 nla_nest_cancel(skb, attrs);
2663genlmsg_cancel:
2664 genlmsg_cancel(skb, hdr);
2665msg_cancel:
2666 return -EMSGSIZE;
2667}
2668
2669int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2670{
2671 int err;
2672 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002673 const struct bucket_table *tbl;
2674 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002675 struct net *net = sock_net(skb->sk);
2676 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002677 u32 tbl_id = cb->args[0];
2678 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002679
Ying Xue07f6c4b2015-01-07 13:41:58 +08002680 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002681 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002682 for (; tbl_id < tbl->size; tbl_id++) {
2683 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002684 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002685 if (prev_portid && prev_portid != tsk->portid) {
2686 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2687 continue;
2688 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002689
Richard Alped6e164e2015-01-16 12:30:40 +01002690 err = __tipc_nl_add_sk(skb, cb, tsk);
2691 if (err) {
2692 prev_portid = tsk->portid;
2693 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2694 goto out;
2695 }
2696 prev_portid = 0;
2697 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002698 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002699 }
Richard Alped6e164e2015-01-16 12:30:40 +01002700out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002701 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002702 cb->args[0] = tbl_id;
2703 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002704
2705 return skb->len;
2706}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002707
2708/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002709static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2710 struct netlink_callback *cb,
2711 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002712{
2713 void *hdr;
2714 struct nlattr *attrs;
2715
2716 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002717 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002718 if (!hdr)
2719 goto msg_cancel;
2720
2721 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2722 if (!attrs)
2723 goto genlmsg_cancel;
2724
2725 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2726 goto attr_msg_cancel;
2727 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2728 goto attr_msg_cancel;
2729 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2730 goto attr_msg_cancel;
2731 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2732 goto attr_msg_cancel;
2733
2734 nla_nest_end(skb, attrs);
2735 genlmsg_end(skb, hdr);
2736
2737 return 0;
2738
2739attr_msg_cancel:
2740 nla_nest_cancel(skb, attrs);
2741genlmsg_cancel:
2742 genlmsg_cancel(skb, hdr);
2743msg_cancel:
2744 return -EMSGSIZE;
2745}
2746
2747/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002748static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2749 struct netlink_callback *cb,
2750 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002751{
2752 int err;
2753 struct publication *p;
2754
2755 if (*last_publ) {
2756 list_for_each_entry(p, &tsk->publications, pport_list) {
2757 if (p->key == *last_publ)
2758 break;
2759 }
2760 if (p->key != *last_publ) {
2761 /* We never set seq or call nl_dump_check_consistent()
2762 * this means that setting prev_seq here will cause the
2763 * consistence check to fail in the netlink callback
2764 * handler. Resulting in the last NLMSG_DONE message
2765 * having the NLM_F_DUMP_INTR flag set.
2766 */
2767 cb->prev_seq = 1;
2768 *last_publ = 0;
2769 return -EPIPE;
2770 }
2771 } else {
2772 p = list_first_entry(&tsk->publications, struct publication,
2773 pport_list);
2774 }
2775
2776 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2777 err = __tipc_nl_add_sk_publ(skb, cb, p);
2778 if (err) {
2779 *last_publ = p->key;
2780 return err;
2781 }
2782 }
2783 *last_publ = 0;
2784
2785 return 0;
2786}
2787
2788int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2789{
2790 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002791 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002792 u32 last_publ = cb->args[1];
2793 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002794 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002795 struct tipc_sock *tsk;
2796
Ying Xue07f6c4b2015-01-07 13:41:58 +08002797 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002798 struct nlattr **attrs;
2799 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2800
2801 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2802 if (err)
2803 return err;
2804
2805 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2806 attrs[TIPC_NLA_SOCK],
2807 tipc_nl_sock_policy);
2808 if (err)
2809 return err;
2810
2811 if (!sock[TIPC_NLA_SOCK_REF])
2812 return -EINVAL;
2813
Ying Xue07f6c4b2015-01-07 13:41:58 +08002814 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002815 }
2816
2817 if (done)
2818 return 0;
2819
Ying Xuee05b31f2015-01-09 15:27:08 +08002820 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002821 if (!tsk)
2822 return -EINVAL;
2823
2824 lock_sock(&tsk->sk);
2825 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2826 if (!err)
2827 done = 1;
2828 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002829 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002830
Ying Xue07f6c4b2015-01-07 13:41:58 +08002831 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002832 cb->args[1] = last_publ;
2833 cb->args[2] = done;
2834
2835 return skb->len;
2836}