blob: e16197eb7b9ff52af6a58abb68ebc67cb2553630 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Ying Xue07f6c4b2015-01-07 13:41:58 +080037#include <linux/rhashtable.h>
38#include <linux/jhash.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050040#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020041#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050042#include "link.h"
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -040043#include "config.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040044#include "socket.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040045
Ying Xue07f6c4b2015-01-07 13:41:58 +080046#define SS_LISTENING -1 /* socket is listening */
47#define SS_READY -2 /* socket is connectionless */
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
Ying Xue07f6c4b2015-01-07 13:41:58 +080049#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080050#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080051#define TIPC_FWD_MSG 1
52#define TIPC_CONN_OK 0
53#define TIPC_CONN_PROBING 1
54#define TIPC_MAX_PORT 0xffffffff
55#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040056
57/**
58 * struct tipc_sock - TIPC socket structure
59 * @sk: socket - interacts with 'port' and with user via the socket API
60 * @connected: non-zero if port is currently connected to a peer port
61 * @conn_type: TIPC type used when connection was established
62 * @conn_instance: TIPC instance used when connection was established
63 * @published: non-zero if port has one or more associated names
64 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080065 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040066 * @phdr: preformatted message header used when sending messages
67 * @port_list: adjacent ports in TIPC's global list of ports
68 * @publications: list of publications for port
69 * @pub_count: total # of publications port has made during its lifetime
70 * @probing_state:
Ying Xue2f55c432015-01-09 15:27:00 +080071 * @probing_intv:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040072 * @timer:
73 * @port: port - interacts with 'sk' and with the rest of the TIPC stack
74 * @peer_name: the peer of the connection, if any
75 * @conn_timeout: the time we can wait for an unresponded setup request
76 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
77 * @link_cong: non-zero if owner must sleep because of link congestion
78 * @sent_unacked: # messages sent by socket, and not yet acked by peer
79 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Ying Xue07f6c4b2015-01-07 13:41:58 +080080 * @node: hash table node
81 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040082 */
83struct tipc_sock {
84 struct sock sk;
85 int connected;
86 u32 conn_type;
87 u32 conn_instance;
88 int published;
89 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080090 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040091 struct tipc_msg phdr;
92 struct list_head sock_list;
93 struct list_head publications;
94 u32 pub_count;
95 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080096 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040097 struct timer_list timer;
98 uint conn_timeout;
99 atomic_t dupl_rcvcnt;
100 bool link_cong;
101 uint sent_unacked;
102 uint rcv_unacked;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800103 struct rhash_head node;
104 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400105};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400107static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400108static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800109static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800110static int tipc_release(struct socket *sock);
111static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400112static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800113static void tipc_sk_timeout(unsigned long portid);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400115 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400116static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400117 struct tipc_name_seq const *seq);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800118static struct tipc_sock *tipc_sk_lookup(u32 portid);
119static int tipc_sk_insert(struct tipc_sock *tsk);
120static void tipc_sk_remove(struct tipc_sock *tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121
Florian Westphalbca65ea2008-02-07 18:18:01 -0800122static const struct proto_ops packet_ops;
123static const struct proto_ops stream_ops;
124static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125
126static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400127static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128
Richard Alpe1a1a1432014-11-20 10:29:11 +0100129static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
130 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
131 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
132 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
133 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
134 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
135};
136
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900137/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138 * Revised TIPC socket locking policy:
139 *
140 * Most socket operations take the standard socket lock when they start
141 * and hold it until they finish (or until they need to sleep). Acquiring
142 * this lock grants the owner exclusive access to the fields of the socket
143 * data structures, with the exception of the backlog queue. A few socket
144 * operations can be done without taking the socket lock because they only
145 * read socket information that never changes during the life of the socket.
146 *
147 * Socket operations may acquire the lock for the associated TIPC port if they
148 * need to perform an operation on the port. If any routine needs to acquire
149 * both the socket lock and the port lock it must take the socket lock first
150 * to avoid the risk of deadlock.
151 *
152 * The dispatcher handling incoming messages cannot grab the socket lock in
153 * the standard fashion, since invoked it runs at the BH level and cannot block.
154 * Instead, it checks to see if the socket lock is currently owned by someone,
155 * and either handles the message itself or adds it to the socket's backlog
156 * queue; in the latter case the queued message is processed once the process
157 * owning the socket lock releases it.
158 *
159 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
160 * the problem of a blocked socket operation preventing any other operations
161 * from occurring. However, applications must be careful if they have
162 * multiple threads trying to send (or receive) on the same socket, as these
163 * operations might interfere with each other. For example, doing a connect
164 * and a receive at the same time might allow the receive to consume the
165 * ACK message meant for the connect. While additional work could be done
166 * to try and overcome this, it doesn't seem to be worthwhile at the present.
167 *
168 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
169 * that another operation that must be performed in a non-blocking manner is
170 * not delayed for very long because the lock has already been taken.
171 *
172 * NOTE: This code assumes that certain fields of a port/socket pair are
173 * constant over its lifetime; such fields can be examined without taking
174 * the socket lock and/or port lock, and do not need to be re-read even
175 * after resuming processing after waiting. These fields include:
176 * - socket type
177 * - pointer to socket sk structure (aka tipc_sock structure)
178 * - pointer to port structure
179 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181
Ying Xue07f6c4b2015-01-07 13:41:58 +0800182/* Protects tipc socket hash table mutations */
183static struct rhashtable tipc_sk_rht;
184
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400185static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400186{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400187 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400188}
189
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400190static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400191{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400192 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400193}
194
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400195static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400196{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400197 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400198}
199
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400200static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400201{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400202 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400203}
204
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400205static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400206{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400208}
209
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400210static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400211{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400212 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400213}
214
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400215static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400216{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400217 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400218}
219
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400220static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400221{
222 if (imp > TIPC_CRITICAL_IMPORTANCE)
223 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400224 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400225 return 0;
226}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400227
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400228static struct tipc_sock *tipc_sk(const struct sock *sk)
229{
230 return container_of(sk, struct tipc_sock, sk);
231}
232
233static int tsk_conn_cong(struct tipc_sock *tsk)
234{
235 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
236}
237
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400239 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700240 *
241 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400243static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400245 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246}
247
248/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400249 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700250 *
251 * Caller must hold socket lock
252 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400253static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700254{
Ying Xuea6ca1092014-11-26 11:41:55 +0800255 struct sk_buff *skb;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500256 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700257
Ying Xuea6ca1092014-11-26 11:41:55 +0800258 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) {
259 if (tipc_msg_reverse(skb, &dnode, TIPC_ERR_NO_PORT))
260 tipc_link_xmit_skb(skb, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500261 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700262}
263
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400264/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400265 *
266 * Handles cases where the node's network address has changed from
267 * the default of <0.0.0> to its configured setting.
268 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400269static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400270{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400271 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400272 u32 orig_node;
273 u32 peer_node;
274
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400275 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400276 return false;
277
278 if (unlikely(msg_origport(msg) != peer_port))
279 return false;
280
281 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400282 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400283
284 if (likely(orig_node == peer_node))
285 return true;
286
287 if (!orig_node && (peer_node == tipc_own_addr))
288 return true;
289
290 if (!peer_node && (orig_node == tipc_own_addr))
291 return true;
292
293 return false;
294}
295
Allan Stephens0c3141e2008-04-15 00:22:02 -0700296/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400297 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700298 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 * @sock: pre-allocated socket structure
300 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800301 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900302 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700303 * This routine creates additional data structures used by the TIPC socket,
304 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305 *
306 * Returns 0 on success, errno otherwise
307 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400308static int tipc_sk_create(struct net *net, struct socket *sock,
309 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700311 const struct proto_ops *ops;
312 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400314 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400315 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316
317 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 if (unlikely(protocol != 0))
319 return -EPROTONOSUPPORT;
320
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 switch (sock->type) {
322 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 ops = &stream_ops;
324 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 break;
326 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 ops = &packet_ops;
328 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 break;
330 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700332 ops = &msg_ops;
333 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 break;
Allan Stephens49978652006-06-25 23:47:18 -0700335 default:
Allan Stephens49978652006-06-25 23:47:18 -0700336 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 }
338
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400340 if (!kern)
341 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
342 else
343 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
344
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 if (sk == NULL)
346 return -ENOMEM;
347
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400348 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400349 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400350 INIT_LIST_HEAD(&tsk->publications);
351 msg = &tsk->phdr;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400352 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
353 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 sock->ops = ops;
357 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800359 if (tipc_sk_insert(tsk)) {
360 pr_warn("Socket create failed; port numbrer exhausted\n");
361 return -EINVAL;
362 }
363 msg_set_origport(msg, tsk->portid);
Ying Xue2f55c432015-01-09 15:27:00 +0800364 setup_timer(&tsk->timer, tipc_sk_timeout, tsk->portid);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400365 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400366 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800367 sk->sk_data_ready = tipc_data_ready;
368 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400369 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500370 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400371 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700372
Allan Stephens0c3141e2008-04-15 00:22:02 -0700373 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400374 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700375 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400376 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 return 0;
379}
380
381/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400382 * tipc_sock_create_local - create TIPC socket from inside TIPC module
383 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
384 *
385 * We cannot use sock_creat_kern here because it bumps module user count.
386 * Since socket owner and creator is the same module we must make sure
387 * that module count remains zero for module local sockets, otherwise
388 * we cannot do rmmod.
389 *
390 * Returns 0 on success, errno otherwise
391 */
392int tipc_sock_create_local(int type, struct socket **res)
393{
394 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400395
396 rc = sock_create_lite(AF_TIPC, type, 0, res);
397 if (rc < 0) {
398 pr_err("Failed to create kernel socket\n");
399 return rc;
400 }
401 tipc_sk_create(&init_net, *res, 0, 1);
402
Ying Xuec5fa7b32013-06-17 10:54:39 -0400403 return 0;
404}
405
406/**
407 * tipc_sock_release_local - release socket created by tipc_sock_create_local
408 * @sock: the socket to be released.
409 *
410 * Module reference count is not incremented when such sockets are created,
411 * so we must keep it from being decremented when they are released.
412 */
413void tipc_sock_release_local(struct socket *sock)
414{
Ying Xue247f0f32014-02-18 16:06:46 +0800415 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400416 sock->ops = NULL;
417 sock_release(sock);
418}
419
420/**
421 * tipc_sock_accept_local - accept a connection on a socket created
422 * with tipc_sock_create_local. Use this function to avoid that
423 * module reference count is inadvertently incremented.
424 *
425 * @sock: the accepting socket
426 * @newsock: reference to the new socket to be created
427 * @flags: socket flags
428 */
429
430int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400431 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400432{
433 struct sock *sk = sock->sk;
434 int ret;
435
436 ret = sock_create_lite(sk->sk_family, sk->sk_type,
437 sk->sk_protocol, newsock);
438 if (ret < 0)
439 return ret;
440
Ying Xue247f0f32014-02-18 16:06:46 +0800441 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400442 if (ret < 0) {
443 sock_release(*newsock);
444 return ret;
445 }
446 (*newsock)->ops = sock->ops;
447 return ret;
448}
449
Ying Xue07f6c4b2015-01-07 13:41:58 +0800450static void tipc_sk_callback(struct rcu_head *head)
451{
452 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
453
454 sock_put(&tsk->sk);
455}
456
Ying Xuec5fa7b32013-06-17 10:54:39 -0400457/**
Ying Xue247f0f32014-02-18 16:06:46 +0800458 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 * @sock: socket to destroy
460 *
461 * This routine cleans up any messages that are still queued on the socket.
462 * For DGRAM and RDM socket types, all queued messages are rejected.
463 * For SEQPACKET and STREAM socket types, the first message is rejected
464 * and any others are discarded. (If the first message on a STREAM socket
465 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900466 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 * NOTE: Rejected messages are not necessarily returned to the sender! They
468 * are returned or discarded according to the "destination droppable" setting
469 * specified for the message by the sender.
470 *
471 * Returns 0 on success, errno otherwise
472 */
Ying Xue247f0f32014-02-18 16:06:46 +0800473static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400476 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800477 struct sk_buff *skb;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500478 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479
Allan Stephens0c3141e2008-04-15 00:22:02 -0700480 /*
481 * Exit if socket isn't fully initialized (occurs when a failed accept()
482 * releases a pre-allocated child socket that was never used)
483 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700484 if (sk == NULL)
485 return 0;
486
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400487 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700488 lock_sock(sk);
489
490 /*
491 * Reject all unreceived messages, except on an active connection
492 * (which disconnects locally & sends a 'FIN+' to peer)
493 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400494 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800496 skb = __skb_dequeue(&sk->sk_receive_queue);
497 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 break;
Ying Xuea6ca1092014-11-26 11:41:55 +0800499 if (TIPC_SKB_CB(skb)->handle != NULL)
500 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700501 else {
502 if ((sock->state == SS_CONNECTING) ||
503 (sock->state == SS_CONNECTED)) {
504 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400505 tsk->connected = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800506 tipc_node_remove_conn(dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700507 }
Ying Xuea6ca1092014-11-26 11:41:55 +0800508 if (tipc_msg_reverse(skb, &dnode, TIPC_ERR_NO_PORT))
509 tipc_link_xmit_skb(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 Xue2f55c432015-01-09 15:27:00 +0800514 del_timer_sync(&tsk->timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800515 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400516 if (tsk->connected) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800517 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400518 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400519 tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800520 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800521 if (skb)
Ying Xue07f6c4b2015-01-07 13:41:58 +0800522 tipc_link_xmit_skb(skb, dnode, tsk->portid);
523 tipc_node_remove_conn(dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400524 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525
Allan Stephens0c3141e2008-04-15 00:22:02 -0700526 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100527 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528
Allan Stephens0c3141e2008-04-15 00:22:02 -0700529 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700530 sock->state = SS_DISCONNECTING;
531 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800532
533 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700534 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200536 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537}
538
539/**
Ying Xue247f0f32014-02-18 16:06:46 +0800540 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 * @sock: socket structure
542 * @uaddr: socket address describing name(s) and desired operation
543 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900544 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 * Name and name sequence binding is indicated using a positive scope value;
546 * a negative scope value unbinds the specified name. Specifying no name
547 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900548 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700550 *
551 * NOTE: This routine doesn't need to take the socket lock since it doesn't
552 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 */
Ying Xue247f0f32014-02-18 16:06:46 +0800554static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
555 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556{
Ying Xue84602762013-12-27 10:18:28 +0800557 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400559 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800560 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561
Ying Xue84602762013-12-27 10:18:28 +0800562 lock_sock(sk);
563 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400564 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800565 goto exit;
566 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900567
Ying Xue84602762013-12-27 10:18:28 +0800568 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
569 res = -EINVAL;
570 goto exit;
571 }
572 if (addr->family != AF_TIPC) {
573 res = -EAFNOSUPPORT;
574 goto exit;
575 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 if (addr->addrtype == TIPC_ADDR_NAME)
578 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800579 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
580 res = -EAFNOSUPPORT;
581 goto exit;
582 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900583
Ying Xue13a2e892013-06-17 10:54:40 -0400584 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400585 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800586 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
587 res = -EACCES;
588 goto exit;
589 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400590
Ying Xue84602762013-12-27 10:18:28 +0800591 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400592 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
593 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800594exit:
595 release_sock(sk);
596 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597}
598
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599/**
Ying Xue247f0f32014-02-18 16:06:46 +0800600 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 * @sock: socket structure
602 * @uaddr: area for returned socket address
603 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700604 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900605 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700607 *
Allan Stephens2da59912008-07-14 22:43:32 -0700608 * NOTE: This routine doesn't need to take the socket lock since it only
609 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000610 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 */
Ying Xue247f0f32014-02-18 16:06:46 +0800612static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
613 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400616 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000618 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700619 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700620 if ((sock->state != SS_CONNECTED) &&
621 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
622 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400623 addr->addr.id.ref = tsk_peer_port(tsk);
624 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700625 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800626 addr->addr.id.ref = tsk->portid;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000627 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700628 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629
630 *uaddr_len = sizeof(*addr);
631 addr->addrtype = TIPC_ADDR_ID;
632 addr->family = AF_TIPC;
633 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 addr->addr.name.domain = 0;
635
Allan Stephens0c3141e2008-04-15 00:22:02 -0700636 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637}
638
639/**
Ying Xue247f0f32014-02-18 16:06:46 +0800640 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 * @file: file structure associated with the socket
642 * @sock: socket for which to calculate the poll bits
643 * @wait: ???
644 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700645 * Returns pollmask value
646 *
647 * COMMENTARY:
648 * It appears that the usual socket locking mechanisms are not useful here
649 * since the pollmask info is potentially out-of-date the moment this routine
650 * exits. TCP and other protocols seem to rely on higher level poll routines
651 * to handle any preventable race conditions, so TIPC will do the same ...
652 *
653 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700654 *
Allan Stephensf662c072010-08-17 11:00:06 +0000655 * socket state flags set
656 * ------------ ---------
657 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200658 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000659 *
660 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
661 * no write flags
662 *
663 * connected POLLIN/POLLRDNORM if data in rx queue
664 * POLLOUT if port is not congested
665 *
666 * disconnecting POLLIN/POLLRDNORM/POLLHUP
667 * no write flags
668 *
669 * listening POLLIN if SYN in rx queue
670 * no write flags
671 *
672 * ready POLLIN/POLLRDNORM if data in rx queue
673 * [connectionless] POLLOUT (since port cannot be congested)
674 *
675 * IMPORTANT: The fact that a read or write operation is indicated does NOT
676 * imply that the operation will succeed, merely that it should be performed
677 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 */
Ying Xue247f0f32014-02-18 16:06:46 +0800679static unsigned int tipc_poll(struct file *file, struct socket *sock,
680 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681{
Allan Stephens9b674e82008-03-26 16:48:21 -0700682 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400683 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000684 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700685
Ying Xuef288bef2012-08-21 11:16:57 +0800686 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700687
Allan Stephensf662c072010-08-17 11:00:06 +0000688 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200689 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500690 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200691 mask |= POLLOUT;
692 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000693 case SS_READY:
694 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400695 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000696 mask |= POLLOUT;
697 /* fall thru' */
698 case SS_CONNECTING:
699 case SS_LISTENING:
700 if (!skb_queue_empty(&sk->sk_receive_queue))
701 mask |= (POLLIN | POLLRDNORM);
702 break;
703 case SS_DISCONNECTING:
704 mask = (POLLIN | POLLRDNORM | POLLHUP);
705 break;
706 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700707
708 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709}
710
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400711/**
712 * tipc_sendmcast - send multicast message
713 * @sock: socket structure
714 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500715 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400716 * @dsz: total length of message data
717 * @timeo: timeout to wait for wakeup
718 *
719 * Called from function tipc_sendmsg(), which has done all sanity checks
720 * Returns the number of bytes sent on success, or errno
721 */
722static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500723 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400724{
725 struct sock *sk = sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400726 struct tipc_msg *mhdr = &tipc_sk(sk)->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +0800727 struct sk_buff_head head;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400728 uint mtu;
729 int rc;
730
731 msg_set_type(mhdr, TIPC_MCAST_MSG);
732 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
733 msg_set_destport(mhdr, 0);
734 msg_set_destnode(mhdr, 0);
735 msg_set_nametype(mhdr, seq->type);
736 msg_set_namelower(mhdr, seq->lower);
737 msg_set_nameupper(mhdr, seq->upper);
738 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
739
740new_mtu:
741 mtu = tipc_bclink_get_mtu();
Ying Xuea6ca1092014-11-26 11:41:55 +0800742 __skb_queue_head_init(&head);
743 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400744 if (unlikely(rc < 0))
745 return rc;
746
747 do {
Ying Xuea6ca1092014-11-26 11:41:55 +0800748 rc = tipc_bclink_xmit(&head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400749 if (likely(rc >= 0)) {
750 rc = dsz;
751 break;
752 }
753 if (rc == -EMSGSIZE)
754 goto new_mtu;
755 if (rc != -ELINKCONG)
756 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400757 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400758 rc = tipc_wait_for_sndmsg(sock, &timeo);
759 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800760 __skb_queue_purge(&head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400761 } while (!rc);
762 return rc;
763}
764
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400765/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
766 */
767void tipc_sk_mcast_rcv(struct sk_buff *buf)
768{
769 struct tipc_msg *msg = buf_msg(buf);
770 struct tipc_port_list dports = {0, NULL, };
771 struct tipc_port_list *item;
772 struct sk_buff *b;
773 uint i, last, dst = 0;
774 u32 scope = TIPC_CLUSTER_SCOPE;
775
776 if (in_own_node(msg_orignode(msg)))
777 scope = TIPC_NODE_SCOPE;
778
779 /* Create destination port list: */
780 tipc_nametbl_mc_translate(msg_nametype(msg),
781 msg_namelower(msg),
782 msg_nameupper(msg),
783 scope,
784 &dports);
785 last = dports.count;
786 if (!last) {
787 kfree_skb(buf);
788 return;
789 }
790
791 for (item = &dports; item; item = item->next) {
792 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
793 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
794 if (!b) {
795 pr_warn("Failed do clone mcast rcv buffer\n");
796 continue;
797 }
798 msg_set_destport(msg, item->ports[i]);
799 tipc_sk_rcv(b);
800 }
801 }
802 tipc_port_list_free(&dports);
803}
804
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900805/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500806 * tipc_sk_proto_rcv - receive a connection mng protocol message
807 * @tsk: receiving socket
808 * @dnode: node to send response message to, if any
809 * @buf: buffer containing protocol message
810 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
811 * (CONN_PROBE_REPLY) message should be forwarded.
812 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800813static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
814 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500815{
816 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500817 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500818
819 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400820 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500821 goto exit;
822
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400823 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500824
825 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400826 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500827 tsk->sent_unacked -= msg_msgcnt(msg);
828 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400829 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500830 } else if (msg_type(msg) == CONN_PROBE) {
831 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
832 return TIPC_OK;
833 msg_set_type(msg, CONN_PROBE_REPLY);
834 return TIPC_FWD_MSG;
835 }
836 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
837exit:
838 kfree_skb(buf);
839 return TIPC_OK;
840}
841
Ying Xue3f405042014-01-17 09:50:05 +0800842static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
843{
844 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400845 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800846 DEFINE_WAIT(wait);
847 int done;
848
849 do {
850 int err = sock_error(sk);
851 if (err)
852 return err;
853 if (sock->state == SS_DISCONNECTING)
854 return -EPIPE;
855 if (!*timeo_p)
856 return -EAGAIN;
857 if (signal_pending(current))
858 return sock_intr_errno(*timeo_p);
859
860 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500861 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800862 finish_wait(sk_sleep(sk), &wait);
863 } while (!done);
864 return 0;
865}
866
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500867/**
Ying Xue247f0f32014-02-18 16:06:46 +0800868 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700869 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 * @sock: socket structure
871 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500872 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900875 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
877 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900878 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 * Returns the number of bytes sent on success, or errno otherwise
880 */
Ying Xue247f0f32014-02-18 16:06:46 +0800881static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500882 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500884 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700885 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400886 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400887 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500888 u32 dnode, dport;
Ying Xuea6ca1092014-11-26 11:41:55 +0800889 struct sk_buff_head head;
890 struct sk_buff *skb;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500891 struct tipc_name_seq *seq = &dest->addr.nameseq;
892 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800893 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100894 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895
896 if (unlikely(!dest))
897 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500898
Allan Stephens51f9cc12006-06-25 23:49:06 -0700899 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
900 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500902
903 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400904 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905
Allan Stephens0c3141e2008-04-15 00:22:02 -0700906 if (iocb)
907 lock_sock(sk);
908
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500909 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700910 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500911 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700912 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700913 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700914 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500915 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700916 goto exit;
917 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400918 if (tsk->published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500919 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700920 goto exit;
921 }
922 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400923 tsk->conn_type = dest->addr.name.name.type;
924 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700925 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 }
927
Ying Xue3f405042014-01-17 09:50:05 +0800928 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500929
930 if (dest->addrtype == TIPC_ADDR_MCAST) {
Al Viro562640f2014-11-15 01:13:43 -0500931 rc = tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500932 goto exit;
933 } else if (dest->addrtype == TIPC_ADDR_NAME) {
934 u32 type = dest->addr.name.name.type;
935 u32 inst = dest->addr.name.name.instance;
936 u32 domain = dest->addr.name.domain;
937
938 dnode = domain;
939 msg_set_type(mhdr, TIPC_NAMED_MSG);
940 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
941 msg_set_nametype(mhdr, type);
942 msg_set_nameinst(mhdr, inst);
943 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
944 dport = tipc_nametbl_translate(type, inst, &dnode);
945 msg_set_destnode(mhdr, dnode);
946 msg_set_destport(mhdr, dport);
947 if (unlikely(!dport && !dnode)) {
948 rc = -EHOSTUNREACH;
949 goto exit;
950 }
951 } else if (dest->addrtype == TIPC_ADDR_ID) {
952 dnode = dest->addr.id.node;
953 msg_set_type(mhdr, TIPC_DIRECT_MSG);
954 msg_set_lookup_scope(mhdr, 0);
955 msg_set_destnode(mhdr, dnode);
956 msg_set_destport(mhdr, dest->addr.id.ref);
957 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
958 }
959
960new_mtu:
Ying Xue07f6c4b2015-01-07 13:41:58 +0800961 mtu = tipc_node_get_mtu(dnode, tsk->portid);
Ying Xuea6ca1092014-11-26 11:41:55 +0800962 __skb_queue_head_init(&head);
963 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500964 if (rc < 0)
965 goto exit;
966
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900967 do {
Ying Xuea6ca1092014-11-26 11:41:55 +0800968 skb = skb_peek(&head);
969 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800970 rc = tipc_link_xmit(&head, dnode, tsk->portid);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500971 if (likely(rc >= 0)) {
972 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700973 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500974 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700975 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900976 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500977 if (rc == -EMSGSIZE)
978 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500979 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700980 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400981 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500982 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400983 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800984 __skb_queue_purge(&head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500985 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700986exit:
987 if (iocb)
988 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500989
990 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991}
992
Ying Xue391a6dd2014-01-17 09:50:06 +0800993static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
994{
995 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400996 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800997 DEFINE_WAIT(wait);
998 int done;
999
1000 do {
1001 int err = sock_error(sk);
1002 if (err)
1003 return err;
1004 if (sock->state == SS_DISCONNECTING)
1005 return -EPIPE;
1006 else if (sock->state != SS_CONNECTED)
1007 return -ENOTCONN;
1008 if (!*timeo_p)
1009 return -EAGAIN;
1010 if (signal_pending(current))
1011 return sock_intr_errno(*timeo_p);
1012
1013 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1014 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001015 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001016 !tsk_conn_cong(tsk)) ||
1017 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +08001018 finish_wait(sk_sleep(sk), &wait);
1019 } while (!done);
1020 return 0;
1021}
1022
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001023/**
Ying Xue247f0f32014-02-18 16:06:46 +08001024 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 * @iocb: (unused)
1026 * @sock: socket structure
1027 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001028 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031 *
1032 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001033 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 */
Ying Xue247f0f32014-02-18 16:06:46 +08001035static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001036 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001038 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001039 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001040 struct tipc_msg *mhdr = &tsk->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +08001041 struct sk_buff_head head;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001042 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001043 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001044 int rc = -EINVAL;
1045 long timeo;
1046 u32 dnode;
1047 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001048
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001049 /* Handle implied connection establishment */
1050 if (unlikely(dest)) {
1051 rc = tipc_sendmsg(iocb, sock, m, dsz);
1052 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001053 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001054 return rc;
1055 }
1056 if (dsz > (uint)INT_MAX)
1057 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001058
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001059 if (iocb)
1060 lock_sock(sk);
1061
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001062 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001063 if (sock->state == SS_DISCONNECTING)
1064 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001065 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001066 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +08001067 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001068 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001070 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001071 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001072
1073next:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001074 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001075 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001076 __skb_queue_head_init(&head);
1077 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001078 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001079 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001080 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001081 if (likely(!tsk_conn_cong(tsk))) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08001082 rc = tipc_link_xmit(&head, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001083 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001084 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001085 sent += send;
1086 if (sent == dsz)
1087 break;
1088 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001089 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001090 if (rc == -EMSGSIZE) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08001091 tsk->max_pkt = tipc_node_get_mtu(dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001092 goto next;
1093 }
1094 if (rc != -ELINKCONG)
1095 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001096 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001098 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001099 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +08001100 __skb_queue_purge(&head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001101 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001102exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001103 if (iocb)
1104 release_sock(sk);
1105 return sent ? sent : rc;
1106}
1107
1108/**
1109 * tipc_send_packet - send a connection-oriented message
1110 * @iocb: if NULL, indicates that socket lock is already held
1111 * @sock: socket structure
1112 * @m: message to send
1113 * @dsz: length of data to be transmitted
1114 *
1115 * Used for SOCK_SEQPACKET messages.
1116 *
1117 * Returns the number of bytes sent on success, or errno otherwise
1118 */
1119static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1120 struct msghdr *m, size_t dsz)
1121{
1122 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1123 return -EMSGSIZE;
1124
1125 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126}
1127
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001128/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001130static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001131 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132{
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001133 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001135 msg_set_destnode(msg, peer_node);
1136 msg_set_destport(msg, peer_port);
1137 msg_set_type(msg, TIPC_CONN_MSG);
1138 msg_set_lookup_scope(msg, 0);
1139 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001140
Ying Xue2f55c432015-01-09 15:27:00 +08001141 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001142 tsk->probing_state = TIPC_CONN_OK;
1143 tsk->connected = 1;
Ying Xue2f55c432015-01-09 15:27:00 +08001144 mod_timer(&tsk->timer, jiffies + tsk->probing_intv);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001145 tipc_node_add_conn(peer_node, tsk->portid, peer_port);
1146 tsk->max_pkt = tipc_node_get_mtu(peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147}
1148
1149/**
1150 * set_orig_addr - capture sender's address for received message
1151 * @m: descriptor for message info
1152 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001153 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 * Note: Address is not captured if not requested by receiver.
1155 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001156static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001158 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001160 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 addr->family = AF_TIPC;
1162 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001163 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 addr->addr.id.ref = msg_origport(msg);
1165 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001166 addr->addr.name.domain = 0; /* could leave uninitialized */
1167 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 m->msg_namelen = sizeof(struct sockaddr_tipc);
1169 }
1170}
1171
1172/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001173 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 * @m: descriptor for message info
1175 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001176 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001177 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001179 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 * Returns 0 if successful, otherwise errno
1181 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001182static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1183 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184{
1185 u32 anc_data[3];
1186 u32 err;
1187 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001188 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 int res;
1190
1191 if (likely(m->msg_controllen == 0))
1192 return 0;
1193
1194 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 err = msg ? msg_errcode(msg) : 0;
1196 if (unlikely(err)) {
1197 anc_data[0] = err;
1198 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001199 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1200 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001202 if (anc_data[1]) {
1203 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1204 msg_data(msg));
1205 if (res)
1206 return res;
1207 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 }
1209
1210 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1212 switch (dest_type) {
1213 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001214 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 anc_data[0] = msg_nametype(msg);
1216 anc_data[1] = msg_namelower(msg);
1217 anc_data[2] = msg_namelower(msg);
1218 break;
1219 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001220 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 anc_data[0] = msg_nametype(msg);
1222 anc_data[1] = msg_namelower(msg);
1223 anc_data[2] = msg_nameupper(msg);
1224 break;
1225 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001226 has_name = (tsk->conn_type != 0);
1227 anc_data[0] = tsk->conn_type;
1228 anc_data[1] = tsk->conn_instance;
1229 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 break;
1231 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001232 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 }
Allan Stephens2db99832010-12-31 18:59:33 +00001234 if (has_name) {
1235 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1236 if (res)
1237 return res;
1238 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239
1240 return 0;
1241}
1242
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001243static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001244{
Ying Xuea6ca1092014-11-26 11:41:55 +08001245 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001246 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001247 u32 peer_port = tsk_peer_port(tsk);
1248 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001249
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001250 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001251 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001252 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
Ying Xue07f6c4b2015-01-07 13:41:58 +08001253 tipc_own_addr, peer_port, tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001254 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001255 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001256 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001257 msg_set_msgcnt(msg, ack);
Ying Xuea6ca1092014-11-26 11:41:55 +08001258 tipc_link_xmit_skb(skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001259}
1260
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001261static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001262{
1263 struct sock *sk = sock->sk;
1264 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001265 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001266 int err;
1267
1268 for (;;) {
1269 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001270 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001271 if (sock->state == SS_DISCONNECTING) {
1272 err = -ENOTCONN;
1273 break;
1274 }
1275 release_sock(sk);
1276 timeo = schedule_timeout(timeo);
1277 lock_sock(sk);
1278 }
1279 err = 0;
1280 if (!skb_queue_empty(&sk->sk_receive_queue))
1281 break;
1282 err = sock_intr_errno(timeo);
1283 if (signal_pending(current))
1284 break;
1285 err = -EAGAIN;
1286 if (!timeo)
1287 break;
1288 }
1289 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001290 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001291 return err;
1292}
1293
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001294/**
Ying Xue247f0f32014-02-18 16:06:46 +08001295 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 * @iocb: (unused)
1297 * @m: descriptor for message info
1298 * @buf_len: total size of user buffer area
1299 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001300 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001301 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1302 * If the complete message doesn't fit in user area, truncate it.
1303 *
1304 * Returns size of returned message data, errno otherwise
1305 */
Ying Xue247f0f32014-02-18 16:06:46 +08001306static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1307 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001309 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001310 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 struct sk_buff *buf;
1312 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001313 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 unsigned int sz;
1315 u32 err;
1316 int res;
1317
Allan Stephens0c3141e2008-04-15 00:22:02 -07001318 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 if (unlikely(!buf_len))
1320 return -EINVAL;
1321
Allan Stephens0c3141e2008-04-15 00:22:02 -07001322 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323
Allan Stephens0c3141e2008-04-15 00:22:02 -07001324 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 res = -ENOTCONN;
1326 goto exit;
1327 }
1328
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001329 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001330restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331
Allan Stephens0c3141e2008-04-15 00:22:02 -07001332 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001333 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001334 if (res)
1335 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001336
1337 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001338 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 msg = buf_msg(buf);
1340 sz = msg_data_sz(msg);
1341 err = msg_errcode(msg);
1342
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001345 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 goto restart;
1347 }
1348
1349 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 set_orig_addr(m, msg);
1351
1352 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001353 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001354 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 goto exit;
1356
1357 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 if (!err) {
1359 if (unlikely(buf_len < sz)) {
1360 sz = buf_len;
1361 m->msg_flags |= MSG_TRUNC;
1362 }
David S. Miller51f3d022014-11-05 16:46:40 -05001363 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001364 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 res = sz;
1367 } else {
1368 if ((sock->state == SS_READY) ||
1369 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1370 res = 0;
1371 else
1372 res = -ECONNRESET;
1373 }
1374
1375 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001377 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001378 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001379 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001380 tsk->rcv_unacked = 0;
1381 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001382 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001383 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001385 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 return res;
1387}
1388
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001389/**
Ying Xue247f0f32014-02-18 16:06:46 +08001390 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391 * @iocb: (unused)
1392 * @m: descriptor for message info
1393 * @buf_len: total size of user buffer area
1394 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001395 *
1396 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 * will optionally wait for more; never truncates data.
1398 *
1399 * Returns size of returned message data, errno otherwise
1400 */
Ying Xue247f0f32014-02-18 16:06:46 +08001401static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1402 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001404 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001405 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 struct sk_buff *buf;
1407 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001408 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001410 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001411 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001413 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001414
1415 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 if (unlikely(!buf_len))
1417 return -EINVAL;
1418
Allan Stephens0c3141e2008-04-15 00:22:02 -07001419 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001421 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 res = -ENOTCONN;
1423 goto exit;
1424 }
1425
Florian Westphal3720d402010-08-17 11:00:04 +00001426 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001427 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001428
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001430 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001431 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001432 if (res)
1433 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001434
1435 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001436 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437 msg = buf_msg(buf);
1438 sz = msg_data_sz(msg);
1439 err = msg_errcode(msg);
1440
1441 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001443 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 goto restart;
1445 }
1446
1447 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 if (sz_copied == 0) {
1449 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001450 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001451 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 goto exit;
1453 }
1454
1455 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001457 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001458
Allan Stephens0232fd02011-02-21 09:45:40 -05001459 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 needed = (buf_len - sz_copied);
1461 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001462
David S. Miller51f3d022014-11-05 16:46:40 -05001463 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1464 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001465 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001467
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 sz_copied += sz_to_copy;
1469
1470 if (sz_to_copy < sz) {
1471 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001472 TIPC_SKB_CB(buf)->handle =
1473 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474 goto exit;
1475 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 } else {
1477 if (sz_copied != 0)
1478 goto exit; /* can't add error msg to valid data */
1479
1480 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1481 res = 0;
1482 else
1483 res = -ECONNRESET;
1484 }
1485
1486 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001488 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001489 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001490 tsk->rcv_unacked = 0;
1491 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001492 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001493 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494
1495 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001496 if ((sz_copied < buf_len) && /* didn't get all requested data */
1497 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001498 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001499 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1500 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501 goto restart;
1502
1503exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001504 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001505 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506}
1507
1508/**
Ying Xuef288bef2012-08-21 11:16:57 +08001509 * tipc_write_space - wake up thread if port congestion is released
1510 * @sk: socket
1511 */
1512static void tipc_write_space(struct sock *sk)
1513{
1514 struct socket_wq *wq;
1515
1516 rcu_read_lock();
1517 wq = rcu_dereference(sk->sk_wq);
1518 if (wq_has_sleeper(wq))
1519 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1520 POLLWRNORM | POLLWRBAND);
1521 rcu_read_unlock();
1522}
1523
1524/**
1525 * tipc_data_ready - wake up threads to indicate messages have been received
1526 * @sk: socket
1527 * @len: the length of messages
1528 */
David S. Miller676d2362014-04-11 16:15:36 -04001529static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001530{
1531 struct socket_wq *wq;
1532
1533 rcu_read_lock();
1534 wq = rcu_dereference(sk->sk_wq);
1535 if (wq_has_sleeper(wq))
1536 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1537 POLLRDNORM | POLLRDBAND);
1538 rcu_read_unlock();
1539}
1540
1541/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001542 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001543 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001544 * @msg: message
1545 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001546 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001547 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001548static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001549{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001550 struct sock *sk = &tsk->sk;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001551 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001552 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001553 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001554
1555 if (msg_mcast(msg))
1556 return retval;
1557
1558 switch ((int)sock->state) {
1559 case SS_CONNECTED:
1560 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001561 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001562 if (unlikely(msg_errcode(msg))) {
1563 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001564 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001565 /* let timer expire on it's own */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001566 tipc_node_remove_conn(tsk_peer_node(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08001567 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001568 }
1569 retval = TIPC_OK;
1570 }
1571 break;
1572 case SS_CONNECTING:
1573 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001574
1575 if (unlikely(!msg_connected(msg)))
1576 break;
1577
Ying Xue584d24b2012-11-29 18:51:19 -05001578 if (unlikely(msg_errcode(msg))) {
1579 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001580 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001581 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001582 break;
1583 }
1584
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001585 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001586 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001587 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001588 retval = TIPC_OK;
1589 break;
1590 }
1591
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001592 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1593 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001594 sock->state = SS_CONNECTED;
1595
Ying Xue584d24b2012-11-29 18:51:19 -05001596 /* If an incoming message is an 'ACK-', it should be
1597 * discarded here because it doesn't contain useful
1598 * data. In addition, we should try to wake up
1599 * connect() routine if sleeping.
1600 */
1601 if (msg_data_sz(msg) == 0) {
1602 kfree_skb(*buf);
1603 *buf = NULL;
1604 if (waitqueue_active(sk_sleep(sk)))
1605 wake_up_interruptible(sk_sleep(sk));
1606 }
1607 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001608 break;
1609 case SS_LISTENING:
1610 case SS_UNCONNECTED:
1611 /* Accept only SYN message */
1612 if (!msg_connected(msg) && !(msg_errcode(msg)))
1613 retval = TIPC_OK;
1614 break;
1615 case SS_DISCONNECTING:
1616 break;
1617 default:
1618 pr_err("Unknown socket state %u\n", sock->state);
1619 }
1620 return retval;
1621}
1622
1623/**
Ying Xueaba79f32013-01-20 23:30:09 +01001624 * rcvbuf_limit - get proper overload limit of socket receive queue
1625 * @sk: socket
1626 * @buf: message
1627 *
1628 * For all connection oriented messages, irrespective of importance,
1629 * the default overload value (i.e. 67MB) is set as limit.
1630 *
1631 * For all connectionless messages, by default new queue limits are
1632 * as belows:
1633 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001634 * TIPC_LOW_IMPORTANCE (4 MB)
1635 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1636 * TIPC_HIGH_IMPORTANCE (16 MB)
1637 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001638 *
1639 * Returns overload limit according to corresponding message importance
1640 */
1641static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1642{
1643 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001644
1645 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001646 return sysctl_tipc_rmem[2];
1647
1648 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1649 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001650}
1651
1652/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001653 * filter_rcv - validate incoming message
1654 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001656 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001657 * Enqueues message on receive queue if acceptable; optionally handles
1658 * disconnect indication for a connected socket.
1659 *
1660 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001661 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001662 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001663 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001664 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001665static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001667 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001668 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001670 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001671 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001672 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001674 if (unlikely(msg_user(msg) == CONN_MANAGER))
1675 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001676
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001677 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1678 kfree_skb(buf);
1679 tsk->link_cong = 0;
1680 sk->sk_write_space(sk);
1681 return TIPC_OK;
1682 }
1683
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001685 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001686 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001687
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001689 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001690 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001692 rc = filter_connect(tsk, &buf);
1693 if (rc != TIPC_OK || buf == NULL)
1694 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695 }
1696
1697 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001698 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001699 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700
Ying Xueaba79f32013-01-20 23:30:09 +01001701 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001702 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001703 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001704 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001705
David S. Miller676d2362014-04-11 16:15:36 -04001706 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 return TIPC_OK;
1708}
1709
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001710/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001711 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001712 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001713 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001714 *
1715 * Caller must hold socket lock, but not port lock.
1716 *
1717 * Returns 0
1718 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001719static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001720{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001721 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001722 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001723 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001724 uint truesize = skb->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725
Ying Xuea6ca1092014-11-26 11:41:55 +08001726 rc = filter_rcv(sk, skb);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001727
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001728 if (likely(!rc)) {
1729 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1730 atomic_add(truesize, &tsk->dupl_rcvcnt);
1731 return 0;
1732 }
1733
Ying Xuea6ca1092014-11-26 11:41:55 +08001734 if ((rc < 0) && !tipc_msg_reverse(skb, &onode, -rc))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001735 return 0;
1736
Ying Xuea6ca1092014-11-26 11:41:55 +08001737 tipc_link_xmit_skb(skb, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001738
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 return 0;
1740}
1741
1742/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001743 * tipc_sk_rcv - handle incoming message
Ying Xuea6ca1092014-11-26 11:41:55 +08001744 * @skb: buffer containing arriving message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001745 * Consumes buffer
1746 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001747 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001748int tipc_sk_rcv(struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001749{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001750 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001751 struct sock *sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001752 u32 dport = msg_destport(buf_msg(skb));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001753 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001754 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001755 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001756
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001757 /* Validate destination and message */
Ying Xue07f6c4b2015-01-07 13:41:58 +08001758 tsk = tipc_sk_lookup(dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001759 if (unlikely(!tsk)) {
Ying Xuea6ca1092014-11-26 11:41:55 +08001760 rc = tipc_msg_eval(skb, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001761 goto exit;
1762 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001763 sk = &tsk->sk;
1764
1765 /* Queue message */
Ying Xue1a194c22014-10-20 14:46:35 +08001766 spin_lock_bh(&sk->sk_lock.slock);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001767
Allan Stephens0c3141e2008-04-15 00:22:02 -07001768 if (!sock_owned_by_user(sk)) {
Ying Xuea6ca1092014-11-26 11:41:55 +08001769 rc = filter_rcv(sk, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001770 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001771 if (sk->sk_backlog.len == 0)
1772 atomic_set(&tsk->dupl_rcvcnt, 0);
Ying Xuea6ca1092014-11-26 11:41:55 +08001773 limit = rcvbuf_limit(sk, skb) + atomic_read(&tsk->dupl_rcvcnt);
1774 if (sk_add_backlog(sk, skb, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001775 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001776 }
Ying Xue1a194c22014-10-20 14:46:35 +08001777 spin_unlock_bh(&sk->sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001778 sock_put(sk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001779 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001780 return 0;
1781exit:
Ying Xuea6ca1092014-11-26 11:41:55 +08001782 if ((rc < 0) && !tipc_msg_reverse(skb, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001783 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001784
Ying Xuea6ca1092014-11-26 11:41:55 +08001785 tipc_link_xmit_skb(skb, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001786 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001787}
1788
Ying Xue78eb3a52014-01-17 09:50:03 +08001789static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1790{
1791 struct sock *sk = sock->sk;
1792 DEFINE_WAIT(wait);
1793 int done;
1794
1795 do {
1796 int err = sock_error(sk);
1797 if (err)
1798 return err;
1799 if (!*timeo_p)
1800 return -ETIMEDOUT;
1801 if (signal_pending(current))
1802 return sock_intr_errno(*timeo_p);
1803
1804 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1805 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1806 finish_wait(sk_sleep(sk), &wait);
1807 } while (!done);
1808 return 0;
1809}
1810
Per Lidenb97bf3f2006-01-02 19:04:38 +01001811/**
Ying Xue247f0f32014-02-18 16:06:46 +08001812 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813 * @sock: socket structure
1814 * @dest: socket address for destination port
1815 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001816 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817 *
1818 * Returns 0 on success, errno otherwise
1819 */
Ying Xue247f0f32014-02-18 16:06:46 +08001820static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1821 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001823 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001824 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1825 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001826 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1827 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001828 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829
Allan Stephens0c3141e2008-04-15 00:22:02 -07001830 lock_sock(sk);
1831
Allan Stephensb89741a2008-04-15 00:20:37 -07001832 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001833 if (sock->state == SS_READY) {
1834 res = -EOPNOTSUPP;
1835 goto exit;
1836 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837
Allan Stephensb89741a2008-04-15 00:20:37 -07001838 /*
1839 * Reject connection attempt using multicast address
1840 *
1841 * Note: send_msg() validates the rest of the address fields,
1842 * so there's no need to do it here
1843 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001844 if (dst->addrtype == TIPC_ADDR_MCAST) {
1845 res = -EINVAL;
1846 goto exit;
1847 }
1848
Ying Xue78eb3a52014-01-17 09:50:03 +08001849 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001850 switch (sock->state) {
1851 case SS_UNCONNECTED:
1852 /* Send a 'SYN-' to destination */
1853 m.msg_name = dest;
1854 m.msg_namelen = destlen;
1855
1856 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1857 * indicate send_msg() is never blocked.
1858 */
1859 if (!timeout)
1860 m.msg_flags = MSG_DONTWAIT;
1861
Ying Xue247f0f32014-02-18 16:06:46 +08001862 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001863 if ((res < 0) && (res != -EWOULDBLOCK))
1864 goto exit;
1865
1866 /* Just entered SS_CONNECTING state; the only
1867 * difference is that return value in non-blocking
1868 * case is EINPROGRESS, rather than EALREADY.
1869 */
1870 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001871 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001872 if (previous == SS_CONNECTING)
1873 res = -EALREADY;
1874 if (!timeout)
1875 goto exit;
1876 timeout = msecs_to_jiffies(timeout);
1877 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1878 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001879 break;
1880 case SS_CONNECTED:
1881 res = -EISCONN;
1882 break;
1883 default:
1884 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001885 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001886 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001887exit:
1888 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001889 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890}
1891
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001892/**
Ying Xue247f0f32014-02-18 16:06:46 +08001893 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894 * @sock: socket structure
1895 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001896 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897 * Returns 0 on success, errno otherwise
1898 */
Ying Xue247f0f32014-02-18 16:06:46 +08001899static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001901 struct sock *sk = sock->sk;
1902 int res;
1903
1904 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905
Ying Xue245f3d32011-07-06 06:01:13 -04001906 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001907 res = -EINVAL;
1908 else {
1909 sock->state = SS_LISTENING;
1910 res = 0;
1911 }
1912
1913 release_sock(sk);
1914 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915}
1916
Ying Xue6398e232014-01-17 09:50:04 +08001917static int tipc_wait_for_accept(struct socket *sock, long timeo)
1918{
1919 struct sock *sk = sock->sk;
1920 DEFINE_WAIT(wait);
1921 int err;
1922
1923 /* True wake-one mechanism for incoming connections: only
1924 * one process gets woken up, not the 'whole herd'.
1925 * Since we do not 'race & poll' for established sockets
1926 * anymore, the common case will execute the loop only once.
1927 */
1928 for (;;) {
1929 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1930 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001931 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001932 release_sock(sk);
1933 timeo = schedule_timeout(timeo);
1934 lock_sock(sk);
1935 }
1936 err = 0;
1937 if (!skb_queue_empty(&sk->sk_receive_queue))
1938 break;
1939 err = -EINVAL;
1940 if (sock->state != SS_LISTENING)
1941 break;
1942 err = sock_intr_errno(timeo);
1943 if (signal_pending(current))
1944 break;
1945 err = -EAGAIN;
1946 if (!timeo)
1947 break;
1948 }
1949 finish_wait(sk_sleep(sk), &wait);
1950 return err;
1951}
1952
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001953/**
Ying Xue247f0f32014-02-18 16:06:46 +08001954 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955 * @sock: listening socket
1956 * @newsock: new socket that is to be connected
1957 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001958 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959 * Returns 0 on success, errno otherwise
1960 */
Ying Xue247f0f32014-02-18 16:06:46 +08001961static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001962{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001963 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001965 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001966 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001967 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001968 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001969
Allan Stephens0c3141e2008-04-15 00:22:02 -07001970 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001971
Allan Stephens0c3141e2008-04-15 00:22:02 -07001972 if (sock->state != SS_LISTENING) {
1973 res = -EINVAL;
1974 goto exit;
1975 }
Ying Xue6398e232014-01-17 09:50:04 +08001976 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1977 res = tipc_wait_for_accept(sock, timeo);
1978 if (res)
1979 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001980
1981 buf = skb_peek(&sk->sk_receive_queue);
1982
Ying Xuec5fa7b32013-06-17 10:54:39 -04001983 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001984 if (res)
1985 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001986
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001987 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001988 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001989 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001990
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001991 /* we lock on new_sk; but lockdep sees the lock on sk */
1992 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001993
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001994 /*
1995 * Reject any stray messages received by new socket
1996 * before the socket lock was taken (very, very unlikely)
1997 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001998 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002000 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002001 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002002 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002003
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002004 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002005 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002006 new_tsock->conn_type = msg_nametype(msg);
2007 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002008 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002009
2010 /*
2011 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2012 * Respond to 'SYN+' by queuing it on new socket.
2013 */
2014 if (!msg_data_sz(msg)) {
2015 struct msghdr m = {NULL,};
2016
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002017 tsk_advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08002018 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002019 } else {
2020 __skb_dequeue(&sk->sk_receive_queue);
2021 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002022 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002023 }
2024 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002025exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002026 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027 return res;
2028}
2029
2030/**
Ying Xue247f0f32014-02-18 16:06:46 +08002031 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002032 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002033 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034 *
2035 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002036 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002037 * Returns 0 on success, errno otherwise
2038 */
Ying Xue247f0f32014-02-18 16:06:46 +08002039static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002040{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002041 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002042 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002043 struct sk_buff *skb;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002044 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002045 int res;
2046
Allan Stephense247a8f2008-03-06 15:05:38 -08002047 if (how != SHUT_RDWR)
2048 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002049
Allan Stephens0c3141e2008-04-15 00:22:02 -07002050 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051
2052 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002053 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002054 case SS_CONNECTED:
2055
Per Lidenb97bf3f2006-01-02 19:04:38 +01002056restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002057 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002058 skb = __skb_dequeue(&sk->sk_receive_queue);
2059 if (skb) {
2060 if (TIPC_SKB_CB(skb)->handle != NULL) {
2061 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002062 goto restart;
2063 }
Ying Xuea6ca1092014-11-26 11:41:55 +08002064 if (tipc_msg_reverse(skb, &dnode, TIPC_CONN_SHUTDOWN))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002065 tipc_link_xmit_skb(skb, dnode, tsk->portid);
2066 tipc_node_remove_conn(dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002067 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002068 dnode = tsk_peer_node(tsk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002069 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002070 TIPC_CONN_MSG, SHORT_H_SIZE,
2071 0, dnode, tipc_own_addr,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002072 tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002073 tsk->portid, TIPC_CONN_SHUTDOWN);
2074 tipc_link_xmit_skb(skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002076 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002077 sock->state = SS_DISCONNECTING;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002078 tipc_node_remove_conn(dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002079 /* fall through */
2080
2081 case SS_DISCONNECTING:
2082
Ying Xue75031152012-10-29 09:38:15 -04002083 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002084 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002085
2086 /* Wake up anyone sleeping in poll */
2087 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002088 res = 0;
2089 break;
2090
2091 default:
2092 res = -ENOTCONN;
2093 }
2094
Allan Stephens0c3141e2008-04-15 00:22:02 -07002095 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096 return res;
2097}
2098
Ying Xue07f6c4b2015-01-07 13:41:58 +08002099static void tipc_sk_timeout(unsigned long portid)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002100{
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002101 struct tipc_sock *tsk;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002102 struct sock *sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002103 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002104 u32 peer_port, peer_node;
2105
Ying Xue07f6c4b2015-01-07 13:41:58 +08002106 tsk = tipc_sk_lookup(portid);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04002107 if (!tsk)
Ying Xuecc086fc2014-08-28 10:02:41 +08002108 return;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002109
Ying Xuecc086fc2014-08-28 10:02:41 +08002110 sk = &tsk->sk;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002111 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002112 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002113 bh_unlock_sock(sk);
2114 goto exit;
2115 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002116 peer_port = tsk_peer_port(tsk);
2117 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002118
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002119 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002120 /* Previous probe not answered -> self abort */
Ying Xuea6ca1092014-11-26 11:41:55 +08002121 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy57289012014-08-22 18:09:09 -04002122 SHORT_H_SIZE, 0, tipc_own_addr,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002123 peer_node, portid, peer_port,
Jon Paul Maloy57289012014-08-22 18:09:09 -04002124 TIPC_ERR_NO_PORT);
2125 } else {
Ying Xuea6ca1092014-11-26 11:41:55 +08002126 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
Jon Paul Maloy57289012014-08-22 18:09:09 -04002127 0, peer_node, tipc_own_addr,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002128 peer_port, portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002129 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue2f55c432015-01-09 15:27:00 +08002130 mod_timer(&tsk->timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002131 }
2132 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002133 if (skb)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002134 tipc_link_xmit_skb(skb, peer_node, portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002135exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002136 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002137}
2138
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002139static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002140 struct tipc_name_seq const *seq)
2141{
2142 struct publication *publ;
2143 u32 key;
2144
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002145 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002146 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002147 key = tsk->portid + tsk->pub_count + 1;
2148 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002149 return -EADDRINUSE;
2150
2151 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002152 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002153 if (unlikely(!publ))
2154 return -EINVAL;
2155
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002156 list_add(&publ->pport_list, &tsk->publications);
2157 tsk->pub_count++;
2158 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002159 return 0;
2160}
2161
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002162static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002163 struct tipc_name_seq const *seq)
2164{
2165 struct publication *publ;
2166 struct publication *safe;
2167 int rc = -EINVAL;
2168
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002169 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002170 if (seq) {
2171 if (publ->scope != scope)
2172 continue;
2173 if (publ->type != seq->type)
2174 continue;
2175 if (publ->lower != seq->lower)
2176 continue;
2177 if (publ->upper != seq->upper)
2178 break;
2179 tipc_nametbl_withdraw(publ->type, publ->lower,
2180 publ->ref, publ->key);
2181 rc = 0;
2182 break;
2183 }
2184 tipc_nametbl_withdraw(publ->type, publ->lower,
2185 publ->ref, publ->key);
2186 rc = 0;
2187 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002188 if (list_empty(&tsk->publications))
2189 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002190 return rc;
2191}
2192
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002193static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002194 int len, int full_id)
2195{
2196 struct publication *publ;
2197 int ret;
2198
2199 if (full_id)
2200 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2201 tipc_zone(tipc_own_addr),
2202 tipc_cluster(tipc_own_addr),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002203 tipc_node(tipc_own_addr), tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002204 else
Ying Xue07f6c4b2015-01-07 13:41:58 +08002205 ret = tipc_snprintf(buf, len, "%-10u:", tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002206
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002207 if (tsk->connected) {
2208 u32 dport = tsk_peer_port(tsk);
2209 u32 destnode = tsk_peer_node(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002210
2211 ret += tipc_snprintf(buf + ret, len - ret,
2212 " connected to <%u.%u.%u:%u>",
2213 tipc_zone(destnode),
2214 tipc_cluster(destnode),
2215 tipc_node(destnode), dport);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002216 if (tsk->conn_type != 0)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002217 ret += tipc_snprintf(buf + ret, len - ret,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002218 " via {%u,%u}", tsk->conn_type,
2219 tsk->conn_instance);
2220 } else if (tsk->published) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002221 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002222 list_for_each_entry(publ, &tsk->publications, pport_list) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002223 if (publ->lower == publ->upper)
2224 ret += tipc_snprintf(buf + ret, len - ret,
2225 " {%u,%u}", publ->type,
2226 publ->lower);
2227 else
2228 ret += tipc_snprintf(buf + ret, len - ret,
2229 " {%u,%u,%u}", publ->type,
2230 publ->lower, publ->upper);
2231 }
2232 }
2233 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2234 return ret;
2235}
2236
2237struct sk_buff *tipc_sk_socks_show(void)
2238{
Ying Xue07f6c4b2015-01-07 13:41:58 +08002239 const struct bucket_table *tbl;
2240 struct rhash_head *pos;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002241 struct sk_buff *buf;
2242 struct tlv_desc *rep_tlv;
2243 char *pb;
2244 int pb_len;
2245 struct tipc_sock *tsk;
2246 int str_len = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002247 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002248
2249 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2250 if (!buf)
2251 return NULL;
2252 rep_tlv = (struct tlv_desc *)buf->data;
2253 pb = TLV_DATA(rep_tlv);
2254 pb_len = ULTRA_STRING_MAX_LEN;
2255
Ying Xue07f6c4b2015-01-07 13:41:58 +08002256 rcu_read_lock();
2257 tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht);
2258 for (i = 0; i < tbl->size; i++) {
2259 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2260 spin_lock_bh(&tsk->sk.sk_lock.slock);
2261 str_len += tipc_sk_show(tsk, pb + str_len,
2262 pb_len - str_len, 0);
2263 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2264 }
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002265 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002266 rcu_read_unlock();
2267
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002268 str_len += 1; /* for "\0" */
2269 skb_put(buf, TLV_SPACE(str_len));
2270 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2271
2272 return buf;
2273}
2274
2275/* tipc_sk_reinit: set non-zero address in all existing sockets
2276 * when we go from standalone to network mode.
2277 */
2278void tipc_sk_reinit(void)
2279{
Ying Xue07f6c4b2015-01-07 13:41:58 +08002280 const struct bucket_table *tbl;
2281 struct rhash_head *pos;
2282 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002283 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002284 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002285
Ying Xue07f6c4b2015-01-07 13:41:58 +08002286 rcu_read_lock();
2287 tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht);
2288 for (i = 0; i < tbl->size; i++) {
2289 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2290 spin_lock_bh(&tsk->sk.sk_lock.slock);
2291 msg = &tsk->phdr;
2292 msg_set_prevnode(msg, tipc_own_addr);
2293 msg_set_orignode(msg, tipc_own_addr);
2294 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2295 }
2296 }
2297 rcu_read_unlock();
2298}
2299
2300static struct tipc_sock *tipc_sk_lookup(u32 portid)
2301{
2302 struct tipc_sock *tsk;
2303
2304 rcu_read_lock();
2305 tsk = rhashtable_lookup(&tipc_sk_rht, &portid);
2306 if (tsk)
2307 sock_hold(&tsk->sk);
2308 rcu_read_unlock();
2309
2310 return tsk;
2311}
2312
2313static int tipc_sk_insert(struct tipc_sock *tsk)
2314{
2315 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2316 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2317
2318 while (remaining--) {
2319 portid++;
2320 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2321 portid = TIPC_MIN_PORT;
2322 tsk->portid = portid;
2323 sock_hold(&tsk->sk);
2324 if (rhashtable_lookup_insert(&tipc_sk_rht, &tsk->node))
2325 return 0;
2326 sock_put(&tsk->sk);
2327 }
2328
2329 return -1;
2330}
2331
2332static void tipc_sk_remove(struct tipc_sock *tsk)
2333{
2334 struct sock *sk = &tsk->sk;
2335
2336 if (rhashtable_remove(&tipc_sk_rht, &tsk->node)) {
2337 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2338 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002339 }
2340}
2341
Ying Xue07f6c4b2015-01-07 13:41:58 +08002342int tipc_sk_rht_init(void)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002343{
Ying Xue07f6c4b2015-01-07 13:41:58 +08002344 struct rhashtable_params rht_params = {
2345 .nelem_hint = 192,
2346 .head_offset = offsetof(struct tipc_sock, node),
2347 .key_offset = offsetof(struct tipc_sock, portid),
2348 .key_len = sizeof(u32), /* portid */
2349 .hashfn = jhash,
2350 .max_shift = 20, /* 1M */
2351 .min_shift = 8, /* 256 */
2352 .grow_decision = rht_grow_above_75,
2353 .shrink_decision = rht_shrink_below_30,
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002354 };
2355
Ying Xue07f6c4b2015-01-07 13:41:58 +08002356 return rhashtable_init(&tipc_sk_rht, &rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002357}
2358
Ying Xue07f6c4b2015-01-07 13:41:58 +08002359void tipc_sk_rht_destroy(void)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002360{
Ying Xue07f6c4b2015-01-07 13:41:58 +08002361 /* Wait for socket readers to complete */
2362 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002363
Ying Xue07f6c4b2015-01-07 13:41:58 +08002364 rhashtable_destroy(&tipc_sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002365}
2366
2367/**
Ying Xue247f0f32014-02-18 16:06:46 +08002368 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002369 * @sock: socket structure
2370 * @lvl: option level
2371 * @opt: option identifier
2372 * @ov: pointer to new option value
2373 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002374 *
2375 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002376 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002377 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002378 * Returns 0 on success, errno otherwise
2379 */
Ying Xue247f0f32014-02-18 16:06:46 +08002380static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2381 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002382{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002383 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002384 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002385 u32 value;
2386 int res;
2387
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002388 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2389 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002390 if (lvl != SOL_TIPC)
2391 return -ENOPROTOOPT;
2392 if (ol < sizeof(value))
2393 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002394 res = get_user(value, (u32 __user *)ov);
2395 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002396 return res;
2397
Allan Stephens0c3141e2008-04-15 00:22:02 -07002398 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002399
Per Lidenb97bf3f2006-01-02 19:04:38 +01002400 switch (opt) {
2401 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002402 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002403 break;
2404 case TIPC_SRC_DROPPABLE:
2405 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002406 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002407 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002408 res = -ENOPROTOOPT;
2409 break;
2410 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002411 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002412 break;
2413 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002414 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002415 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002416 break;
2417 default:
2418 res = -EINVAL;
2419 }
2420
Allan Stephens0c3141e2008-04-15 00:22:02 -07002421 release_sock(sk);
2422
Per Lidenb97bf3f2006-01-02 19:04:38 +01002423 return res;
2424}
2425
2426/**
Ying Xue247f0f32014-02-18 16:06:46 +08002427 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002428 * @sock: socket structure
2429 * @lvl: option level
2430 * @opt: option identifier
2431 * @ov: receptacle for option value
2432 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002433 *
2434 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002435 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002436 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002437 * Returns 0 on success, errno otherwise
2438 */
Ying Xue247f0f32014-02-18 16:06:46 +08002439static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2440 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002441{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002442 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002443 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002444 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002445 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002446 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002447
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002448 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2449 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450 if (lvl != SOL_TIPC)
2451 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002452 res = get_user(len, ol);
2453 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002454 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002455
Allan Stephens0c3141e2008-04-15 00:22:02 -07002456 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457
2458 switch (opt) {
2459 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002460 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002461 break;
2462 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002463 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464 break;
2465 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002466 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002467 break;
2468 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002469 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002470 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002471 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002472 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002473 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002474 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002475 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002476 value = skb_queue_len(&sk->sk_receive_queue);
2477 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002478 default:
2479 res = -EINVAL;
2480 }
2481
Allan Stephens0c3141e2008-04-15 00:22:02 -07002482 release_sock(sk);
2483
Paul Gortmaker25860c32010-12-31 18:59:31 +00002484 if (res)
2485 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002486
Paul Gortmaker25860c32010-12-31 18:59:31 +00002487 if (len < sizeof(value))
2488 return -EINVAL;
2489
2490 if (copy_to_user(ov, &value, sizeof(value)))
2491 return -EFAULT;
2492
2493 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002494}
2495
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002496static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002497{
2498 struct tipc_sioc_ln_req lnr;
2499 void __user *argp = (void __user *)arg;
2500
2501 switch (cmd) {
2502 case SIOCGETLINKNAME:
2503 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2504 return -EFAULT;
Ying Xue7b8613e2014-10-20 14:44:25 +08002505 if (!tipc_node_get_linkname(lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002506 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2507 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2508 return -EFAULT;
2509 return 0;
2510 }
2511 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002512 default:
2513 return -ENOIOCTLCMD;
2514 }
2515}
2516
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002517/* Protocol switches for the various types of TIPC sockets */
2518
Florian Westphalbca65ea2008-02-07 18:18:01 -08002519static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002520 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002521 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002522 .release = tipc_release,
2523 .bind = tipc_bind,
2524 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002525 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002526 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002527 .getname = tipc_getname,
2528 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002529 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002530 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002531 .shutdown = tipc_shutdown,
2532 .setsockopt = tipc_setsockopt,
2533 .getsockopt = tipc_getsockopt,
2534 .sendmsg = tipc_sendmsg,
2535 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002536 .mmap = sock_no_mmap,
2537 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002538};
2539
Florian Westphalbca65ea2008-02-07 18:18:01 -08002540static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002541 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002542 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002543 .release = tipc_release,
2544 .bind = tipc_bind,
2545 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002546 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002547 .accept = tipc_accept,
2548 .getname = tipc_getname,
2549 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002550 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002551 .listen = tipc_listen,
2552 .shutdown = tipc_shutdown,
2553 .setsockopt = tipc_setsockopt,
2554 .getsockopt = tipc_getsockopt,
2555 .sendmsg = tipc_send_packet,
2556 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002557 .mmap = sock_no_mmap,
2558 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002559};
2560
Florian Westphalbca65ea2008-02-07 18:18:01 -08002561static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002562 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002563 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002564 .release = tipc_release,
2565 .bind = tipc_bind,
2566 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002567 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002568 .accept = tipc_accept,
2569 .getname = tipc_getname,
2570 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002571 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002572 .listen = tipc_listen,
2573 .shutdown = tipc_shutdown,
2574 .setsockopt = tipc_setsockopt,
2575 .getsockopt = tipc_getsockopt,
2576 .sendmsg = tipc_send_stream,
2577 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002578 .mmap = sock_no_mmap,
2579 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002580};
2581
Florian Westphalbca65ea2008-02-07 18:18:01 -08002582static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002583 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002584 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002585 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002586};
2587
2588static struct proto tipc_proto = {
2589 .name = "TIPC",
2590 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002591 .obj_size = sizeof(struct tipc_sock),
2592 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002593};
2594
Ying Xuec5fa7b32013-06-17 10:54:39 -04002595static struct proto tipc_proto_kern = {
2596 .name = "TIPC",
2597 .obj_size = sizeof(struct tipc_sock),
2598 .sysctl_rmem = sysctl_tipc_rmem
2599};
2600
Per Lidenb97bf3f2006-01-02 19:04:38 +01002601/**
Per Liden4323add2006-01-18 00:38:21 +01002602 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002603 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002604 * Returns 0 on success, errno otherwise
2605 */
Per Liden4323add2006-01-18 00:38:21 +01002606int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002607{
2608 int res;
2609
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002610 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002611 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002612 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002613 goto out;
2614 }
2615
2616 res = sock_register(&tipc_family_ops);
2617 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002618 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002619 proto_unregister(&tipc_proto);
2620 goto out;
2621 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002622 out:
2623 return res;
2624}
2625
2626/**
Per Liden4323add2006-01-18 00:38:21 +01002627 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002628 */
Per Liden4323add2006-01-18 00:38:21 +01002629void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002630{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002631 sock_unregister(tipc_family_ops.family);
2632 proto_unregister(&tipc_proto);
2633}
Richard Alpe34b78a122014-11-20 10:29:10 +01002634
2635/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002636static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002637{
2638 u32 peer_node;
2639 u32 peer_port;
2640 struct nlattr *nest;
2641
2642 peer_node = tsk_peer_node(tsk);
2643 peer_port = tsk_peer_port(tsk);
2644
2645 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2646
2647 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2648 goto msg_full;
2649 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2650 goto msg_full;
2651
2652 if (tsk->conn_type != 0) {
2653 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2654 goto msg_full;
2655 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2656 goto msg_full;
2657 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2658 goto msg_full;
2659 }
2660 nla_nest_end(skb, nest);
2661
2662 return 0;
2663
2664msg_full:
2665 nla_nest_cancel(skb, nest);
2666
2667 return -EMSGSIZE;
2668}
2669
2670/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002671static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2672 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002673{
2674 int err;
2675 void *hdr;
2676 struct nlattr *attrs;
2677
2678 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2679 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2680 if (!hdr)
2681 goto msg_cancel;
2682
2683 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2684 if (!attrs)
2685 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002686 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002687 goto attr_msg_cancel;
2688 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr))
2689 goto attr_msg_cancel;
2690
2691 if (tsk->connected) {
2692 err = __tipc_nl_add_sk_con(skb, tsk);
2693 if (err)
2694 goto attr_msg_cancel;
2695 } else if (!list_empty(&tsk->publications)) {
2696 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2697 goto attr_msg_cancel;
2698 }
2699 nla_nest_end(skb, attrs);
2700 genlmsg_end(skb, hdr);
2701
2702 return 0;
2703
2704attr_msg_cancel:
2705 nla_nest_cancel(skb, attrs);
2706genlmsg_cancel:
2707 genlmsg_cancel(skb, hdr);
2708msg_cancel:
2709 return -EMSGSIZE;
2710}
2711
2712int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2713{
2714 int err;
2715 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002716 const struct bucket_table *tbl;
2717 struct rhash_head *pos;
2718 u32 prev_portid = cb->args[0];
2719 u32 portid = prev_portid;
2720 int i;
Richard Alpe34b78a122014-11-20 10:29:10 +01002721
Ying Xue07f6c4b2015-01-07 13:41:58 +08002722 rcu_read_lock();
2723 tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht);
2724 for (i = 0; i < tbl->size; i++) {
2725 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2726 spin_lock_bh(&tsk->sk.sk_lock.slock);
2727 portid = tsk->portid;
2728 err = __tipc_nl_add_sk(skb, cb, tsk);
2729 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2730 if (err)
2731 break;
Richard Alpe34b78a122014-11-20 10:29:10 +01002732
Ying Xue07f6c4b2015-01-07 13:41:58 +08002733 prev_portid = portid;
2734 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002735 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002736 rcu_read_unlock();
Richard Alpe34b78a122014-11-20 10:29:10 +01002737
Ying Xue07f6c4b2015-01-07 13:41:58 +08002738 cb->args[0] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002739
2740 return skb->len;
2741}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002742
2743/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002744static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2745 struct netlink_callback *cb,
2746 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002747{
2748 void *hdr;
2749 struct nlattr *attrs;
2750
2751 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2752 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2753 if (!hdr)
2754 goto msg_cancel;
2755
2756 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2757 if (!attrs)
2758 goto genlmsg_cancel;
2759
2760 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2761 goto attr_msg_cancel;
2762 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2763 goto attr_msg_cancel;
2764 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2765 goto attr_msg_cancel;
2766 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2767 goto attr_msg_cancel;
2768
2769 nla_nest_end(skb, attrs);
2770 genlmsg_end(skb, hdr);
2771
2772 return 0;
2773
2774attr_msg_cancel:
2775 nla_nest_cancel(skb, attrs);
2776genlmsg_cancel:
2777 genlmsg_cancel(skb, hdr);
2778msg_cancel:
2779 return -EMSGSIZE;
2780}
2781
2782/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002783static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2784 struct netlink_callback *cb,
2785 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002786{
2787 int err;
2788 struct publication *p;
2789
2790 if (*last_publ) {
2791 list_for_each_entry(p, &tsk->publications, pport_list) {
2792 if (p->key == *last_publ)
2793 break;
2794 }
2795 if (p->key != *last_publ) {
2796 /* We never set seq or call nl_dump_check_consistent()
2797 * this means that setting prev_seq here will cause the
2798 * consistence check to fail in the netlink callback
2799 * handler. Resulting in the last NLMSG_DONE message
2800 * having the NLM_F_DUMP_INTR flag set.
2801 */
2802 cb->prev_seq = 1;
2803 *last_publ = 0;
2804 return -EPIPE;
2805 }
2806 } else {
2807 p = list_first_entry(&tsk->publications, struct publication,
2808 pport_list);
2809 }
2810
2811 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2812 err = __tipc_nl_add_sk_publ(skb, cb, p);
2813 if (err) {
2814 *last_publ = p->key;
2815 return err;
2816 }
2817 }
2818 *last_publ = 0;
2819
2820 return 0;
2821}
2822
2823int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2824{
2825 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002826 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002827 u32 last_publ = cb->args[1];
2828 u32 done = cb->args[2];
2829 struct tipc_sock *tsk;
2830
Ying Xue07f6c4b2015-01-07 13:41:58 +08002831 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002832 struct nlattr **attrs;
2833 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2834
2835 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2836 if (err)
2837 return err;
2838
2839 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2840 attrs[TIPC_NLA_SOCK],
2841 tipc_nl_sock_policy);
2842 if (err)
2843 return err;
2844
2845 if (!sock[TIPC_NLA_SOCK_REF])
2846 return -EINVAL;
2847
Ying Xue07f6c4b2015-01-07 13:41:58 +08002848 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002849 }
2850
2851 if (done)
2852 return 0;
2853
Ying Xue07f6c4b2015-01-07 13:41:58 +08002854 tsk = tipc_sk_lookup(tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002855 if (!tsk)
2856 return -EINVAL;
2857
2858 lock_sock(&tsk->sk);
2859 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2860 if (!err)
2861 done = 1;
2862 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002863 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002864
Ying Xue07f6c4b2015-01-07 13:41:58 +08002865 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002866 cb->args[1] = last_publ;
2867 cb->args[2] = done;
2868
2869 return skb->len;
2870}