blob: 2cec496ba691930f372efbf01830fc4e8e01c576 [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 Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, 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>
38#include <linux/jhash.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050040#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020041#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050042#include "link.h"
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -040043#include "config.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040044#include "socket.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040045
Ying Xue07f6c4b2015-01-07 13:41:58 +080046#define SS_LISTENING -1 /* socket is listening */
47#define SS_READY -2 /* socket is connectionless */
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
Ying Xue07f6c4b2015-01-07 13:41:58 +080049#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080050#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080051#define TIPC_FWD_MSG 1
52#define TIPC_CONN_OK 0
53#define TIPC_CONN_PROBING 1
54#define TIPC_MAX_PORT 0xffffffff
55#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040056
57/**
58 * struct tipc_sock - TIPC socket structure
59 * @sk: socket - interacts with 'port' and with user via the socket API
60 * @connected: non-zero if port is currently connected to a peer port
61 * @conn_type: TIPC type used when connection was established
62 * @conn_instance: TIPC instance used when connection was established
63 * @published: non-zero if port has one or more associated names
64 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080065 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040066 * @phdr: preformatted message header used when sending messages
67 * @port_list: adjacent ports in TIPC's global list of ports
68 * @publications: list of publications for port
69 * @pub_count: total # of publications port has made during its lifetime
70 * @probing_state:
Ying Xue2f55c432015-01-09 15:27:00 +080071 * @probing_intv:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040072 * @timer:
73 * @port: port - interacts with 'sk' and with the rest of the TIPC stack
74 * @peer_name: the peer of the connection, if any
75 * @conn_timeout: the time we can wait for an unresponded setup request
76 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
77 * @link_cong: non-zero if owner must sleep because of link congestion
78 * @sent_unacked: # messages sent by socket, and not yet acked by peer
79 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Ying Xue07f6c4b2015-01-07 13:41:58 +080080 * @node: hash table node
81 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040082 */
83struct tipc_sock {
84 struct sock sk;
85 int connected;
86 u32 conn_type;
87 u32 conn_instance;
88 int published;
89 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080090 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040091 struct tipc_msg phdr;
92 struct list_head sock_list;
93 struct list_head publications;
94 u32 pub_count;
95 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080096 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040097 struct timer_list timer;
98 uint conn_timeout;
99 atomic_t dupl_rcvcnt;
100 bool link_cong;
101 uint sent_unacked;
102 uint rcv_unacked;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800103 struct rhash_head node;
104 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400105};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400107static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400108static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800109static void tipc_write_space(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);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121
Florian Westphalbca65ea2008-02-07 18:18:01 -0800122static const struct proto_ops packet_ops;
123static const struct proto_ops stream_ops;
124static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125
126static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400127static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128
Richard Alpe1a1a1432014-11-20 10:29:11 +0100129static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
130 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
131 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
132 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
133 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
134 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
135};
136
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900137/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138 * Revised TIPC socket locking policy:
139 *
140 * Most socket operations take the standard socket lock when they start
141 * and hold it until they finish (or until they need to sleep). Acquiring
142 * this lock grants the owner exclusive access to the fields of the socket
143 * data structures, with the exception of the backlog queue. A few socket
144 * operations can be done without taking the socket lock because they only
145 * read socket information that never changes during the life of the socket.
146 *
147 * Socket operations may acquire the lock for the associated TIPC port if they
148 * need to perform an operation on the port. If any routine needs to acquire
149 * both the socket lock and the port lock it must take the socket lock first
150 * to avoid the risk of deadlock.
151 *
152 * The dispatcher handling incoming messages cannot grab the socket lock in
153 * the standard fashion, since invoked it runs at the BH level and cannot block.
154 * Instead, it checks to see if the socket lock is currently owned by someone,
155 * and either handles the message itself or adds it to the socket's backlog
156 * queue; in the latter case the queued message is processed once the process
157 * owning the socket lock releases it.
158 *
159 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
160 * the problem of a blocked socket operation preventing any other operations
161 * from occurring. However, applications must be careful if they have
162 * multiple threads trying to send (or receive) on the same socket, as these
163 * operations might interfere with each other. For example, doing a connect
164 * and a receive at the same time might allow the receive to consume the
165 * ACK message meant for the connect. While additional work could be done
166 * to try and overcome this, it doesn't seem to be worthwhile at the present.
167 *
168 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
169 * that another operation that must be performed in a non-blocking manner is
170 * not delayed for very long because the lock has already been taken.
171 *
172 * NOTE: This code assumes that certain fields of a port/socket pair are
173 * constant over its lifetime; such fields can be examined without taking
174 * the socket lock and/or port lock, and do not need to be re-read even
175 * after resuming processing after waiting. These fields include:
176 * - socket type
177 * - pointer to socket sk structure (aka tipc_sock structure)
178 * - pointer to port structure
179 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181
Jon Paul 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
245/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400246 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700247 *
248 * Caller must hold socket lock
249 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400250static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700251{
Ying Xuea6ca1092014-11-26 11:41:55 +0800252 struct sk_buff *skb;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500253 u32 dnode;
Ying Xue34747532015-01-09 15:27:10 +0800254 struct net *net = sock_net(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700255
Ying Xuea6ca1092014-11-26 11:41:55 +0800256 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) {
Ying Xue34747532015-01-09 15:27:10 +0800257 if (tipc_msg_reverse(net, skb, &dnode, TIPC_ERR_NO_PORT))
258 tipc_link_xmit_skb(net, skb, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500259 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700260}
261
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400262/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400263 *
264 * Handles cases where the node's network address has changed from
265 * the default of <0.0.0> to its configured setting.
266 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400267static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400268{
Ying Xue34747532015-01-09 15:27:10 +0800269 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400270 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400271 u32 orig_node;
272 u32 peer_node;
273
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400274 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400275 return false;
276
277 if (unlikely(msg_origport(msg) != peer_port))
278 return false;
279
280 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400281 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400282
283 if (likely(orig_node == peer_node))
284 return true;
285
Ying Xue34747532015-01-09 15:27:10 +0800286 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400287 return true;
288
Ying Xue34747532015-01-09 15:27:10 +0800289 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400290 return true;
291
292 return false;
293}
294
Allan Stephens0c3141e2008-04-15 00:22:02 -0700295/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400296 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700297 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 * @sock: pre-allocated socket structure
299 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800300 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900301 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700302 * This routine creates additional data structures used by the TIPC socket,
303 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 *
305 * Returns 0 on success, errno otherwise
306 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400307static int tipc_sk_create(struct net *net, struct socket *sock,
308 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700310 const struct proto_ops *ops;
311 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400313 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400314 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700315
316 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 if (unlikely(protocol != 0))
318 return -EPROTONOSUPPORT;
319
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 switch (sock->type) {
321 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700322 ops = &stream_ops;
323 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 break;
325 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700326 ops = &packet_ops;
327 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 break;
329 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700331 ops = &msg_ops;
332 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 break;
Allan Stephens49978652006-06-25 23:47:18 -0700334 default:
Allan Stephens49978652006-06-25 23:47:18 -0700335 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 }
337
Allan Stephens0c3141e2008-04-15 00:22:02 -0700338 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400339 if (!kern)
340 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
341 else
342 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
343
Allan Stephens0c3141e2008-04-15 00:22:02 -0700344 if (sk == NULL)
345 return -ENOMEM;
346
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400347 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400348 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400349 INIT_LIST_HEAD(&tsk->publications);
350 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +0800351 tipc_msg_init(net, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400352 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 sock->ops = ops;
356 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800358 if (tipc_sk_insert(tsk)) {
359 pr_warn("Socket create failed; port numbrer exhausted\n");
360 return -EINVAL;
361 }
362 msg_set_origport(msg, tsk->portid);
Ying Xuef2f2a962015-01-09 15:27:02 +0800363 setup_timer(&tsk->timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400364 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400365 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800366 sk->sk_data_ready = tipc_data_ready;
367 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400368 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500369 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400370 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700371
Allan Stephens0c3141e2008-04-15 00:22:02 -0700372 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400373 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700374 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400375 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700376 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 return 0;
378}
379
380/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400381 * tipc_sock_create_local - create TIPC socket from inside TIPC module
382 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
383 *
384 * We cannot use sock_creat_kern here because it bumps module user count.
385 * Since socket owner and creator is the same module we must make sure
386 * that module count remains zero for module local sockets, otherwise
387 * we cannot do rmmod.
388 *
389 * Returns 0 on success, errno otherwise
390 */
Ying Xuea62fbcc2015-01-09 15:27:11 +0800391int tipc_sock_create_local(struct net *net, int type, struct socket **res)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400392{
393 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400394
395 rc = sock_create_lite(AF_TIPC, type, 0, res);
396 if (rc < 0) {
397 pr_err("Failed to create kernel socket\n");
398 return rc;
399 }
Ying Xuea62fbcc2015-01-09 15:27:11 +0800400 tipc_sk_create(net, *res, 0, 1);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400401
Ying Xuec5fa7b32013-06-17 10:54:39 -0400402 return 0;
403}
404
405/**
406 * tipc_sock_release_local - release socket created by tipc_sock_create_local
407 * @sock: the socket to be released.
408 *
409 * Module reference count is not incremented when such sockets are created,
410 * so we must keep it from being decremented when they are released.
411 */
412void tipc_sock_release_local(struct socket *sock)
413{
Ying Xue247f0f32014-02-18 16:06:46 +0800414 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400415 sock->ops = NULL;
416 sock_release(sock);
417}
418
419/**
420 * tipc_sock_accept_local - accept a connection on a socket created
421 * with tipc_sock_create_local. Use this function to avoid that
422 * module reference count is inadvertently incremented.
423 *
424 * @sock: the accepting socket
425 * @newsock: reference to the new socket to be created
426 * @flags: socket flags
427 */
428
429int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400430 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400431{
432 struct sock *sk = sock->sk;
433 int ret;
434
435 ret = sock_create_lite(sk->sk_family, sk->sk_type,
436 sk->sk_protocol, newsock);
437 if (ret < 0)
438 return ret;
439
Ying Xue247f0f32014-02-18 16:06:46 +0800440 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400441 if (ret < 0) {
442 sock_release(*newsock);
443 return ret;
444 }
445 (*newsock)->ops = sock->ops;
446 return ret;
447}
448
Ying Xue07f6c4b2015-01-07 13:41:58 +0800449static void tipc_sk_callback(struct rcu_head *head)
450{
451 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
452
453 sock_put(&tsk->sk);
454}
455
Ying Xuec5fa7b32013-06-17 10:54:39 -0400456/**
Ying Xue247f0f32014-02-18 16:06:46 +0800457 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 * @sock: socket to destroy
459 *
460 * This routine cleans up any messages that are still queued on the socket.
461 * For DGRAM and RDM socket types, all queued messages are rejected.
462 * For SEQPACKET and STREAM socket types, the first message is rejected
463 * and any others are discarded. (If the first message on a STREAM socket
464 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900465 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 * NOTE: Rejected messages are not necessarily returned to the sender! They
467 * are returned or discarded according to the "destination droppable" setting
468 * specified for the message by the sender.
469 *
470 * Returns 0 on success, errno otherwise
471 */
Ying Xue247f0f32014-02-18 16:06:46 +0800472static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800475 struct net *net = sock_net(sk);
Ying Xue34747532015-01-09 15:27:10 +0800476 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400477 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800478 struct sk_buff *skb;
Ying Xuef2f2a962015-01-09 15:27:02 +0800479 u32 dnode, probing_state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480
Allan Stephens0c3141e2008-04-15 00:22:02 -0700481 /*
482 * Exit if socket isn't fully initialized (occurs when a failed accept()
483 * releases a pre-allocated child socket that was never used)
484 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700485 if (sk == NULL)
486 return 0;
487
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400488 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700489 lock_sock(sk);
490
491 /*
492 * Reject all unreceived messages, except on an active connection
493 * (which disconnects locally & sends a 'FIN+' to peer)
494 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400495 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800497 skb = __skb_dequeue(&sk->sk_receive_queue);
498 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 break;
Ying Xuea6ca1092014-11-26 11:41:55 +0800500 if (TIPC_SKB_CB(skb)->handle != NULL)
501 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700502 else {
503 if ((sock->state == SS_CONNECTING) ||
504 (sock->state == SS_CONNECTED)) {
505 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400506 tsk->connected = 0;
Ying Xuef2f98002015-01-09 15:27:05 +0800507 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700508 }
Ying Xue34747532015-01-09 15:27:10 +0800509 if (tipc_msg_reverse(net, skb, &dnode,
510 TIPC_ERR_NO_PORT))
Ying Xuef2f98002015-01-09 15:27:05 +0800511 tipc_link_xmit_skb(net, skb, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700512 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 }
514
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400515 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xuef2f2a962015-01-09 15:27:02 +0800516 probing_state = tsk->probing_state;
517 if (del_timer_sync(&tsk->timer) && probing_state != TIPC_CONN_PROBING)
518 sock_put(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800519 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400520 if (tsk->connected) {
Ying Xue34747532015-01-09 15:27:10 +0800521 skb = tipc_msg_create(net, TIPC_CRITICAL_IMPORTANCE,
522 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
523 tn->own_addr, tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800524 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800525 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +0800526 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
527 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400528 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529
Allan Stephens0c3141e2008-04-15 00:22:02 -0700530 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100531 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532
Allan Stephens0c3141e2008-04-15 00:22:02 -0700533 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700534 sock->state = SS_DISCONNECTING;
535 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800536
537 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700538 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200540 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541}
542
543/**
Ying Xue247f0f32014-02-18 16:06:46 +0800544 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 * @sock: socket structure
546 * @uaddr: socket address describing name(s) and desired operation
547 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900548 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 * Name and name sequence binding is indicated using a positive scope value;
550 * a negative scope value unbinds the specified name. Specifying no name
551 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700554 *
555 * NOTE: This routine doesn't need to take the socket lock since it doesn't
556 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 */
Ying Xue247f0f32014-02-18 16:06:46 +0800558static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
559 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560{
Ying Xue84602762013-12-27 10:18:28 +0800561 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400563 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800564 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565
Ying Xue84602762013-12-27 10:18:28 +0800566 lock_sock(sk);
567 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400568 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800569 goto exit;
570 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900571
Ying Xue84602762013-12-27 10:18:28 +0800572 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
573 res = -EINVAL;
574 goto exit;
575 }
576 if (addr->family != AF_TIPC) {
577 res = -EAFNOSUPPORT;
578 goto exit;
579 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 if (addr->addrtype == TIPC_ADDR_NAME)
582 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800583 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
584 res = -EAFNOSUPPORT;
585 goto exit;
586 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587
Ying Xue13a2e892013-06-17 10:54:40 -0400588 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400589 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800590 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
591 res = -EACCES;
592 goto exit;
593 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400594
Ying Xue84602762013-12-27 10:18:28 +0800595 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400596 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
597 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800598exit:
599 release_sock(sk);
600 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601}
602
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603/**
Ying Xue247f0f32014-02-18 16:06:46 +0800604 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 * @sock: socket structure
606 * @uaddr: area for returned socket address
607 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700608 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900609 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700611 *
Allan Stephens2da59912008-07-14 22:43:32 -0700612 * NOTE: This routine doesn't need to take the socket lock since it only
613 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000614 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 */
Ying Xue247f0f32014-02-18 16:06:46 +0800616static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
617 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400620 struct tipc_sock *tsk = tipc_sk(sock->sk);
Ying Xue34747532015-01-09 15:27:10 +0800621 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000623 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700624 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700625 if ((sock->state != SS_CONNECTED) &&
626 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
627 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400628 addr->addr.id.ref = tsk_peer_port(tsk);
629 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700630 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800631 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800632 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700633 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634
635 *uaddr_len = sizeof(*addr);
636 addr->addrtype = TIPC_ADDR_ID;
637 addr->family = AF_TIPC;
638 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639 addr->addr.name.domain = 0;
640
Allan Stephens0c3141e2008-04-15 00:22:02 -0700641 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642}
643
644/**
Ying Xue247f0f32014-02-18 16:06:46 +0800645 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 * @file: file structure associated with the socket
647 * @sock: socket for which to calculate the poll bits
648 * @wait: ???
649 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700650 * Returns pollmask value
651 *
652 * COMMENTARY:
653 * It appears that the usual socket locking mechanisms are not useful here
654 * since the pollmask info is potentially out-of-date the moment this routine
655 * exits. TCP and other protocols seem to rely on higher level poll routines
656 * to handle any preventable race conditions, so TIPC will do the same ...
657 *
658 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700659 *
Allan Stephensf662c072010-08-17 11:00:06 +0000660 * socket state flags set
661 * ------------ ---------
662 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200663 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000664 *
665 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
666 * no write flags
667 *
668 * connected POLLIN/POLLRDNORM if data in rx queue
669 * POLLOUT if port is not congested
670 *
671 * disconnecting POLLIN/POLLRDNORM/POLLHUP
672 * no write flags
673 *
674 * listening POLLIN if SYN in rx queue
675 * no write flags
676 *
677 * ready POLLIN/POLLRDNORM if data in rx queue
678 * [connectionless] POLLOUT (since port cannot be congested)
679 *
680 * IMPORTANT: The fact that a read or write operation is indicated does NOT
681 * imply that the operation will succeed, merely that it should be performed
682 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 */
Ying Xue247f0f32014-02-18 16:06:46 +0800684static unsigned int tipc_poll(struct file *file, struct socket *sock,
685 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686{
Allan Stephens9b674e82008-03-26 16:48:21 -0700687 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400688 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000689 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700690
Ying Xuef288bef2012-08-21 11:16:57 +0800691 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700692
Allan Stephensf662c072010-08-17 11:00:06 +0000693 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200694 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500695 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200696 mask |= POLLOUT;
697 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000698 case SS_READY:
699 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400700 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000701 mask |= POLLOUT;
702 /* fall thru' */
703 case SS_CONNECTING:
704 case SS_LISTENING:
705 if (!skb_queue_empty(&sk->sk_receive_queue))
706 mask |= (POLLIN | POLLRDNORM);
707 break;
708 case SS_DISCONNECTING:
709 mask = (POLLIN | POLLRDNORM | POLLHUP);
710 break;
711 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700712
713 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714}
715
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400716/**
717 * tipc_sendmcast - send multicast message
718 * @sock: socket structure
719 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500720 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400721 * @dsz: total length of message data
722 * @timeo: timeout to wait for wakeup
723 *
724 * Called from function tipc_sendmsg(), which has done all sanity checks
725 * Returns the number of bytes sent on success, or errno
726 */
727static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500728 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400729{
730 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800731 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400732 struct tipc_msg *mhdr = &tipc_sk(sk)->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +0800733 struct sk_buff_head head;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400734 uint mtu;
735 int rc;
736
737 msg_set_type(mhdr, TIPC_MCAST_MSG);
738 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
739 msg_set_destport(mhdr, 0);
740 msg_set_destnode(mhdr, 0);
741 msg_set_nametype(mhdr, seq->type);
742 msg_set_namelower(mhdr, seq->lower);
743 msg_set_nameupper(mhdr, seq->upper);
744 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
745
746new_mtu:
747 mtu = tipc_bclink_get_mtu();
Ying Xuea6ca1092014-11-26 11:41:55 +0800748 __skb_queue_head_init(&head);
Ying Xue34747532015-01-09 15:27:10 +0800749 rc = tipc_msg_build(net, mhdr, msg, 0, dsz, mtu, &head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400750 if (unlikely(rc < 0))
751 return rc;
752
753 do {
Ying Xuef2f98002015-01-09 15:27:05 +0800754 rc = tipc_bclink_xmit(net, &head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400755 if (likely(rc >= 0)) {
756 rc = dsz;
757 break;
758 }
759 if (rc == -EMSGSIZE)
760 goto new_mtu;
761 if (rc != -ELINKCONG)
762 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400763 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400764 rc = tipc_wait_for_sndmsg(sock, &timeo);
765 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800766 __skb_queue_purge(&head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400767 } while (!rc);
768 return rc;
769}
770
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400771/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
772 */
Ying Xuef2f98002015-01-09 15:27:05 +0800773void tipc_sk_mcast_rcv(struct net *net, struct sk_buff *buf)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400774{
775 struct tipc_msg *msg = buf_msg(buf);
776 struct tipc_port_list dports = {0, NULL, };
777 struct tipc_port_list *item;
778 struct sk_buff *b;
779 uint i, last, dst = 0;
780 u32 scope = TIPC_CLUSTER_SCOPE;
781
Ying Xue34747532015-01-09 15:27:10 +0800782 if (in_own_node(net, msg_orignode(msg)))
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400783 scope = TIPC_NODE_SCOPE;
784
785 /* Create destination port list: */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800786 tipc_nametbl_mc_translate(net, msg_nametype(msg), msg_namelower(msg),
787 msg_nameupper(msg), scope, &dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400788 last = dports.count;
789 if (!last) {
790 kfree_skb(buf);
791 return;
792 }
793
794 for (item = &dports; item; item = item->next) {
795 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
796 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
797 if (!b) {
798 pr_warn("Failed do clone mcast rcv buffer\n");
799 continue;
800 }
801 msg_set_destport(msg, item->ports[i]);
Ying Xuef2f98002015-01-09 15:27:05 +0800802 tipc_sk_rcv(net, b);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400803 }
804 }
805 tipc_port_list_free(&dports);
806}
807
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900808/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500809 * tipc_sk_proto_rcv - receive a connection mng protocol message
810 * @tsk: receiving socket
811 * @dnode: node to send response message to, if any
812 * @buf: buffer containing protocol message
813 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
814 * (CONN_PROBE_REPLY) message should be forwarded.
815 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800816static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
817 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500818{
819 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500820 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500821
822 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400823 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500824 goto exit;
825
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400826 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500827
828 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400829 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500830 tsk->sent_unacked -= msg_msgcnt(msg);
831 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400832 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500833 } else if (msg_type(msg) == CONN_PROBE) {
Ying Xue34747532015-01-09 15:27:10 +0800834 if (!tipc_msg_reverse(sock_net(&tsk->sk), buf, dnode, TIPC_OK))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500835 return TIPC_OK;
836 msg_set_type(msg, CONN_PROBE_REPLY);
837 return TIPC_FWD_MSG;
838 }
839 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
840exit:
841 kfree_skb(buf);
842 return TIPC_OK;
843}
844
Ying Xue3f405042014-01-17 09:50:05 +0800845static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
846{
847 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400848 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800849 DEFINE_WAIT(wait);
850 int done;
851
852 do {
853 int err = sock_error(sk);
854 if (err)
855 return err;
856 if (sock->state == SS_DISCONNECTING)
857 return -EPIPE;
858 if (!*timeo_p)
859 return -EAGAIN;
860 if (signal_pending(current))
861 return sock_intr_errno(*timeo_p);
862
863 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500864 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800865 finish_wait(sk_sleep(sk), &wait);
866 } while (!done);
867 return 0;
868}
869
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500870/**
Ying Xue247f0f32014-02-18 16:06:46 +0800871 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700872 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 * @sock: socket structure
874 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500875 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900876 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900878 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
880 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900881 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 * Returns the number of bytes sent on success, or errno otherwise
883 */
Ying Xue247f0f32014-02-18 16:06:46 +0800884static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500885 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500887 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700888 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400889 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800890 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400891 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500892 u32 dnode, dport;
Ying Xuea6ca1092014-11-26 11:41:55 +0800893 struct sk_buff_head head;
894 struct sk_buff *skb;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500895 struct tipc_name_seq *seq = &dest->addr.nameseq;
896 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800897 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100898 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899
900 if (unlikely(!dest))
901 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500902
Allan Stephens51f9cc12006-06-25 23:49:06 -0700903 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
904 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500906
907 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400908 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909
Allan Stephens0c3141e2008-04-15 00:22:02 -0700910 if (iocb)
911 lock_sock(sk);
912
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500913 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700914 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500915 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700916 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700917 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700918 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500919 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700920 goto exit;
921 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400922 if (tsk->published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500923 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700924 goto exit;
925 }
926 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400927 tsk->conn_type = dest->addr.name.name.type;
928 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700929 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 }
931
Ying Xue3f405042014-01-17 09:50:05 +0800932 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500933
934 if (dest->addrtype == TIPC_ADDR_MCAST) {
Al Viro562640f2014-11-15 01:13:43 -0500935 rc = tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500936 goto exit;
937 } else if (dest->addrtype == TIPC_ADDR_NAME) {
938 u32 type = dest->addr.name.name.type;
939 u32 inst = dest->addr.name.name.instance;
940 u32 domain = dest->addr.name.domain;
941
942 dnode = domain;
943 msg_set_type(mhdr, TIPC_NAMED_MSG);
944 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
945 msg_set_nametype(mhdr, type);
946 msg_set_nameinst(mhdr, inst);
947 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800948 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500949 msg_set_destnode(mhdr, dnode);
950 msg_set_destport(mhdr, dport);
951 if (unlikely(!dport && !dnode)) {
952 rc = -EHOSTUNREACH;
953 goto exit;
954 }
955 } else if (dest->addrtype == TIPC_ADDR_ID) {
956 dnode = dest->addr.id.node;
957 msg_set_type(mhdr, TIPC_DIRECT_MSG);
958 msg_set_lookup_scope(mhdr, 0);
959 msg_set_destnode(mhdr, dnode);
960 msg_set_destport(mhdr, dest->addr.id.ref);
961 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
962 }
963
964new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800965 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Ying Xuea6ca1092014-11-26 11:41:55 +0800966 __skb_queue_head_init(&head);
Ying Xue34747532015-01-09 15:27:10 +0800967 rc = tipc_msg_build(net, mhdr, m, 0, dsz, mtu, &head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500968 if (rc < 0)
969 goto exit;
970
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900971 do {
Ying Xuea6ca1092014-11-26 11:41:55 +0800972 skb = skb_peek(&head);
973 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Ying Xuef2f98002015-01-09 15:27:05 +0800974 rc = tipc_link_xmit(net, &head, dnode, tsk->portid);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500975 if (likely(rc >= 0)) {
976 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700977 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500978 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700979 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900980 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500981 if (rc == -EMSGSIZE)
982 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500983 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700984 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400985 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500986 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400987 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800988 __skb_queue_purge(&head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500989 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700990exit:
991 if (iocb)
992 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500993
994 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995}
996
Ying Xue391a6dd2014-01-17 09:50:06 +0800997static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
998{
999 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001000 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +08001001 DEFINE_WAIT(wait);
1002 int done;
1003
1004 do {
1005 int err = sock_error(sk);
1006 if (err)
1007 return err;
1008 if (sock->state == SS_DISCONNECTING)
1009 return -EPIPE;
1010 else if (sock->state != SS_CONNECTED)
1011 return -ENOTCONN;
1012 if (!*timeo_p)
1013 return -EAGAIN;
1014 if (signal_pending(current))
1015 return sock_intr_errno(*timeo_p);
1016
1017 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1018 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001019 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001020 !tsk_conn_cong(tsk)) ||
1021 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +08001022 finish_wait(sk_sleep(sk), &wait);
1023 } while (!done);
1024 return 0;
1025}
1026
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027/**
Ying Xue247f0f32014-02-18 16:06:46 +08001028 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 * @iocb: (unused)
1030 * @sock: socket structure
1031 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001032 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001033 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001035 *
1036 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001037 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038 */
Ying Xue247f0f32014-02-18 16:06:46 +08001039static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001040 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001042 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001043 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001044 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001045 struct tipc_msg *mhdr = &tsk->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +08001046 struct sk_buff_head head;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001047 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001048 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001049 int rc = -EINVAL;
1050 long timeo;
1051 u32 dnode;
1052 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001053
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001054 /* Handle implied connection establishment */
1055 if (unlikely(dest)) {
1056 rc = tipc_sendmsg(iocb, sock, m, dsz);
1057 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001058 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001059 return rc;
1060 }
1061 if (dsz > (uint)INT_MAX)
1062 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001063
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001064 if (iocb)
1065 lock_sock(sk);
1066
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001067 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001068 if (sock->state == SS_DISCONNECTING)
1069 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001070 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001071 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +08001072 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001073 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001075 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001076 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001077
1078next:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001079 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001080 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001081 __skb_queue_head_init(&head);
Ying Xue34747532015-01-09 15:27:10 +08001082 rc = tipc_msg_build(net, mhdr, m, sent, send, mtu, &head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001083 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001084 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001085 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001086 if (likely(!tsk_conn_cong(tsk))) {
Ying Xuef2f98002015-01-09 15:27:05 +08001087 rc = tipc_link_xmit(net, &head, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001088 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001089 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001090 sent += send;
1091 if (sent == dsz)
1092 break;
1093 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001094 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001095 if (rc == -EMSGSIZE) {
Ying Xuef2f98002015-01-09 15:27:05 +08001096 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1097 portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001098 goto next;
1099 }
1100 if (rc != -ELINKCONG)
1101 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001102 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001104 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001105 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +08001106 __skb_queue_purge(&head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001107 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001108exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001109 if (iocb)
1110 release_sock(sk);
1111 return sent ? sent : rc;
1112}
1113
1114/**
1115 * tipc_send_packet - send a connection-oriented message
1116 * @iocb: if NULL, indicates that socket lock is already held
1117 * @sock: socket structure
1118 * @m: message to send
1119 * @dsz: length of data to be transmitted
1120 *
1121 * Used for SOCK_SEQPACKET messages.
1122 *
1123 * Returns the number of bytes sent on success, or errno otherwise
1124 */
1125static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1126 struct msghdr *m, size_t dsz)
1127{
1128 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1129 return -EMSGSIZE;
1130
1131 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132}
1133
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001134/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001136static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001137 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138{
Ying Xuef2f98002015-01-09 15:27:05 +08001139 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001140 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001142 msg_set_destnode(msg, peer_node);
1143 msg_set_destport(msg, peer_port);
1144 msg_set_type(msg, TIPC_CONN_MSG);
1145 msg_set_lookup_scope(msg, 0);
1146 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001147
Ying Xue2f55c432015-01-09 15:27:00 +08001148 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001149 tsk->probing_state = TIPC_CONN_OK;
1150 tsk->connected = 1;
Ying Xuef2f2a962015-01-09 15:27:02 +08001151 if (!mod_timer(&tsk->timer, jiffies + tsk->probing_intv))
1152 sock_hold(&tsk->sk);
Ying Xuef2f98002015-01-09 15:27:05 +08001153 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1154 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155}
1156
1157/**
1158 * set_orig_addr - capture sender's address for received message
1159 * @m: descriptor for message info
1160 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001161 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 * Note: Address is not captured if not requested by receiver.
1163 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001164static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001166 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001168 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 addr->family = AF_TIPC;
1170 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001171 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 addr->addr.id.ref = msg_origport(msg);
1173 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001174 addr->addr.name.domain = 0; /* could leave uninitialized */
1175 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 m->msg_namelen = sizeof(struct sockaddr_tipc);
1177 }
1178}
1179
1180/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001181 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 * @m: descriptor for message info
1183 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001184 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001185 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001187 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 * Returns 0 if successful, otherwise errno
1189 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001190static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1191 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192{
1193 u32 anc_data[3];
1194 u32 err;
1195 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001196 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 int res;
1198
1199 if (likely(m->msg_controllen == 0))
1200 return 0;
1201
1202 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 err = msg ? msg_errcode(msg) : 0;
1204 if (unlikely(err)) {
1205 anc_data[0] = err;
1206 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001207 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1208 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001210 if (anc_data[1]) {
1211 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1212 msg_data(msg));
1213 if (res)
1214 return res;
1215 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 }
1217
1218 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1220 switch (dest_type) {
1221 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001222 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 anc_data[0] = msg_nametype(msg);
1224 anc_data[1] = msg_namelower(msg);
1225 anc_data[2] = msg_namelower(msg);
1226 break;
1227 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001228 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 anc_data[0] = msg_nametype(msg);
1230 anc_data[1] = msg_namelower(msg);
1231 anc_data[2] = msg_nameupper(msg);
1232 break;
1233 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001234 has_name = (tsk->conn_type != 0);
1235 anc_data[0] = tsk->conn_type;
1236 anc_data[1] = tsk->conn_instance;
1237 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 break;
1239 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001240 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 }
Allan Stephens2db99832010-12-31 18:59:33 +00001242 if (has_name) {
1243 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1244 if (res)
1245 return res;
1246 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247
1248 return 0;
1249}
1250
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001251static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001252{
Ying Xuef2f98002015-01-09 15:27:05 +08001253 struct net *net = sock_net(&tsk->sk);
Ying Xue34747532015-01-09 15:27:10 +08001254 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuea6ca1092014-11-26 11:41:55 +08001255 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001256 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001257 u32 peer_port = tsk_peer_port(tsk);
1258 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001259
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001260 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001261 return;
Ying Xue34747532015-01-09 15:27:10 +08001262 skb = tipc_msg_create(net, CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1263 dnode, tn->own_addr, peer_port, tsk->portid,
1264 TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001265 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001266 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001267 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001268 msg_set_msgcnt(msg, ack);
Ying Xuef2f98002015-01-09 15:27:05 +08001269 tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001270}
1271
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001272static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001273{
1274 struct sock *sk = sock->sk;
1275 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001276 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001277 int err;
1278
1279 for (;;) {
1280 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001281 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001282 if (sock->state == SS_DISCONNECTING) {
1283 err = -ENOTCONN;
1284 break;
1285 }
1286 release_sock(sk);
1287 timeo = schedule_timeout(timeo);
1288 lock_sock(sk);
1289 }
1290 err = 0;
1291 if (!skb_queue_empty(&sk->sk_receive_queue))
1292 break;
1293 err = sock_intr_errno(timeo);
1294 if (signal_pending(current))
1295 break;
1296 err = -EAGAIN;
1297 if (!timeo)
1298 break;
1299 }
1300 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001301 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001302 return err;
1303}
1304
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001305/**
Ying Xue247f0f32014-02-18 16:06:46 +08001306 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 * @iocb: (unused)
1308 * @m: descriptor for message info
1309 * @buf_len: total size of user buffer area
1310 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001311 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1313 * If the complete message doesn't fit in user area, truncate it.
1314 *
1315 * Returns size of returned message data, errno otherwise
1316 */
Ying Xue247f0f32014-02-18 16:06:46 +08001317static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1318 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001320 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001321 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 struct sk_buff *buf;
1323 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001324 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 unsigned int sz;
1326 u32 err;
1327 int res;
1328
Allan Stephens0c3141e2008-04-15 00:22:02 -07001329 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330 if (unlikely(!buf_len))
1331 return -EINVAL;
1332
Allan Stephens0c3141e2008-04-15 00:22:02 -07001333 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334
Allan Stephens0c3141e2008-04-15 00:22:02 -07001335 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 res = -ENOTCONN;
1337 goto exit;
1338 }
1339
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001340 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001341restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342
Allan Stephens0c3141e2008-04-15 00:22:02 -07001343 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001344 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001345 if (res)
1346 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001347
1348 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001349 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 msg = buf_msg(buf);
1351 sz = msg_data_sz(msg);
1352 err = msg_errcode(msg);
1353
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001356 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 goto restart;
1358 }
1359
1360 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 set_orig_addr(m, msg);
1362
1363 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001364 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001365 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 goto exit;
1367
1368 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369 if (!err) {
1370 if (unlikely(buf_len < sz)) {
1371 sz = buf_len;
1372 m->msg_flags |= MSG_TRUNC;
1373 }
David S. Miller51f3d022014-11-05 16:46:40 -05001374 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001375 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 res = sz;
1378 } else {
1379 if ((sock->state == SS_READY) ||
1380 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1381 res = 0;
1382 else
1383 res = -ECONNRESET;
1384 }
1385
1386 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens990098062008-04-15 00:06:12 -07001388 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001389 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001390 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001391 tsk->rcv_unacked = 0;
1392 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001393 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001394 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001396 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 return res;
1398}
1399
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001400/**
Ying Xue247f0f32014-02-18 16:06:46 +08001401 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 * @iocb: (unused)
1403 * @m: descriptor for message info
1404 * @buf_len: total size of user buffer area
1405 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001406 *
1407 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408 * will optionally wait for more; never truncates data.
1409 *
1410 * Returns size of returned message data, errno otherwise
1411 */
Ying Xue247f0f32014-02-18 16:06:46 +08001412static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1413 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001414{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001415 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001416 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 struct sk_buff *buf;
1418 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001419 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001421 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001424 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425
1426 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427 if (unlikely(!buf_len))
1428 return -EINVAL;
1429
Allan Stephens0c3141e2008-04-15 00:22:02 -07001430 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001432 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 res = -ENOTCONN;
1434 goto exit;
1435 }
1436
Florian Westphal3720d402010-08-17 11:00:04 +00001437 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001438 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001439
Allan Stephens0c3141e2008-04-15 00:22:02 -07001440restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001441 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001442 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001443 if (res)
1444 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001445
1446 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001447 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 msg = buf_msg(buf);
1449 sz = msg_data_sz(msg);
1450 err = msg_errcode(msg);
1451
1452 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001454 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 goto restart;
1456 }
1457
1458 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 if (sz_copied == 0) {
1460 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001461 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001462 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 goto exit;
1464 }
1465
1466 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001468 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469
Allan Stephens0232fd02011-02-21 09:45:40 -05001470 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 needed = (buf_len - sz_copied);
1472 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001473
David S. Miller51f3d022014-11-05 16:46:40 -05001474 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1475 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001476 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001478
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 sz_copied += sz_to_copy;
1480
1481 if (sz_to_copy < sz) {
1482 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001483 TIPC_SKB_CB(buf)->handle =
1484 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485 goto exit;
1486 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487 } else {
1488 if (sz_copied != 0)
1489 goto exit; /* can't add error msg to valid data */
1490
1491 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1492 res = 0;
1493 else
1494 res = -ECONNRESET;
1495 }
1496
1497 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001499 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001500 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001501 tsk->rcv_unacked = 0;
1502 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001503 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001504 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505
1506 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001507 if ((sz_copied < buf_len) && /* didn't get all requested data */
1508 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001509 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001510 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1511 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512 goto restart;
1513
1514exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001515 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001516 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517}
1518
1519/**
Ying Xuef288bef2012-08-21 11:16:57 +08001520 * tipc_write_space - wake up thread if port congestion is released
1521 * @sk: socket
1522 */
1523static void tipc_write_space(struct sock *sk)
1524{
1525 struct socket_wq *wq;
1526
1527 rcu_read_lock();
1528 wq = rcu_dereference(sk->sk_wq);
1529 if (wq_has_sleeper(wq))
1530 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1531 POLLWRNORM | POLLWRBAND);
1532 rcu_read_unlock();
1533}
1534
1535/**
1536 * tipc_data_ready - wake up threads to indicate messages have been received
1537 * @sk: socket
1538 * @len: the length of messages
1539 */
David S. Miller676d2362014-04-11 16:15:36 -04001540static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001541{
1542 struct socket_wq *wq;
1543
1544 rcu_read_lock();
1545 wq = rcu_dereference(sk->sk_wq);
1546 if (wq_has_sleeper(wq))
1547 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1548 POLLRDNORM | POLLRDBAND);
1549 rcu_read_unlock();
1550}
1551
1552/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001553 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001554 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001555 * @msg: message
1556 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001557 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001558 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001559static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001560{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001561 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001562 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001563 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001564 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001565 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001566
1567 if (msg_mcast(msg))
1568 return retval;
1569
1570 switch ((int)sock->state) {
1571 case SS_CONNECTED:
1572 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001573 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001574 if (unlikely(msg_errcode(msg))) {
1575 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001576 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001577 /* let timer expire on it's own */
Ying Xuef2f98002015-01-09 15:27:05 +08001578 tipc_node_remove_conn(net, tsk_peer_node(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08001579 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001580 }
1581 retval = TIPC_OK;
1582 }
1583 break;
1584 case SS_CONNECTING:
1585 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001586
1587 if (unlikely(!msg_connected(msg)))
1588 break;
1589
Ying Xue584d24b2012-11-29 18:51:19 -05001590 if (unlikely(msg_errcode(msg))) {
1591 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001592 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001593 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001594 break;
1595 }
1596
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001597 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001598 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001599 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001600 retval = TIPC_OK;
1601 break;
1602 }
1603
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001604 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1605 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001606 sock->state = SS_CONNECTED;
1607
Ying Xue584d24b2012-11-29 18:51:19 -05001608 /* If an incoming message is an 'ACK-', it should be
1609 * discarded here because it doesn't contain useful
1610 * data. In addition, we should try to wake up
1611 * connect() routine if sleeping.
1612 */
1613 if (msg_data_sz(msg) == 0) {
1614 kfree_skb(*buf);
1615 *buf = NULL;
1616 if (waitqueue_active(sk_sleep(sk)))
1617 wake_up_interruptible(sk_sleep(sk));
1618 }
1619 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001620 break;
1621 case SS_LISTENING:
1622 case SS_UNCONNECTED:
1623 /* Accept only SYN message */
1624 if (!msg_connected(msg) && !(msg_errcode(msg)))
1625 retval = TIPC_OK;
1626 break;
1627 case SS_DISCONNECTING:
1628 break;
1629 default:
1630 pr_err("Unknown socket state %u\n", sock->state);
1631 }
1632 return retval;
1633}
1634
1635/**
Ying Xueaba79f32013-01-20 23:30:09 +01001636 * rcvbuf_limit - get proper overload limit of socket receive queue
1637 * @sk: socket
1638 * @buf: message
1639 *
1640 * For all connection oriented messages, irrespective of importance,
1641 * the default overload value (i.e. 67MB) is set as limit.
1642 *
1643 * For all connectionless messages, by default new queue limits are
1644 * as belows:
1645 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001646 * TIPC_LOW_IMPORTANCE (4 MB)
1647 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1648 * TIPC_HIGH_IMPORTANCE (16 MB)
1649 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001650 *
1651 * Returns overload limit according to corresponding message importance
1652 */
1653static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1654{
1655 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001656
1657 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001658 return sysctl_tipc_rmem[2];
1659
1660 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1661 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001662}
1663
1664/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001665 * filter_rcv - validate incoming message
1666 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001668 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001669 * Enqueues message on receive queue if acceptable; optionally handles
1670 * disconnect indication for a connected socket.
1671 *
1672 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001673 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001674 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001675 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001677static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001679 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001680 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001682 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001683 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001684 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001686 if (unlikely(msg_user(msg) == CONN_MANAGER))
1687 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001688
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001689 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1690 kfree_skb(buf);
1691 tsk->link_cong = 0;
1692 sk->sk_write_space(sk);
1693 return TIPC_OK;
1694 }
1695
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001697 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001698 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001699
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001701 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001702 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001704 rc = filter_connect(tsk, &buf);
1705 if (rc != TIPC_OK || buf == NULL)
1706 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 }
1708
1709 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001710 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001711 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712
Ying Xueaba79f32013-01-20 23:30:09 +01001713 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001714 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001715 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001716 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001717
David S. Miller676d2362014-04-11 16:15:36 -04001718 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 return TIPC_OK;
1720}
1721
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001722/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001723 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001724 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001725 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001726 *
1727 * Caller must hold socket lock, but not port lock.
1728 *
1729 * Returns 0
1730 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001731static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001732{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001733 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001734 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001735 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +08001736 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001737 uint truesize = skb->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001738
Ying Xuea6ca1092014-11-26 11:41:55 +08001739 rc = filter_rcv(sk, skb);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001740
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001741 if (likely(!rc)) {
1742 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1743 atomic_add(truesize, &tsk->dupl_rcvcnt);
1744 return 0;
1745 }
1746
Ying Xue34747532015-01-09 15:27:10 +08001747 if ((rc < 0) && !tipc_msg_reverse(net, skb, &onode, -rc))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001748 return 0;
1749
Ying Xue34747532015-01-09 15:27:10 +08001750 tipc_link_xmit_skb(net, skb, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001751
Allan Stephens0c3141e2008-04-15 00:22:02 -07001752 return 0;
1753}
1754
1755/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001756 * tipc_sk_rcv - handle incoming message
Ying Xuea6ca1092014-11-26 11:41:55 +08001757 * @skb: buffer containing arriving message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001758 * Consumes buffer
1759 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001760 */
Ying Xuef2f98002015-01-09 15:27:05 +08001761int tipc_sk_rcv(struct net *net, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001762{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001763 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001764 struct sock *sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001765 u32 dport = msg_destport(buf_msg(skb));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001766 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001767 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001768 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001769
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001770 /* Validate destination and message */
Ying Xuee05b31f2015-01-09 15:27:08 +08001771 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001772 if (unlikely(!tsk)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001773 rc = tipc_msg_eval(net, skb, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001774 goto exit;
1775 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001776 sk = &tsk->sk;
1777
1778 /* Queue message */
Ying Xue1a194c22014-10-20 14:46:35 +08001779 spin_lock_bh(&sk->sk_lock.slock);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001780
Allan Stephens0c3141e2008-04-15 00:22:02 -07001781 if (!sock_owned_by_user(sk)) {
Ying Xuea6ca1092014-11-26 11:41:55 +08001782 rc = filter_rcv(sk, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001783 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001784 if (sk->sk_backlog.len == 0)
1785 atomic_set(&tsk->dupl_rcvcnt, 0);
Ying Xuea6ca1092014-11-26 11:41:55 +08001786 limit = rcvbuf_limit(sk, skb) + atomic_read(&tsk->dupl_rcvcnt);
1787 if (sk_add_backlog(sk, skb, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001788 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001789 }
Ying Xue1a194c22014-10-20 14:46:35 +08001790 spin_unlock_bh(&sk->sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001791 sock_put(sk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001792 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001793 return 0;
1794exit:
Ying Xue34747532015-01-09 15:27:10 +08001795 if ((rc < 0) && !tipc_msg_reverse(net, skb, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001796 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001797
Ying Xuef2f98002015-01-09 15:27:05 +08001798 tipc_link_xmit_skb(net, skb, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001799 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001800}
1801
Ying Xue78eb3a52014-01-17 09:50:03 +08001802static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1803{
1804 struct sock *sk = sock->sk;
1805 DEFINE_WAIT(wait);
1806 int done;
1807
1808 do {
1809 int err = sock_error(sk);
1810 if (err)
1811 return err;
1812 if (!*timeo_p)
1813 return -ETIMEDOUT;
1814 if (signal_pending(current))
1815 return sock_intr_errno(*timeo_p);
1816
1817 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1818 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1819 finish_wait(sk_sleep(sk), &wait);
1820 } while (!done);
1821 return 0;
1822}
1823
Per Lidenb97bf3f2006-01-02 19:04:38 +01001824/**
Ying Xue247f0f32014-02-18 16:06:46 +08001825 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 * @sock: socket structure
1827 * @dest: socket address for destination port
1828 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001829 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 *
1831 * Returns 0 on success, errno otherwise
1832 */
Ying Xue247f0f32014-02-18 16:06:46 +08001833static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1834 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001836 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001837 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1838 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001839 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1840 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001841 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842
Allan Stephens0c3141e2008-04-15 00:22:02 -07001843 lock_sock(sk);
1844
Allan Stephensb89741a2008-04-15 00:20:37 -07001845 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001846 if (sock->state == SS_READY) {
1847 res = -EOPNOTSUPP;
1848 goto exit;
1849 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850
Allan Stephensb89741a2008-04-15 00:20:37 -07001851 /*
1852 * Reject connection attempt using multicast address
1853 *
1854 * Note: send_msg() validates the rest of the address fields,
1855 * so there's no need to do it here
1856 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001857 if (dst->addrtype == TIPC_ADDR_MCAST) {
1858 res = -EINVAL;
1859 goto exit;
1860 }
1861
Ying Xue78eb3a52014-01-17 09:50:03 +08001862 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001863 switch (sock->state) {
1864 case SS_UNCONNECTED:
1865 /* Send a 'SYN-' to destination */
1866 m.msg_name = dest;
1867 m.msg_namelen = destlen;
1868
1869 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1870 * indicate send_msg() is never blocked.
1871 */
1872 if (!timeout)
1873 m.msg_flags = MSG_DONTWAIT;
1874
Ying Xue247f0f32014-02-18 16:06:46 +08001875 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001876 if ((res < 0) && (res != -EWOULDBLOCK))
1877 goto exit;
1878
1879 /* Just entered SS_CONNECTING state; the only
1880 * difference is that return value in non-blocking
1881 * case is EINPROGRESS, rather than EALREADY.
1882 */
1883 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001884 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001885 if (previous == SS_CONNECTING)
1886 res = -EALREADY;
1887 if (!timeout)
1888 goto exit;
1889 timeout = msecs_to_jiffies(timeout);
1890 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1891 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001892 break;
1893 case SS_CONNECTED:
1894 res = -EISCONN;
1895 break;
1896 default:
1897 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001898 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001899 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001900exit:
1901 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001902 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903}
1904
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001905/**
Ying Xue247f0f32014-02-18 16:06:46 +08001906 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 * @sock: socket structure
1908 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001909 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 * Returns 0 on success, errno otherwise
1911 */
Ying Xue247f0f32014-02-18 16:06:46 +08001912static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001914 struct sock *sk = sock->sk;
1915 int res;
1916
1917 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918
Ying Xue245f3d32011-07-06 06:01:13 -04001919 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001920 res = -EINVAL;
1921 else {
1922 sock->state = SS_LISTENING;
1923 res = 0;
1924 }
1925
1926 release_sock(sk);
1927 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928}
1929
Ying Xue6398e232014-01-17 09:50:04 +08001930static int tipc_wait_for_accept(struct socket *sock, long timeo)
1931{
1932 struct sock *sk = sock->sk;
1933 DEFINE_WAIT(wait);
1934 int err;
1935
1936 /* True wake-one mechanism for incoming connections: only
1937 * one process gets woken up, not the 'whole herd'.
1938 * Since we do not 'race & poll' for established sockets
1939 * anymore, the common case will execute the loop only once.
1940 */
1941 for (;;) {
1942 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1943 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001944 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001945 release_sock(sk);
1946 timeo = schedule_timeout(timeo);
1947 lock_sock(sk);
1948 }
1949 err = 0;
1950 if (!skb_queue_empty(&sk->sk_receive_queue))
1951 break;
1952 err = -EINVAL;
1953 if (sock->state != SS_LISTENING)
1954 break;
1955 err = sock_intr_errno(timeo);
1956 if (signal_pending(current))
1957 break;
1958 err = -EAGAIN;
1959 if (!timeo)
1960 break;
1961 }
1962 finish_wait(sk_sleep(sk), &wait);
1963 return err;
1964}
1965
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001966/**
Ying Xue247f0f32014-02-18 16:06:46 +08001967 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968 * @sock: listening socket
1969 * @newsock: new socket that is to be connected
1970 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001971 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001972 * Returns 0 on success, errno otherwise
1973 */
Ying Xue247f0f32014-02-18 16:06:46 +08001974static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001976 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001978 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001979 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001980 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001981 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001982
Allan Stephens0c3141e2008-04-15 00:22:02 -07001983 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001984
Allan Stephens0c3141e2008-04-15 00:22:02 -07001985 if (sock->state != SS_LISTENING) {
1986 res = -EINVAL;
1987 goto exit;
1988 }
Ying Xue6398e232014-01-17 09:50:04 +08001989 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1990 res = tipc_wait_for_accept(sock, timeo);
1991 if (res)
1992 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001993
1994 buf = skb_peek(&sk->sk_receive_queue);
1995
Ying Xuec5fa7b32013-06-17 10:54:39 -04001996 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001997 if (res)
1998 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001999
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002000 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002001 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002002 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002003
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002004 /* we lock on new_sk; but lockdep sees the lock on sk */
2005 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002006
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002007 /*
2008 * Reject any stray messages received by new socket
2009 * before the socket lock was taken (very, very unlikely)
2010 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002011 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002012
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002013 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002014 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002015 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002016
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002017 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002018 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002019 new_tsock->conn_type = msg_nametype(msg);
2020 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002022
2023 /*
2024 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2025 * Respond to 'SYN+' by queuing it on new socket.
2026 */
2027 if (!msg_data_sz(msg)) {
2028 struct msghdr m = {NULL,};
2029
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002030 tsk_advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08002031 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002032 } else {
2033 __skb_dequeue(&sk->sk_receive_queue);
2034 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002035 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002036 }
2037 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002038exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002039 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002040 return res;
2041}
2042
2043/**
Ying Xue247f0f32014-02-18 16:06:46 +08002044 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002045 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002046 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002047 *
2048 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002049 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002050 * Returns 0 on success, errno otherwise
2051 */
Ying Xue247f0f32014-02-18 16:06:46 +08002052static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002054 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002055 struct net *net = sock_net(sk);
Ying Xue34747532015-01-09 15:27:10 +08002056 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002057 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002058 struct sk_buff *skb;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002059 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002060 int res;
2061
Allan Stephense247a8f2008-03-06 15:05:38 -08002062 if (how != SHUT_RDWR)
2063 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002064
Allan Stephens0c3141e2008-04-15 00:22:02 -07002065 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002066
2067 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002068 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069 case SS_CONNECTED:
2070
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002072 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002073 skb = __skb_dequeue(&sk->sk_receive_queue);
2074 if (skb) {
2075 if (TIPC_SKB_CB(skb)->handle != NULL) {
2076 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077 goto restart;
2078 }
Ying Xue34747532015-01-09 15:27:10 +08002079 if (tipc_msg_reverse(net, skb, &dnode,
2080 TIPC_CONN_SHUTDOWN))
Ying Xuef2f98002015-01-09 15:27:05 +08002081 tipc_link_xmit_skb(net, skb, dnode,
2082 tsk->portid);
2083 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002084 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002085 dnode = tsk_peer_node(tsk);
Ying Xue34747532015-01-09 15:27:10 +08002086 skb = tipc_msg_create(net, TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002087 TIPC_CONN_MSG, SHORT_H_SIZE,
Ying Xue34747532015-01-09 15:27:10 +08002088 0, dnode, tn->own_addr,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002089 tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002090 tsk->portid, TIPC_CONN_SHUTDOWN);
Ying Xuef2f98002015-01-09 15:27:05 +08002091 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002093 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002094 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002095 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096 /* fall through */
2097
2098 case SS_DISCONNECTING:
2099
Ying Xue75031152012-10-29 09:38:15 -04002100 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002101 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002102
2103 /* Wake up anyone sleeping in poll */
2104 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002105 res = 0;
2106 break;
2107
2108 default:
2109 res = -ENOTCONN;
2110 }
2111
Allan Stephens0c3141e2008-04-15 00:22:02 -07002112 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113 return res;
2114}
2115
Ying Xuef2f2a962015-01-09 15:27:02 +08002116static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002117{
Ying Xuef2f2a962015-01-09 15:27:02 +08002118 struct tipc_sock *tsk = (struct tipc_sock *)data;
2119 struct sock *sk = &tsk->sk;
Ying Xue34747532015-01-09 15:27:10 +08002120 struct net *net = sock_net(sk);
2121 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuea6ca1092014-11-26 11:41:55 +08002122 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002123 u32 peer_port, peer_node;
2124
Jon Paul Maloy57289012014-08-22 18:09:09 -04002125 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002126 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002127 bh_unlock_sock(sk);
2128 goto exit;
2129 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002130 peer_port = tsk_peer_port(tsk);
2131 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002132
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002133 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002134 /* Previous probe not answered -> self abort */
Ying Xue34747532015-01-09 15:27:10 +08002135 skb = tipc_msg_create(net, TIPC_CRITICAL_IMPORTANCE,
2136 TIPC_CONN_MSG, SHORT_H_SIZE, 0,
2137 tn->own_addr, peer_node, tsk->portid,
2138 peer_port, TIPC_ERR_NO_PORT);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002139 } else {
Ying Xue34747532015-01-09 15:27:10 +08002140 skb = tipc_msg_create(net, CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2141 0, peer_node, tn->own_addr,
Ying Xuef2f2a962015-01-09 15:27:02 +08002142 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002143 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xuef2f2a962015-01-09 15:27:02 +08002144 if (!mod_timer(&tsk->timer, jiffies + tsk->probing_intv))
2145 sock_hold(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002146 }
2147 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002148 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +08002149 tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002150exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002151 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002152}
2153
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002154static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002155 struct tipc_name_seq const *seq)
2156{
Ying Xuef2f98002015-01-09 15:27:05 +08002157 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002158 struct publication *publ;
2159 u32 key;
2160
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002161 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002162 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002163 key = tsk->portid + tsk->pub_count + 1;
2164 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002165 return -EADDRINUSE;
2166
Ying Xuef2f98002015-01-09 15:27:05 +08002167 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002168 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002169 if (unlikely(!publ))
2170 return -EINVAL;
2171
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002172 list_add(&publ->pport_list, &tsk->publications);
2173 tsk->pub_count++;
2174 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002175 return 0;
2176}
2177
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002178static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002179 struct tipc_name_seq const *seq)
2180{
Ying Xuef2f98002015-01-09 15:27:05 +08002181 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002182 struct publication *publ;
2183 struct publication *safe;
2184 int rc = -EINVAL;
2185
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002186 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002187 if (seq) {
2188 if (publ->scope != scope)
2189 continue;
2190 if (publ->type != seq->type)
2191 continue;
2192 if (publ->lower != seq->lower)
2193 continue;
2194 if (publ->upper != seq->upper)
2195 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002196 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002197 publ->ref, publ->key);
2198 rc = 0;
2199 break;
2200 }
Ying Xuef2f98002015-01-09 15:27:05 +08002201 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002202 publ->ref, publ->key);
2203 rc = 0;
2204 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002205 if (list_empty(&tsk->publications))
2206 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002207 return rc;
2208}
2209
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002210static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002211 int len, int full_id)
2212{
Ying Xue34747532015-01-09 15:27:10 +08002213 struct net *net = sock_net(&tsk->sk);
2214 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002215 struct publication *publ;
2216 int ret;
2217
2218 if (full_id)
2219 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
Ying Xue34747532015-01-09 15:27:10 +08002220 tipc_zone(tn->own_addr),
2221 tipc_cluster(tn->own_addr),
2222 tipc_node(tn->own_addr), tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002223 else
Ying Xue07f6c4b2015-01-07 13:41:58 +08002224 ret = tipc_snprintf(buf, len, "%-10u:", tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002225
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002226 if (tsk->connected) {
2227 u32 dport = tsk_peer_port(tsk);
2228 u32 destnode = tsk_peer_node(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002229
2230 ret += tipc_snprintf(buf + ret, len - ret,
2231 " connected to <%u.%u.%u:%u>",
2232 tipc_zone(destnode),
2233 tipc_cluster(destnode),
2234 tipc_node(destnode), dport);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002235 if (tsk->conn_type != 0)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002236 ret += tipc_snprintf(buf + ret, len - ret,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002237 " via {%u,%u}", tsk->conn_type,
2238 tsk->conn_instance);
2239 } else if (tsk->published) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002240 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002241 list_for_each_entry(publ, &tsk->publications, pport_list) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002242 if (publ->lower == publ->upper)
2243 ret += tipc_snprintf(buf + ret, len - ret,
2244 " {%u,%u}", publ->type,
2245 publ->lower);
2246 else
2247 ret += tipc_snprintf(buf + ret, len - ret,
2248 " {%u,%u,%u}", publ->type,
2249 publ->lower, publ->upper);
2250 }
2251 }
2252 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2253 return ret;
2254}
2255
Ying Xuee05b31f2015-01-09 15:27:08 +08002256struct sk_buff *tipc_sk_socks_show(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002257{
Ying Xuee05b31f2015-01-09 15:27:08 +08002258 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002259 const struct bucket_table *tbl;
2260 struct rhash_head *pos;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002261 struct sk_buff *buf;
2262 struct tlv_desc *rep_tlv;
2263 char *pb;
2264 int pb_len;
2265 struct tipc_sock *tsk;
2266 int str_len = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002267 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002268
2269 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2270 if (!buf)
2271 return NULL;
2272 rep_tlv = (struct tlv_desc *)buf->data;
2273 pb = TLV_DATA(rep_tlv);
2274 pb_len = ULTRA_STRING_MAX_LEN;
2275
Ying Xue07f6c4b2015-01-07 13:41:58 +08002276 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002277 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002278 for (i = 0; i < tbl->size; i++) {
2279 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2280 spin_lock_bh(&tsk->sk.sk_lock.slock);
2281 str_len += tipc_sk_show(tsk, pb + str_len,
2282 pb_len - str_len, 0);
2283 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2284 }
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002285 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002286 rcu_read_unlock();
2287
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002288 str_len += 1; /* for "\0" */
2289 skb_put(buf, TLV_SPACE(str_len));
2290 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2291
2292 return buf;
2293}
2294
2295/* tipc_sk_reinit: set non-zero address in all existing sockets
2296 * when we go from standalone to network mode.
2297 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002298void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002299{
Ying Xuee05b31f2015-01-09 15:27:08 +08002300 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002301 const struct bucket_table *tbl;
2302 struct rhash_head *pos;
2303 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002304 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002305 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002306
Ying Xue07f6c4b2015-01-07 13:41:58 +08002307 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002308 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002309 for (i = 0; i < tbl->size; i++) {
2310 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2311 spin_lock_bh(&tsk->sk.sk_lock.slock);
2312 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002313 msg_set_prevnode(msg, tn->own_addr);
2314 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002315 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2316 }
2317 }
2318 rcu_read_unlock();
2319}
2320
Ying Xuee05b31f2015-01-09 15:27:08 +08002321static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002322{
Ying Xuee05b31f2015-01-09 15:27:08 +08002323 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002324 struct tipc_sock *tsk;
2325
2326 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002327 tsk = rhashtable_lookup(&tn->sk_rht, &portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002328 if (tsk)
2329 sock_hold(&tsk->sk);
2330 rcu_read_unlock();
2331
2332 return tsk;
2333}
2334
2335static int tipc_sk_insert(struct tipc_sock *tsk)
2336{
Ying Xuee05b31f2015-01-09 15:27:08 +08002337 struct sock *sk = &tsk->sk;
2338 struct net *net = sock_net(sk);
2339 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002340 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2341 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2342
2343 while (remaining--) {
2344 portid++;
2345 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2346 portid = TIPC_MIN_PORT;
2347 tsk->portid = portid;
2348 sock_hold(&tsk->sk);
Ying Xuee05b31f2015-01-09 15:27:08 +08002349 if (rhashtable_lookup_insert(&tn->sk_rht, &tsk->node))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002350 return 0;
2351 sock_put(&tsk->sk);
2352 }
2353
2354 return -1;
2355}
2356
2357static void tipc_sk_remove(struct tipc_sock *tsk)
2358{
2359 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002360 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002361
Ying Xuee05b31f2015-01-09 15:27:08 +08002362 if (rhashtable_remove(&tn->sk_rht, &tsk->node)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002363 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2364 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002365 }
2366}
2367
Ying Xuee05b31f2015-01-09 15:27:08 +08002368int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002369{
Ying Xuee05b31f2015-01-09 15:27:08 +08002370 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002371 struct rhashtable_params rht_params = {
2372 .nelem_hint = 192,
2373 .head_offset = offsetof(struct tipc_sock, node),
2374 .key_offset = offsetof(struct tipc_sock, portid),
2375 .key_len = sizeof(u32), /* portid */
2376 .hashfn = jhash,
2377 .max_shift = 20, /* 1M */
2378 .min_shift = 8, /* 256 */
2379 .grow_decision = rht_grow_above_75,
2380 .shrink_decision = rht_shrink_below_30,
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002381 };
2382
Ying Xuee05b31f2015-01-09 15:27:08 +08002383 return rhashtable_init(&tn->sk_rht, &rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002384}
2385
Ying Xuee05b31f2015-01-09 15:27:08 +08002386void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002387{
Ying Xuee05b31f2015-01-09 15:27:08 +08002388 struct tipc_net *tn = net_generic(net, tipc_net_id);
2389
Ying Xue07f6c4b2015-01-07 13:41:58 +08002390 /* Wait for socket readers to complete */
2391 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002392
Ying Xuee05b31f2015-01-09 15:27:08 +08002393 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002394}
2395
2396/**
Ying Xue247f0f32014-02-18 16:06:46 +08002397 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002398 * @sock: socket structure
2399 * @lvl: option level
2400 * @opt: option identifier
2401 * @ov: pointer to new option value
2402 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002403 *
2404 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002405 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002406 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002407 * Returns 0 on success, errno otherwise
2408 */
Ying Xue247f0f32014-02-18 16:06:46 +08002409static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2410 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002411{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002412 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002413 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002414 u32 value;
2415 int res;
2416
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002417 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2418 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002419 if (lvl != SOL_TIPC)
2420 return -ENOPROTOOPT;
2421 if (ol < sizeof(value))
2422 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002423 res = get_user(value, (u32 __user *)ov);
2424 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002425 return res;
2426
Allan Stephens0c3141e2008-04-15 00:22:02 -07002427 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002428
Per Lidenb97bf3f2006-01-02 19:04:38 +01002429 switch (opt) {
2430 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002431 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002432 break;
2433 case TIPC_SRC_DROPPABLE:
2434 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002435 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002436 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437 res = -ENOPROTOOPT;
2438 break;
2439 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002440 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002441 break;
2442 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002443 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002444 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002445 break;
2446 default:
2447 res = -EINVAL;
2448 }
2449
Allan Stephens0c3141e2008-04-15 00:22:02 -07002450 release_sock(sk);
2451
Per Lidenb97bf3f2006-01-02 19:04:38 +01002452 return res;
2453}
2454
2455/**
Ying Xue247f0f32014-02-18 16:06:46 +08002456 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457 * @sock: socket structure
2458 * @lvl: option level
2459 * @opt: option identifier
2460 * @ov: receptacle for option value
2461 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002462 *
2463 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002465 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002466 * Returns 0 on success, errno otherwise
2467 */
Ying Xue247f0f32014-02-18 16:06:46 +08002468static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2469 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002470{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002471 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002472 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002473 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002474 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002475 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002476
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002477 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2478 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002479 if (lvl != SOL_TIPC)
2480 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002481 res = get_user(len, ol);
2482 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002483 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002484
Allan Stephens0c3141e2008-04-15 00:22:02 -07002485 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002486
2487 switch (opt) {
2488 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002489 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002490 break;
2491 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002492 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002493 break;
2494 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002495 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002496 break;
2497 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002498 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002499 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002500 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002501 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002502 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002503 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002504 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002505 value = skb_queue_len(&sk->sk_receive_queue);
2506 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002507 default:
2508 res = -EINVAL;
2509 }
2510
Allan Stephens0c3141e2008-04-15 00:22:02 -07002511 release_sock(sk);
2512
Paul Gortmaker25860c32010-12-31 18:59:31 +00002513 if (res)
2514 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002515
Paul Gortmaker25860c32010-12-31 18:59:31 +00002516 if (len < sizeof(value))
2517 return -EINVAL;
2518
2519 if (copy_to_user(ov, &value, sizeof(value)))
2520 return -EFAULT;
2521
2522 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002523}
2524
Ying Xuef2f98002015-01-09 15:27:05 +08002525static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002526{
Ying Xuef2f98002015-01-09 15:27:05 +08002527 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002528 struct tipc_sioc_ln_req lnr;
2529 void __user *argp = (void __user *)arg;
2530
2531 switch (cmd) {
2532 case SIOCGETLINKNAME:
2533 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2534 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002535 if (!tipc_node_get_linkname(sock_net(sk),
2536 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002537 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2538 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2539 return -EFAULT;
2540 return 0;
2541 }
2542 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002543 default:
2544 return -ENOIOCTLCMD;
2545 }
2546}
2547
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002548/* Protocol switches for the various types of TIPC sockets */
2549
Florian Westphalbca65ea2008-02-07 18:18:01 -08002550static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002551 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002552 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002553 .release = tipc_release,
2554 .bind = tipc_bind,
2555 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002556 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002557 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002558 .getname = tipc_getname,
2559 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002560 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002561 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002562 .shutdown = tipc_shutdown,
2563 .setsockopt = tipc_setsockopt,
2564 .getsockopt = tipc_getsockopt,
2565 .sendmsg = tipc_sendmsg,
2566 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002567 .mmap = sock_no_mmap,
2568 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002569};
2570
Florian Westphalbca65ea2008-02-07 18:18:01 -08002571static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002572 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002573 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002574 .release = tipc_release,
2575 .bind = tipc_bind,
2576 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002577 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002578 .accept = tipc_accept,
2579 .getname = tipc_getname,
2580 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002581 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002582 .listen = tipc_listen,
2583 .shutdown = tipc_shutdown,
2584 .setsockopt = tipc_setsockopt,
2585 .getsockopt = tipc_getsockopt,
2586 .sendmsg = tipc_send_packet,
2587 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002588 .mmap = sock_no_mmap,
2589 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002590};
2591
Florian Westphalbca65ea2008-02-07 18:18:01 -08002592static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002593 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002594 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002595 .release = tipc_release,
2596 .bind = tipc_bind,
2597 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002598 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002599 .accept = tipc_accept,
2600 .getname = tipc_getname,
2601 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002602 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002603 .listen = tipc_listen,
2604 .shutdown = tipc_shutdown,
2605 .setsockopt = tipc_setsockopt,
2606 .getsockopt = tipc_getsockopt,
2607 .sendmsg = tipc_send_stream,
2608 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002609 .mmap = sock_no_mmap,
2610 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002611};
2612
Florian Westphalbca65ea2008-02-07 18:18:01 -08002613static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002614 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002615 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002616 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002617};
2618
2619static struct proto tipc_proto = {
2620 .name = "TIPC",
2621 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002622 .obj_size = sizeof(struct tipc_sock),
2623 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002624};
2625
Ying Xuec5fa7b32013-06-17 10:54:39 -04002626static struct proto tipc_proto_kern = {
2627 .name = "TIPC",
2628 .obj_size = sizeof(struct tipc_sock),
2629 .sysctl_rmem = sysctl_tipc_rmem
2630};
2631
Per Lidenb97bf3f2006-01-02 19:04:38 +01002632/**
Per Liden4323add2006-01-18 00:38:21 +01002633 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002634 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002635 * Returns 0 on success, errno otherwise
2636 */
Per Liden4323add2006-01-18 00:38:21 +01002637int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002638{
2639 int res;
2640
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002641 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002642 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002643 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002644 goto out;
2645 }
2646
2647 res = sock_register(&tipc_family_ops);
2648 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002649 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002650 proto_unregister(&tipc_proto);
2651 goto out;
2652 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002653 out:
2654 return res;
2655}
2656
2657/**
Per Liden4323add2006-01-18 00:38:21 +01002658 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002659 */
Per Liden4323add2006-01-18 00:38:21 +01002660void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002661{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002662 sock_unregister(tipc_family_ops.family);
2663 proto_unregister(&tipc_proto);
2664}
Richard Alpe34b78a122014-11-20 10:29:10 +01002665
2666/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002667static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002668{
2669 u32 peer_node;
2670 u32 peer_port;
2671 struct nlattr *nest;
2672
2673 peer_node = tsk_peer_node(tsk);
2674 peer_port = tsk_peer_port(tsk);
2675
2676 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2677
2678 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2679 goto msg_full;
2680 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2681 goto msg_full;
2682
2683 if (tsk->conn_type != 0) {
2684 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2685 goto msg_full;
2686 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2687 goto msg_full;
2688 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2689 goto msg_full;
2690 }
2691 nla_nest_end(skb, nest);
2692
2693 return 0;
2694
2695msg_full:
2696 nla_nest_cancel(skb, nest);
2697
2698 return -EMSGSIZE;
2699}
2700
2701/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002702static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2703 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002704{
2705 int err;
2706 void *hdr;
2707 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002708 struct net *net = sock_net(skb->sk);
2709 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002710
2711 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2712 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2713 if (!hdr)
2714 goto msg_cancel;
2715
2716 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2717 if (!attrs)
2718 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002719 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002720 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002721 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002722 goto attr_msg_cancel;
2723
2724 if (tsk->connected) {
2725 err = __tipc_nl_add_sk_con(skb, tsk);
2726 if (err)
2727 goto attr_msg_cancel;
2728 } else if (!list_empty(&tsk->publications)) {
2729 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2730 goto attr_msg_cancel;
2731 }
2732 nla_nest_end(skb, attrs);
2733 genlmsg_end(skb, hdr);
2734
2735 return 0;
2736
2737attr_msg_cancel:
2738 nla_nest_cancel(skb, attrs);
2739genlmsg_cancel:
2740 genlmsg_cancel(skb, hdr);
2741msg_cancel:
2742 return -EMSGSIZE;
2743}
2744
2745int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2746{
2747 int err;
2748 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002749 const struct bucket_table *tbl;
2750 struct rhash_head *pos;
2751 u32 prev_portid = cb->args[0];
2752 u32 portid = prev_portid;
Ying Xuee05b31f2015-01-09 15:27:08 +08002753 struct net *net = sock_net(skb->sk);
2754 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002755 int i;
Richard Alpe34b78a122014-11-20 10:29:10 +01002756
Ying Xue07f6c4b2015-01-07 13:41:58 +08002757 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002758 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002759 for (i = 0; i < tbl->size; i++) {
2760 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2761 spin_lock_bh(&tsk->sk.sk_lock.slock);
2762 portid = tsk->portid;
2763 err = __tipc_nl_add_sk(skb, cb, tsk);
2764 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2765 if (err)
2766 break;
Richard Alpe34b78a122014-11-20 10:29:10 +01002767
Ying Xue07f6c4b2015-01-07 13:41:58 +08002768 prev_portid = portid;
2769 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002770 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002771 rcu_read_unlock();
Richard Alpe34b78a122014-11-20 10:29:10 +01002772
Ying Xue07f6c4b2015-01-07 13:41:58 +08002773 cb->args[0] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002774
2775 return skb->len;
2776}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002777
2778/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002779static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2780 struct netlink_callback *cb,
2781 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002782{
2783 void *hdr;
2784 struct nlattr *attrs;
2785
2786 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2787 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2788 if (!hdr)
2789 goto msg_cancel;
2790
2791 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2792 if (!attrs)
2793 goto genlmsg_cancel;
2794
2795 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2796 goto attr_msg_cancel;
2797 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2798 goto attr_msg_cancel;
2799 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2800 goto attr_msg_cancel;
2801 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2802 goto attr_msg_cancel;
2803
2804 nla_nest_end(skb, attrs);
2805 genlmsg_end(skb, hdr);
2806
2807 return 0;
2808
2809attr_msg_cancel:
2810 nla_nest_cancel(skb, attrs);
2811genlmsg_cancel:
2812 genlmsg_cancel(skb, hdr);
2813msg_cancel:
2814 return -EMSGSIZE;
2815}
2816
2817/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002818static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2819 struct netlink_callback *cb,
2820 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002821{
2822 int err;
2823 struct publication *p;
2824
2825 if (*last_publ) {
2826 list_for_each_entry(p, &tsk->publications, pport_list) {
2827 if (p->key == *last_publ)
2828 break;
2829 }
2830 if (p->key != *last_publ) {
2831 /* We never set seq or call nl_dump_check_consistent()
2832 * this means that setting prev_seq here will cause the
2833 * consistence check to fail in the netlink callback
2834 * handler. Resulting in the last NLMSG_DONE message
2835 * having the NLM_F_DUMP_INTR flag set.
2836 */
2837 cb->prev_seq = 1;
2838 *last_publ = 0;
2839 return -EPIPE;
2840 }
2841 } else {
2842 p = list_first_entry(&tsk->publications, struct publication,
2843 pport_list);
2844 }
2845
2846 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2847 err = __tipc_nl_add_sk_publ(skb, cb, p);
2848 if (err) {
2849 *last_publ = p->key;
2850 return err;
2851 }
2852 }
2853 *last_publ = 0;
2854
2855 return 0;
2856}
2857
2858int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2859{
2860 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002861 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002862 u32 last_publ = cb->args[1];
2863 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002864 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002865 struct tipc_sock *tsk;
2866
Ying Xue07f6c4b2015-01-07 13:41:58 +08002867 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002868 struct nlattr **attrs;
2869 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2870
2871 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2872 if (err)
2873 return err;
2874
2875 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2876 attrs[TIPC_NLA_SOCK],
2877 tipc_nl_sock_policy);
2878 if (err)
2879 return err;
2880
2881 if (!sock[TIPC_NLA_SOCK_REF])
2882 return -EINVAL;
2883
Ying Xue07f6c4b2015-01-07 13:41:58 +08002884 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002885 }
2886
2887 if (done)
2888 return 0;
2889
Ying Xuee05b31f2015-01-09 15:27:08 +08002890 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002891 if (!tsk)
2892 return -EINVAL;
2893
2894 lock_sock(&tsk->sk);
2895 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2896 if (!err)
2897 done = 1;
2898 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002899 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002900
Ying Xue07f6c4b2015-01-07 13:41:58 +08002901 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002902 cb->args[1] = last_publ;
2903 cb->args[2] = done;
2904
2905 return skb->len;
2906}