blob: 701f31bbbbfb727202e2cc72e9a5a940c7014bc5 [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 */
50#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
51#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:
71 * @probing_interval:
72 * @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;
96 u32 probing_interval;
97 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);
364 k_init_timer(&tsk->timer, (Handler)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);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400514 k_cancel_timer(&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 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400525 k_term_timer(&tsk->timer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526
Allan Stephens0c3141e2008-04-15 00:22:02 -0700527 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100528 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529
Allan Stephens0c3141e2008-04-15 00:22:02 -0700530 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700531 sock->state = SS_DISCONNECTING;
532 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800533
534 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700535 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200537 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538}
539
540/**
Ying Xue247f0f32014-02-18 16:06:46 +0800541 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 * @sock: socket structure
543 * @uaddr: socket address describing name(s) and desired operation
544 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900545 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 * Name and name sequence binding is indicated using a positive scope value;
547 * a negative scope value unbinds the specified name. Specifying no name
548 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700551 *
552 * NOTE: This routine doesn't need to take the socket lock since it doesn't
553 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 */
Ying Xue247f0f32014-02-18 16:06:46 +0800555static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
556 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557{
Ying Xue84602762013-12-27 10:18:28 +0800558 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400560 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800561 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562
Ying Xue84602762013-12-27 10:18:28 +0800563 lock_sock(sk);
564 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400565 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800566 goto exit;
567 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900568
Ying Xue84602762013-12-27 10:18:28 +0800569 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
570 res = -EINVAL;
571 goto exit;
572 }
573 if (addr->family != AF_TIPC) {
574 res = -EAFNOSUPPORT;
575 goto exit;
576 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 if (addr->addrtype == TIPC_ADDR_NAME)
579 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800580 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
581 res = -EAFNOSUPPORT;
582 goto exit;
583 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900584
Ying Xue13a2e892013-06-17 10:54:40 -0400585 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400586 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800587 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
588 res = -EACCES;
589 goto exit;
590 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400591
Ying Xue84602762013-12-27 10:18:28 +0800592 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400593 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
594 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800595exit:
596 release_sock(sk);
597 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598}
599
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900600/**
Ying Xue247f0f32014-02-18 16:06:46 +0800601 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 * @sock: socket structure
603 * @uaddr: area for returned socket address
604 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700605 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700608 *
Allan Stephens2da59912008-07-14 22:43:32 -0700609 * NOTE: This routine doesn't need to take the socket lock since it only
610 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000611 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 */
Ying Xue247f0f32014-02-18 16:06:46 +0800613static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
614 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400617 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000619 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700620 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700621 if ((sock->state != SS_CONNECTED) &&
622 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
623 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400624 addr->addr.id.ref = tsk_peer_port(tsk);
625 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700626 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800627 addr->addr.id.ref = tsk->portid;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000628 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700629 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630
631 *uaddr_len = sizeof(*addr);
632 addr->addrtype = TIPC_ADDR_ID;
633 addr->family = AF_TIPC;
634 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 addr->addr.name.domain = 0;
636
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638}
639
640/**
Ying Xue247f0f32014-02-18 16:06:46 +0800641 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 * @file: file structure associated with the socket
643 * @sock: socket for which to calculate the poll bits
644 * @wait: ???
645 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700646 * Returns pollmask value
647 *
648 * COMMENTARY:
649 * It appears that the usual socket locking mechanisms are not useful here
650 * since the pollmask info is potentially out-of-date the moment this routine
651 * exits. TCP and other protocols seem to rely on higher level poll routines
652 * to handle any preventable race conditions, so TIPC will do the same ...
653 *
654 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700655 *
Allan Stephensf662c072010-08-17 11:00:06 +0000656 * socket state flags set
657 * ------------ ---------
658 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200659 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000660 *
661 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
662 * no write flags
663 *
664 * connected POLLIN/POLLRDNORM if data in rx queue
665 * POLLOUT if port is not congested
666 *
667 * disconnecting POLLIN/POLLRDNORM/POLLHUP
668 * no write flags
669 *
670 * listening POLLIN if SYN in rx queue
671 * no write flags
672 *
673 * ready POLLIN/POLLRDNORM if data in rx queue
674 * [connectionless] POLLOUT (since port cannot be congested)
675 *
676 * IMPORTANT: The fact that a read or write operation is indicated does NOT
677 * imply that the operation will succeed, merely that it should be performed
678 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 */
Ying Xue247f0f32014-02-18 16:06:46 +0800680static unsigned int tipc_poll(struct file *file, struct socket *sock,
681 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682{
Allan Stephens9b674e82008-03-26 16:48:21 -0700683 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400684 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000685 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700686
Ying Xuef288bef2012-08-21 11:16:57 +0800687 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700688
Allan Stephensf662c072010-08-17 11:00:06 +0000689 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200690 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500691 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200692 mask |= POLLOUT;
693 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000694 case SS_READY:
695 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400696 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000697 mask |= POLLOUT;
698 /* fall thru' */
699 case SS_CONNECTING:
700 case SS_LISTENING:
701 if (!skb_queue_empty(&sk->sk_receive_queue))
702 mask |= (POLLIN | POLLRDNORM);
703 break;
704 case SS_DISCONNECTING:
705 mask = (POLLIN | POLLRDNORM | POLLHUP);
706 break;
707 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700708
709 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710}
711
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400712/**
713 * tipc_sendmcast - send multicast message
714 * @sock: socket structure
715 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500716 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400717 * @dsz: total length of message data
718 * @timeo: timeout to wait for wakeup
719 *
720 * Called from function tipc_sendmsg(), which has done all sanity checks
721 * Returns the number of bytes sent on success, or errno
722 */
723static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500724 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400725{
726 struct sock *sk = sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400727 struct tipc_msg *mhdr = &tipc_sk(sk)->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +0800728 struct sk_buff_head head;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400729 uint mtu;
730 int rc;
731
732 msg_set_type(mhdr, TIPC_MCAST_MSG);
733 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
734 msg_set_destport(mhdr, 0);
735 msg_set_destnode(mhdr, 0);
736 msg_set_nametype(mhdr, seq->type);
737 msg_set_namelower(mhdr, seq->lower);
738 msg_set_nameupper(mhdr, seq->upper);
739 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
740
741new_mtu:
742 mtu = tipc_bclink_get_mtu();
Ying Xuea6ca1092014-11-26 11:41:55 +0800743 __skb_queue_head_init(&head);
744 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400745 if (unlikely(rc < 0))
746 return rc;
747
748 do {
Ying Xuea6ca1092014-11-26 11:41:55 +0800749 rc = tipc_bclink_xmit(&head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400750 if (likely(rc >= 0)) {
751 rc = dsz;
752 break;
753 }
754 if (rc == -EMSGSIZE)
755 goto new_mtu;
756 if (rc != -ELINKCONG)
757 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400758 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400759 rc = tipc_wait_for_sndmsg(sock, &timeo);
760 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800761 __skb_queue_purge(&head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400762 } while (!rc);
763 return rc;
764}
765
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400766/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
767 */
768void tipc_sk_mcast_rcv(struct sk_buff *buf)
769{
770 struct tipc_msg *msg = buf_msg(buf);
771 struct tipc_port_list dports = {0, NULL, };
772 struct tipc_port_list *item;
773 struct sk_buff *b;
774 uint i, last, dst = 0;
775 u32 scope = TIPC_CLUSTER_SCOPE;
776
777 if (in_own_node(msg_orignode(msg)))
778 scope = TIPC_NODE_SCOPE;
779
780 /* Create destination port list: */
781 tipc_nametbl_mc_translate(msg_nametype(msg),
782 msg_namelower(msg),
783 msg_nameupper(msg),
784 scope,
785 &dports);
786 last = dports.count;
787 if (!last) {
788 kfree_skb(buf);
789 return;
790 }
791
792 for (item = &dports; item; item = item->next) {
793 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
794 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
795 if (!b) {
796 pr_warn("Failed do clone mcast rcv buffer\n");
797 continue;
798 }
799 msg_set_destport(msg, item->ports[i]);
800 tipc_sk_rcv(b);
801 }
802 }
803 tipc_port_list_free(&dports);
804}
805
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900806/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500807 * tipc_sk_proto_rcv - receive a connection mng protocol message
808 * @tsk: receiving socket
809 * @dnode: node to send response message to, if any
810 * @buf: buffer containing protocol message
811 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
812 * (CONN_PROBE_REPLY) message should be forwarded.
813 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800814static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
815 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500816{
817 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500818 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500819
820 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400821 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500822 goto exit;
823
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400824 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500825
826 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400827 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500828 tsk->sent_unacked -= msg_msgcnt(msg);
829 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400830 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500831 } else if (msg_type(msg) == CONN_PROBE) {
832 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
833 return TIPC_OK;
834 msg_set_type(msg, CONN_PROBE_REPLY);
835 return TIPC_FWD_MSG;
836 }
837 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
838exit:
839 kfree_skb(buf);
840 return TIPC_OK;
841}
842
Ying Xue3f405042014-01-17 09:50:05 +0800843static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
844{
845 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400846 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800847 DEFINE_WAIT(wait);
848 int done;
849
850 do {
851 int err = sock_error(sk);
852 if (err)
853 return err;
854 if (sock->state == SS_DISCONNECTING)
855 return -EPIPE;
856 if (!*timeo_p)
857 return -EAGAIN;
858 if (signal_pending(current))
859 return sock_intr_errno(*timeo_p);
860
861 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500862 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800863 finish_wait(sk_sleep(sk), &wait);
864 } while (!done);
865 return 0;
866}
867
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500868/**
Ying Xue247f0f32014-02-18 16:06:46 +0800869 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700870 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 * @sock: socket structure
872 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500873 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900874 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900876 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
878 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 * Returns the number of bytes sent on success, or errno otherwise
881 */
Ying Xue247f0f32014-02-18 16:06:46 +0800882static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500883 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500885 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700886 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400887 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400888 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500889 u32 dnode, dport;
Ying Xuea6ca1092014-11-26 11:41:55 +0800890 struct sk_buff_head head;
891 struct sk_buff *skb;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500892 struct tipc_name_seq *seq = &dest->addr.nameseq;
893 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800894 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100895 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896
897 if (unlikely(!dest))
898 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500899
Allan Stephens51f9cc12006-06-25 23:49:06 -0700900 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
901 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500903
904 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400905 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906
Allan Stephens0c3141e2008-04-15 00:22:02 -0700907 if (iocb)
908 lock_sock(sk);
909
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500910 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700911 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500912 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700913 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700914 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700915 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500916 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700917 goto exit;
918 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400919 if (tsk->published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500920 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700921 goto exit;
922 }
923 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400924 tsk->conn_type = dest->addr.name.name.type;
925 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700926 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 }
928
Ying Xue3f405042014-01-17 09:50:05 +0800929 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500930
931 if (dest->addrtype == TIPC_ADDR_MCAST) {
Al Viro562640f2014-11-15 01:13:43 -0500932 rc = tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500933 goto exit;
934 } else if (dest->addrtype == TIPC_ADDR_NAME) {
935 u32 type = dest->addr.name.name.type;
936 u32 inst = dest->addr.name.name.instance;
937 u32 domain = dest->addr.name.domain;
938
939 dnode = domain;
940 msg_set_type(mhdr, TIPC_NAMED_MSG);
941 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
942 msg_set_nametype(mhdr, type);
943 msg_set_nameinst(mhdr, inst);
944 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
945 dport = tipc_nametbl_translate(type, inst, &dnode);
946 msg_set_destnode(mhdr, dnode);
947 msg_set_destport(mhdr, dport);
948 if (unlikely(!dport && !dnode)) {
949 rc = -EHOSTUNREACH;
950 goto exit;
951 }
952 } else if (dest->addrtype == TIPC_ADDR_ID) {
953 dnode = dest->addr.id.node;
954 msg_set_type(mhdr, TIPC_DIRECT_MSG);
955 msg_set_lookup_scope(mhdr, 0);
956 msg_set_destnode(mhdr, dnode);
957 msg_set_destport(mhdr, dest->addr.id.ref);
958 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
959 }
960
961new_mtu:
Ying Xue07f6c4b2015-01-07 13:41:58 +0800962 mtu = tipc_node_get_mtu(dnode, tsk->portid);
Ying Xuea6ca1092014-11-26 11:41:55 +0800963 __skb_queue_head_init(&head);
964 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500965 if (rc < 0)
966 goto exit;
967
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900968 do {
Ying Xuea6ca1092014-11-26 11:41:55 +0800969 skb = skb_peek(&head);
970 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800971 rc = tipc_link_xmit(&head, dnode, tsk->portid);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500972 if (likely(rc >= 0)) {
973 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700974 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500975 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700976 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900977 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500978 if (rc == -EMSGSIZE)
979 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500980 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700981 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400982 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500983 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400984 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800985 __skb_queue_purge(&head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500986 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700987exit:
988 if (iocb)
989 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500990
991 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992}
993
Ying Xue391a6dd2014-01-17 09:50:06 +0800994static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
995{
996 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400997 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800998 DEFINE_WAIT(wait);
999 int done;
1000
1001 do {
1002 int err = sock_error(sk);
1003 if (err)
1004 return err;
1005 if (sock->state == SS_DISCONNECTING)
1006 return -EPIPE;
1007 else if (sock->state != SS_CONNECTED)
1008 return -ENOTCONN;
1009 if (!*timeo_p)
1010 return -EAGAIN;
1011 if (signal_pending(current))
1012 return sock_intr_errno(*timeo_p);
1013
1014 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1015 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001016 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001017 !tsk_conn_cong(tsk)) ||
1018 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +08001019 finish_wait(sk_sleep(sk), &wait);
1020 } while (!done);
1021 return 0;
1022}
1023
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001024/**
Ying Xue247f0f32014-02-18 16:06:46 +08001025 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 * @iocb: (unused)
1027 * @sock: socket structure
1028 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001029 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001030 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001032 *
1033 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001034 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 */
Ying Xue247f0f32014-02-18 16:06:46 +08001036static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001037 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001039 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001040 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001041 struct tipc_msg *mhdr = &tsk->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +08001042 struct sk_buff_head head;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001043 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001044 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001045 int rc = -EINVAL;
1046 long timeo;
1047 u32 dnode;
1048 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001049
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001050 /* Handle implied connection establishment */
1051 if (unlikely(dest)) {
1052 rc = tipc_sendmsg(iocb, sock, m, dsz);
1053 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001054 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001055 return rc;
1056 }
1057 if (dsz > (uint)INT_MAX)
1058 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001059
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001060 if (iocb)
1061 lock_sock(sk);
1062
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001063 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001064 if (sock->state == SS_DISCONNECTING)
1065 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001066 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001067 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +08001068 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001069 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001071 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001072 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001073
1074next:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001075 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001076 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001077 __skb_queue_head_init(&head);
1078 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001079 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001080 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001081 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001082 if (likely(!tsk_conn_cong(tsk))) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08001083 rc = tipc_link_xmit(&head, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001084 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001085 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001086 sent += send;
1087 if (sent == dsz)
1088 break;
1089 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001090 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001091 if (rc == -EMSGSIZE) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08001092 tsk->max_pkt = tipc_node_get_mtu(dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001093 goto next;
1094 }
1095 if (rc != -ELINKCONG)
1096 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001097 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001099 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001100 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +08001101 __skb_queue_purge(&head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001102 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001103exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001104 if (iocb)
1105 release_sock(sk);
1106 return sent ? sent : rc;
1107}
1108
1109/**
1110 * tipc_send_packet - send a connection-oriented message
1111 * @iocb: if NULL, indicates that socket lock is already held
1112 * @sock: socket structure
1113 * @m: message to send
1114 * @dsz: length of data to be transmitted
1115 *
1116 * Used for SOCK_SEQPACKET messages.
1117 *
1118 * Returns the number of bytes sent on success, or errno otherwise
1119 */
1120static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1121 struct msghdr *m, size_t dsz)
1122{
1123 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1124 return -EMSGSIZE;
1125
1126 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127}
1128
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001129/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001131static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001132 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133{
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001134 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001136 msg_set_destnode(msg, peer_node);
1137 msg_set_destport(msg, peer_port);
1138 msg_set_type(msg, TIPC_CONN_MSG);
1139 msg_set_lookup_scope(msg, 0);
1140 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001141
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001142 tsk->probing_interval = CONN_PROBING_INTERVAL;
1143 tsk->probing_state = TIPC_CONN_OK;
1144 tsk->connected = 1;
1145 k_start_timer(&tsk->timer, tsk->probing_interval);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001146 tipc_node_add_conn(peer_node, tsk->portid, peer_port);
1147 tsk->max_pkt = tipc_node_get_mtu(peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148}
1149
1150/**
1151 * set_orig_addr - capture sender's address for received message
1152 * @m: descriptor for message info
1153 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001154 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 * Note: Address is not captured if not requested by receiver.
1156 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001157static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001159 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001161 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 addr->family = AF_TIPC;
1163 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001164 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 addr->addr.id.ref = msg_origport(msg);
1166 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001167 addr->addr.name.domain = 0; /* could leave uninitialized */
1168 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 m->msg_namelen = sizeof(struct sockaddr_tipc);
1170 }
1171}
1172
1173/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001174 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 * @m: descriptor for message info
1176 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001177 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001178 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001180 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 * Returns 0 if successful, otherwise errno
1182 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001183static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1184 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185{
1186 u32 anc_data[3];
1187 u32 err;
1188 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001189 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 int res;
1191
1192 if (likely(m->msg_controllen == 0))
1193 return 0;
1194
1195 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 err = msg ? msg_errcode(msg) : 0;
1197 if (unlikely(err)) {
1198 anc_data[0] = err;
1199 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001200 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1201 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001203 if (anc_data[1]) {
1204 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1205 msg_data(msg));
1206 if (res)
1207 return res;
1208 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 }
1210
1211 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1213 switch (dest_type) {
1214 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001215 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 anc_data[0] = msg_nametype(msg);
1217 anc_data[1] = msg_namelower(msg);
1218 anc_data[2] = msg_namelower(msg);
1219 break;
1220 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001221 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 anc_data[0] = msg_nametype(msg);
1223 anc_data[1] = msg_namelower(msg);
1224 anc_data[2] = msg_nameupper(msg);
1225 break;
1226 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001227 has_name = (tsk->conn_type != 0);
1228 anc_data[0] = tsk->conn_type;
1229 anc_data[1] = tsk->conn_instance;
1230 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 break;
1232 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001233 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 }
Allan Stephens2db99832010-12-31 18:59:33 +00001235 if (has_name) {
1236 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1237 if (res)
1238 return res;
1239 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240
1241 return 0;
1242}
1243
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001244static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001245{
Ying Xuea6ca1092014-11-26 11:41:55 +08001246 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001247 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001248 u32 peer_port = tsk_peer_port(tsk);
1249 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001250
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001251 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001252 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001253 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
Ying Xue07f6c4b2015-01-07 13:41:58 +08001254 tipc_own_addr, peer_port, tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001255 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001256 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001257 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001258 msg_set_msgcnt(msg, ack);
Ying Xuea6ca1092014-11-26 11:41:55 +08001259 tipc_link_xmit_skb(skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001260}
1261
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001262static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001263{
1264 struct sock *sk = sock->sk;
1265 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001266 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001267 int err;
1268
1269 for (;;) {
1270 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001271 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001272 if (sock->state == SS_DISCONNECTING) {
1273 err = -ENOTCONN;
1274 break;
1275 }
1276 release_sock(sk);
1277 timeo = schedule_timeout(timeo);
1278 lock_sock(sk);
1279 }
1280 err = 0;
1281 if (!skb_queue_empty(&sk->sk_receive_queue))
1282 break;
1283 err = sock_intr_errno(timeo);
1284 if (signal_pending(current))
1285 break;
1286 err = -EAGAIN;
1287 if (!timeo)
1288 break;
1289 }
1290 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001291 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001292 return err;
1293}
1294
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001295/**
Ying Xue247f0f32014-02-18 16:06:46 +08001296 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 * @iocb: (unused)
1298 * @m: descriptor for message info
1299 * @buf_len: total size of user buffer area
1300 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001301 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1303 * If the complete message doesn't fit in user area, truncate it.
1304 *
1305 * Returns size of returned message data, errno otherwise
1306 */
Ying Xue247f0f32014-02-18 16:06:46 +08001307static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1308 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001310 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001311 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 struct sk_buff *buf;
1313 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001314 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 unsigned int sz;
1316 u32 err;
1317 int res;
1318
Allan Stephens0c3141e2008-04-15 00:22:02 -07001319 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 if (unlikely(!buf_len))
1321 return -EINVAL;
1322
Allan Stephens0c3141e2008-04-15 00:22:02 -07001323 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324
Allan Stephens0c3141e2008-04-15 00:22:02 -07001325 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 res = -ENOTCONN;
1327 goto exit;
1328 }
1329
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001330 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001331restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332
Allan Stephens0c3141e2008-04-15 00:22:02 -07001333 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001334 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001335 if (res)
1336 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001337
1338 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001339 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 msg = buf_msg(buf);
1341 sz = msg_data_sz(msg);
1342 err = msg_errcode(msg);
1343
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001346 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 goto restart;
1348 }
1349
1350 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 set_orig_addr(m, msg);
1352
1353 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001354 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001355 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 goto exit;
1357
1358 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 if (!err) {
1360 if (unlikely(buf_len < sz)) {
1361 sz = buf_len;
1362 m->msg_flags |= MSG_TRUNC;
1363 }
David S. Miller51f3d022014-11-05 16:46:40 -05001364 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001365 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 res = sz;
1368 } else {
1369 if ((sock->state == SS_READY) ||
1370 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1371 res = 0;
1372 else
1373 res = -ECONNRESET;
1374 }
1375
1376 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001378 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001379 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001380 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001381 tsk->rcv_unacked = 0;
1382 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001383 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001384 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001386 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 return res;
1388}
1389
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001390/**
Ying Xue247f0f32014-02-18 16:06:46 +08001391 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 * @iocb: (unused)
1393 * @m: descriptor for message info
1394 * @buf_len: total size of user buffer area
1395 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001396 *
1397 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 * will optionally wait for more; never truncates data.
1399 *
1400 * Returns size of returned message data, errno otherwise
1401 */
Ying Xue247f0f32014-02-18 16:06:46 +08001402static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1403 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001405 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001406 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407 struct sk_buff *buf;
1408 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001409 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001411 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415
1416 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 if (unlikely(!buf_len))
1418 return -EINVAL;
1419
Allan Stephens0c3141e2008-04-15 00:22:02 -07001420 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001422 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423 res = -ENOTCONN;
1424 goto exit;
1425 }
1426
Florian Westphal3720d402010-08-17 11:00:04 +00001427 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001428 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001429
Allan Stephens0c3141e2008-04-15 00:22:02 -07001430restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001431 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001432 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001433 if (res)
1434 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001435
1436 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001437 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 msg = buf_msg(buf);
1439 sz = msg_data_sz(msg);
1440 err = msg_errcode(msg);
1441
1442 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001443 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001444 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445 goto restart;
1446 }
1447
1448 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 if (sz_copied == 0) {
1450 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001451 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001452 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453 goto exit;
1454 }
1455
1456 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001458 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459
Allan Stephens0232fd02011-02-21 09:45:40 -05001460 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 needed = (buf_len - sz_copied);
1462 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001463
David S. Miller51f3d022014-11-05 16:46:40 -05001464 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1465 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001466 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001468
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469 sz_copied += sz_to_copy;
1470
1471 if (sz_to_copy < sz) {
1472 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001473 TIPC_SKB_CB(buf)->handle =
1474 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 goto exit;
1476 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477 } else {
1478 if (sz_copied != 0)
1479 goto exit; /* can't add error msg to valid data */
1480
1481 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1482 res = 0;
1483 else
1484 res = -ECONNRESET;
1485 }
1486
1487 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001489 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001490 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001491 tsk->rcv_unacked = 0;
1492 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001493 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001494 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495
1496 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001497 if ((sz_copied < buf_len) && /* didn't get all requested data */
1498 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001499 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001500 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1501 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 goto restart;
1503
1504exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001505 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001506 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507}
1508
1509/**
Ying Xuef288bef2012-08-21 11:16:57 +08001510 * tipc_write_space - wake up thread if port congestion is released
1511 * @sk: socket
1512 */
1513static void tipc_write_space(struct sock *sk)
1514{
1515 struct socket_wq *wq;
1516
1517 rcu_read_lock();
1518 wq = rcu_dereference(sk->sk_wq);
1519 if (wq_has_sleeper(wq))
1520 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1521 POLLWRNORM | POLLWRBAND);
1522 rcu_read_unlock();
1523}
1524
1525/**
1526 * tipc_data_ready - wake up threads to indicate messages have been received
1527 * @sk: socket
1528 * @len: the length of messages
1529 */
David S. Miller676d2362014-04-11 16:15:36 -04001530static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001531{
1532 struct socket_wq *wq;
1533
1534 rcu_read_lock();
1535 wq = rcu_dereference(sk->sk_wq);
1536 if (wq_has_sleeper(wq))
1537 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1538 POLLRDNORM | POLLRDBAND);
1539 rcu_read_unlock();
1540}
1541
1542/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001543 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001544 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001545 * @msg: message
1546 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001547 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001548 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001549static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001550{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001551 struct sock *sk = &tsk->sk;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001552 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001553 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001554 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001555
1556 if (msg_mcast(msg))
1557 return retval;
1558
1559 switch ((int)sock->state) {
1560 case SS_CONNECTED:
1561 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001562 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001563 if (unlikely(msg_errcode(msg))) {
1564 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001565 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001566 /* let timer expire on it's own */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001567 tipc_node_remove_conn(tsk_peer_node(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08001568 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001569 }
1570 retval = TIPC_OK;
1571 }
1572 break;
1573 case SS_CONNECTING:
1574 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001575
1576 if (unlikely(!msg_connected(msg)))
1577 break;
1578
Ying Xue584d24b2012-11-29 18:51:19 -05001579 if (unlikely(msg_errcode(msg))) {
1580 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001581 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001582 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001583 break;
1584 }
1585
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001586 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001587 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001588 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001589 retval = TIPC_OK;
1590 break;
1591 }
1592
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001593 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1594 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001595 sock->state = SS_CONNECTED;
1596
Ying Xue584d24b2012-11-29 18:51:19 -05001597 /* If an incoming message is an 'ACK-', it should be
1598 * discarded here because it doesn't contain useful
1599 * data. In addition, we should try to wake up
1600 * connect() routine if sleeping.
1601 */
1602 if (msg_data_sz(msg) == 0) {
1603 kfree_skb(*buf);
1604 *buf = NULL;
1605 if (waitqueue_active(sk_sleep(sk)))
1606 wake_up_interruptible(sk_sleep(sk));
1607 }
1608 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001609 break;
1610 case SS_LISTENING:
1611 case SS_UNCONNECTED:
1612 /* Accept only SYN message */
1613 if (!msg_connected(msg) && !(msg_errcode(msg)))
1614 retval = TIPC_OK;
1615 break;
1616 case SS_DISCONNECTING:
1617 break;
1618 default:
1619 pr_err("Unknown socket state %u\n", sock->state);
1620 }
1621 return retval;
1622}
1623
1624/**
Ying Xueaba79f32013-01-20 23:30:09 +01001625 * rcvbuf_limit - get proper overload limit of socket receive queue
1626 * @sk: socket
1627 * @buf: message
1628 *
1629 * For all connection oriented messages, irrespective of importance,
1630 * the default overload value (i.e. 67MB) is set as limit.
1631 *
1632 * For all connectionless messages, by default new queue limits are
1633 * as belows:
1634 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001635 * TIPC_LOW_IMPORTANCE (4 MB)
1636 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1637 * TIPC_HIGH_IMPORTANCE (16 MB)
1638 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001639 *
1640 * Returns overload limit according to corresponding message importance
1641 */
1642static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1643{
1644 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001645
1646 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001647 return sysctl_tipc_rmem[2];
1648
1649 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1650 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001651}
1652
1653/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001654 * filter_rcv - validate incoming message
1655 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001657 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001658 * Enqueues message on receive queue if acceptable; optionally handles
1659 * disconnect indication for a connected socket.
1660 *
1661 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001662 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001663 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001664 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001666static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001668 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001669 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001671 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001672 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001673 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001675 if (unlikely(msg_user(msg) == CONN_MANAGER))
1676 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001677
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001678 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1679 kfree_skb(buf);
1680 tsk->link_cong = 0;
1681 sk->sk_write_space(sk);
1682 return TIPC_OK;
1683 }
1684
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001686 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001687 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001688
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001690 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001691 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001693 rc = filter_connect(tsk, &buf);
1694 if (rc != TIPC_OK || buf == NULL)
1695 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 }
1697
1698 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001699 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001700 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701
Ying Xueaba79f32013-01-20 23:30:09 +01001702 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001703 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001704 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001705 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001706
David S. Miller676d2362014-04-11 16:15:36 -04001707 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 return TIPC_OK;
1709}
1710
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001711/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001712 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001713 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001714 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001715 *
1716 * Caller must hold socket lock, but not port lock.
1717 *
1718 * Returns 0
1719 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001720static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001721{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001722 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001723 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001724 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001725 uint truesize = skb->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001726
Ying Xuea6ca1092014-11-26 11:41:55 +08001727 rc = filter_rcv(sk, skb);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001728
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001729 if (likely(!rc)) {
1730 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1731 atomic_add(truesize, &tsk->dupl_rcvcnt);
1732 return 0;
1733 }
1734
Ying Xuea6ca1092014-11-26 11:41:55 +08001735 if ((rc < 0) && !tipc_msg_reverse(skb, &onode, -rc))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001736 return 0;
1737
Ying Xuea6ca1092014-11-26 11:41:55 +08001738 tipc_link_xmit_skb(skb, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001739
Allan Stephens0c3141e2008-04-15 00:22:02 -07001740 return 0;
1741}
1742
1743/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001744 * tipc_sk_rcv - handle incoming message
Ying Xuea6ca1092014-11-26 11:41:55 +08001745 * @skb: buffer containing arriving message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001746 * Consumes buffer
1747 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001749int tipc_sk_rcv(struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001750{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001751 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001752 struct sock *sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001753 u32 dport = msg_destport(buf_msg(skb));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001754 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001755 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001756 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001757
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001758 /* Validate destination and message */
Ying Xue07f6c4b2015-01-07 13:41:58 +08001759 tsk = tipc_sk_lookup(dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001760 if (unlikely(!tsk)) {
Ying Xuea6ca1092014-11-26 11:41:55 +08001761 rc = tipc_msg_eval(skb, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001762 goto exit;
1763 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001764 sk = &tsk->sk;
1765
1766 /* Queue message */
Ying Xue1a194c22014-10-20 14:46:35 +08001767 spin_lock_bh(&sk->sk_lock.slock);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001768
Allan Stephens0c3141e2008-04-15 00:22:02 -07001769 if (!sock_owned_by_user(sk)) {
Ying Xuea6ca1092014-11-26 11:41:55 +08001770 rc = filter_rcv(sk, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001771 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001772 if (sk->sk_backlog.len == 0)
1773 atomic_set(&tsk->dupl_rcvcnt, 0);
Ying Xuea6ca1092014-11-26 11:41:55 +08001774 limit = rcvbuf_limit(sk, skb) + atomic_read(&tsk->dupl_rcvcnt);
1775 if (sk_add_backlog(sk, skb, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001776 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001777 }
Ying Xue1a194c22014-10-20 14:46:35 +08001778 spin_unlock_bh(&sk->sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001779 sock_put(sk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001780 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001781 return 0;
1782exit:
Ying Xuea6ca1092014-11-26 11:41:55 +08001783 if ((rc < 0) && !tipc_msg_reverse(skb, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001784 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001785
Ying Xuea6ca1092014-11-26 11:41:55 +08001786 tipc_link_xmit_skb(skb, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001787 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001788}
1789
Ying Xue78eb3a52014-01-17 09:50:03 +08001790static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1791{
1792 struct sock *sk = sock->sk;
1793 DEFINE_WAIT(wait);
1794 int done;
1795
1796 do {
1797 int err = sock_error(sk);
1798 if (err)
1799 return err;
1800 if (!*timeo_p)
1801 return -ETIMEDOUT;
1802 if (signal_pending(current))
1803 return sock_intr_errno(*timeo_p);
1804
1805 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1806 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1807 finish_wait(sk_sleep(sk), &wait);
1808 } while (!done);
1809 return 0;
1810}
1811
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812/**
Ying Xue247f0f32014-02-18 16:06:46 +08001813 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 * @sock: socket structure
1815 * @dest: socket address for destination port
1816 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001817 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001818 *
1819 * Returns 0 on success, errno otherwise
1820 */
Ying Xue247f0f32014-02-18 16:06:46 +08001821static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1822 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001824 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001825 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1826 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001827 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1828 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001829 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830
Allan Stephens0c3141e2008-04-15 00:22:02 -07001831 lock_sock(sk);
1832
Allan Stephensb89741a2008-04-15 00:20:37 -07001833 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001834 if (sock->state == SS_READY) {
1835 res = -EOPNOTSUPP;
1836 goto exit;
1837 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838
Allan Stephensb89741a2008-04-15 00:20:37 -07001839 /*
1840 * Reject connection attempt using multicast address
1841 *
1842 * Note: send_msg() validates the rest of the address fields,
1843 * so there's no need to do it here
1844 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001845 if (dst->addrtype == TIPC_ADDR_MCAST) {
1846 res = -EINVAL;
1847 goto exit;
1848 }
1849
Ying Xue78eb3a52014-01-17 09:50:03 +08001850 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001851 switch (sock->state) {
1852 case SS_UNCONNECTED:
1853 /* Send a 'SYN-' to destination */
1854 m.msg_name = dest;
1855 m.msg_namelen = destlen;
1856
1857 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1858 * indicate send_msg() is never blocked.
1859 */
1860 if (!timeout)
1861 m.msg_flags = MSG_DONTWAIT;
1862
Ying Xue247f0f32014-02-18 16:06:46 +08001863 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001864 if ((res < 0) && (res != -EWOULDBLOCK))
1865 goto exit;
1866
1867 /* Just entered SS_CONNECTING state; the only
1868 * difference is that return value in non-blocking
1869 * case is EINPROGRESS, rather than EALREADY.
1870 */
1871 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001872 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001873 if (previous == SS_CONNECTING)
1874 res = -EALREADY;
1875 if (!timeout)
1876 goto exit;
1877 timeout = msecs_to_jiffies(timeout);
1878 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1879 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001880 break;
1881 case SS_CONNECTED:
1882 res = -EISCONN;
1883 break;
1884 default:
1885 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001886 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001887 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001888exit:
1889 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001890 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891}
1892
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001893/**
Ying Xue247f0f32014-02-18 16:06:46 +08001894 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 * @sock: socket structure
1896 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001897 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898 * Returns 0 on success, errno otherwise
1899 */
Ying Xue247f0f32014-02-18 16:06:46 +08001900static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001902 struct sock *sk = sock->sk;
1903 int res;
1904
1905 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906
Ying Xue245f3d32011-07-06 06:01:13 -04001907 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001908 res = -EINVAL;
1909 else {
1910 sock->state = SS_LISTENING;
1911 res = 0;
1912 }
1913
1914 release_sock(sk);
1915 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916}
1917
Ying Xue6398e232014-01-17 09:50:04 +08001918static int tipc_wait_for_accept(struct socket *sock, long timeo)
1919{
1920 struct sock *sk = sock->sk;
1921 DEFINE_WAIT(wait);
1922 int err;
1923
1924 /* True wake-one mechanism for incoming connections: only
1925 * one process gets woken up, not the 'whole herd'.
1926 * Since we do not 'race & poll' for established sockets
1927 * anymore, the common case will execute the loop only once.
1928 */
1929 for (;;) {
1930 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1931 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001932 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001933 release_sock(sk);
1934 timeo = schedule_timeout(timeo);
1935 lock_sock(sk);
1936 }
1937 err = 0;
1938 if (!skb_queue_empty(&sk->sk_receive_queue))
1939 break;
1940 err = -EINVAL;
1941 if (sock->state != SS_LISTENING)
1942 break;
1943 err = sock_intr_errno(timeo);
1944 if (signal_pending(current))
1945 break;
1946 err = -EAGAIN;
1947 if (!timeo)
1948 break;
1949 }
1950 finish_wait(sk_sleep(sk), &wait);
1951 return err;
1952}
1953
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001954/**
Ying Xue247f0f32014-02-18 16:06:46 +08001955 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001956 * @sock: listening socket
1957 * @newsock: new socket that is to be connected
1958 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001959 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001960 * Returns 0 on success, errno otherwise
1961 */
Ying Xue247f0f32014-02-18 16:06:46 +08001962static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001963{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001964 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001966 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001967 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001968 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001969 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970
Allan Stephens0c3141e2008-04-15 00:22:02 -07001971 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001972
Allan Stephens0c3141e2008-04-15 00:22:02 -07001973 if (sock->state != SS_LISTENING) {
1974 res = -EINVAL;
1975 goto exit;
1976 }
Ying Xue6398e232014-01-17 09:50:04 +08001977 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1978 res = tipc_wait_for_accept(sock, timeo);
1979 if (res)
1980 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001981
1982 buf = skb_peek(&sk->sk_receive_queue);
1983
Ying Xuec5fa7b32013-06-17 10:54:39 -04001984 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001985 if (res)
1986 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001987
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001988 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001989 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001990 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001991
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001992 /* we lock on new_sk; but lockdep sees the lock on sk */
1993 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001994
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001995 /*
1996 * Reject any stray messages received by new socket
1997 * before the socket lock was taken (very, very unlikely)
1998 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001999 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002001 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002002 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002003 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002005 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002006 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002007 new_tsock->conn_type = msg_nametype(msg);
2008 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002009 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002010
2011 /*
2012 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2013 * Respond to 'SYN+' by queuing it on new socket.
2014 */
2015 if (!msg_data_sz(msg)) {
2016 struct msghdr m = {NULL,};
2017
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002018 tsk_advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08002019 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002020 } else {
2021 __skb_dequeue(&sk->sk_receive_queue);
2022 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002023 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002024 }
2025 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002026exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002027 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002028 return res;
2029}
2030
2031/**
Ying Xue247f0f32014-02-18 16:06:46 +08002032 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002033 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002034 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002035 *
2036 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002037 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002038 * Returns 0 on success, errno otherwise
2039 */
Ying Xue247f0f32014-02-18 16:06:46 +08002040static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002041{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002042 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002043 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002044 struct sk_buff *skb;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002045 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002046 int res;
2047
Allan Stephense247a8f2008-03-06 15:05:38 -08002048 if (how != SHUT_RDWR)
2049 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002050
Allan Stephens0c3141e2008-04-15 00:22:02 -07002051 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002052
2053 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002054 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 case SS_CONNECTED:
2056
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002058 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002059 skb = __skb_dequeue(&sk->sk_receive_queue);
2060 if (skb) {
2061 if (TIPC_SKB_CB(skb)->handle != NULL) {
2062 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063 goto restart;
2064 }
Ying Xuea6ca1092014-11-26 11:41:55 +08002065 if (tipc_msg_reverse(skb, &dnode, TIPC_CONN_SHUTDOWN))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002066 tipc_link_xmit_skb(skb, dnode, tsk->portid);
2067 tipc_node_remove_conn(dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002068 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002069 dnode = tsk_peer_node(tsk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002070 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002071 TIPC_CONN_MSG, SHORT_H_SIZE,
2072 0, dnode, tipc_own_addr,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002073 tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002074 tsk->portid, TIPC_CONN_SHUTDOWN);
2075 tipc_link_xmit_skb(skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002077 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002078 sock->state = SS_DISCONNECTING;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002079 tipc_node_remove_conn(dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080 /* fall through */
2081
2082 case SS_DISCONNECTING:
2083
Ying Xue75031152012-10-29 09:38:15 -04002084 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002085 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002086
2087 /* Wake up anyone sleeping in poll */
2088 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089 res = 0;
2090 break;
2091
2092 default:
2093 res = -ENOTCONN;
2094 }
2095
Allan Stephens0c3141e2008-04-15 00:22:02 -07002096 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002097 return res;
2098}
2099
Ying Xue07f6c4b2015-01-07 13:41:58 +08002100static void tipc_sk_timeout(unsigned long portid)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002101{
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002102 struct tipc_sock *tsk;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002103 struct sock *sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002104 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002105 u32 peer_port, peer_node;
2106
Ying Xue07f6c4b2015-01-07 13:41:58 +08002107 tsk = tipc_sk_lookup(portid);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04002108 if (!tsk)
Ying Xuecc086fc2014-08-28 10:02:41 +08002109 return;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002110
Ying Xuecc086fc2014-08-28 10:02:41 +08002111 sk = &tsk->sk;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002112 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002113 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002114 bh_unlock_sock(sk);
2115 goto exit;
2116 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002117 peer_port = tsk_peer_port(tsk);
2118 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002119
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002120 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002121 /* Previous probe not answered -> self abort */
Ying Xuea6ca1092014-11-26 11:41:55 +08002122 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
Jon Paul Maloy57289012014-08-22 18:09:09 -04002123 SHORT_H_SIZE, 0, tipc_own_addr,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002124 peer_node, portid, peer_port,
Jon Paul Maloy57289012014-08-22 18:09:09 -04002125 TIPC_ERR_NO_PORT);
2126 } else {
Ying Xuea6ca1092014-11-26 11:41:55 +08002127 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
Jon Paul Maloy57289012014-08-22 18:09:09 -04002128 0, peer_node, tipc_own_addr,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002129 peer_port, portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002130 tsk->probing_state = TIPC_CONN_PROBING;
2131 k_start_timer(&tsk->timer, tsk->probing_interval);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002132 }
2133 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002134 if (skb)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002135 tipc_link_xmit_skb(skb, peer_node, portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002136exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002137 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002138}
2139
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002140static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002141 struct tipc_name_seq const *seq)
2142{
2143 struct publication *publ;
2144 u32 key;
2145
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002146 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002147 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002148 key = tsk->portid + tsk->pub_count + 1;
2149 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002150 return -EADDRINUSE;
2151
2152 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002153 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002154 if (unlikely(!publ))
2155 return -EINVAL;
2156
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002157 list_add(&publ->pport_list, &tsk->publications);
2158 tsk->pub_count++;
2159 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002160 return 0;
2161}
2162
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002163static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002164 struct tipc_name_seq const *seq)
2165{
2166 struct publication *publ;
2167 struct publication *safe;
2168 int rc = -EINVAL;
2169
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002170 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002171 if (seq) {
2172 if (publ->scope != scope)
2173 continue;
2174 if (publ->type != seq->type)
2175 continue;
2176 if (publ->lower != seq->lower)
2177 continue;
2178 if (publ->upper != seq->upper)
2179 break;
2180 tipc_nametbl_withdraw(publ->type, publ->lower,
2181 publ->ref, publ->key);
2182 rc = 0;
2183 break;
2184 }
2185 tipc_nametbl_withdraw(publ->type, publ->lower,
2186 publ->ref, publ->key);
2187 rc = 0;
2188 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002189 if (list_empty(&tsk->publications))
2190 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002191 return rc;
2192}
2193
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002194static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002195 int len, int full_id)
2196{
2197 struct publication *publ;
2198 int ret;
2199
2200 if (full_id)
2201 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2202 tipc_zone(tipc_own_addr),
2203 tipc_cluster(tipc_own_addr),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002204 tipc_node(tipc_own_addr), tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002205 else
Ying Xue07f6c4b2015-01-07 13:41:58 +08002206 ret = tipc_snprintf(buf, len, "%-10u:", tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002207
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002208 if (tsk->connected) {
2209 u32 dport = tsk_peer_port(tsk);
2210 u32 destnode = tsk_peer_node(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002211
2212 ret += tipc_snprintf(buf + ret, len - ret,
2213 " connected to <%u.%u.%u:%u>",
2214 tipc_zone(destnode),
2215 tipc_cluster(destnode),
2216 tipc_node(destnode), dport);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002217 if (tsk->conn_type != 0)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002218 ret += tipc_snprintf(buf + ret, len - ret,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002219 " via {%u,%u}", tsk->conn_type,
2220 tsk->conn_instance);
2221 } else if (tsk->published) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002222 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002223 list_for_each_entry(publ, &tsk->publications, pport_list) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002224 if (publ->lower == publ->upper)
2225 ret += tipc_snprintf(buf + ret, len - ret,
2226 " {%u,%u}", publ->type,
2227 publ->lower);
2228 else
2229 ret += tipc_snprintf(buf + ret, len - ret,
2230 " {%u,%u,%u}", publ->type,
2231 publ->lower, publ->upper);
2232 }
2233 }
2234 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2235 return ret;
2236}
2237
2238struct sk_buff *tipc_sk_socks_show(void)
2239{
Ying Xue07f6c4b2015-01-07 13:41:58 +08002240 const struct bucket_table *tbl;
2241 struct rhash_head *pos;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002242 struct sk_buff *buf;
2243 struct tlv_desc *rep_tlv;
2244 char *pb;
2245 int pb_len;
2246 struct tipc_sock *tsk;
2247 int str_len = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002248 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002249
2250 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2251 if (!buf)
2252 return NULL;
2253 rep_tlv = (struct tlv_desc *)buf->data;
2254 pb = TLV_DATA(rep_tlv);
2255 pb_len = ULTRA_STRING_MAX_LEN;
2256
Ying Xue07f6c4b2015-01-07 13:41:58 +08002257 rcu_read_lock();
2258 tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht);
2259 for (i = 0; i < tbl->size; i++) {
2260 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2261 spin_lock_bh(&tsk->sk.sk_lock.slock);
2262 str_len += tipc_sk_show(tsk, pb + str_len,
2263 pb_len - str_len, 0);
2264 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2265 }
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002266 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002267 rcu_read_unlock();
2268
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002269 str_len += 1; /* for "\0" */
2270 skb_put(buf, TLV_SPACE(str_len));
2271 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2272
2273 return buf;
2274}
2275
2276/* tipc_sk_reinit: set non-zero address in all existing sockets
2277 * when we go from standalone to network mode.
2278 */
2279void tipc_sk_reinit(void)
2280{
Ying Xue07f6c4b2015-01-07 13:41:58 +08002281 const struct bucket_table *tbl;
2282 struct rhash_head *pos;
2283 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002284 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002285 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002286
Ying Xue07f6c4b2015-01-07 13:41:58 +08002287 rcu_read_lock();
2288 tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht);
2289 for (i = 0; i < tbl->size; i++) {
2290 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2291 spin_lock_bh(&tsk->sk.sk_lock.slock);
2292 msg = &tsk->phdr;
2293 msg_set_prevnode(msg, tipc_own_addr);
2294 msg_set_orignode(msg, tipc_own_addr);
2295 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2296 }
2297 }
2298 rcu_read_unlock();
2299}
2300
2301static struct tipc_sock *tipc_sk_lookup(u32 portid)
2302{
2303 struct tipc_sock *tsk;
2304
2305 rcu_read_lock();
2306 tsk = rhashtable_lookup(&tipc_sk_rht, &portid);
2307 if (tsk)
2308 sock_hold(&tsk->sk);
2309 rcu_read_unlock();
2310
2311 return tsk;
2312}
2313
2314static int tipc_sk_insert(struct tipc_sock *tsk)
2315{
2316 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2317 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2318
2319 while (remaining--) {
2320 portid++;
2321 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2322 portid = TIPC_MIN_PORT;
2323 tsk->portid = portid;
2324 sock_hold(&tsk->sk);
2325 if (rhashtable_lookup_insert(&tipc_sk_rht, &tsk->node))
2326 return 0;
2327 sock_put(&tsk->sk);
2328 }
2329
2330 return -1;
2331}
2332
2333static void tipc_sk_remove(struct tipc_sock *tsk)
2334{
2335 struct sock *sk = &tsk->sk;
2336
2337 if (rhashtable_remove(&tipc_sk_rht, &tsk->node)) {
2338 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2339 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002340 }
2341}
2342
Ying Xue07f6c4b2015-01-07 13:41:58 +08002343int tipc_sk_rht_init(void)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002344{
Ying Xue07f6c4b2015-01-07 13:41:58 +08002345 struct rhashtable_params rht_params = {
2346 .nelem_hint = 192,
2347 .head_offset = offsetof(struct tipc_sock, node),
2348 .key_offset = offsetof(struct tipc_sock, portid),
2349 .key_len = sizeof(u32), /* portid */
2350 .hashfn = jhash,
2351 .max_shift = 20, /* 1M */
2352 .min_shift = 8, /* 256 */
2353 .grow_decision = rht_grow_above_75,
2354 .shrink_decision = rht_shrink_below_30,
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002355 };
2356
Ying Xue07f6c4b2015-01-07 13:41:58 +08002357 return rhashtable_init(&tipc_sk_rht, &rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002358}
2359
Ying Xue07f6c4b2015-01-07 13:41:58 +08002360void tipc_sk_rht_destroy(void)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002361{
Ying Xue07f6c4b2015-01-07 13:41:58 +08002362 /* Wait for socket readers to complete */
2363 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002364
Ying Xue07f6c4b2015-01-07 13:41:58 +08002365 rhashtable_destroy(&tipc_sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002366}
2367
2368/**
Ying Xue247f0f32014-02-18 16:06:46 +08002369 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002370 * @sock: socket structure
2371 * @lvl: option level
2372 * @opt: option identifier
2373 * @ov: pointer to new option value
2374 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002375 *
2376 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002377 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002378 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002379 * Returns 0 on success, errno otherwise
2380 */
Ying Xue247f0f32014-02-18 16:06:46 +08002381static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2382 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002383{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002384 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002385 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002386 u32 value;
2387 int res;
2388
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002389 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2390 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002391 if (lvl != SOL_TIPC)
2392 return -ENOPROTOOPT;
2393 if (ol < sizeof(value))
2394 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002395 res = get_user(value, (u32 __user *)ov);
2396 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002397 return res;
2398
Allan Stephens0c3141e2008-04-15 00:22:02 -07002399 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002400
Per Lidenb97bf3f2006-01-02 19:04:38 +01002401 switch (opt) {
2402 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002403 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002404 break;
2405 case TIPC_SRC_DROPPABLE:
2406 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002407 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002408 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409 res = -ENOPROTOOPT;
2410 break;
2411 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002412 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002413 break;
2414 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002415 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002416 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002417 break;
2418 default:
2419 res = -EINVAL;
2420 }
2421
Allan Stephens0c3141e2008-04-15 00:22:02 -07002422 release_sock(sk);
2423
Per Lidenb97bf3f2006-01-02 19:04:38 +01002424 return res;
2425}
2426
2427/**
Ying Xue247f0f32014-02-18 16:06:46 +08002428 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002429 * @sock: socket structure
2430 * @lvl: option level
2431 * @opt: option identifier
2432 * @ov: receptacle for option value
2433 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002434 *
2435 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002436 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002437 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002438 * Returns 0 on success, errno otherwise
2439 */
Ying Xue247f0f32014-02-18 16:06:46 +08002440static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2441 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002442{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002443 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002444 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002445 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002446 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002447 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002449 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2450 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002451 if (lvl != SOL_TIPC)
2452 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002453 res = get_user(len, ol);
2454 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002455 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002456
Allan Stephens0c3141e2008-04-15 00:22:02 -07002457 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002458
2459 switch (opt) {
2460 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002461 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002462 break;
2463 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002464 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002465 break;
2466 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002467 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002468 break;
2469 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002470 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002471 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002472 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002473 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002474 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002475 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002476 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002477 value = skb_queue_len(&sk->sk_receive_queue);
2478 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002479 default:
2480 res = -EINVAL;
2481 }
2482
Allan Stephens0c3141e2008-04-15 00:22:02 -07002483 release_sock(sk);
2484
Paul Gortmaker25860c32010-12-31 18:59:31 +00002485 if (res)
2486 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002487
Paul Gortmaker25860c32010-12-31 18:59:31 +00002488 if (len < sizeof(value))
2489 return -EINVAL;
2490
2491 if (copy_to_user(ov, &value, sizeof(value)))
2492 return -EFAULT;
2493
2494 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002495}
2496
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002497static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002498{
2499 struct tipc_sioc_ln_req lnr;
2500 void __user *argp = (void __user *)arg;
2501
2502 switch (cmd) {
2503 case SIOCGETLINKNAME:
2504 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2505 return -EFAULT;
Ying Xue7b8613e2014-10-20 14:44:25 +08002506 if (!tipc_node_get_linkname(lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002507 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2508 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2509 return -EFAULT;
2510 return 0;
2511 }
2512 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002513 default:
2514 return -ENOIOCTLCMD;
2515 }
2516}
2517
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002518/* Protocol switches for the various types of TIPC sockets */
2519
Florian Westphalbca65ea2008-02-07 18:18:01 -08002520static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002521 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002522 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002523 .release = tipc_release,
2524 .bind = tipc_bind,
2525 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002526 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002527 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002528 .getname = tipc_getname,
2529 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002530 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002531 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002532 .shutdown = tipc_shutdown,
2533 .setsockopt = tipc_setsockopt,
2534 .getsockopt = tipc_getsockopt,
2535 .sendmsg = tipc_sendmsg,
2536 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002537 .mmap = sock_no_mmap,
2538 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002539};
2540
Florian Westphalbca65ea2008-02-07 18:18:01 -08002541static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002542 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002543 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002544 .release = tipc_release,
2545 .bind = tipc_bind,
2546 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002547 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002548 .accept = tipc_accept,
2549 .getname = tipc_getname,
2550 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002551 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002552 .listen = tipc_listen,
2553 .shutdown = tipc_shutdown,
2554 .setsockopt = tipc_setsockopt,
2555 .getsockopt = tipc_getsockopt,
2556 .sendmsg = tipc_send_packet,
2557 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002558 .mmap = sock_no_mmap,
2559 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002560};
2561
Florian Westphalbca65ea2008-02-07 18:18:01 -08002562static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002563 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002564 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002565 .release = tipc_release,
2566 .bind = tipc_bind,
2567 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002568 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002569 .accept = tipc_accept,
2570 .getname = tipc_getname,
2571 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002572 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002573 .listen = tipc_listen,
2574 .shutdown = tipc_shutdown,
2575 .setsockopt = tipc_setsockopt,
2576 .getsockopt = tipc_getsockopt,
2577 .sendmsg = tipc_send_stream,
2578 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002579 .mmap = sock_no_mmap,
2580 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002581};
2582
Florian Westphalbca65ea2008-02-07 18:18:01 -08002583static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002584 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002585 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002586 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002587};
2588
2589static struct proto tipc_proto = {
2590 .name = "TIPC",
2591 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002592 .obj_size = sizeof(struct tipc_sock),
2593 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002594};
2595
Ying Xuec5fa7b32013-06-17 10:54:39 -04002596static struct proto tipc_proto_kern = {
2597 .name = "TIPC",
2598 .obj_size = sizeof(struct tipc_sock),
2599 .sysctl_rmem = sysctl_tipc_rmem
2600};
2601
Per Lidenb97bf3f2006-01-02 19:04:38 +01002602/**
Per Liden4323add2006-01-18 00:38:21 +01002603 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002604 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002605 * Returns 0 on success, errno otherwise
2606 */
Per Liden4323add2006-01-18 00:38:21 +01002607int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002608{
2609 int res;
2610
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002611 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002612 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002613 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002614 goto out;
2615 }
2616
2617 res = sock_register(&tipc_family_ops);
2618 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002619 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002620 proto_unregister(&tipc_proto);
2621 goto out;
2622 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002623 out:
2624 return res;
2625}
2626
2627/**
Per Liden4323add2006-01-18 00:38:21 +01002628 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002629 */
Per Liden4323add2006-01-18 00:38:21 +01002630void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002631{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002632 sock_unregister(tipc_family_ops.family);
2633 proto_unregister(&tipc_proto);
2634}
Richard Alpe34b78a122014-11-20 10:29:10 +01002635
2636/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002637static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002638{
2639 u32 peer_node;
2640 u32 peer_port;
2641 struct nlattr *nest;
2642
2643 peer_node = tsk_peer_node(tsk);
2644 peer_port = tsk_peer_port(tsk);
2645
2646 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2647
2648 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2649 goto msg_full;
2650 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2651 goto msg_full;
2652
2653 if (tsk->conn_type != 0) {
2654 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2655 goto msg_full;
2656 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2657 goto msg_full;
2658 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2659 goto msg_full;
2660 }
2661 nla_nest_end(skb, nest);
2662
2663 return 0;
2664
2665msg_full:
2666 nla_nest_cancel(skb, nest);
2667
2668 return -EMSGSIZE;
2669}
2670
2671/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002672static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2673 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002674{
2675 int err;
2676 void *hdr;
2677 struct nlattr *attrs;
2678
2679 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2680 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2681 if (!hdr)
2682 goto msg_cancel;
2683
2684 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2685 if (!attrs)
2686 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002687 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002688 goto attr_msg_cancel;
2689 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr))
2690 goto attr_msg_cancel;
2691
2692 if (tsk->connected) {
2693 err = __tipc_nl_add_sk_con(skb, tsk);
2694 if (err)
2695 goto attr_msg_cancel;
2696 } else if (!list_empty(&tsk->publications)) {
2697 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2698 goto attr_msg_cancel;
2699 }
2700 nla_nest_end(skb, attrs);
2701 genlmsg_end(skb, hdr);
2702
2703 return 0;
2704
2705attr_msg_cancel:
2706 nla_nest_cancel(skb, attrs);
2707genlmsg_cancel:
2708 genlmsg_cancel(skb, hdr);
2709msg_cancel:
2710 return -EMSGSIZE;
2711}
2712
2713int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2714{
2715 int err;
2716 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002717 const struct bucket_table *tbl;
2718 struct rhash_head *pos;
2719 u32 prev_portid = cb->args[0];
2720 u32 portid = prev_portid;
2721 int i;
Richard Alpe34b78a122014-11-20 10:29:10 +01002722
Ying Xue07f6c4b2015-01-07 13:41:58 +08002723 rcu_read_lock();
2724 tbl = rht_dereference_rcu((&tipc_sk_rht)->tbl, &tipc_sk_rht);
2725 for (i = 0; i < tbl->size; i++) {
2726 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2727 spin_lock_bh(&tsk->sk.sk_lock.slock);
2728 portid = tsk->portid;
2729 err = __tipc_nl_add_sk(skb, cb, tsk);
2730 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2731 if (err)
2732 break;
Richard Alpe34b78a122014-11-20 10:29:10 +01002733
Ying Xue07f6c4b2015-01-07 13:41:58 +08002734 prev_portid = portid;
2735 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002736 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002737 rcu_read_unlock();
Richard Alpe34b78a122014-11-20 10:29:10 +01002738
Ying Xue07f6c4b2015-01-07 13:41:58 +08002739 cb->args[0] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002740
2741 return skb->len;
2742}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002743
2744/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002745static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2746 struct netlink_callback *cb,
2747 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002748{
2749 void *hdr;
2750 struct nlattr *attrs;
2751
2752 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2753 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2754 if (!hdr)
2755 goto msg_cancel;
2756
2757 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2758 if (!attrs)
2759 goto genlmsg_cancel;
2760
2761 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2762 goto attr_msg_cancel;
2763 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2764 goto attr_msg_cancel;
2765 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2766 goto attr_msg_cancel;
2767 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2768 goto attr_msg_cancel;
2769
2770 nla_nest_end(skb, attrs);
2771 genlmsg_end(skb, hdr);
2772
2773 return 0;
2774
2775attr_msg_cancel:
2776 nla_nest_cancel(skb, attrs);
2777genlmsg_cancel:
2778 genlmsg_cancel(skb, hdr);
2779msg_cancel:
2780 return -EMSGSIZE;
2781}
2782
2783/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002784static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2785 struct netlink_callback *cb,
2786 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002787{
2788 int err;
2789 struct publication *p;
2790
2791 if (*last_publ) {
2792 list_for_each_entry(p, &tsk->publications, pport_list) {
2793 if (p->key == *last_publ)
2794 break;
2795 }
2796 if (p->key != *last_publ) {
2797 /* We never set seq or call nl_dump_check_consistent()
2798 * this means that setting prev_seq here will cause the
2799 * consistence check to fail in the netlink callback
2800 * handler. Resulting in the last NLMSG_DONE message
2801 * having the NLM_F_DUMP_INTR flag set.
2802 */
2803 cb->prev_seq = 1;
2804 *last_publ = 0;
2805 return -EPIPE;
2806 }
2807 } else {
2808 p = list_first_entry(&tsk->publications, struct publication,
2809 pport_list);
2810 }
2811
2812 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2813 err = __tipc_nl_add_sk_publ(skb, cb, p);
2814 if (err) {
2815 *last_publ = p->key;
2816 return err;
2817 }
2818 }
2819 *last_publ = 0;
2820
2821 return 0;
2822}
2823
2824int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2825{
2826 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002827 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002828 u32 last_publ = cb->args[1];
2829 u32 done = cb->args[2];
2830 struct tipc_sock *tsk;
2831
Ying Xue07f6c4b2015-01-07 13:41:58 +08002832 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002833 struct nlattr **attrs;
2834 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2835
2836 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2837 if (err)
2838 return err;
2839
2840 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2841 attrs[TIPC_NLA_SOCK],
2842 tipc_nl_sock_policy);
2843 if (err)
2844 return err;
2845
2846 if (!sock[TIPC_NLA_SOCK_REF])
2847 return -EINVAL;
2848
Ying Xue07f6c4b2015-01-07 13:41:58 +08002849 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002850 }
2851
2852 if (done)
2853 return 0;
2854
Ying Xue07f6c4b2015-01-07 13:41:58 +08002855 tsk = tipc_sk_lookup(tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002856 if (!tsk)
2857 return -EINVAL;
2858
2859 lock_sock(&tsk->sk);
2860 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2861 if (!err)
2862 done = 1;
2863 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002864 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002865
Ying Xue07f6c4b2015-01-07 13:41:58 +08002866 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002867 cb->args[1] = last_publ;
2868 cb->args[2] = done;
2869
2870 return skb->len;
2871}