blob: c9c34a6679213e61c19cb6f691d7173776315369 [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 * @port: port - interacts with 'sk' and with the rest of the TIPC stack
73 * @peer_name: the peer of the connection, if any
74 * @conn_timeout: the time we can wait for an unresponded setup request
75 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
76 * @link_cong: non-zero if owner must sleep because of link congestion
77 * @sent_unacked: # messages sent by socket, and not yet acked by peer
78 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Ying Xue07f6c4b2015-01-07 13:41:58 +080079 * @node: hash table node
80 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040081 */
82struct tipc_sock {
83 struct sock sk;
84 int connected;
85 u32 conn_type;
86 u32 conn_instance;
87 int published;
88 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080089 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040090 struct tipc_msg phdr;
91 struct list_head sock_list;
92 struct list_head publications;
93 u32 pub_count;
94 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080095 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 uint conn_timeout;
97 atomic_t dupl_rcvcnt;
98 bool link_cong;
99 uint sent_unacked;
100 uint rcv_unacked;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800101 struct rhash_head node;
102 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400103};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400105static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400106static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800107static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800108static int tipc_release(struct socket *sock);
109static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400110static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Ying Xuef2f2a962015-01-09 15:27:02 +0800111static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400112static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400113 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400115 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800116static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800117static int tipc_sk_insert(struct tipc_sock *tsk);
118static void tipc_sk_remove(struct tipc_sock *tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119
Florian Westphalbca65ea2008-02-07 18:18:01 -0800120static const struct proto_ops packet_ops;
121static const struct proto_ops stream_ops;
122static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123
124static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400125static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126
Richard Alpe1a1a1432014-11-20 10:29:11 +0100127static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
128 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
129 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
130 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
131 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
132 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
133};
134
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900135/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700136 * Revised TIPC socket locking policy:
137 *
138 * Most socket operations take the standard socket lock when they start
139 * and hold it until they finish (or until they need to sleep). Acquiring
140 * this lock grants the owner exclusive access to the fields of the socket
141 * data structures, with the exception of the backlog queue. A few socket
142 * operations can be done without taking the socket lock because they only
143 * read socket information that never changes during the life of the socket.
144 *
145 * Socket operations may acquire the lock for the associated TIPC port if they
146 * need to perform an operation on the port. If any routine needs to acquire
147 * both the socket lock and the port lock it must take the socket lock first
148 * to avoid the risk of deadlock.
149 *
150 * The dispatcher handling incoming messages cannot grab the socket lock in
151 * the standard fashion, since invoked it runs at the BH level and cannot block.
152 * Instead, it checks to see if the socket lock is currently owned by someone,
153 * and either handles the message itself or adds it to the socket's backlog
154 * queue; in the latter case the queued message is processed once the process
155 * owning the socket lock releases it.
156 *
157 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
158 * the problem of a blocked socket operation preventing any other operations
159 * from occurring. However, applications must be careful if they have
160 * multiple threads trying to send (or receive) on the same socket, as these
161 * operations might interfere with each other. For example, doing a connect
162 * and a receive at the same time might allow the receive to consume the
163 * ACK message meant for the connect. While additional work could be done
164 * to try and overcome this, it doesn't seem to be worthwhile at the present.
165 *
166 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
167 * that another operation that must be performed in a non-blocking manner is
168 * not delayed for very long because the lock has already been taken.
169 *
170 * NOTE: This code assumes that certain fields of a port/socket pair are
171 * constant over its lifetime; such fields can be examined without taking
172 * the socket lock and/or port lock, and do not need to be re-read even
173 * after resuming processing after waiting. These fields include:
174 * - socket type
175 * - pointer to socket sk structure (aka tipc_sock structure)
176 * - pointer to port structure
177 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400180static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400181{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400182 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400183}
184
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400185static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400186{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400187 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400188}
189
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400190static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400191{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400192 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400193}
194
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400195static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400196{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400197 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400198}
199
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400200static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400201{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400202 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400203}
204
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400205static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400206{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400208}
209
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400210static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400211{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400212 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400213}
214
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400215static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400216{
217 if (imp > TIPC_CRITICAL_IMPORTANCE)
218 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400219 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400220 return 0;
221}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400222
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400223static struct tipc_sock *tipc_sk(const struct sock *sk)
224{
225 return container_of(sk, struct tipc_sock, sk);
226}
227
228static int tsk_conn_cong(struct tipc_sock *tsk)
229{
230 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
231}
232
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400234 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700235 *
236 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400238static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400240 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241}
242
243/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400244 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700245 *
246 * Caller must hold socket lock
247 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400248static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249{
Ying Xuea6ca1092014-11-26 11:41:55 +0800250 struct sk_buff *skb;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500251 u32 dnode;
Ying Xue34747532015-01-09 15:27:10 +0800252 struct net *net = sock_net(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700253
Ying Xuea6ca1092014-11-26 11:41:55 +0800254 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) {
Ying Xue34747532015-01-09 15:27:10 +0800255 if (tipc_msg_reverse(net, skb, &dnode, TIPC_ERR_NO_PORT))
256 tipc_link_xmit_skb(net, skb, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500257 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700258}
259
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400260/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400261 *
262 * Handles cases where the node's network address has changed from
263 * the default of <0.0.0> to its configured setting.
264 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400265static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400266{
Ying Xue34747532015-01-09 15:27:10 +0800267 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400268 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400269 u32 orig_node;
270 u32 peer_node;
271
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400272 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400273 return false;
274
275 if (unlikely(msg_origport(msg) != peer_port))
276 return false;
277
278 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400279 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400280
281 if (likely(orig_node == peer_node))
282 return true;
283
Ying Xue34747532015-01-09 15:27:10 +0800284 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400285 return true;
286
Ying Xue34747532015-01-09 15:27:10 +0800287 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400288 return true;
289
290 return false;
291}
292
Allan Stephens0c3141e2008-04-15 00:22:02 -0700293/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400294 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700295 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296 * @sock: pre-allocated socket structure
297 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800298 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900299 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700300 * This routine creates additional data structures used by the TIPC socket,
301 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 *
303 * Returns 0 on success, errno otherwise
304 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400305static int tipc_sk_create(struct net *net, struct socket *sock,
306 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700308 const struct proto_ops *ops;
309 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400311 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400312 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700313
314 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 if (unlikely(protocol != 0))
316 return -EPROTONOSUPPORT;
317
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 switch (sock->type) {
319 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700320 ops = &stream_ops;
321 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 break;
323 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700324 ops = &packet_ops;
325 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 break;
327 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700329 ops = &msg_ops;
330 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 break;
Allan Stephens49978652006-06-25 23:47:18 -0700332 default:
Allan Stephens49978652006-06-25 23:47:18 -0700333 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 }
335
Allan Stephens0c3141e2008-04-15 00:22:02 -0700336 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400337 if (!kern)
338 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
339 else
340 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
341
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 if (sk == NULL)
343 return -ENOMEM;
344
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400345 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400346 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400347 INIT_LIST_HEAD(&tsk->publications);
348 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +0800349 tipc_msg_init(net, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400350 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351
Allan Stephens0c3141e2008-04-15 00:22:02 -0700352 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 sock->ops = ops;
354 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800356 if (tipc_sk_insert(tsk)) {
357 pr_warn("Socket create failed; port numbrer exhausted\n");
358 return -EINVAL;
359 }
360 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800361 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400362 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400363 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800364 sk->sk_data_ready = tipc_data_ready;
365 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400366 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500367 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400368 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700369
Allan Stephens0c3141e2008-04-15 00:22:02 -0700370 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400371 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700372 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400373 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700374 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 return 0;
376}
377
378/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400379 * tipc_sock_create_local - create TIPC socket from inside TIPC module
380 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
381 *
382 * We cannot use sock_creat_kern here because it bumps module user count.
383 * Since socket owner and creator is the same module we must make sure
384 * that module count remains zero for module local sockets, otherwise
385 * we cannot do rmmod.
386 *
387 * Returns 0 on success, errno otherwise
388 */
Ying Xuea62fbcc2015-01-09 15:27:11 +0800389int tipc_sock_create_local(struct net *net, int type, struct socket **res)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400390{
391 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400392
393 rc = sock_create_lite(AF_TIPC, type, 0, res);
394 if (rc < 0) {
395 pr_err("Failed to create kernel socket\n");
396 return rc;
397 }
Ying Xuea62fbcc2015-01-09 15:27:11 +0800398 tipc_sk_create(net, *res, 0, 1);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400399
Ying Xuec5fa7b32013-06-17 10:54:39 -0400400 return 0;
401}
402
403/**
404 * tipc_sock_release_local - release socket created by tipc_sock_create_local
405 * @sock: the socket to be released.
406 *
407 * Module reference count is not incremented when such sockets are created,
408 * so we must keep it from being decremented when they are released.
409 */
410void tipc_sock_release_local(struct socket *sock)
411{
Ying Xue247f0f32014-02-18 16:06:46 +0800412 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400413 sock->ops = NULL;
414 sock_release(sock);
415}
416
417/**
418 * tipc_sock_accept_local - accept a connection on a socket created
419 * with tipc_sock_create_local. Use this function to avoid that
420 * module reference count is inadvertently incremented.
421 *
422 * @sock: the accepting socket
423 * @newsock: reference to the new socket to be created
424 * @flags: socket flags
425 */
426
427int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400428 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400429{
430 struct sock *sk = sock->sk;
431 int ret;
432
433 ret = sock_create_lite(sk->sk_family, sk->sk_type,
434 sk->sk_protocol, newsock);
435 if (ret < 0)
436 return ret;
437
Ying Xue247f0f32014-02-18 16:06:46 +0800438 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400439 if (ret < 0) {
440 sock_release(*newsock);
441 return ret;
442 }
443 (*newsock)->ops = sock->ops;
444 return ret;
445}
446
Ying Xue07f6c4b2015-01-07 13:41:58 +0800447static void tipc_sk_callback(struct rcu_head *head)
448{
449 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
450
451 sock_put(&tsk->sk);
452}
453
Ying Xuec5fa7b32013-06-17 10:54:39 -0400454/**
Ying Xue247f0f32014-02-18 16:06:46 +0800455 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 * @sock: socket to destroy
457 *
458 * This routine cleans up any messages that are still queued on the socket.
459 * For DGRAM and RDM socket types, all queued messages are rejected.
460 * For SEQPACKET and STREAM socket types, the first message is rejected
461 * and any others are discarded. (If the first message on a STREAM socket
462 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900463 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * NOTE: Rejected messages are not necessarily returned to the sender! They
465 * are returned or discarded according to the "destination droppable" setting
466 * specified for the message by the sender.
467 *
468 * Returns 0 on success, errno otherwise
469 */
Ying Xue247f0f32014-02-18 16:06:46 +0800470static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800473 struct net *net = sock_net(sk);
Ying Xue34747532015-01-09 15:27:10 +0800474 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400475 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800476 struct sk_buff *skb;
Ying Xuef2f2a962015-01-09 15:27:02 +0800477 u32 dnode, probing_state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478
Allan Stephens0c3141e2008-04-15 00:22:02 -0700479 /*
480 * Exit if socket isn't fully initialized (occurs when a failed accept()
481 * releases a pre-allocated child socket that was never used)
482 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700483 if (sk == NULL)
484 return 0;
485
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400486 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700487 lock_sock(sk);
488
489 /*
490 * Reject all unreceived messages, except on an active connection
491 * (which disconnects locally & sends a 'FIN+' to peer)
492 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400493 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800495 skb = __skb_dequeue(&sk->sk_receive_queue);
496 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 break;
Ying Xuea6ca1092014-11-26 11:41:55 +0800498 if (TIPC_SKB_CB(skb)->handle != NULL)
499 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700500 else {
501 if ((sock->state == SS_CONNECTING) ||
502 (sock->state == SS_CONNECTED)) {
503 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400504 tsk->connected = 0;
Ying Xuef2f98002015-01-09 15:27:05 +0800505 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700506 }
Ying Xue34747532015-01-09 15:27:10 +0800507 if (tipc_msg_reverse(net, skb, &dnode,
508 TIPC_ERR_NO_PORT))
Ying Xuef2f98002015-01-09 15:27:05 +0800509 tipc_link_xmit_skb(net, skb, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700510 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 }
512
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400513 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xuef2f2a962015-01-09 15:27:02 +0800514 probing_state = tsk->probing_state;
Ying Xue3721e9c2015-01-13 17:07:48 +0800515 if (del_timer_sync(&sk->sk_timer) &&
516 probing_state != TIPC_CONN_PROBING)
Ying Xuef2f2a962015-01-09 15:27:02 +0800517 sock_put(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800518 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400519 if (tsk->connected) {
Ying Xue34747532015-01-09 15:27:10 +0800520 skb = tipc_msg_create(net, TIPC_CRITICAL_IMPORTANCE,
521 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
522 tn->own_addr, tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800523 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800524 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +0800525 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
526 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400527 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528
Allan Stephens0c3141e2008-04-15 00:22:02 -0700529 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100530 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531
Allan Stephens0c3141e2008-04-15 00:22:02 -0700532 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700533 sock->state = SS_DISCONNECTING;
534 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800535
536 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700537 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200539 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540}
541
542/**
Ying Xue247f0f32014-02-18 16:06:46 +0800543 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 * @sock: socket structure
545 * @uaddr: socket address describing name(s) and desired operation
546 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900547 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 * Name and name sequence binding is indicated using a positive scope value;
549 * a negative scope value unbinds the specified name. Specifying no name
550 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900551 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700553 *
554 * NOTE: This routine doesn't need to take the socket lock since it doesn't
555 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 */
Ying Xue247f0f32014-02-18 16:06:46 +0800557static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
558 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559{
Ying Xue84602762013-12-27 10:18:28 +0800560 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400562 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800563 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564
Ying Xue84602762013-12-27 10:18:28 +0800565 lock_sock(sk);
566 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400567 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800568 goto exit;
569 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900570
Ying Xue84602762013-12-27 10:18:28 +0800571 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
572 res = -EINVAL;
573 goto exit;
574 }
575 if (addr->family != AF_TIPC) {
576 res = -EAFNOSUPPORT;
577 goto exit;
578 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580 if (addr->addrtype == TIPC_ADDR_NAME)
581 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800582 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
583 res = -EAFNOSUPPORT;
584 goto exit;
585 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586
Ying Xue13a2e892013-06-17 10:54:40 -0400587 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400588 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800589 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
590 res = -EACCES;
591 goto exit;
592 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400593
Ying Xue84602762013-12-27 10:18:28 +0800594 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400595 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
596 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800597exit:
598 release_sock(sk);
599 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600}
601
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602/**
Ying Xue247f0f32014-02-18 16:06:46 +0800603 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 * @sock: socket structure
605 * @uaddr: area for returned socket address
606 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700607 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700610 *
Allan Stephens2da59912008-07-14 22:43:32 -0700611 * NOTE: This routine doesn't need to take the socket lock since it only
612 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000613 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 */
Ying Xue247f0f32014-02-18 16:06:46 +0800615static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
616 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400619 struct tipc_sock *tsk = tipc_sk(sock->sk);
Ying Xue34747532015-01-09 15:27:10 +0800620 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000622 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700623 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700624 if ((sock->state != SS_CONNECTED) &&
625 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
626 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400627 addr->addr.id.ref = tsk_peer_port(tsk);
628 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700629 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800630 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800631 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633
634 *uaddr_len = sizeof(*addr);
635 addr->addrtype = TIPC_ADDR_ID;
636 addr->family = AF_TIPC;
637 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 addr->addr.name.domain = 0;
639
Allan Stephens0c3141e2008-04-15 00:22:02 -0700640 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641}
642
643/**
Ying Xue247f0f32014-02-18 16:06:46 +0800644 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 * @file: file structure associated with the socket
646 * @sock: socket for which to calculate the poll bits
647 * @wait: ???
648 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700649 * Returns pollmask value
650 *
651 * COMMENTARY:
652 * It appears that the usual socket locking mechanisms are not useful here
653 * since the pollmask info is potentially out-of-date the moment this routine
654 * exits. TCP and other protocols seem to rely on higher level poll routines
655 * to handle any preventable race conditions, so TIPC will do the same ...
656 *
657 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700658 *
Allan Stephensf662c072010-08-17 11:00:06 +0000659 * socket state flags set
660 * ------------ ---------
661 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200662 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000663 *
664 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
665 * no write flags
666 *
667 * connected POLLIN/POLLRDNORM if data in rx queue
668 * POLLOUT if port is not congested
669 *
670 * disconnecting POLLIN/POLLRDNORM/POLLHUP
671 * no write flags
672 *
673 * listening POLLIN if SYN in rx queue
674 * no write flags
675 *
676 * ready POLLIN/POLLRDNORM if data in rx queue
677 * [connectionless] POLLOUT (since port cannot be congested)
678 *
679 * IMPORTANT: The fact that a read or write operation is indicated does NOT
680 * imply that the operation will succeed, merely that it should be performed
681 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 */
Ying Xue247f0f32014-02-18 16:06:46 +0800683static unsigned int tipc_poll(struct file *file, struct socket *sock,
684 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685{
Allan Stephens9b674e82008-03-26 16:48:21 -0700686 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400687 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000688 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700689
Ying Xuef288bef2012-08-21 11:16:57 +0800690 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700691
Allan Stephensf662c072010-08-17 11:00:06 +0000692 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200693 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500694 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200695 mask |= POLLOUT;
696 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000697 case SS_READY:
698 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400699 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000700 mask |= POLLOUT;
701 /* fall thru' */
702 case SS_CONNECTING:
703 case SS_LISTENING:
704 if (!skb_queue_empty(&sk->sk_receive_queue))
705 mask |= (POLLIN | POLLRDNORM);
706 break;
707 case SS_DISCONNECTING:
708 mask = (POLLIN | POLLRDNORM | POLLHUP);
709 break;
710 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700711
712 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713}
714
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400715/**
716 * tipc_sendmcast - send multicast message
717 * @sock: socket structure
718 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500719 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400720 * @dsz: total length of message data
721 * @timeo: timeout to wait for wakeup
722 *
723 * Called from function tipc_sendmsg(), which has done all sanity checks
724 * Returns the number of bytes sent on success, or errno
725 */
726static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500727 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400728{
729 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800730 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400731 struct tipc_msg *mhdr = &tipc_sk(sk)->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +0800732 struct sk_buff_head head;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400733 uint mtu;
734 int rc;
735
736 msg_set_type(mhdr, TIPC_MCAST_MSG);
737 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
738 msg_set_destport(mhdr, 0);
739 msg_set_destnode(mhdr, 0);
740 msg_set_nametype(mhdr, seq->type);
741 msg_set_namelower(mhdr, seq->lower);
742 msg_set_nameupper(mhdr, seq->upper);
743 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
744
745new_mtu:
746 mtu = tipc_bclink_get_mtu();
Ying Xuea6ca1092014-11-26 11:41:55 +0800747 __skb_queue_head_init(&head);
Ying Xue34747532015-01-09 15:27:10 +0800748 rc = tipc_msg_build(net, mhdr, msg, 0, dsz, mtu, &head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400749 if (unlikely(rc < 0))
750 return rc;
751
752 do {
Ying Xuef2f98002015-01-09 15:27:05 +0800753 rc = tipc_bclink_xmit(net, &head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400754 if (likely(rc >= 0)) {
755 rc = dsz;
756 break;
757 }
758 if (rc == -EMSGSIZE)
759 goto new_mtu;
760 if (rc != -ELINKCONG)
761 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400762 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400763 rc = tipc_wait_for_sndmsg(sock, &timeo);
764 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800765 __skb_queue_purge(&head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400766 } while (!rc);
767 return rc;
768}
769
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400770/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
771 */
Ying Xuef2f98002015-01-09 15:27:05 +0800772void tipc_sk_mcast_rcv(struct net *net, struct sk_buff *buf)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400773{
774 struct tipc_msg *msg = buf_msg(buf);
775 struct tipc_port_list dports = {0, NULL, };
776 struct tipc_port_list *item;
777 struct sk_buff *b;
778 uint i, last, dst = 0;
779 u32 scope = TIPC_CLUSTER_SCOPE;
780
Ying Xue34747532015-01-09 15:27:10 +0800781 if (in_own_node(net, msg_orignode(msg)))
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400782 scope = TIPC_NODE_SCOPE;
783
784 /* Create destination port list: */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800785 tipc_nametbl_mc_translate(net, msg_nametype(msg), msg_namelower(msg),
786 msg_nameupper(msg), scope, &dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400787 last = dports.count;
788 if (!last) {
789 kfree_skb(buf);
790 return;
791 }
792
793 for (item = &dports; item; item = item->next) {
794 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
795 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
796 if (!b) {
797 pr_warn("Failed do clone mcast rcv buffer\n");
798 continue;
799 }
800 msg_set_destport(msg, item->ports[i]);
Ying Xuef2f98002015-01-09 15:27:05 +0800801 tipc_sk_rcv(net, b);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400802 }
803 }
804 tipc_port_list_free(&dports);
805}
806
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900807/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500808 * tipc_sk_proto_rcv - receive a connection mng protocol message
809 * @tsk: receiving socket
810 * @dnode: node to send response message to, if any
811 * @buf: buffer containing protocol message
812 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
813 * (CONN_PROBE_REPLY) message should be forwarded.
814 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800815static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
816 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500817{
818 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500819 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500820
821 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400822 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500823 goto exit;
824
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400825 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500826
827 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400828 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500829 tsk->sent_unacked -= msg_msgcnt(msg);
830 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400831 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500832 } else if (msg_type(msg) == CONN_PROBE) {
Ying Xue34747532015-01-09 15:27:10 +0800833 if (!tipc_msg_reverse(sock_net(&tsk->sk), buf, dnode, TIPC_OK))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500834 return TIPC_OK;
835 msg_set_type(msg, CONN_PROBE_REPLY);
836 return TIPC_FWD_MSG;
837 }
838 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
839exit:
840 kfree_skb(buf);
841 return TIPC_OK;
842}
843
Ying Xue3f405042014-01-17 09:50:05 +0800844static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
845{
846 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400847 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800848 DEFINE_WAIT(wait);
849 int done;
850
851 do {
852 int err = sock_error(sk);
853 if (err)
854 return err;
855 if (sock->state == SS_DISCONNECTING)
856 return -EPIPE;
857 if (!*timeo_p)
858 return -EAGAIN;
859 if (signal_pending(current))
860 return sock_intr_errno(*timeo_p);
861
862 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500863 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800864 finish_wait(sk_sleep(sk), &wait);
865 } while (!done);
866 return 0;
867}
868
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500869/**
Ying Xue247f0f32014-02-18 16:06:46 +0800870 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700871 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 * @sock: socket structure
873 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500874 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900875 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900877 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
879 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900880 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 * Returns the number of bytes sent on success, or errno otherwise
882 */
Ying Xue247f0f32014-02-18 16:06:46 +0800883static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500884 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500886 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700887 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400888 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800889 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400890 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500891 u32 dnode, dport;
Ying Xuea6ca1092014-11-26 11:41:55 +0800892 struct sk_buff_head head;
893 struct sk_buff *skb;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500894 struct tipc_name_seq *seq = &dest->addr.nameseq;
895 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800896 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100897 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898
899 if (unlikely(!dest))
900 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500901
Allan Stephens51f9cc12006-06-25 23:49:06 -0700902 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
903 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500905
906 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400907 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908
Allan Stephens0c3141e2008-04-15 00:22:02 -0700909 if (iocb)
910 lock_sock(sk);
911
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500912 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700913 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500914 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700915 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700916 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700917 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500918 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700919 goto exit;
920 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400921 if (tsk->published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500922 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700923 goto exit;
924 }
925 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400926 tsk->conn_type = dest->addr.name.name.type;
927 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700928 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929 }
930
Ying Xue3f405042014-01-17 09:50:05 +0800931 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500932
933 if (dest->addrtype == TIPC_ADDR_MCAST) {
Al Viro562640f2014-11-15 01:13:43 -0500934 rc = tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500935 goto exit;
936 } else if (dest->addrtype == TIPC_ADDR_NAME) {
937 u32 type = dest->addr.name.name.type;
938 u32 inst = dest->addr.name.name.instance;
939 u32 domain = dest->addr.name.domain;
940
941 dnode = domain;
942 msg_set_type(mhdr, TIPC_NAMED_MSG);
943 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
944 msg_set_nametype(mhdr, type);
945 msg_set_nameinst(mhdr, inst);
946 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800947 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500948 msg_set_destnode(mhdr, dnode);
949 msg_set_destport(mhdr, dport);
950 if (unlikely(!dport && !dnode)) {
951 rc = -EHOSTUNREACH;
952 goto exit;
953 }
954 } else if (dest->addrtype == TIPC_ADDR_ID) {
955 dnode = dest->addr.id.node;
956 msg_set_type(mhdr, TIPC_DIRECT_MSG);
957 msg_set_lookup_scope(mhdr, 0);
958 msg_set_destnode(mhdr, dnode);
959 msg_set_destport(mhdr, dest->addr.id.ref);
960 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
961 }
962
963new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800964 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Ying Xuea6ca1092014-11-26 11:41:55 +0800965 __skb_queue_head_init(&head);
Ying Xue34747532015-01-09 15:27:10 +0800966 rc = tipc_msg_build(net, mhdr, m, 0, dsz, mtu, &head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500967 if (rc < 0)
968 goto exit;
969
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900970 do {
Ying Xuea6ca1092014-11-26 11:41:55 +0800971 skb = skb_peek(&head);
972 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Ying Xuef2f98002015-01-09 15:27:05 +0800973 rc = tipc_link_xmit(net, &head, dnode, tsk->portid);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500974 if (likely(rc >= 0)) {
975 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700976 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500977 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700978 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900979 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500980 if (rc == -EMSGSIZE)
981 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500982 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700983 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400984 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500985 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400986 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800987 __skb_queue_purge(&head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500988 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700989exit:
990 if (iocb)
991 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500992
993 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994}
995
Ying Xue391a6dd2014-01-17 09:50:06 +0800996static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
997{
998 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400999 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +08001000 DEFINE_WAIT(wait);
1001 int done;
1002
1003 do {
1004 int err = sock_error(sk);
1005 if (err)
1006 return err;
1007 if (sock->state == SS_DISCONNECTING)
1008 return -EPIPE;
1009 else if (sock->state != SS_CONNECTED)
1010 return -ENOTCONN;
1011 if (!*timeo_p)
1012 return -EAGAIN;
1013 if (signal_pending(current))
1014 return sock_intr_errno(*timeo_p);
1015
1016 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1017 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001018 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001019 !tsk_conn_cong(tsk)) ||
1020 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +08001021 finish_wait(sk_sleep(sk), &wait);
1022 } while (!done);
1023 return 0;
1024}
1025
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026/**
Ying Xue247f0f32014-02-18 16:06:46 +08001027 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 * @iocb: (unused)
1029 * @sock: socket structure
1030 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001031 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001032 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001034 *
1035 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001036 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 */
Ying Xue247f0f32014-02-18 16:06:46 +08001038static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001039 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001041 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001042 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001043 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001044 struct tipc_msg *mhdr = &tsk->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +08001045 struct sk_buff_head head;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001046 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001047 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001048 int rc = -EINVAL;
1049 long timeo;
1050 u32 dnode;
1051 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001052
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001053 /* Handle implied connection establishment */
1054 if (unlikely(dest)) {
1055 rc = tipc_sendmsg(iocb, sock, m, dsz);
1056 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001057 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001058 return rc;
1059 }
1060 if (dsz > (uint)INT_MAX)
1061 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001062
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001063 if (iocb)
1064 lock_sock(sk);
1065
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001066 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001067 if (sock->state == SS_DISCONNECTING)
1068 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001069 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001070 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +08001071 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001072 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001074 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001075 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001076
1077next:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001078 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001079 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001080 __skb_queue_head_init(&head);
Ying Xue34747532015-01-09 15:27:10 +08001081 rc = tipc_msg_build(net, mhdr, m, sent, send, mtu, &head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001082 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001083 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001084 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001085 if (likely(!tsk_conn_cong(tsk))) {
Ying Xuef2f98002015-01-09 15:27:05 +08001086 rc = tipc_link_xmit(net, &head, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001087 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001088 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001089 sent += send;
1090 if (sent == dsz)
1091 break;
1092 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001093 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001094 if (rc == -EMSGSIZE) {
Ying Xuef2f98002015-01-09 15:27:05 +08001095 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1096 portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001097 goto next;
1098 }
1099 if (rc != -ELINKCONG)
1100 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001101 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001102 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001103 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001104 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +08001105 __skb_queue_purge(&head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001106 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001107exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001108 if (iocb)
1109 release_sock(sk);
1110 return sent ? sent : rc;
1111}
1112
1113/**
1114 * tipc_send_packet - send a connection-oriented message
1115 * @iocb: if NULL, indicates that socket lock is already held
1116 * @sock: socket structure
1117 * @m: message to send
1118 * @dsz: length of data to be transmitted
1119 *
1120 * Used for SOCK_SEQPACKET messages.
1121 *
1122 * Returns the number of bytes sent on success, or errno otherwise
1123 */
1124static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1125 struct msghdr *m, size_t dsz)
1126{
1127 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1128 return -EMSGSIZE;
1129
1130 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131}
1132
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001133/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001135static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001136 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137{
Ying Xue3721e9c2015-01-13 17:07:48 +08001138 struct sock *sk = &tsk->sk;
1139 struct net *net = sock_net(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 Xue3721e9c2015-01-13 17:07:48 +08001151 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001152 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1153 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154}
1155
1156/**
1157 * set_orig_addr - capture sender's address for received message
1158 * @m: descriptor for message info
1159 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001160 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 * Note: Address is not captured if not requested by receiver.
1162 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001163static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001165 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001167 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 addr->family = AF_TIPC;
1169 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001170 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 addr->addr.id.ref = msg_origport(msg);
1172 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001173 addr->addr.name.domain = 0; /* could leave uninitialized */
1174 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 m->msg_namelen = sizeof(struct sockaddr_tipc);
1176 }
1177}
1178
1179/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001180 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 * @m: descriptor for message info
1182 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001183 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001184 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001186 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 * Returns 0 if successful, otherwise errno
1188 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001189static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1190 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191{
1192 u32 anc_data[3];
1193 u32 err;
1194 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001195 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 int res;
1197
1198 if (likely(m->msg_controllen == 0))
1199 return 0;
1200
1201 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 err = msg ? msg_errcode(msg) : 0;
1203 if (unlikely(err)) {
1204 anc_data[0] = err;
1205 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001206 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1207 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001209 if (anc_data[1]) {
1210 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1211 msg_data(msg));
1212 if (res)
1213 return res;
1214 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 }
1216
1217 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1219 switch (dest_type) {
1220 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001221 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 anc_data[0] = msg_nametype(msg);
1223 anc_data[1] = msg_namelower(msg);
1224 anc_data[2] = msg_namelower(msg);
1225 break;
1226 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001227 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 anc_data[0] = msg_nametype(msg);
1229 anc_data[1] = msg_namelower(msg);
1230 anc_data[2] = msg_nameupper(msg);
1231 break;
1232 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001233 has_name = (tsk->conn_type != 0);
1234 anc_data[0] = tsk->conn_type;
1235 anc_data[1] = tsk->conn_instance;
1236 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 break;
1238 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001239 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 }
Allan Stephens2db99832010-12-31 18:59:33 +00001241 if (has_name) {
1242 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1243 if (res)
1244 return res;
1245 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246
1247 return 0;
1248}
1249
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001250static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001251{
Ying Xuef2f98002015-01-09 15:27:05 +08001252 struct net *net = sock_net(&tsk->sk);
Ying Xue34747532015-01-09 15:27:10 +08001253 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuea6ca1092014-11-26 11:41:55 +08001254 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001255 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001256 u32 peer_port = tsk_peer_port(tsk);
1257 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001258
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001259 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001260 return;
Ying Xue34747532015-01-09 15:27:10 +08001261 skb = tipc_msg_create(net, CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1262 dnode, tn->own_addr, peer_port, tsk->portid,
1263 TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001264 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001265 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001266 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001267 msg_set_msgcnt(msg, ack);
Ying Xuef2f98002015-01-09 15:27:05 +08001268 tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001269}
1270
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001271static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001272{
1273 struct sock *sk = sock->sk;
1274 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001275 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001276 int err;
1277
1278 for (;;) {
1279 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001280 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001281 if (sock->state == SS_DISCONNECTING) {
1282 err = -ENOTCONN;
1283 break;
1284 }
1285 release_sock(sk);
1286 timeo = schedule_timeout(timeo);
1287 lock_sock(sk);
1288 }
1289 err = 0;
1290 if (!skb_queue_empty(&sk->sk_receive_queue))
1291 break;
1292 err = sock_intr_errno(timeo);
1293 if (signal_pending(current))
1294 break;
1295 err = -EAGAIN;
1296 if (!timeo)
1297 break;
1298 }
1299 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001300 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001301 return err;
1302}
1303
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001304/**
Ying Xue247f0f32014-02-18 16:06:46 +08001305 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 * @iocb: (unused)
1307 * @m: descriptor for message info
1308 * @buf_len: total size of user buffer area
1309 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001310 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1312 * If the complete message doesn't fit in user area, truncate it.
1313 *
1314 * Returns size of returned message data, errno otherwise
1315 */
Ying Xue247f0f32014-02-18 16:06:46 +08001316static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1317 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001319 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001320 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321 struct sk_buff *buf;
1322 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001323 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 unsigned int sz;
1325 u32 err;
1326 int res;
1327
Allan Stephens0c3141e2008-04-15 00:22:02 -07001328 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 if (unlikely(!buf_len))
1330 return -EINVAL;
1331
Allan Stephens0c3141e2008-04-15 00:22:02 -07001332 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333
Allan Stephens0c3141e2008-04-15 00:22:02 -07001334 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 res = -ENOTCONN;
1336 goto exit;
1337 }
1338
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001339 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001340restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341
Allan Stephens0c3141e2008-04-15 00:22:02 -07001342 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001343 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001344 if (res)
1345 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001346
1347 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 msg = buf_msg(buf);
1350 sz = msg_data_sz(msg);
1351 err = msg_errcode(msg);
1352
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001355 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 goto restart;
1357 }
1358
1359 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 set_orig_addr(m, msg);
1361
1362 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001363 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001364 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 goto exit;
1366
1367 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368 if (!err) {
1369 if (unlikely(buf_len < sz)) {
1370 sz = buf_len;
1371 m->msg_flags |= MSG_TRUNC;
1372 }
David S. Miller51f3d022014-11-05 16:46:40 -05001373 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001374 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 res = sz;
1377 } else {
1378 if ((sock->state == SS_READY) ||
1379 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1380 res = 0;
1381 else
1382 res = -ECONNRESET;
1383 }
1384
1385 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001387 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001388 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001389 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001390 tsk->rcv_unacked = 0;
1391 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001392 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001393 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001395 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 return res;
1397}
1398
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001399/**
Ying Xue247f0f32014-02-18 16:06:46 +08001400 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401 * @iocb: (unused)
1402 * @m: descriptor for message info
1403 * @buf_len: total size of user buffer area
1404 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001405 *
1406 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407 * will optionally wait for more; never truncates data.
1408 *
1409 * Returns size of returned message data, errno otherwise
1410 */
Ying Xue247f0f32014-02-18 16:06:46 +08001411static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1412 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001415 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 struct sk_buff *buf;
1417 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001418 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001420 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001423 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424
1425 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426 if (unlikely(!buf_len))
1427 return -EINVAL;
1428
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001431 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432 res = -ENOTCONN;
1433 goto exit;
1434 }
1435
Florian Westphal3720d402010-08-17 11:00:04 +00001436 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001437 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001438
Allan Stephens0c3141e2008-04-15 00:22:02 -07001439restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001440 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001441 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001442 if (res)
1443 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001444
1445 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001446 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447 msg = buf_msg(buf);
1448 sz = msg_data_sz(msg);
1449 err = msg_errcode(msg);
1450
1451 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001453 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454 goto restart;
1455 }
1456
1457 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001458 if (sz_copied == 0) {
1459 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001460 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001461 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462 goto exit;
1463 }
1464
1465 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001467 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468
Allan Stephens0232fd02011-02-21 09:45:40 -05001469 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 needed = (buf_len - sz_copied);
1471 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001472
David S. Miller51f3d022014-11-05 16:46:40 -05001473 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1474 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001475 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001477
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 sz_copied += sz_to_copy;
1479
1480 if (sz_to_copy < sz) {
1481 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001482 TIPC_SKB_CB(buf)->handle =
1483 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 goto exit;
1485 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486 } else {
1487 if (sz_copied != 0)
1488 goto exit; /* can't add error msg to valid data */
1489
1490 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1491 res = 0;
1492 else
1493 res = -ECONNRESET;
1494 }
1495
1496 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001498 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001499 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001500 tsk->rcv_unacked = 0;
1501 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001502 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001503 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504
1505 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001506 if ((sz_copied < buf_len) && /* didn't get all requested data */
1507 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001508 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001509 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1510 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 goto restart;
1512
1513exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001514 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001515 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516}
1517
1518/**
Ying Xuef288bef2012-08-21 11:16:57 +08001519 * tipc_write_space - wake up thread if port congestion is released
1520 * @sk: socket
1521 */
1522static void tipc_write_space(struct sock *sk)
1523{
1524 struct socket_wq *wq;
1525
1526 rcu_read_lock();
1527 wq = rcu_dereference(sk->sk_wq);
1528 if (wq_has_sleeper(wq))
1529 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1530 POLLWRNORM | POLLWRBAND);
1531 rcu_read_unlock();
1532}
1533
1534/**
1535 * tipc_data_ready - wake up threads to indicate messages have been received
1536 * @sk: socket
1537 * @len: the length of messages
1538 */
David S. Miller676d2362014-04-11 16:15:36 -04001539static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001540{
1541 struct socket_wq *wq;
1542
1543 rcu_read_lock();
1544 wq = rcu_dereference(sk->sk_wq);
1545 if (wq_has_sleeper(wq))
1546 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1547 POLLRDNORM | POLLRDBAND);
1548 rcu_read_unlock();
1549}
1550
1551/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001552 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001553 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001554 * @msg: message
1555 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001556 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001557 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001558static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001559{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001560 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001561 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001562 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001563 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001564 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001565
1566 if (msg_mcast(msg))
1567 return retval;
1568
1569 switch ((int)sock->state) {
1570 case SS_CONNECTED:
1571 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001572 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001573 if (unlikely(msg_errcode(msg))) {
1574 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001575 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001576 /* let timer expire on it's own */
Ying Xuef2f98002015-01-09 15:27:05 +08001577 tipc_node_remove_conn(net, tsk_peer_node(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08001578 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001579 }
1580 retval = TIPC_OK;
1581 }
1582 break;
1583 case SS_CONNECTING:
1584 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001585
1586 if (unlikely(!msg_connected(msg)))
1587 break;
1588
Ying Xue584d24b2012-11-29 18:51:19 -05001589 if (unlikely(msg_errcode(msg))) {
1590 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001591 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001592 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001593 break;
1594 }
1595
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001596 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001597 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001598 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001599 retval = TIPC_OK;
1600 break;
1601 }
1602
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001603 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1604 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001605 sock->state = SS_CONNECTED;
1606
Ying Xue584d24b2012-11-29 18:51:19 -05001607 /* If an incoming message is an 'ACK-', it should be
1608 * discarded here because it doesn't contain useful
1609 * data. In addition, we should try to wake up
1610 * connect() routine if sleeping.
1611 */
1612 if (msg_data_sz(msg) == 0) {
1613 kfree_skb(*buf);
1614 *buf = NULL;
1615 if (waitqueue_active(sk_sleep(sk)))
1616 wake_up_interruptible(sk_sleep(sk));
1617 }
1618 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001619 break;
1620 case SS_LISTENING:
1621 case SS_UNCONNECTED:
1622 /* Accept only SYN message */
1623 if (!msg_connected(msg) && !(msg_errcode(msg)))
1624 retval = TIPC_OK;
1625 break;
1626 case SS_DISCONNECTING:
1627 break;
1628 default:
1629 pr_err("Unknown socket state %u\n", sock->state);
1630 }
1631 return retval;
1632}
1633
1634/**
Ying Xueaba79f32013-01-20 23:30:09 +01001635 * rcvbuf_limit - get proper overload limit of socket receive queue
1636 * @sk: socket
1637 * @buf: message
1638 *
1639 * For all connection oriented messages, irrespective of importance,
1640 * the default overload value (i.e. 67MB) is set as limit.
1641 *
1642 * For all connectionless messages, by default new queue limits are
1643 * as belows:
1644 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001645 * TIPC_LOW_IMPORTANCE (4 MB)
1646 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1647 * TIPC_HIGH_IMPORTANCE (16 MB)
1648 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001649 *
1650 * Returns overload limit according to corresponding message importance
1651 */
1652static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1653{
1654 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001655
1656 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001657 return sysctl_tipc_rmem[2];
1658
1659 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1660 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001661}
1662
1663/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001664 * filter_rcv - validate incoming message
1665 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001667 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001668 * Enqueues message on receive queue if acceptable; optionally handles
1669 * disconnect indication for a connected socket.
1670 *
1671 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001672 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001673 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001674 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001676static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001678 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001679 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001681 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001682 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001683 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001685 if (unlikely(msg_user(msg) == CONN_MANAGER))
1686 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001687
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001688 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1689 kfree_skb(buf);
1690 tsk->link_cong = 0;
1691 sk->sk_write_space(sk);
1692 return TIPC_OK;
1693 }
1694
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001696 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001697 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001698
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001700 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001701 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001703 rc = filter_connect(tsk, &buf);
1704 if (rc != TIPC_OK || buf == NULL)
1705 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 }
1707
1708 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001709 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001710 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711
Ying Xueaba79f32013-01-20 23:30:09 +01001712 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001713 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001714 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001715 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001716
David S. Miller676d2362014-04-11 16:15:36 -04001717 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718 return TIPC_OK;
1719}
1720
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001721/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001722 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001723 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001724 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725 *
1726 * Caller must hold socket lock, but not port lock.
1727 *
1728 * Returns 0
1729 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001730static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001732 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001733 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001734 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +08001735 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001736 uint truesize = skb->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001737
Ying Xuea6ca1092014-11-26 11:41:55 +08001738 rc = filter_rcv(sk, skb);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001739
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001740 if (likely(!rc)) {
1741 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1742 atomic_add(truesize, &tsk->dupl_rcvcnt);
1743 return 0;
1744 }
1745
Ying Xue34747532015-01-09 15:27:10 +08001746 if ((rc < 0) && !tipc_msg_reverse(net, skb, &onode, -rc))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001747 return 0;
1748
Ying Xue34747532015-01-09 15:27:10 +08001749 tipc_link_xmit_skb(net, skb, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001750
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751 return 0;
1752}
1753
1754/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001755 * tipc_sk_rcv - handle incoming message
Ying Xuea6ca1092014-11-26 11:41:55 +08001756 * @skb: buffer containing arriving message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001757 * Consumes buffer
1758 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759 */
Ying Xuef2f98002015-01-09 15:27:05 +08001760int tipc_sk_rcv(struct net *net, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001761{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001762 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001763 struct sock *sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001764 u32 dport = msg_destport(buf_msg(skb));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001765 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001766 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001767 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001768
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001769 /* Validate destination and message */
Ying Xuee05b31f2015-01-09 15:27:08 +08001770 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001771 if (unlikely(!tsk)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001772 rc = tipc_msg_eval(net, skb, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001773 goto exit;
1774 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001775 sk = &tsk->sk;
1776
1777 /* Queue message */
Ying Xue1a194c22014-10-20 14:46:35 +08001778 spin_lock_bh(&sk->sk_lock.slock);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001779
Allan Stephens0c3141e2008-04-15 00:22:02 -07001780 if (!sock_owned_by_user(sk)) {
Ying Xuea6ca1092014-11-26 11:41:55 +08001781 rc = filter_rcv(sk, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001782 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001783 if (sk->sk_backlog.len == 0)
1784 atomic_set(&tsk->dupl_rcvcnt, 0);
Ying Xuea6ca1092014-11-26 11:41:55 +08001785 limit = rcvbuf_limit(sk, skb) + atomic_read(&tsk->dupl_rcvcnt);
1786 if (sk_add_backlog(sk, skb, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001787 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001788 }
Ying Xue1a194c22014-10-20 14:46:35 +08001789 spin_unlock_bh(&sk->sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001790 sock_put(sk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001791 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001792 return 0;
1793exit:
Ying Xue34747532015-01-09 15:27:10 +08001794 if ((rc < 0) && !tipc_msg_reverse(net, skb, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001795 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001796
Ying Xuef2f98002015-01-09 15:27:05 +08001797 tipc_link_xmit_skb(net, skb, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001798 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001799}
1800
Ying Xue78eb3a52014-01-17 09:50:03 +08001801static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1802{
1803 struct sock *sk = sock->sk;
1804 DEFINE_WAIT(wait);
1805 int done;
1806
1807 do {
1808 int err = sock_error(sk);
1809 if (err)
1810 return err;
1811 if (!*timeo_p)
1812 return -ETIMEDOUT;
1813 if (signal_pending(current))
1814 return sock_intr_errno(*timeo_p);
1815
1816 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1817 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1818 finish_wait(sk_sleep(sk), &wait);
1819 } while (!done);
1820 return 0;
1821}
1822
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823/**
Ying Xue247f0f32014-02-18 16:06:46 +08001824 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825 * @sock: socket structure
1826 * @dest: socket address for destination port
1827 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001828 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 *
1830 * Returns 0 on success, errno otherwise
1831 */
Ying Xue247f0f32014-02-18 16:06:46 +08001832static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1833 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001835 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001836 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1837 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001838 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1839 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001840 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841
Allan Stephens0c3141e2008-04-15 00:22:02 -07001842 lock_sock(sk);
1843
Allan Stephensb89741a2008-04-15 00:20:37 -07001844 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001845 if (sock->state == SS_READY) {
1846 res = -EOPNOTSUPP;
1847 goto exit;
1848 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849
Allan Stephensb89741a2008-04-15 00:20:37 -07001850 /*
1851 * Reject connection attempt using multicast address
1852 *
1853 * Note: send_msg() validates the rest of the address fields,
1854 * so there's no need to do it here
1855 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001856 if (dst->addrtype == TIPC_ADDR_MCAST) {
1857 res = -EINVAL;
1858 goto exit;
1859 }
1860
Ying Xue78eb3a52014-01-17 09:50:03 +08001861 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001862 switch (sock->state) {
1863 case SS_UNCONNECTED:
1864 /* Send a 'SYN-' to destination */
1865 m.msg_name = dest;
1866 m.msg_namelen = destlen;
1867
1868 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1869 * indicate send_msg() is never blocked.
1870 */
1871 if (!timeout)
1872 m.msg_flags = MSG_DONTWAIT;
1873
Ying Xue247f0f32014-02-18 16:06:46 +08001874 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001875 if ((res < 0) && (res != -EWOULDBLOCK))
1876 goto exit;
1877
1878 /* Just entered SS_CONNECTING state; the only
1879 * difference is that return value in non-blocking
1880 * case is EINPROGRESS, rather than EALREADY.
1881 */
1882 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001883 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001884 if (previous == SS_CONNECTING)
1885 res = -EALREADY;
1886 if (!timeout)
1887 goto exit;
1888 timeout = msecs_to_jiffies(timeout);
1889 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1890 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001891 break;
1892 case SS_CONNECTED:
1893 res = -EISCONN;
1894 break;
1895 default:
1896 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001897 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001898 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001899exit:
1900 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001901 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902}
1903
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001904/**
Ying Xue247f0f32014-02-18 16:06:46 +08001905 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906 * @sock: socket structure
1907 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001908 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909 * Returns 0 on success, errno otherwise
1910 */
Ying Xue247f0f32014-02-18 16:06:46 +08001911static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001913 struct sock *sk = sock->sk;
1914 int res;
1915
1916 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917
Ying Xue245f3d32011-07-06 06:01:13 -04001918 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001919 res = -EINVAL;
1920 else {
1921 sock->state = SS_LISTENING;
1922 res = 0;
1923 }
1924
1925 release_sock(sk);
1926 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927}
1928
Ying Xue6398e232014-01-17 09:50:04 +08001929static int tipc_wait_for_accept(struct socket *sock, long timeo)
1930{
1931 struct sock *sk = sock->sk;
1932 DEFINE_WAIT(wait);
1933 int err;
1934
1935 /* True wake-one mechanism for incoming connections: only
1936 * one process gets woken up, not the 'whole herd'.
1937 * Since we do not 'race & poll' for established sockets
1938 * anymore, the common case will execute the loop only once.
1939 */
1940 for (;;) {
1941 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1942 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001943 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001944 release_sock(sk);
1945 timeo = schedule_timeout(timeo);
1946 lock_sock(sk);
1947 }
1948 err = 0;
1949 if (!skb_queue_empty(&sk->sk_receive_queue))
1950 break;
1951 err = -EINVAL;
1952 if (sock->state != SS_LISTENING)
1953 break;
1954 err = sock_intr_errno(timeo);
1955 if (signal_pending(current))
1956 break;
1957 err = -EAGAIN;
1958 if (!timeo)
1959 break;
1960 }
1961 finish_wait(sk_sleep(sk), &wait);
1962 return err;
1963}
1964
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001965/**
Ying Xue247f0f32014-02-18 16:06:46 +08001966 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001967 * @sock: listening socket
1968 * @newsock: new socket that is to be connected
1969 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001970 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971 * Returns 0 on success, errno otherwise
1972 */
Ying Xue247f0f32014-02-18 16:06:46 +08001973static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001975 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001977 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001978 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001979 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001980 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981
Allan Stephens0c3141e2008-04-15 00:22:02 -07001982 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001983
Allan Stephens0c3141e2008-04-15 00:22:02 -07001984 if (sock->state != SS_LISTENING) {
1985 res = -EINVAL;
1986 goto exit;
1987 }
Ying Xue6398e232014-01-17 09:50:04 +08001988 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1989 res = tipc_wait_for_accept(sock, timeo);
1990 if (res)
1991 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001992
1993 buf = skb_peek(&sk->sk_receive_queue);
1994
Ying Xuec5fa7b32013-06-17 10:54:39 -04001995 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001996 if (res)
1997 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001998
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001999 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002000 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002001 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002002
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002003 /* we lock on new_sk; but lockdep sees the lock on sk */
2004 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002005
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002006 /*
2007 * Reject any stray messages received by new socket
2008 * before the socket lock was taken (very, very unlikely)
2009 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002010 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002012 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002013 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002014 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002016 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002017 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002018 new_tsock->conn_type = msg_nametype(msg);
2019 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002021
2022 /*
2023 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2024 * Respond to 'SYN+' by queuing it on new socket.
2025 */
2026 if (!msg_data_sz(msg)) {
2027 struct msghdr m = {NULL,};
2028
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002029 tsk_advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08002030 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002031 } else {
2032 __skb_dequeue(&sk->sk_receive_queue);
2033 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002034 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002035 }
2036 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002037exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002038 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 return res;
2040}
2041
2042/**
Ying Xue247f0f32014-02-18 16:06:46 +08002043 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002045 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002046 *
2047 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002048 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002049 * Returns 0 on success, errno otherwise
2050 */
Ying Xue247f0f32014-02-18 16:06:46 +08002051static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002052{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002053 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002054 struct net *net = sock_net(sk);
Ying Xue34747532015-01-09 15:27:10 +08002055 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002056 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002057 struct sk_buff *skb;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002058 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002059 int res;
2060
Allan Stephense247a8f2008-03-06 15:05:38 -08002061 if (how != SHUT_RDWR)
2062 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063
Allan Stephens0c3141e2008-04-15 00:22:02 -07002064 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002065
2066 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002067 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068 case SS_CONNECTED:
2069
Per Lidenb97bf3f2006-01-02 19:04:38 +01002070restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002071 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002072 skb = __skb_dequeue(&sk->sk_receive_queue);
2073 if (skb) {
2074 if (TIPC_SKB_CB(skb)->handle != NULL) {
2075 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076 goto restart;
2077 }
Ying Xue34747532015-01-09 15:27:10 +08002078 if (tipc_msg_reverse(net, skb, &dnode,
2079 TIPC_CONN_SHUTDOWN))
Ying Xuef2f98002015-01-09 15:27:05 +08002080 tipc_link_xmit_skb(net, skb, dnode,
2081 tsk->portid);
2082 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002083 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002084 dnode = tsk_peer_node(tsk);
Ying Xue34747532015-01-09 15:27:10 +08002085 skb = tipc_msg_create(net, TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002086 TIPC_CONN_MSG, SHORT_H_SIZE,
Ying Xue34747532015-01-09 15:27:10 +08002087 0, dnode, tn->own_addr,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002088 tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002089 tsk->portid, TIPC_CONN_SHUTDOWN);
Ying Xuef2f98002015-01-09 15:27:05 +08002090 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002091 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002092 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002093 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002094 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002095 /* fall through */
2096
2097 case SS_DISCONNECTING:
2098
Ying Xue75031152012-10-29 09:38:15 -04002099 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002100 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002101
2102 /* Wake up anyone sleeping in poll */
2103 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104 res = 0;
2105 break;
2106
2107 default:
2108 res = -ENOTCONN;
2109 }
2110
Allan Stephens0c3141e2008-04-15 00:22:02 -07002111 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002112 return res;
2113}
2114
Ying Xuef2f2a962015-01-09 15:27:02 +08002115static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002116{
Ying Xuef2f2a962015-01-09 15:27:02 +08002117 struct tipc_sock *tsk = (struct tipc_sock *)data;
2118 struct sock *sk = &tsk->sk;
Ying Xue34747532015-01-09 15:27:10 +08002119 struct net *net = sock_net(sk);
2120 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xuea6ca1092014-11-26 11:41:55 +08002121 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002122 u32 peer_port, peer_node;
2123
Jon Paul Maloy57289012014-08-22 18:09:09 -04002124 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002125 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002126 bh_unlock_sock(sk);
2127 goto exit;
2128 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002129 peer_port = tsk_peer_port(tsk);
2130 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002131
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002132 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002133 /* Previous probe not answered -> self abort */
Ying Xue34747532015-01-09 15:27:10 +08002134 skb = tipc_msg_create(net, TIPC_CRITICAL_IMPORTANCE,
2135 TIPC_CONN_MSG, SHORT_H_SIZE, 0,
2136 tn->own_addr, peer_node, tsk->portid,
2137 peer_port, TIPC_ERR_NO_PORT);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002138 } else {
Ying Xue34747532015-01-09 15:27:10 +08002139 skb = tipc_msg_create(net, CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2140 0, peer_node, tn->own_addr,
Ying Xuef2f2a962015-01-09 15:27:02 +08002141 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002142 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002143 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002144 }
2145 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002146 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +08002147 tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002148exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002149 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002150}
2151
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002152static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002153 struct tipc_name_seq const *seq)
2154{
Ying Xuef2f98002015-01-09 15:27:05 +08002155 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002156 struct publication *publ;
2157 u32 key;
2158
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002159 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002160 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002161 key = tsk->portid + tsk->pub_count + 1;
2162 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002163 return -EADDRINUSE;
2164
Ying Xuef2f98002015-01-09 15:27:05 +08002165 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002166 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002167 if (unlikely(!publ))
2168 return -EINVAL;
2169
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002170 list_add(&publ->pport_list, &tsk->publications);
2171 tsk->pub_count++;
2172 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002173 return 0;
2174}
2175
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002176static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002177 struct tipc_name_seq const *seq)
2178{
Ying Xuef2f98002015-01-09 15:27:05 +08002179 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002180 struct publication *publ;
2181 struct publication *safe;
2182 int rc = -EINVAL;
2183
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002184 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002185 if (seq) {
2186 if (publ->scope != scope)
2187 continue;
2188 if (publ->type != seq->type)
2189 continue;
2190 if (publ->lower != seq->lower)
2191 continue;
2192 if (publ->upper != seq->upper)
2193 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002194 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002195 publ->ref, publ->key);
2196 rc = 0;
2197 break;
2198 }
Ying Xuef2f98002015-01-09 15:27:05 +08002199 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002200 publ->ref, publ->key);
2201 rc = 0;
2202 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002203 if (list_empty(&tsk->publications))
2204 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002205 return rc;
2206}
2207
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002208static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002209 int len, int full_id)
2210{
Ying Xue34747532015-01-09 15:27:10 +08002211 struct net *net = sock_net(&tsk->sk);
2212 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002213 struct publication *publ;
2214 int ret;
2215
2216 if (full_id)
2217 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
Ying Xue34747532015-01-09 15:27:10 +08002218 tipc_zone(tn->own_addr),
2219 tipc_cluster(tn->own_addr),
2220 tipc_node(tn->own_addr), tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002221 else
Ying Xue07f6c4b2015-01-07 13:41:58 +08002222 ret = tipc_snprintf(buf, len, "%-10u:", tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002223
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002224 if (tsk->connected) {
2225 u32 dport = tsk_peer_port(tsk);
2226 u32 destnode = tsk_peer_node(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002227
2228 ret += tipc_snprintf(buf + ret, len - ret,
2229 " connected to <%u.%u.%u:%u>",
2230 tipc_zone(destnode),
2231 tipc_cluster(destnode),
2232 tipc_node(destnode), dport);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002233 if (tsk->conn_type != 0)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002234 ret += tipc_snprintf(buf + ret, len - ret,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002235 " via {%u,%u}", tsk->conn_type,
2236 tsk->conn_instance);
2237 } else if (tsk->published) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002238 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002239 list_for_each_entry(publ, &tsk->publications, pport_list) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002240 if (publ->lower == publ->upper)
2241 ret += tipc_snprintf(buf + ret, len - ret,
2242 " {%u,%u}", publ->type,
2243 publ->lower);
2244 else
2245 ret += tipc_snprintf(buf + ret, len - ret,
2246 " {%u,%u,%u}", publ->type,
2247 publ->lower, publ->upper);
2248 }
2249 }
2250 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2251 return ret;
2252}
2253
Ying Xuee05b31f2015-01-09 15:27:08 +08002254struct sk_buff *tipc_sk_socks_show(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002255{
Ying Xuee05b31f2015-01-09 15:27:08 +08002256 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002257 const struct bucket_table *tbl;
2258 struct rhash_head *pos;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002259 struct sk_buff *buf;
2260 struct tlv_desc *rep_tlv;
2261 char *pb;
2262 int pb_len;
2263 struct tipc_sock *tsk;
2264 int str_len = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002265 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002266
2267 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2268 if (!buf)
2269 return NULL;
2270 rep_tlv = (struct tlv_desc *)buf->data;
2271 pb = TLV_DATA(rep_tlv);
2272 pb_len = ULTRA_STRING_MAX_LEN;
2273
Ying Xue07f6c4b2015-01-07 13:41:58 +08002274 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002275 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002276 for (i = 0; i < tbl->size; i++) {
2277 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2278 spin_lock_bh(&tsk->sk.sk_lock.slock);
2279 str_len += tipc_sk_show(tsk, pb + str_len,
2280 pb_len - str_len, 0);
2281 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2282 }
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002283 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002284 rcu_read_unlock();
2285
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002286 str_len += 1; /* for "\0" */
2287 skb_put(buf, TLV_SPACE(str_len));
2288 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2289
2290 return buf;
2291}
2292
2293/* tipc_sk_reinit: set non-zero address in all existing sockets
2294 * when we go from standalone to network mode.
2295 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002296void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002297{
Ying Xuee05b31f2015-01-09 15:27:08 +08002298 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002299 const struct bucket_table *tbl;
2300 struct rhash_head *pos;
2301 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002302 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002303 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002304
Ying Xue07f6c4b2015-01-07 13:41:58 +08002305 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002306 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002307 for (i = 0; i < tbl->size; i++) {
2308 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2309 spin_lock_bh(&tsk->sk.sk_lock.slock);
2310 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002311 msg_set_prevnode(msg, tn->own_addr);
2312 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002313 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2314 }
2315 }
2316 rcu_read_unlock();
2317}
2318
Ying Xuee05b31f2015-01-09 15:27:08 +08002319static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002320{
Ying Xuee05b31f2015-01-09 15:27:08 +08002321 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002322 struct tipc_sock *tsk;
2323
2324 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002325 tsk = rhashtable_lookup(&tn->sk_rht, &portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002326 if (tsk)
2327 sock_hold(&tsk->sk);
2328 rcu_read_unlock();
2329
2330 return tsk;
2331}
2332
2333static int tipc_sk_insert(struct tipc_sock *tsk)
2334{
Ying Xuee05b31f2015-01-09 15:27:08 +08002335 struct sock *sk = &tsk->sk;
2336 struct net *net = sock_net(sk);
2337 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002338 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2339 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2340
2341 while (remaining--) {
2342 portid++;
2343 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2344 portid = TIPC_MIN_PORT;
2345 tsk->portid = portid;
2346 sock_hold(&tsk->sk);
Ying Xuee05b31f2015-01-09 15:27:08 +08002347 if (rhashtable_lookup_insert(&tn->sk_rht, &tsk->node))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002348 return 0;
2349 sock_put(&tsk->sk);
2350 }
2351
2352 return -1;
2353}
2354
2355static void tipc_sk_remove(struct tipc_sock *tsk)
2356{
2357 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002358 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002359
Ying Xuee05b31f2015-01-09 15:27:08 +08002360 if (rhashtable_remove(&tn->sk_rht, &tsk->node)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002361 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2362 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002363 }
2364}
2365
Ying Xuee05b31f2015-01-09 15:27:08 +08002366int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002367{
Ying Xuee05b31f2015-01-09 15:27:08 +08002368 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002369 struct rhashtable_params rht_params = {
2370 .nelem_hint = 192,
2371 .head_offset = offsetof(struct tipc_sock, node),
2372 .key_offset = offsetof(struct tipc_sock, portid),
2373 .key_len = sizeof(u32), /* portid */
2374 .hashfn = jhash,
2375 .max_shift = 20, /* 1M */
2376 .min_shift = 8, /* 256 */
2377 .grow_decision = rht_grow_above_75,
2378 .shrink_decision = rht_shrink_below_30,
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002379 };
2380
Ying Xuee05b31f2015-01-09 15:27:08 +08002381 return rhashtable_init(&tn->sk_rht, &rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002382}
2383
Ying Xuee05b31f2015-01-09 15:27:08 +08002384void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002385{
Ying Xuee05b31f2015-01-09 15:27:08 +08002386 struct tipc_net *tn = net_generic(net, tipc_net_id);
2387
Ying Xue07f6c4b2015-01-07 13:41:58 +08002388 /* Wait for socket readers to complete */
2389 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002390
Ying Xuee05b31f2015-01-09 15:27:08 +08002391 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002392}
2393
2394/**
Ying Xue247f0f32014-02-18 16:06:46 +08002395 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002396 * @sock: socket structure
2397 * @lvl: option level
2398 * @opt: option identifier
2399 * @ov: pointer to new option value
2400 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002401 *
2402 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002403 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002404 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002405 * Returns 0 on success, errno otherwise
2406 */
Ying Xue247f0f32014-02-18 16:06:46 +08002407static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2408 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002410 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002411 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002412 u32 value;
2413 int res;
2414
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002415 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2416 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002417 if (lvl != SOL_TIPC)
2418 return -ENOPROTOOPT;
2419 if (ol < sizeof(value))
2420 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002421 res = get_user(value, (u32 __user *)ov);
2422 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002423 return res;
2424
Allan Stephens0c3141e2008-04-15 00:22:02 -07002425 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002426
Per Lidenb97bf3f2006-01-02 19:04:38 +01002427 switch (opt) {
2428 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002429 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002430 break;
2431 case TIPC_SRC_DROPPABLE:
2432 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002433 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002434 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002435 res = -ENOPROTOOPT;
2436 break;
2437 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002438 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002439 break;
2440 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002441 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002442 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002443 break;
2444 default:
2445 res = -EINVAL;
2446 }
2447
Allan Stephens0c3141e2008-04-15 00:22:02 -07002448 release_sock(sk);
2449
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450 return res;
2451}
2452
2453/**
Ying Xue247f0f32014-02-18 16:06:46 +08002454 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002455 * @sock: socket structure
2456 * @lvl: option level
2457 * @opt: option identifier
2458 * @ov: receptacle for option value
2459 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002460 *
2461 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002462 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002463 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464 * Returns 0 on success, errno otherwise
2465 */
Ying Xue247f0f32014-02-18 16:06:46 +08002466static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2467 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002468{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002469 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002470 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002471 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002472 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002473 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002474
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002475 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2476 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002477 if (lvl != SOL_TIPC)
2478 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002479 res = get_user(len, ol);
2480 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002481 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002482
Allan Stephens0c3141e2008-04-15 00:22:02 -07002483 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002484
2485 switch (opt) {
2486 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002487 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002488 break;
2489 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002490 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002491 break;
2492 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002493 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002494 break;
2495 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002496 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002497 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002498 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002499 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002500 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002501 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002502 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002503 value = skb_queue_len(&sk->sk_receive_queue);
2504 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002505 default:
2506 res = -EINVAL;
2507 }
2508
Allan Stephens0c3141e2008-04-15 00:22:02 -07002509 release_sock(sk);
2510
Paul Gortmaker25860c32010-12-31 18:59:31 +00002511 if (res)
2512 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002513
Paul Gortmaker25860c32010-12-31 18:59:31 +00002514 if (len < sizeof(value))
2515 return -EINVAL;
2516
2517 if (copy_to_user(ov, &value, sizeof(value)))
2518 return -EFAULT;
2519
2520 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002521}
2522
Ying Xuef2f98002015-01-09 15:27:05 +08002523static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002524{
Ying Xuef2f98002015-01-09 15:27:05 +08002525 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002526 struct tipc_sioc_ln_req lnr;
2527 void __user *argp = (void __user *)arg;
2528
2529 switch (cmd) {
2530 case SIOCGETLINKNAME:
2531 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2532 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002533 if (!tipc_node_get_linkname(sock_net(sk),
2534 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002535 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2536 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2537 return -EFAULT;
2538 return 0;
2539 }
2540 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002541 default:
2542 return -ENOIOCTLCMD;
2543 }
2544}
2545
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002546/* Protocol switches for the various types of TIPC sockets */
2547
Florian Westphalbca65ea2008-02-07 18:18:01 -08002548static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002549 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002550 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002551 .release = tipc_release,
2552 .bind = tipc_bind,
2553 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002554 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002555 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002556 .getname = tipc_getname,
2557 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002558 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002559 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002560 .shutdown = tipc_shutdown,
2561 .setsockopt = tipc_setsockopt,
2562 .getsockopt = tipc_getsockopt,
2563 .sendmsg = tipc_sendmsg,
2564 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002565 .mmap = sock_no_mmap,
2566 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002567};
2568
Florian Westphalbca65ea2008-02-07 18:18:01 -08002569static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002570 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002571 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002572 .release = tipc_release,
2573 .bind = tipc_bind,
2574 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002575 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002576 .accept = tipc_accept,
2577 .getname = tipc_getname,
2578 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002579 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002580 .listen = tipc_listen,
2581 .shutdown = tipc_shutdown,
2582 .setsockopt = tipc_setsockopt,
2583 .getsockopt = tipc_getsockopt,
2584 .sendmsg = tipc_send_packet,
2585 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002586 .mmap = sock_no_mmap,
2587 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002588};
2589
Florian Westphalbca65ea2008-02-07 18:18:01 -08002590static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002591 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002592 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002593 .release = tipc_release,
2594 .bind = tipc_bind,
2595 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002596 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002597 .accept = tipc_accept,
2598 .getname = tipc_getname,
2599 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002600 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002601 .listen = tipc_listen,
2602 .shutdown = tipc_shutdown,
2603 .setsockopt = tipc_setsockopt,
2604 .getsockopt = tipc_getsockopt,
2605 .sendmsg = tipc_send_stream,
2606 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002607 .mmap = sock_no_mmap,
2608 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002609};
2610
Florian Westphalbca65ea2008-02-07 18:18:01 -08002611static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002612 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002613 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002614 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002615};
2616
2617static struct proto tipc_proto = {
2618 .name = "TIPC",
2619 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002620 .obj_size = sizeof(struct tipc_sock),
2621 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002622};
2623
Ying Xuec5fa7b32013-06-17 10:54:39 -04002624static struct proto tipc_proto_kern = {
2625 .name = "TIPC",
2626 .obj_size = sizeof(struct tipc_sock),
2627 .sysctl_rmem = sysctl_tipc_rmem
2628};
2629
Per Lidenb97bf3f2006-01-02 19:04:38 +01002630/**
Per Liden4323add2006-01-18 00:38:21 +01002631 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002632 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002633 * Returns 0 on success, errno otherwise
2634 */
Per Liden4323add2006-01-18 00:38:21 +01002635int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002636{
2637 int res;
2638
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002639 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002640 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002641 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002642 goto out;
2643 }
2644
2645 res = sock_register(&tipc_family_ops);
2646 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002647 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002648 proto_unregister(&tipc_proto);
2649 goto out;
2650 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002651 out:
2652 return res;
2653}
2654
2655/**
Per Liden4323add2006-01-18 00:38:21 +01002656 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002657 */
Per Liden4323add2006-01-18 00:38:21 +01002658void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002659{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002660 sock_unregister(tipc_family_ops.family);
2661 proto_unregister(&tipc_proto);
2662}
Richard Alpe34b78a122014-11-20 10:29:10 +01002663
2664/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002665static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002666{
2667 u32 peer_node;
2668 u32 peer_port;
2669 struct nlattr *nest;
2670
2671 peer_node = tsk_peer_node(tsk);
2672 peer_port = tsk_peer_port(tsk);
2673
2674 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2675
2676 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2677 goto msg_full;
2678 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2679 goto msg_full;
2680
2681 if (tsk->conn_type != 0) {
2682 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2683 goto msg_full;
2684 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2685 goto msg_full;
2686 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2687 goto msg_full;
2688 }
2689 nla_nest_end(skb, nest);
2690
2691 return 0;
2692
2693msg_full:
2694 nla_nest_cancel(skb, nest);
2695
2696 return -EMSGSIZE;
2697}
2698
2699/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002700static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2701 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002702{
2703 int err;
2704 void *hdr;
2705 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002706 struct net *net = sock_net(skb->sk);
2707 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002708
2709 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2710 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2711 if (!hdr)
2712 goto msg_cancel;
2713
2714 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2715 if (!attrs)
2716 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002717 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002718 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002719 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002720 goto attr_msg_cancel;
2721
2722 if (tsk->connected) {
2723 err = __tipc_nl_add_sk_con(skb, tsk);
2724 if (err)
2725 goto attr_msg_cancel;
2726 } else if (!list_empty(&tsk->publications)) {
2727 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2728 goto attr_msg_cancel;
2729 }
2730 nla_nest_end(skb, attrs);
2731 genlmsg_end(skb, hdr);
2732
2733 return 0;
2734
2735attr_msg_cancel:
2736 nla_nest_cancel(skb, attrs);
2737genlmsg_cancel:
2738 genlmsg_cancel(skb, hdr);
2739msg_cancel:
2740 return -EMSGSIZE;
2741}
2742
2743int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2744{
2745 int err;
2746 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002747 const struct bucket_table *tbl;
2748 struct rhash_head *pos;
2749 u32 prev_portid = cb->args[0];
2750 u32 portid = prev_portid;
Ying Xuee05b31f2015-01-09 15:27:08 +08002751 struct net *net = sock_net(skb->sk);
2752 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002753 int i;
Richard Alpe34b78a122014-11-20 10:29:10 +01002754
Ying Xue07f6c4b2015-01-07 13:41:58 +08002755 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002756 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002757 for (i = 0; i < tbl->size; i++) {
2758 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2759 spin_lock_bh(&tsk->sk.sk_lock.slock);
2760 portid = tsk->portid;
2761 err = __tipc_nl_add_sk(skb, cb, tsk);
2762 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2763 if (err)
2764 break;
Richard Alpe34b78a122014-11-20 10:29:10 +01002765
Ying Xue07f6c4b2015-01-07 13:41:58 +08002766 prev_portid = portid;
2767 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002768 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002769 rcu_read_unlock();
Richard Alpe34b78a122014-11-20 10:29:10 +01002770
Ying Xue07f6c4b2015-01-07 13:41:58 +08002771 cb->args[0] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002772
2773 return skb->len;
2774}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002775
2776/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002777static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2778 struct netlink_callback *cb,
2779 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002780{
2781 void *hdr;
2782 struct nlattr *attrs;
2783
2784 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2785 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2786 if (!hdr)
2787 goto msg_cancel;
2788
2789 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2790 if (!attrs)
2791 goto genlmsg_cancel;
2792
2793 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2794 goto attr_msg_cancel;
2795 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2796 goto attr_msg_cancel;
2797 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2798 goto attr_msg_cancel;
2799 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2800 goto attr_msg_cancel;
2801
2802 nla_nest_end(skb, attrs);
2803 genlmsg_end(skb, hdr);
2804
2805 return 0;
2806
2807attr_msg_cancel:
2808 nla_nest_cancel(skb, attrs);
2809genlmsg_cancel:
2810 genlmsg_cancel(skb, hdr);
2811msg_cancel:
2812 return -EMSGSIZE;
2813}
2814
2815/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002816static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2817 struct netlink_callback *cb,
2818 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002819{
2820 int err;
2821 struct publication *p;
2822
2823 if (*last_publ) {
2824 list_for_each_entry(p, &tsk->publications, pport_list) {
2825 if (p->key == *last_publ)
2826 break;
2827 }
2828 if (p->key != *last_publ) {
2829 /* We never set seq or call nl_dump_check_consistent()
2830 * this means that setting prev_seq here will cause the
2831 * consistence check to fail in the netlink callback
2832 * handler. Resulting in the last NLMSG_DONE message
2833 * having the NLM_F_DUMP_INTR flag set.
2834 */
2835 cb->prev_seq = 1;
2836 *last_publ = 0;
2837 return -EPIPE;
2838 }
2839 } else {
2840 p = list_first_entry(&tsk->publications, struct publication,
2841 pport_list);
2842 }
2843
2844 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2845 err = __tipc_nl_add_sk_publ(skb, cb, p);
2846 if (err) {
2847 *last_publ = p->key;
2848 return err;
2849 }
2850 }
2851 *last_publ = 0;
2852
2853 return 0;
2854}
2855
2856int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2857{
2858 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002859 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002860 u32 last_publ = cb->args[1];
2861 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002862 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002863 struct tipc_sock *tsk;
2864
Ying Xue07f6c4b2015-01-07 13:41:58 +08002865 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002866 struct nlattr **attrs;
2867 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2868
2869 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2870 if (err)
2871 return err;
2872
2873 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2874 attrs[TIPC_NLA_SOCK],
2875 tipc_nl_sock_policy);
2876 if (err)
2877 return err;
2878
2879 if (!sock[TIPC_NLA_SOCK_REF])
2880 return -EINVAL;
2881
Ying Xue07f6c4b2015-01-07 13:41:58 +08002882 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002883 }
2884
2885 if (done)
2886 return 0;
2887
Ying Xuee05b31f2015-01-09 15:27:08 +08002888 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002889 if (!tsk)
2890 return -EINVAL;
2891
2892 lock_sock(&tsk->sk);
2893 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2894 if (!err)
2895 done = 1;
2896 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002897 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002898
Ying Xue07f6c4b2015-01-07 13:41:58 +08002899 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002900 cb->args[1] = last_publ;
2901 cb->args[2] = done;
2902
2903 return skb->len;
2904}