blob: 1d98bfcda6f6fac0e809ba892dab06ca747f279c [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Ying Xue07f6c4b2015-01-07 13:41:58 +080037#include <linux/rhashtable.h>
38#include <linux/jhash.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050040#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020041#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050042#include "link.h"
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -040043#include "config.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040044#include "socket.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040045
Ying Xue07f6c4b2015-01-07 13:41:58 +080046#define SS_LISTENING -1 /* socket is listening */
47#define SS_READY -2 /* socket is connectionless */
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
Ying Xue07f6c4b2015-01-07 13:41:58 +080049#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080050#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080051#define TIPC_FWD_MSG 1
52#define TIPC_CONN_OK 0
53#define TIPC_CONN_PROBING 1
54#define TIPC_MAX_PORT 0xffffffff
55#define TIPC_MIN_PORT 1
Jon Paul Maloy301bae52014-08-22 18:09:20 -040056
57/**
58 * struct tipc_sock - TIPC socket structure
59 * @sk: socket - interacts with 'port' and with user via the socket API
60 * @connected: non-zero if port is currently connected to a peer port
61 * @conn_type: TIPC type used when connection was established
62 * @conn_instance: TIPC instance used when connection was established
63 * @published: non-zero if port has one or more associated names
64 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080065 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040066 * @phdr: preformatted message header used when sending messages
67 * @port_list: adjacent ports in TIPC's global list of ports
68 * @publications: list of publications for port
69 * @pub_count: total # of publications port has made during its lifetime
70 * @probing_state:
Ying Xue2f55c432015-01-09 15:27:00 +080071 * @probing_intv:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040072 * @port: port - interacts with 'sk' and with the rest of the TIPC stack
73 * @peer_name: the peer of the connection, if any
74 * @conn_timeout: the time we can wait for an unresponded setup request
75 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
76 * @link_cong: non-zero if owner must sleep because of link congestion
77 * @sent_unacked: # messages sent by socket, and not yet acked by peer
78 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Ying Xue07f6c4b2015-01-07 13:41:58 +080079 * @node: hash table node
80 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040081 */
82struct tipc_sock {
83 struct sock sk;
84 int connected;
85 u32 conn_type;
86 u32 conn_instance;
87 int published;
88 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080089 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040090 struct tipc_msg phdr;
91 struct list_head sock_list;
92 struct list_head publications;
93 u32 pub_count;
94 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080095 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 uint conn_timeout;
97 atomic_t dupl_rcvcnt;
98 bool link_cong;
99 uint sent_unacked;
100 uint rcv_unacked;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800101 struct rhash_head node;
102 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400103};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400105static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400106static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800107static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800108static int tipc_release(struct socket *sock);
109static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400110static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Ying Xuef2f2a962015-01-09 15:27:02 +0800111static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400112static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400113 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400115 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800116static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800117static int tipc_sk_insert(struct tipc_sock *tsk);
118static void tipc_sk_remove(struct tipc_sock *tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119
Florian Westphalbca65ea2008-02-07 18:18:01 -0800120static const struct proto_ops packet_ops;
121static const struct proto_ops stream_ops;
122static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123
124static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400125static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126
Richard Alpe1a1a1432014-11-20 10:29:11 +0100127static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
128 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
129 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
130 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
131 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
132 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
133};
134
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900135/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700136 * Revised TIPC socket locking policy:
137 *
138 * Most socket operations take the standard socket lock when they start
139 * and hold it until they finish (or until they need to sleep). Acquiring
140 * this lock grants the owner exclusive access to the fields of the socket
141 * data structures, with the exception of the backlog queue. A few socket
142 * operations can be done without taking the socket lock because they only
143 * read socket information that never changes during the life of the socket.
144 *
145 * Socket operations may acquire the lock for the associated TIPC port if they
146 * need to perform an operation on the port. If any routine needs to acquire
147 * both the socket lock and the port lock it must take the socket lock first
148 * to avoid the risk of deadlock.
149 *
150 * The dispatcher handling incoming messages cannot grab the socket lock in
151 * the standard fashion, since invoked it runs at the BH level and cannot block.
152 * Instead, it checks to see if the socket lock is currently owned by someone,
153 * and either handles the message itself or adds it to the socket's backlog
154 * queue; in the latter case the queued message is processed once the process
155 * owning the socket lock releases it.
156 *
157 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
158 * the problem of a blocked socket operation preventing any other operations
159 * from occurring. However, applications must be careful if they have
160 * multiple threads trying to send (or receive) on the same socket, as these
161 * operations might interfere with each other. For example, doing a connect
162 * and a receive at the same time might allow the receive to consume the
163 * ACK message meant for the connect. While additional work could be done
164 * to try and overcome this, it doesn't seem to be worthwhile at the present.
165 *
166 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
167 * that another operation that must be performed in a non-blocking manner is
168 * not delayed for very long because the lock has already been taken.
169 *
170 * NOTE: This code assumes that certain fields of a port/socket pair are
171 * constant over its lifetime; such fields can be examined without taking
172 * the socket lock and/or port lock, and do not need to be re-read even
173 * after resuming processing after waiting. These fields include:
174 * - socket type
175 * - pointer to socket sk structure (aka tipc_sock structure)
176 * - pointer to port structure
177 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500180static u32 tsk_own_node(struct tipc_sock *tsk)
181{
182 return msg_prevnode(&tsk->phdr);
183}
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;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500257 u32 own_node = tsk_own_node(tipc_sk(sk));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700258
Ying Xuea6ca1092014-11-26 11:41:55 +0800259 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500260 if (tipc_msg_reverse(own_node, skb, &dnode, TIPC_ERR_NO_PORT))
261 tipc_link_xmit_skb(sock_net(sk), skb, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500262 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700263}
264
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400265/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400266 *
267 * Handles cases where the node's network address has changed from
268 * the default of <0.0.0> to its configured setting.
269 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400270static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400271{
Ying Xue34747532015-01-09 15:27:10 +0800272 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400273 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400274 u32 orig_node;
275 u32 peer_node;
276
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400277 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400278 return false;
279
280 if (unlikely(msg_origport(msg) != peer_port))
281 return false;
282
283 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400284 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400285
286 if (likely(orig_node == peer_node))
287 return true;
288
Ying Xue34747532015-01-09 15:27:10 +0800289 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400290 return true;
291
Ying Xue34747532015-01-09 15:27:10 +0800292 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400293 return true;
294
295 return false;
296}
297
Allan Stephens0c3141e2008-04-15 00:22:02 -0700298/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400299 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700300 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 * @sock: pre-allocated socket structure
302 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800303 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900304 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700305 * This routine creates additional data structures used by the TIPC socket,
306 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 *
308 * Returns 0 on success, errno otherwise
309 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400310static int tipc_sk_create(struct net *net, struct socket *sock,
311 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500313 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 const struct proto_ops *ops;
315 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400317 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400318 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700319
320 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 if (unlikely(protocol != 0))
322 return -EPROTONOSUPPORT;
323
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 switch (sock->type) {
325 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700326 ops = &stream_ops;
327 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 break;
329 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 ops = &packet_ops;
331 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 break;
333 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 ops = &msg_ops;
336 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 break;
Allan Stephens49978652006-06-25 23:47:18 -0700338 default:
Allan Stephens49978652006-06-25 23:47:18 -0700339 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 }
341
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400343 if (!kern)
344 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
345 else
346 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
347
Allan Stephens0c3141e2008-04-15 00:22:02 -0700348 if (sk == NULL)
349 return -ENOMEM;
350
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400351 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400352 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400353 INIT_LIST_HEAD(&tsk->publications);
354 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500355 tn = net_generic(sock_net(sk), tipc_net_id);
356 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400357 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358
Allan Stephens0c3141e2008-04-15 00:22:02 -0700359 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 sock->ops = ops;
361 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800363 if (tipc_sk_insert(tsk)) {
364 pr_warn("Socket create failed; port numbrer exhausted\n");
365 return -EINVAL;
366 }
367 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800368 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400369 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400370 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800371 sk->sk_data_ready = tipc_data_ready;
372 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400373 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500374 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400375 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700376
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400378 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700379 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400380 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700381 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 return 0;
383}
384
385/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400386 * tipc_sock_create_local - create TIPC socket from inside TIPC module
387 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
388 *
389 * We cannot use sock_creat_kern here because it bumps module user count.
390 * Since socket owner and creator is the same module we must make sure
391 * that module count remains zero for module local sockets, otherwise
392 * we cannot do rmmod.
393 *
394 * Returns 0 on success, errno otherwise
395 */
Ying Xuea62fbcc2015-01-09 15:27:11 +0800396int tipc_sock_create_local(struct net *net, int type, struct socket **res)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400397{
398 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400399
400 rc = sock_create_lite(AF_TIPC, type, 0, res);
401 if (rc < 0) {
402 pr_err("Failed to create kernel socket\n");
403 return rc;
404 }
Ying Xuea62fbcc2015-01-09 15:27:11 +0800405 tipc_sk_create(net, *res, 0, 1);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400406
Ying Xuec5fa7b32013-06-17 10:54:39 -0400407 return 0;
408}
409
410/**
411 * tipc_sock_release_local - release socket created by tipc_sock_create_local
412 * @sock: the socket to be released.
413 *
414 * Module reference count is not incremented when such sockets are created,
415 * so we must keep it from being decremented when they are released.
416 */
417void tipc_sock_release_local(struct socket *sock)
418{
Ying Xue247f0f32014-02-18 16:06:46 +0800419 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400420 sock->ops = NULL;
421 sock_release(sock);
422}
423
424/**
425 * tipc_sock_accept_local - accept a connection on a socket created
426 * with tipc_sock_create_local. Use this function to avoid that
427 * module reference count is inadvertently incremented.
428 *
429 * @sock: the accepting socket
430 * @newsock: reference to the new socket to be created
431 * @flags: socket flags
432 */
433
434int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400435 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400436{
437 struct sock *sk = sock->sk;
438 int ret;
439
440 ret = sock_create_lite(sk->sk_family, sk->sk_type,
441 sk->sk_protocol, newsock);
442 if (ret < 0)
443 return ret;
444
Ying Xue247f0f32014-02-18 16:06:46 +0800445 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400446 if (ret < 0) {
447 sock_release(*newsock);
448 return ret;
449 }
450 (*newsock)->ops = sock->ops;
451 return ret;
452}
453
Ying Xue07f6c4b2015-01-07 13:41:58 +0800454static void tipc_sk_callback(struct rcu_head *head)
455{
456 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
457
458 sock_put(&tsk->sk);
459}
460
Ying Xuec5fa7b32013-06-17 10:54:39 -0400461/**
Ying Xue247f0f32014-02-18 16:06:46 +0800462 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 * @sock: socket to destroy
464 *
465 * This routine cleans up any messages that are still queued on the socket.
466 * For DGRAM and RDM socket types, all queued messages are rejected.
467 * For SEQPACKET and STREAM socket types, the first message is rejected
468 * and any others are discarded. (If the first message on a STREAM socket
469 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900470 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 * NOTE: Rejected messages are not necessarily returned to the sender! They
472 * are returned or discarded according to the "destination droppable" setting
473 * specified for the message by the sender.
474 *
475 * Returns 0 on success, errno otherwise
476 */
Ying Xue247f0f32014-02-18 16:06:46 +0800477static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479 struct sock *sk = sock->sk;
Sasha Levin357c4772015-01-13 12:46:41 -0500480 struct net *net;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400481 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800482 struct sk_buff *skb;
Ying Xuef2f2a962015-01-09 15:27:02 +0800483 u32 dnode, probing_state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484
Allan Stephens0c3141e2008-04-15 00:22:02 -0700485 /*
486 * Exit if socket isn't fully initialized (occurs when a failed accept()
487 * releases a pre-allocated child socket that was never used)
488 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700489 if (sk == NULL)
490 return 0;
491
Sasha Levin357c4772015-01-13 12:46:41 -0500492 net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400493 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700494 lock_sock(sk);
495
496 /*
497 * Reject all unreceived messages, except on an active connection
498 * (which disconnects locally & sends a 'FIN+' to peer)
499 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400500 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800502 skb = __skb_dequeue(&sk->sk_receive_queue);
503 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 break;
Ying Xuea6ca1092014-11-26 11:41:55 +0800505 if (TIPC_SKB_CB(skb)->handle != NULL)
506 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700507 else {
508 if ((sock->state == SS_CONNECTING) ||
509 (sock->state == SS_CONNECTED)) {
510 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400511 tsk->connected = 0;
Ying Xuef2f98002015-01-09 15:27:05 +0800512 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700513 }
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500514 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
Ying Xue34747532015-01-09 15:27:10 +0800515 TIPC_ERR_NO_PORT))
Ying Xuef2f98002015-01-09 15:27:05 +0800516 tipc_link_xmit_skb(net, skb, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700517 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 }
519
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400520 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xuef2f2a962015-01-09 15:27:02 +0800521 probing_state = tsk->probing_state;
Ying Xue3721e9c2015-01-13 17:07:48 +0800522 if (del_timer_sync(&sk->sk_timer) &&
523 probing_state != TIPC_CONN_PROBING)
Ying Xuef2f2a962015-01-09 15:27:02 +0800524 sock_put(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800525 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400526 if (tsk->connected) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500527 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +0800528 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500529 tsk_own_node(tsk), tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800530 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800531 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +0800532 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
533 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400534 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535
Allan Stephens0c3141e2008-04-15 00:22:02 -0700536 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100537 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538
Allan Stephens0c3141e2008-04-15 00:22:02 -0700539 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700540 sock->state = SS_DISCONNECTING;
541 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800542
543 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700544 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200546 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547}
548
549/**
Ying Xue247f0f32014-02-18 16:06:46 +0800550 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 * @sock: socket structure
552 * @uaddr: socket address describing name(s) and desired operation
553 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900554 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 * Name and name sequence binding is indicated using a positive scope value;
556 * a negative scope value unbinds the specified name. Specifying no name
557 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900558 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700560 *
561 * NOTE: This routine doesn't need to take the socket lock since it doesn't
562 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 */
Ying Xue247f0f32014-02-18 16:06:46 +0800564static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
565 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566{
Ying Xue84602762013-12-27 10:18:28 +0800567 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400569 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800570 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571
Ying Xue84602762013-12-27 10:18:28 +0800572 lock_sock(sk);
573 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400574 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800575 goto exit;
576 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900577
Ying Xue84602762013-12-27 10:18:28 +0800578 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
579 res = -EINVAL;
580 goto exit;
581 }
582 if (addr->family != AF_TIPC) {
583 res = -EAFNOSUPPORT;
584 goto exit;
585 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 if (addr->addrtype == TIPC_ADDR_NAME)
588 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800589 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
590 res = -EAFNOSUPPORT;
591 goto exit;
592 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900593
Ying Xue13a2e892013-06-17 10:54:40 -0400594 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400595 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800596 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
597 res = -EACCES;
598 goto exit;
599 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400600
Ying Xue84602762013-12-27 10:18:28 +0800601 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400602 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
603 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800604exit:
605 release_sock(sk);
606 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607}
608
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900609/**
Ying Xue247f0f32014-02-18 16:06:46 +0800610 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 * @sock: socket structure
612 * @uaddr: area for returned socket address
613 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700614 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900615 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700617 *
Allan Stephens2da59912008-07-14 22:43:32 -0700618 * NOTE: This routine doesn't need to take the socket lock since it only
619 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000620 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 */
Ying Xue247f0f32014-02-18 16:06:46 +0800622static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
623 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400626 struct tipc_sock *tsk = tipc_sk(sock->sk);
Ying Xue34747532015-01-09 15:27:10 +0800627 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000629 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700630 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700631 if ((sock->state != SS_CONNECTED) &&
632 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
633 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400634 addr->addr.id.ref = tsk_peer_port(tsk);
635 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700636 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800637 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800638 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700639 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640
641 *uaddr_len = sizeof(*addr);
642 addr->addrtype = TIPC_ADDR_ID;
643 addr->family = AF_TIPC;
644 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 addr->addr.name.domain = 0;
646
Allan Stephens0c3141e2008-04-15 00:22:02 -0700647 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648}
649
650/**
Ying Xue247f0f32014-02-18 16:06:46 +0800651 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 * @file: file structure associated with the socket
653 * @sock: socket for which to calculate the poll bits
654 * @wait: ???
655 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700656 * Returns pollmask value
657 *
658 * COMMENTARY:
659 * It appears that the usual socket locking mechanisms are not useful here
660 * since the pollmask info is potentially out-of-date the moment this routine
661 * exits. TCP and other protocols seem to rely on higher level poll routines
662 * to handle any preventable race conditions, so TIPC will do the same ...
663 *
664 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700665 *
Allan Stephensf662c072010-08-17 11:00:06 +0000666 * socket state flags set
667 * ------------ ---------
668 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200669 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000670 *
671 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
672 * no write flags
673 *
674 * connected POLLIN/POLLRDNORM if data in rx queue
675 * POLLOUT if port is not congested
676 *
677 * disconnecting POLLIN/POLLRDNORM/POLLHUP
678 * no write flags
679 *
680 * listening POLLIN if SYN in rx queue
681 * no write flags
682 *
683 * ready POLLIN/POLLRDNORM if data in rx queue
684 * [connectionless] POLLOUT (since port cannot be congested)
685 *
686 * IMPORTANT: The fact that a read or write operation is indicated does NOT
687 * imply that the operation will succeed, merely that it should be performed
688 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 */
Ying Xue247f0f32014-02-18 16:06:46 +0800690static unsigned int tipc_poll(struct file *file, struct socket *sock,
691 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692{
Allan Stephens9b674e82008-03-26 16:48:21 -0700693 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400694 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000695 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700696
Ying Xuef288bef2012-08-21 11:16:57 +0800697 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700698
Allan Stephensf662c072010-08-17 11:00:06 +0000699 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200700 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500701 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200702 mask |= POLLOUT;
703 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000704 case SS_READY:
705 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400706 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000707 mask |= POLLOUT;
708 /* fall thru' */
709 case SS_CONNECTING:
710 case SS_LISTENING:
711 if (!skb_queue_empty(&sk->sk_receive_queue))
712 mask |= (POLLIN | POLLRDNORM);
713 break;
714 case SS_DISCONNECTING:
715 mask = (POLLIN | POLLRDNORM | POLLHUP);
716 break;
717 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700718
719 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720}
721
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400722/**
723 * tipc_sendmcast - send multicast message
724 * @sock: socket structure
725 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500726 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400727 * @dsz: total length of message data
728 * @timeo: timeout to wait for wakeup
729 *
730 * Called from function tipc_sendmsg(), which has done all sanity checks
731 * Returns the number of bytes sent on success, or errno
732 */
733static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500734 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400735{
736 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500737 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800738 struct net *net = sock_net(sk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500739 struct tipc_msg *mhdr = &tsk->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +0800740 struct sk_buff_head head;
Al Virof25dcc72014-11-28 15:52:29 -0500741 struct iov_iter save = msg->msg_iter;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400742 uint mtu;
743 int rc;
744
745 msg_set_type(mhdr, TIPC_MCAST_MSG);
746 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
747 msg_set_destport(mhdr, 0);
748 msg_set_destnode(mhdr, 0);
749 msg_set_nametype(mhdr, seq->type);
750 msg_set_namelower(mhdr, seq->lower);
751 msg_set_nameupper(mhdr, seq->upper);
752 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
753
754new_mtu:
755 mtu = tipc_bclink_get_mtu();
Ying Xuea6ca1092014-11-26 11:41:55 +0800756 __skb_queue_head_init(&head);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500757 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400758 if (unlikely(rc < 0))
759 return rc;
760
761 do {
Ying Xuef2f98002015-01-09 15:27:05 +0800762 rc = tipc_bclink_xmit(net, &head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400763 if (likely(rc >= 0)) {
764 rc = dsz;
765 break;
766 }
Al Virof25dcc72014-11-28 15:52:29 -0500767 if (rc == -EMSGSIZE) {
768 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400769 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500770 }
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400771 if (rc != -ELINKCONG)
772 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400773 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400774 rc = tipc_wait_for_sndmsg(sock, &timeo);
775 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800776 __skb_queue_purge(&head);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400777 } while (!rc);
778 return rc;
779}
780
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400781/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
782 */
Ying Xuef2f98002015-01-09 15:27:05 +0800783void tipc_sk_mcast_rcv(struct net *net, struct sk_buff *buf)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400784{
785 struct tipc_msg *msg = buf_msg(buf);
786 struct tipc_port_list dports = {0, NULL, };
787 struct tipc_port_list *item;
788 struct sk_buff *b;
789 uint i, last, dst = 0;
790 u32 scope = TIPC_CLUSTER_SCOPE;
791
Ying Xue34747532015-01-09 15:27:10 +0800792 if (in_own_node(net, msg_orignode(msg)))
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400793 scope = TIPC_NODE_SCOPE;
794
795 /* Create destination port list: */
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800796 tipc_nametbl_mc_translate(net, msg_nametype(msg), msg_namelower(msg),
797 msg_nameupper(msg), scope, &dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400798 last = dports.count;
799 if (!last) {
800 kfree_skb(buf);
801 return;
802 }
803
804 for (item = &dports; item; item = item->next) {
805 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
806 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
807 if (!b) {
808 pr_warn("Failed do clone mcast rcv buffer\n");
809 continue;
810 }
811 msg_set_destport(msg, item->ports[i]);
Ying Xuef2f98002015-01-09 15:27:05 +0800812 tipc_sk_rcv(net, b);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400813 }
814 }
815 tipc_port_list_free(&dports);
816}
817
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900818/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500819 * tipc_sk_proto_rcv - receive a connection mng protocol message
820 * @tsk: receiving socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500821 * @skb: pointer to message buffer. Set to NULL if buffer is consumed.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500822 */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500823static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff **skb)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500824{
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500825 struct tipc_msg *msg = buf_msg(*skb);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500826 int conn_cong;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500827 u32 dnode;
828 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500829 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400830 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500831 goto exit;
832
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400833 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500834
835 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400836 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500837 tsk->sent_unacked -= msg_msgcnt(msg);
838 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400839 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500840 } else if (msg_type(msg) == CONN_PROBE) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500841 if (tipc_msg_reverse(own_node, *skb, &dnode, TIPC_OK)) {
842 msg_set_type(msg, CONN_PROBE_REPLY);
843 return;
844 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500845 }
846 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
847exit:
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500848 kfree_skb(*skb);
849 *skb = NULL;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500850}
851
Ying Xue3f405042014-01-17 09:50:05 +0800852static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
853{
854 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400855 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800856 DEFINE_WAIT(wait);
857 int done;
858
859 do {
860 int err = sock_error(sk);
861 if (err)
862 return err;
863 if (sock->state == SS_DISCONNECTING)
864 return -EPIPE;
865 if (!*timeo_p)
866 return -EAGAIN;
867 if (signal_pending(current))
868 return sock_intr_errno(*timeo_p);
869
870 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500871 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800872 finish_wait(sk_sleep(sk), &wait);
873 } while (!done);
874 return 0;
875}
876
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500877/**
Ying Xue247f0f32014-02-18 16:06:46 +0800878 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700879 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 * @sock: socket structure
881 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500882 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900883 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900885 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
887 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900888 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 * Returns the number of bytes sent on success, or errno otherwise
890 */
Ying Xue247f0f32014-02-18 16:06:46 +0800891static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500892 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500894 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700895 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400896 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800897 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400898 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500899 u32 dnode, dport;
Ying Xuea6ca1092014-11-26 11:41:55 +0800900 struct sk_buff_head head;
901 struct sk_buff *skb;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500902 struct tipc_name_seq *seq = &dest->addr.nameseq;
Al Virof25dcc72014-11-28 15:52:29 -0500903 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500904 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800905 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100906 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907
908 if (unlikely(!dest))
909 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500910
Allan Stephens51f9cc12006-06-25 23:49:06 -0700911 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
912 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500914
915 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400916 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917
Allan Stephens0c3141e2008-04-15 00:22:02 -0700918 if (iocb)
919 lock_sock(sk);
920
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500921 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700922 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500923 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700924 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700925 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700926 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500927 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700928 goto exit;
929 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400930 if (tsk->published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500931 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700932 goto exit;
933 }
934 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400935 tsk->conn_type = dest->addr.name.name.type;
936 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700937 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938 }
939
Ying Xue3f405042014-01-17 09:50:05 +0800940 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500941
942 if (dest->addrtype == TIPC_ADDR_MCAST) {
Al Viro562640f2014-11-15 01:13:43 -0500943 rc = tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500944 goto exit;
945 } else if (dest->addrtype == TIPC_ADDR_NAME) {
946 u32 type = dest->addr.name.name.type;
947 u32 inst = dest->addr.name.name.instance;
948 u32 domain = dest->addr.name.domain;
949
950 dnode = domain;
951 msg_set_type(mhdr, TIPC_NAMED_MSG);
952 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
953 msg_set_nametype(mhdr, type);
954 msg_set_nameinst(mhdr, inst);
955 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800956 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500957 msg_set_destnode(mhdr, dnode);
958 msg_set_destport(mhdr, dport);
959 if (unlikely(!dport && !dnode)) {
960 rc = -EHOSTUNREACH;
961 goto exit;
962 }
963 } else if (dest->addrtype == TIPC_ADDR_ID) {
964 dnode = dest->addr.id.node;
965 msg_set_type(mhdr, TIPC_DIRECT_MSG);
966 msg_set_lookup_scope(mhdr, 0);
967 msg_set_destnode(mhdr, dnode);
968 msg_set_destport(mhdr, dest->addr.id.ref);
969 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
970 }
971
Al Virof25dcc72014-11-28 15:52:29 -0500972 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500973new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800974 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Ying Xuea6ca1092014-11-26 11:41:55 +0800975 __skb_queue_head_init(&head);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500976 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500977 if (rc < 0)
978 goto exit;
979
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900980 do {
Ying Xuea6ca1092014-11-26 11:41:55 +0800981 skb = skb_peek(&head);
982 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Ying Xuef2f98002015-01-09 15:27:05 +0800983 rc = tipc_link_xmit(net, &head, dnode, tsk->portid);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500984 if (likely(rc >= 0)) {
985 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700986 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500987 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700988 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900989 }
Al Virof25dcc72014-11-28 15:52:29 -0500990 if (rc == -EMSGSIZE) {
991 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500992 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500993 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500994 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700995 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400996 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500997 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400998 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +0800999 __skb_queue_purge(&head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001000 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001001exit:
1002 if (iocb)
1003 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001004
1005 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006}
1007
Ying Xue391a6dd2014-01-17 09:50:06 +08001008static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
1009{
1010 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001011 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +08001012 DEFINE_WAIT(wait);
1013 int done;
1014
1015 do {
1016 int err = sock_error(sk);
1017 if (err)
1018 return err;
1019 if (sock->state == SS_DISCONNECTING)
1020 return -EPIPE;
1021 else if (sock->state != SS_CONNECTED)
1022 return -ENOTCONN;
1023 if (!*timeo_p)
1024 return -EAGAIN;
1025 if (signal_pending(current))
1026 return sock_intr_errno(*timeo_p);
1027
1028 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1029 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001030 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001031 !tsk_conn_cong(tsk)) ||
1032 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +08001033 finish_wait(sk_sleep(sk), &wait);
1034 } while (!done);
1035 return 0;
1036}
1037
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001038/**
Ying Xue247f0f32014-02-18 16:06:46 +08001039 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 * @iocb: (unused)
1041 * @sock: socket structure
1042 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001043 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001044 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001046 *
1047 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001048 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 */
Ying Xue247f0f32014-02-18 16:06:46 +08001050static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001051 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001053 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001054 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001055 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001056 struct tipc_msg *mhdr = &tsk->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +08001057 struct sk_buff_head head;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001058 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001059 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001060 int rc = -EINVAL;
1061 long timeo;
1062 u32 dnode;
1063 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001064 struct iov_iter save;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001065
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001066 /* Handle implied connection establishment */
1067 if (unlikely(dest)) {
1068 rc = tipc_sendmsg(iocb, sock, m, dsz);
1069 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001070 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001071 return rc;
1072 }
1073 if (dsz > (uint)INT_MAX)
1074 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001075
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001076 if (iocb)
1077 lock_sock(sk);
1078
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001079 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001080 if (sock->state == SS_DISCONNECTING)
1081 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001082 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001083 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +08001084 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001085 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001087 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001088 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001089
1090next:
Al Virof25dcc72014-11-28 15:52:29 -05001091 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001092 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001093 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001094 __skb_queue_head_init(&head);
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001095 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001096 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001097 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001098 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001099 if (likely(!tsk_conn_cong(tsk))) {
Ying Xuef2f98002015-01-09 15:27:05 +08001100 rc = tipc_link_xmit(net, &head, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001101 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001102 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001103 sent += send;
1104 if (sent == dsz)
1105 break;
1106 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001107 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001108 if (rc == -EMSGSIZE) {
Ying Xuef2f98002015-01-09 15:27:05 +08001109 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1110 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001111 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001112 goto next;
1113 }
1114 if (rc != -ELINKCONG)
1115 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001116 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001118 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001119 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +08001120 __skb_queue_purge(&head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001121 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001122exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001123 if (iocb)
1124 release_sock(sk);
1125 return sent ? sent : rc;
1126}
1127
1128/**
1129 * tipc_send_packet - send a connection-oriented message
1130 * @iocb: if NULL, indicates that socket lock is already held
1131 * @sock: socket structure
1132 * @m: message to send
1133 * @dsz: length of data to be transmitted
1134 *
1135 * Used for SOCK_SEQPACKET messages.
1136 *
1137 * Returns the number of bytes sent on success, or errno otherwise
1138 */
1139static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1140 struct msghdr *m, size_t dsz)
1141{
1142 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1143 return -EMSGSIZE;
1144
1145 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146}
1147
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001148/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001150static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001151 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152{
Ying Xue3721e9c2015-01-13 17:07:48 +08001153 struct sock *sk = &tsk->sk;
1154 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001155 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001157 msg_set_destnode(msg, peer_node);
1158 msg_set_destport(msg, peer_port);
1159 msg_set_type(msg, TIPC_CONN_MSG);
1160 msg_set_lookup_scope(msg, 0);
1161 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001162
Ying Xue2f55c432015-01-09 15:27:00 +08001163 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001164 tsk->probing_state = TIPC_CONN_OK;
1165 tsk->connected = 1;
Ying Xue3721e9c2015-01-13 17:07:48 +08001166 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001167 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1168 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169}
1170
1171/**
1172 * set_orig_addr - capture sender's address for received message
1173 * @m: descriptor for message info
1174 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001175 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 * Note: Address is not captured if not requested by receiver.
1177 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001178static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001180 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001182 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 addr->family = AF_TIPC;
1184 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001185 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 addr->addr.id.ref = msg_origport(msg);
1187 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001188 addr->addr.name.domain = 0; /* could leave uninitialized */
1189 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 m->msg_namelen = sizeof(struct sockaddr_tipc);
1191 }
1192}
1193
1194/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001195 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 * @m: descriptor for message info
1197 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001198 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001199 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001201 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 * Returns 0 if successful, otherwise errno
1203 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001204static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1205 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206{
1207 u32 anc_data[3];
1208 u32 err;
1209 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001210 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 int res;
1212
1213 if (likely(m->msg_controllen == 0))
1214 return 0;
1215
1216 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 err = msg ? msg_errcode(msg) : 0;
1218 if (unlikely(err)) {
1219 anc_data[0] = err;
1220 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001221 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1222 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001224 if (anc_data[1]) {
1225 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1226 msg_data(msg));
1227 if (res)
1228 return res;
1229 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 }
1231
1232 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1234 switch (dest_type) {
1235 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001236 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 anc_data[0] = msg_nametype(msg);
1238 anc_data[1] = msg_namelower(msg);
1239 anc_data[2] = msg_namelower(msg);
1240 break;
1241 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001242 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 anc_data[0] = msg_nametype(msg);
1244 anc_data[1] = msg_namelower(msg);
1245 anc_data[2] = msg_nameupper(msg);
1246 break;
1247 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001248 has_name = (tsk->conn_type != 0);
1249 anc_data[0] = tsk->conn_type;
1250 anc_data[1] = tsk->conn_instance;
1251 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 break;
1253 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001254 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255 }
Allan Stephens2db99832010-12-31 18:59:33 +00001256 if (has_name) {
1257 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1258 if (res)
1259 return res;
1260 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261
1262 return 0;
1263}
1264
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001265static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001266{
Ying Xuef2f98002015-01-09 15:27:05 +08001267 struct net *net = sock_net(&tsk->sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001268 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001269 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001270 u32 peer_port = tsk_peer_port(tsk);
1271 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001272
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001273 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001274 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001275 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1276 dnode, tsk_own_node(tsk), peer_port,
1277 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001278 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001279 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001280 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001281 msg_set_msgcnt(msg, ack);
Ying Xuef2f98002015-01-09 15:27:05 +08001282 tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001283}
1284
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001285static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001286{
1287 struct sock *sk = sock->sk;
1288 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001289 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001290 int err;
1291
1292 for (;;) {
1293 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001294 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001295 if (sock->state == SS_DISCONNECTING) {
1296 err = -ENOTCONN;
1297 break;
1298 }
1299 release_sock(sk);
1300 timeo = schedule_timeout(timeo);
1301 lock_sock(sk);
1302 }
1303 err = 0;
1304 if (!skb_queue_empty(&sk->sk_receive_queue))
1305 break;
1306 err = sock_intr_errno(timeo);
1307 if (signal_pending(current))
1308 break;
1309 err = -EAGAIN;
1310 if (!timeo)
1311 break;
1312 }
1313 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001314 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001315 return err;
1316}
1317
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001318/**
Ying Xue247f0f32014-02-18 16:06:46 +08001319 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 * @iocb: (unused)
1321 * @m: descriptor for message info
1322 * @buf_len: total size of user buffer area
1323 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001324 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1326 * If the complete message doesn't fit in user area, truncate it.
1327 *
1328 * Returns size of returned message data, errno otherwise
1329 */
Ying Xue247f0f32014-02-18 16:06:46 +08001330static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1331 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001333 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001334 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 struct sk_buff *buf;
1336 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001337 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 unsigned int sz;
1339 u32 err;
1340 int res;
1341
Allan Stephens0c3141e2008-04-15 00:22:02 -07001342 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 if (unlikely(!buf_len))
1344 return -EINVAL;
1345
Allan Stephens0c3141e2008-04-15 00:22:02 -07001346 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 res = -ENOTCONN;
1350 goto exit;
1351 }
1352
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001353 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001354restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355
Allan Stephens0c3141e2008-04-15 00:22:02 -07001356 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001357 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001358 if (res)
1359 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001360
1361 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001362 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 msg = buf_msg(buf);
1364 sz = msg_data_sz(msg);
1365 err = msg_errcode(msg);
1366
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001369 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 goto restart;
1371 }
1372
1373 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 set_orig_addr(m, msg);
1375
1376 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001377 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001378 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379 goto exit;
1380
1381 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 if (!err) {
1383 if (unlikely(buf_len < sz)) {
1384 sz = buf_len;
1385 m->msg_flags |= MSG_TRUNC;
1386 }
David S. Miller51f3d022014-11-05 16:46:40 -05001387 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001388 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 res = sz;
1391 } else {
1392 if ((sock->state == SS_READY) ||
1393 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1394 res = 0;
1395 else
1396 res = -ECONNRESET;
1397 }
1398
1399 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001401 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001402 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001403 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001404 tsk->rcv_unacked = 0;
1405 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001406 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001407 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001409 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 return res;
1411}
1412
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001413/**
Ying Xue247f0f32014-02-18 16:06:46 +08001414 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415 * @iocb: (unused)
1416 * @m: descriptor for message info
1417 * @buf_len: total size of user buffer area
1418 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001419 *
1420 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 * will optionally wait for more; never truncates data.
1422 *
1423 * Returns size of returned message data, errno otherwise
1424 */
Ying Xue247f0f32014-02-18 16:06:46 +08001425static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1426 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001428 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001429 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430 struct sk_buff *buf;
1431 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001432 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001434 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001437 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438
1439 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 if (unlikely(!buf_len))
1441 return -EINVAL;
1442
Allan Stephens0c3141e2008-04-15 00:22:02 -07001443 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001445 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 res = -ENOTCONN;
1447 goto exit;
1448 }
1449
Florian Westphal3720d402010-08-17 11:00:04 +00001450 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001451 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001452
Allan Stephens0c3141e2008-04-15 00:22:02 -07001453restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001454 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001455 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001456 if (res)
1457 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001458
1459 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001460 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 msg = buf_msg(buf);
1462 sz = msg_data_sz(msg);
1463 err = msg_errcode(msg);
1464
1465 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001467 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 goto restart;
1469 }
1470
1471 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472 if (sz_copied == 0) {
1473 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001474 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001475 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 goto exit;
1477 }
1478
1479 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001481 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482
Allan Stephens0232fd02011-02-21 09:45:40 -05001483 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484 needed = (buf_len - sz_copied);
1485 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001486
David S. Miller51f3d022014-11-05 16:46:40 -05001487 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1488 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001489 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001491
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492 sz_copied += sz_to_copy;
1493
1494 if (sz_to_copy < sz) {
1495 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001496 TIPC_SKB_CB(buf)->handle =
1497 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 goto exit;
1499 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500 } else {
1501 if (sz_copied != 0)
1502 goto exit; /* can't add error msg to valid data */
1503
1504 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1505 res = 0;
1506 else
1507 res = -ECONNRESET;
1508 }
1509
1510 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001512 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001513 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001514 tsk->rcv_unacked = 0;
1515 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001516 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001517 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518
1519 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001520 if ((sz_copied < buf_len) && /* didn't get all requested data */
1521 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001522 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001523 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1524 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525 goto restart;
1526
1527exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001528 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001529 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530}
1531
1532/**
Ying Xuef288bef2012-08-21 11:16:57 +08001533 * tipc_write_space - wake up thread if port congestion is released
1534 * @sk: socket
1535 */
1536static void tipc_write_space(struct sock *sk)
1537{
1538 struct socket_wq *wq;
1539
1540 rcu_read_lock();
1541 wq = rcu_dereference(sk->sk_wq);
1542 if (wq_has_sleeper(wq))
1543 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1544 POLLWRNORM | POLLWRBAND);
1545 rcu_read_unlock();
1546}
1547
1548/**
1549 * tipc_data_ready - wake up threads to indicate messages have been received
1550 * @sk: socket
1551 * @len: the length of messages
1552 */
David S. Miller676d2362014-04-11 16:15:36 -04001553static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001554{
1555 struct socket_wq *wq;
1556
1557 rcu_read_lock();
1558 wq = rcu_dereference(sk->sk_wq);
1559 if (wq_has_sleeper(wq))
1560 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1561 POLLRDNORM | POLLRDBAND);
1562 rcu_read_unlock();
1563}
1564
1565/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001566 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001567 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001568 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001569 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001570 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001571 */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001572static int filter_connect(struct tipc_sock *tsk, struct sk_buff **skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001573{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001574 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001575 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001576 struct socket *sock = sk->sk_socket;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001577 struct tipc_msg *msg = buf_msg(*skb);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001578 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001579
1580 if (msg_mcast(msg))
1581 return retval;
1582
1583 switch ((int)sock->state) {
1584 case SS_CONNECTED:
1585 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001586 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001587 if (unlikely(msg_errcode(msg))) {
1588 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001589 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001590 /* let timer expire on it's own */
Ying Xuef2f98002015-01-09 15:27:05 +08001591 tipc_node_remove_conn(net, tsk_peer_node(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08001592 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001593 }
1594 retval = TIPC_OK;
1595 }
1596 break;
1597 case SS_CONNECTING:
1598 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001599
1600 if (unlikely(!msg_connected(msg)))
1601 break;
1602
Ying Xue584d24b2012-11-29 18:51:19 -05001603 if (unlikely(msg_errcode(msg))) {
1604 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001605 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001606 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001607 break;
1608 }
1609
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001610 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001611 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001612 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001613 retval = TIPC_OK;
1614 break;
1615 }
1616
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001617 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1618 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001619 sock->state = SS_CONNECTED;
1620
Ying Xue584d24b2012-11-29 18:51:19 -05001621 /* If an incoming message is an 'ACK-', it should be
1622 * discarded here because it doesn't contain useful
1623 * data. In addition, we should try to wake up
1624 * connect() routine if sleeping.
1625 */
1626 if (msg_data_sz(msg) == 0) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001627 kfree_skb(*skb);
1628 *skb = NULL;
Ying Xue584d24b2012-11-29 18:51:19 -05001629 if (waitqueue_active(sk_sleep(sk)))
1630 wake_up_interruptible(sk_sleep(sk));
1631 }
1632 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001633 break;
1634 case SS_LISTENING:
1635 case SS_UNCONNECTED:
1636 /* Accept only SYN message */
1637 if (!msg_connected(msg) && !(msg_errcode(msg)))
1638 retval = TIPC_OK;
1639 break;
1640 case SS_DISCONNECTING:
1641 break;
1642 default:
1643 pr_err("Unknown socket state %u\n", sock->state);
1644 }
1645 return retval;
1646}
1647
1648/**
Ying Xueaba79f32013-01-20 23:30:09 +01001649 * rcvbuf_limit - get proper overload limit of socket receive queue
1650 * @sk: socket
1651 * @buf: message
1652 *
1653 * For all connection oriented messages, irrespective of importance,
1654 * the default overload value (i.e. 67MB) is set as limit.
1655 *
1656 * For all connectionless messages, by default new queue limits are
1657 * as belows:
1658 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001659 * TIPC_LOW_IMPORTANCE (4 MB)
1660 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1661 * TIPC_HIGH_IMPORTANCE (16 MB)
1662 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001663 *
1664 * Returns overload limit according to corresponding message importance
1665 */
1666static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1667{
1668 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001669
1670 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001671 return sysctl_tipc_rmem[2];
1672
1673 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1674 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001675}
1676
1677/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001678 * filter_rcv - validate incoming message
1679 * @sk: socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001680 * @skb: pointer to message. Set to NULL if buffer is consumed.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001681 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001682 * Enqueues message on receive queue if acceptable; optionally handles
1683 * disconnect indication for a connected socket.
1684 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001685 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001686 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001687 * Returns 0 (TIPC_OK) if message was ok, -TIPC error code if rejected
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001689static int filter_rcv(struct sock *sk, struct sk_buff **skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001691 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001692 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001693 struct tipc_msg *msg = buf_msg(*skb);
1694 unsigned int limit = rcvbuf_limit(sk, *skb);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001695 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001697 if (unlikely(msg_user(msg) == CONN_MANAGER)) {
1698 tipc_sk_proto_rcv(tsk, skb);
1699 return TIPC_OK;
1700 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001701
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001702 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001703 kfree_skb(*skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001704 tsk->link_cong = 0;
1705 sk->sk_write_space(sk);
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001706 *skb = NULL;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001707 return TIPC_OK;
1708 }
1709
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001711 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001712 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001713
Per Lidenb97bf3f2006-01-02 19:04:38 +01001714 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001715 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001716 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717 } else {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001718 rc = filter_connect(tsk, skb);
1719 if (rc != TIPC_OK || !*skb)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001720 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721 }
1722
1723 /* Reject message if there isn't room to queue it */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001724 if (sk_rmem_alloc_get(sk) + (*skb)->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001725 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726
Ying Xueaba79f32013-01-20 23:30:09 +01001727 /* Enqueue message */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001728 TIPC_SKB_CB(*skb)->handle = NULL;
1729 __skb_queue_tail(&sk->sk_receive_queue, *skb);
1730 skb_set_owner_r(*skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731
David S. Miller676d2362014-04-11 16:15:36 -04001732 sk->sk_data_ready(sk);
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001733 *skb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734 return TIPC_OK;
1735}
1736
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001737/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001738 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001740 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001741 *
1742 * Caller must hold socket lock, but not port lock.
1743 *
1744 * Returns 0
1745 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001746static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001747{
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001748 int err;
1749 atomic_t *dcnt;
1750 u32 dnode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001751 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +08001752 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001753 uint truesize = skb->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001754
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001755 err = filter_rcv(sk, &skb);
1756 if (likely(!skb)) {
1757 dcnt = &tsk->dupl_rcvcnt;
1758 if (atomic_read(dcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1759 atomic_add(truesize, dcnt);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001760 return 0;
1761 }
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001762 if (!err || tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode, -err))
1763 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001764 return 0;
1765}
1766
1767/**
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001768 * tipc_sk_enqueue_skb - enqueue buffer to socket or backlog queue
1769 * @sk: socket
1770 * @skb: pointer to message. Set to NULL if buffer is consumed.
1771 * @dnode: if buffer should be forwarded/returned, send to this node
1772 *
1773 * Caller must hold socket lock
1774 *
1775 * Returns TIPC_OK (0) or -tipc error code
1776 */
1777static int tipc_sk_enqueue_skb(struct sock *sk, struct sk_buff **skb)
1778{
1779 unsigned int lim;
1780 atomic_t *dcnt;
1781
1782 if (unlikely(!*skb))
1783 return TIPC_OK;
1784 if (!sock_owned_by_user(sk))
1785 return filter_rcv(sk, skb);
1786 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1787 if (sk->sk_backlog.len)
1788 atomic_set(dcnt, 0);
1789 lim = rcvbuf_limit(sk, *skb) + atomic_read(dcnt);
1790 if (unlikely(sk_add_backlog(sk, *skb, lim)))
1791 return -TIPC_ERR_OVERLOAD;
1792 *skb = NULL;
1793 return TIPC_OK;
1794}
1795
1796/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001797 * tipc_sk_rcv - handle incoming message
Ying Xuea6ca1092014-11-26 11:41:55 +08001798 * @skb: buffer containing arriving message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001799 * Consumes buffer
1800 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001801 */
Ying Xuef2f98002015-01-09 15:27:05 +08001802int tipc_sk_rcv(struct net *net, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001803{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001804 struct tipc_sock *tsk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001805 struct tipc_net *tn;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001806 struct sock *sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001807 u32 dport = msg_destport(buf_msg(skb));
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001808 int err;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001809 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001810
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001811 /* Validate destination and message */
Ying Xuee05b31f2015-01-09 15:27:08 +08001812 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001813 if (unlikely(!tsk)) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001814 err = tipc_msg_eval(net, skb, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001815 goto exit;
1816 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001817 sk = &tsk->sk;
1818
Ying Xue1a194c22014-10-20 14:46:35 +08001819 spin_lock_bh(&sk->sk_lock.slock);
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001820 err = tipc_sk_enqueue_skb(sk, &skb);
Ying Xue1a194c22014-10-20 14:46:35 +08001821 spin_unlock_bh(&sk->sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001822 sock_put(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001823exit:
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001824 if (unlikely(skb)) {
1825 tn = net_generic(net, tipc_net_id);
1826 if (!err || tipc_msg_reverse(tn->own_addr, skb, &dnode, -err))
1827 tipc_link_xmit_skb(net, skb, dnode, 0);
1828 }
1829 return err ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001830}
1831
Ying Xue78eb3a52014-01-17 09:50:03 +08001832static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1833{
1834 struct sock *sk = sock->sk;
1835 DEFINE_WAIT(wait);
1836 int done;
1837
1838 do {
1839 int err = sock_error(sk);
1840 if (err)
1841 return err;
1842 if (!*timeo_p)
1843 return -ETIMEDOUT;
1844 if (signal_pending(current))
1845 return sock_intr_errno(*timeo_p);
1846
1847 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1848 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1849 finish_wait(sk_sleep(sk), &wait);
1850 } while (!done);
1851 return 0;
1852}
1853
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854/**
Ying Xue247f0f32014-02-18 16:06:46 +08001855 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856 * @sock: socket structure
1857 * @dest: socket address for destination port
1858 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001859 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860 *
1861 * Returns 0 on success, errno otherwise
1862 */
Ying Xue247f0f32014-02-18 16:06:46 +08001863static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1864 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001866 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001867 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1868 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001869 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1870 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001871 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872
Allan Stephens0c3141e2008-04-15 00:22:02 -07001873 lock_sock(sk);
1874
Allan Stephensb89741a2008-04-15 00:20:37 -07001875 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001876 if (sock->state == SS_READY) {
1877 res = -EOPNOTSUPP;
1878 goto exit;
1879 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880
Allan Stephensb89741a2008-04-15 00:20:37 -07001881 /*
1882 * Reject connection attempt using multicast address
1883 *
1884 * Note: send_msg() validates the rest of the address fields,
1885 * so there's no need to do it here
1886 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001887 if (dst->addrtype == TIPC_ADDR_MCAST) {
1888 res = -EINVAL;
1889 goto exit;
1890 }
1891
Ying Xue78eb3a52014-01-17 09:50:03 +08001892 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001893 switch (sock->state) {
1894 case SS_UNCONNECTED:
1895 /* Send a 'SYN-' to destination */
1896 m.msg_name = dest;
1897 m.msg_namelen = destlen;
1898
1899 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1900 * indicate send_msg() is never blocked.
1901 */
1902 if (!timeout)
1903 m.msg_flags = MSG_DONTWAIT;
1904
Ying Xue247f0f32014-02-18 16:06:46 +08001905 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001906 if ((res < 0) && (res != -EWOULDBLOCK))
1907 goto exit;
1908
1909 /* Just entered SS_CONNECTING state; the only
1910 * difference is that return value in non-blocking
1911 * case is EINPROGRESS, rather than EALREADY.
1912 */
1913 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001914 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001915 if (previous == SS_CONNECTING)
1916 res = -EALREADY;
1917 if (!timeout)
1918 goto exit;
1919 timeout = msecs_to_jiffies(timeout);
1920 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1921 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001922 break;
1923 case SS_CONNECTED:
1924 res = -EISCONN;
1925 break;
1926 default:
1927 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001928 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001929 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001930exit:
1931 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001932 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933}
1934
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001935/**
Ying Xue247f0f32014-02-18 16:06:46 +08001936 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937 * @sock: socket structure
1938 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001939 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 * Returns 0 on success, errno otherwise
1941 */
Ying Xue247f0f32014-02-18 16:06:46 +08001942static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001943{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001944 struct sock *sk = sock->sk;
1945 int res;
1946
1947 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001948
Ying Xue245f3d32011-07-06 06:01:13 -04001949 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001950 res = -EINVAL;
1951 else {
1952 sock->state = SS_LISTENING;
1953 res = 0;
1954 }
1955
1956 release_sock(sk);
1957 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958}
1959
Ying Xue6398e232014-01-17 09:50:04 +08001960static int tipc_wait_for_accept(struct socket *sock, long timeo)
1961{
1962 struct sock *sk = sock->sk;
1963 DEFINE_WAIT(wait);
1964 int err;
1965
1966 /* True wake-one mechanism for incoming connections: only
1967 * one process gets woken up, not the 'whole herd'.
1968 * Since we do not 'race & poll' for established sockets
1969 * anymore, the common case will execute the loop only once.
1970 */
1971 for (;;) {
1972 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1973 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001974 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001975 release_sock(sk);
1976 timeo = schedule_timeout(timeo);
1977 lock_sock(sk);
1978 }
1979 err = 0;
1980 if (!skb_queue_empty(&sk->sk_receive_queue))
1981 break;
1982 err = -EINVAL;
1983 if (sock->state != SS_LISTENING)
1984 break;
1985 err = sock_intr_errno(timeo);
1986 if (signal_pending(current))
1987 break;
1988 err = -EAGAIN;
1989 if (!timeo)
1990 break;
1991 }
1992 finish_wait(sk_sleep(sk), &wait);
1993 return err;
1994}
1995
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001996/**
Ying Xue247f0f32014-02-18 16:06:46 +08001997 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001998 * @sock: listening socket
1999 * @newsock: new socket that is to be connected
2000 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002001 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002002 * Returns 0 on success, errno otherwise
2003 */
Ying Xue247f0f32014-02-18 16:06:46 +08002004static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002005{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002006 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002007 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002008 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002009 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002010 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002011 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002012
Allan Stephens0c3141e2008-04-15 00:22:02 -07002013 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002014
Allan Stephens0c3141e2008-04-15 00:22:02 -07002015 if (sock->state != SS_LISTENING) {
2016 res = -EINVAL;
2017 goto exit;
2018 }
Ying Xue6398e232014-01-17 09:50:04 +08002019 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2020 res = tipc_wait_for_accept(sock, timeo);
2021 if (res)
2022 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002023
2024 buf = skb_peek(&sk->sk_receive_queue);
2025
Ying Xuec5fa7b32013-06-17 10:54:39 -04002026 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002027 if (res)
2028 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002029
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002030 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002031 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002032 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002033
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002034 /* we lock on new_sk; but lockdep sees the lock on sk */
2035 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002036
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002037 /*
2038 * Reject any stray messages received by new socket
2039 * before the socket lock was taken (very, very unlikely)
2040 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002041 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002042
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002043 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002044 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002045 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002046
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002047 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002048 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002049 new_tsock->conn_type = msg_nametype(msg);
2050 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002052
2053 /*
2054 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2055 * Respond to 'SYN+' by queuing it on new socket.
2056 */
2057 if (!msg_data_sz(msg)) {
2058 struct msghdr m = {NULL,};
2059
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002060 tsk_advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08002061 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002062 } else {
2063 __skb_dequeue(&sk->sk_receive_queue);
2064 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002065 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002066 }
2067 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002069 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002070 return res;
2071}
2072
2073/**
Ying Xue247f0f32014-02-18 16:06:46 +08002074 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002076 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077 *
2078 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002079 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080 * Returns 0 on success, errno otherwise
2081 */
Ying Xue247f0f32014-02-18 16:06:46 +08002082static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002084 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002085 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002086 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002087 struct sk_buff *skb;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002088 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089 int res;
2090
Allan Stephense247a8f2008-03-06 15:05:38 -08002091 if (how != SHUT_RDWR)
2092 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002093
Allan Stephens0c3141e2008-04-15 00:22:02 -07002094 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002095
2096 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002097 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002098 case SS_CONNECTED:
2099
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002101 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002102 skb = __skb_dequeue(&sk->sk_receive_queue);
2103 if (skb) {
2104 if (TIPC_SKB_CB(skb)->handle != NULL) {
2105 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 goto restart;
2107 }
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002108 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
Ying Xue34747532015-01-09 15:27:10 +08002109 TIPC_CONN_SHUTDOWN))
Ying Xuef2f98002015-01-09 15:27:05 +08002110 tipc_link_xmit_skb(net, skb, dnode,
2111 tsk->portid);
2112 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002113 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002114 dnode = tsk_peer_node(tsk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002115
2116 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002117 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002118 0, dnode, tsk_own_node(tsk),
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002119 tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002120 tsk->portid, TIPC_CONN_SHUTDOWN);
Ying Xuef2f98002015-01-09 15:27:05 +08002121 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002122 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002123 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002124 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002125 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126 /* fall through */
2127
2128 case SS_DISCONNECTING:
2129
Ying Xue75031152012-10-29 09:38:15 -04002130 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002131 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002132
2133 /* Wake up anyone sleeping in poll */
2134 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135 res = 0;
2136 break;
2137
2138 default:
2139 res = -ENOTCONN;
2140 }
2141
Allan Stephens0c3141e2008-04-15 00:22:02 -07002142 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002143 return res;
2144}
2145
Ying Xuef2f2a962015-01-09 15:27:02 +08002146static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002147{
Ying Xuef2f2a962015-01-09 15:27:02 +08002148 struct tipc_sock *tsk = (struct tipc_sock *)data;
2149 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002150 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002151 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002152 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002153
Jon Paul Maloy57289012014-08-22 18:09:09 -04002154 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002155 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002156 bh_unlock_sock(sk);
2157 goto exit;
2158 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002159 peer_port = tsk_peer_port(tsk);
2160 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002161
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002162 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002163 /* Previous probe not answered -> self abort */
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002164 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +08002165 TIPC_CONN_MSG, SHORT_H_SIZE, 0,
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002166 own_node, peer_node, tsk->portid,
Ying Xue34747532015-01-09 15:27:10 +08002167 peer_port, TIPC_ERR_NO_PORT);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002168 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002169 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2170 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002171 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002172 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002173 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002174 }
2175 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002176 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +08002177 tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002178exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002179 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002180}
2181
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002182static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002183 struct tipc_name_seq const *seq)
2184{
Ying Xuef2f98002015-01-09 15:27:05 +08002185 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002186 struct publication *publ;
2187 u32 key;
2188
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002189 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002190 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002191 key = tsk->portid + tsk->pub_count + 1;
2192 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002193 return -EADDRINUSE;
2194
Ying Xuef2f98002015-01-09 15:27:05 +08002195 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002196 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002197 if (unlikely(!publ))
2198 return -EINVAL;
2199
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002200 list_add(&publ->pport_list, &tsk->publications);
2201 tsk->pub_count++;
2202 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002203 return 0;
2204}
2205
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002206static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002207 struct tipc_name_seq const *seq)
2208{
Ying Xuef2f98002015-01-09 15:27:05 +08002209 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002210 struct publication *publ;
2211 struct publication *safe;
2212 int rc = -EINVAL;
2213
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002214 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002215 if (seq) {
2216 if (publ->scope != scope)
2217 continue;
2218 if (publ->type != seq->type)
2219 continue;
2220 if (publ->lower != seq->lower)
2221 continue;
2222 if (publ->upper != seq->upper)
2223 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002224 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002225 publ->ref, publ->key);
2226 rc = 0;
2227 break;
2228 }
Ying Xuef2f98002015-01-09 15:27:05 +08002229 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002230 publ->ref, publ->key);
2231 rc = 0;
2232 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002233 if (list_empty(&tsk->publications))
2234 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002235 return rc;
2236}
2237
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002238static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002239 int len, int full_id)
2240{
Ying Xue34747532015-01-09 15:27:10 +08002241 struct net *net = sock_net(&tsk->sk);
2242 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002243 struct publication *publ;
2244 int ret;
2245
2246 if (full_id)
2247 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
Ying Xue34747532015-01-09 15:27:10 +08002248 tipc_zone(tn->own_addr),
2249 tipc_cluster(tn->own_addr),
2250 tipc_node(tn->own_addr), tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002251 else
Ying Xue07f6c4b2015-01-07 13:41:58 +08002252 ret = tipc_snprintf(buf, len, "%-10u:", tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002253
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002254 if (tsk->connected) {
2255 u32 dport = tsk_peer_port(tsk);
2256 u32 destnode = tsk_peer_node(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002257
2258 ret += tipc_snprintf(buf + ret, len - ret,
2259 " connected to <%u.%u.%u:%u>",
2260 tipc_zone(destnode),
2261 tipc_cluster(destnode),
2262 tipc_node(destnode), dport);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002263 if (tsk->conn_type != 0)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002264 ret += tipc_snprintf(buf + ret, len - ret,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002265 " via {%u,%u}", tsk->conn_type,
2266 tsk->conn_instance);
2267 } else if (tsk->published) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002268 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002269 list_for_each_entry(publ, &tsk->publications, pport_list) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002270 if (publ->lower == publ->upper)
2271 ret += tipc_snprintf(buf + ret, len - ret,
2272 " {%u,%u}", publ->type,
2273 publ->lower);
2274 else
2275 ret += tipc_snprintf(buf + ret, len - ret,
2276 " {%u,%u,%u}", publ->type,
2277 publ->lower, publ->upper);
2278 }
2279 }
2280 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2281 return ret;
2282}
2283
Ying Xuee05b31f2015-01-09 15:27:08 +08002284struct sk_buff *tipc_sk_socks_show(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002285{
Ying Xuee05b31f2015-01-09 15:27:08 +08002286 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002287 const struct bucket_table *tbl;
2288 struct rhash_head *pos;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002289 struct sk_buff *buf;
2290 struct tlv_desc *rep_tlv;
2291 char *pb;
2292 int pb_len;
2293 struct tipc_sock *tsk;
2294 int str_len = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002295 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002296
2297 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2298 if (!buf)
2299 return NULL;
2300 rep_tlv = (struct tlv_desc *)buf->data;
2301 pb = TLV_DATA(rep_tlv);
2302 pb_len = ULTRA_STRING_MAX_LEN;
2303
Ying Xue07f6c4b2015-01-07 13:41:58 +08002304 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002305 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002306 for (i = 0; i < tbl->size; i++) {
2307 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2308 spin_lock_bh(&tsk->sk.sk_lock.slock);
2309 str_len += tipc_sk_show(tsk, pb + str_len,
2310 pb_len - str_len, 0);
2311 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2312 }
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002313 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002314 rcu_read_unlock();
2315
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002316 str_len += 1; /* for "\0" */
2317 skb_put(buf, TLV_SPACE(str_len));
2318 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2319
2320 return buf;
2321}
2322
2323/* tipc_sk_reinit: set non-zero address in all existing sockets
2324 * when we go from standalone to network mode.
2325 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002326void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002327{
Ying Xuee05b31f2015-01-09 15:27:08 +08002328 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002329 const struct bucket_table *tbl;
2330 struct rhash_head *pos;
2331 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002332 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002333 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002334
Ying Xue07f6c4b2015-01-07 13:41:58 +08002335 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002336 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002337 for (i = 0; i < tbl->size; i++) {
2338 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2339 spin_lock_bh(&tsk->sk.sk_lock.slock);
2340 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002341 msg_set_prevnode(msg, tn->own_addr);
2342 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002343 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2344 }
2345 }
2346 rcu_read_unlock();
2347}
2348
Ying Xuee05b31f2015-01-09 15:27:08 +08002349static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002350{
Ying Xuee05b31f2015-01-09 15:27:08 +08002351 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002352 struct tipc_sock *tsk;
2353
2354 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002355 tsk = rhashtable_lookup(&tn->sk_rht, &portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002356 if (tsk)
2357 sock_hold(&tsk->sk);
2358 rcu_read_unlock();
2359
2360 return tsk;
2361}
2362
2363static int tipc_sk_insert(struct tipc_sock *tsk)
2364{
Ying Xuee05b31f2015-01-09 15:27:08 +08002365 struct sock *sk = &tsk->sk;
2366 struct net *net = sock_net(sk);
2367 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002368 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2369 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2370
2371 while (remaining--) {
2372 portid++;
2373 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2374 portid = TIPC_MIN_PORT;
2375 tsk->portid = portid;
2376 sock_hold(&tsk->sk);
Ying Xuee05b31f2015-01-09 15:27:08 +08002377 if (rhashtable_lookup_insert(&tn->sk_rht, &tsk->node))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002378 return 0;
2379 sock_put(&tsk->sk);
2380 }
2381
2382 return -1;
2383}
2384
2385static void tipc_sk_remove(struct tipc_sock *tsk)
2386{
2387 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002388 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002389
Ying Xuee05b31f2015-01-09 15:27:08 +08002390 if (rhashtable_remove(&tn->sk_rht, &tsk->node)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002391 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2392 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002393 }
2394}
2395
Ying Xuee05b31f2015-01-09 15:27:08 +08002396int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002397{
Ying Xuee05b31f2015-01-09 15:27:08 +08002398 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002399 struct rhashtable_params rht_params = {
2400 .nelem_hint = 192,
2401 .head_offset = offsetof(struct tipc_sock, node),
2402 .key_offset = offsetof(struct tipc_sock, portid),
2403 .key_len = sizeof(u32), /* portid */
2404 .hashfn = jhash,
2405 .max_shift = 20, /* 1M */
2406 .min_shift = 8, /* 256 */
2407 .grow_decision = rht_grow_above_75,
2408 .shrink_decision = rht_shrink_below_30,
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002409 };
2410
Ying Xuee05b31f2015-01-09 15:27:08 +08002411 return rhashtable_init(&tn->sk_rht, &rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002412}
2413
Ying Xuee05b31f2015-01-09 15:27:08 +08002414void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002415{
Ying Xuee05b31f2015-01-09 15:27:08 +08002416 struct tipc_net *tn = net_generic(net, tipc_net_id);
2417
Ying Xue07f6c4b2015-01-07 13:41:58 +08002418 /* Wait for socket readers to complete */
2419 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002420
Ying Xuee05b31f2015-01-09 15:27:08 +08002421 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002422}
2423
2424/**
Ying Xue247f0f32014-02-18 16:06:46 +08002425 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002426 * @sock: socket structure
2427 * @lvl: option level
2428 * @opt: option identifier
2429 * @ov: pointer to new option value
2430 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002431 *
2432 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002433 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002434 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002435 * Returns 0 on success, errno otherwise
2436 */
Ying Xue247f0f32014-02-18 16:06:46 +08002437static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2438 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002439{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002440 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002441 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002442 u32 value;
2443 int res;
2444
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002445 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2446 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002447 if (lvl != SOL_TIPC)
2448 return -ENOPROTOOPT;
2449 if (ol < sizeof(value))
2450 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002451 res = get_user(value, (u32 __user *)ov);
2452 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002453 return res;
2454
Allan Stephens0c3141e2008-04-15 00:22:02 -07002455 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002456
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457 switch (opt) {
2458 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002459 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002460 break;
2461 case TIPC_SRC_DROPPABLE:
2462 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002463 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002464 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002465 res = -ENOPROTOOPT;
2466 break;
2467 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002468 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002469 break;
2470 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002471 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002472 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002473 break;
2474 default:
2475 res = -EINVAL;
2476 }
2477
Allan Stephens0c3141e2008-04-15 00:22:02 -07002478 release_sock(sk);
2479
Per Lidenb97bf3f2006-01-02 19:04:38 +01002480 return res;
2481}
2482
2483/**
Ying Xue247f0f32014-02-18 16:06:46 +08002484 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002485 * @sock: socket structure
2486 * @lvl: option level
2487 * @opt: option identifier
2488 * @ov: receptacle for option value
2489 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002490 *
2491 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002492 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002493 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002494 * Returns 0 on success, errno otherwise
2495 */
Ying Xue247f0f32014-02-18 16:06:46 +08002496static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2497 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002498{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002499 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002500 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002501 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002502 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002503 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002504
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002505 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2506 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002507 if (lvl != SOL_TIPC)
2508 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002509 res = get_user(len, ol);
2510 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002511 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002512
Allan Stephens0c3141e2008-04-15 00:22:02 -07002513 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002514
2515 switch (opt) {
2516 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002517 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002518 break;
2519 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002520 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002521 break;
2522 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002523 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002524 break;
2525 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002526 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002527 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002528 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002529 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002530 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002531 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002532 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002533 value = skb_queue_len(&sk->sk_receive_queue);
2534 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002535 default:
2536 res = -EINVAL;
2537 }
2538
Allan Stephens0c3141e2008-04-15 00:22:02 -07002539 release_sock(sk);
2540
Paul Gortmaker25860c32010-12-31 18:59:31 +00002541 if (res)
2542 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002543
Paul Gortmaker25860c32010-12-31 18:59:31 +00002544 if (len < sizeof(value))
2545 return -EINVAL;
2546
2547 if (copy_to_user(ov, &value, sizeof(value)))
2548 return -EFAULT;
2549
2550 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002551}
2552
Ying Xuef2f98002015-01-09 15:27:05 +08002553static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002554{
Ying Xuef2f98002015-01-09 15:27:05 +08002555 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002556 struct tipc_sioc_ln_req lnr;
2557 void __user *argp = (void __user *)arg;
2558
2559 switch (cmd) {
2560 case SIOCGETLINKNAME:
2561 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2562 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002563 if (!tipc_node_get_linkname(sock_net(sk),
2564 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002565 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2566 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2567 return -EFAULT;
2568 return 0;
2569 }
2570 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002571 default:
2572 return -ENOIOCTLCMD;
2573 }
2574}
2575
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002576/* Protocol switches for the various types of TIPC sockets */
2577
Florian Westphalbca65ea2008-02-07 18:18:01 -08002578static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002579 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002580 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002581 .release = tipc_release,
2582 .bind = tipc_bind,
2583 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002584 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002585 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002586 .getname = tipc_getname,
2587 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002588 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002589 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002590 .shutdown = tipc_shutdown,
2591 .setsockopt = tipc_setsockopt,
2592 .getsockopt = tipc_getsockopt,
2593 .sendmsg = tipc_sendmsg,
2594 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002595 .mmap = sock_no_mmap,
2596 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002597};
2598
Florian Westphalbca65ea2008-02-07 18:18:01 -08002599static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002600 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002601 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002602 .release = tipc_release,
2603 .bind = tipc_bind,
2604 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002605 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002606 .accept = tipc_accept,
2607 .getname = tipc_getname,
2608 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002609 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002610 .listen = tipc_listen,
2611 .shutdown = tipc_shutdown,
2612 .setsockopt = tipc_setsockopt,
2613 .getsockopt = tipc_getsockopt,
2614 .sendmsg = tipc_send_packet,
2615 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002616 .mmap = sock_no_mmap,
2617 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002618};
2619
Florian Westphalbca65ea2008-02-07 18:18:01 -08002620static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002621 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002622 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002623 .release = tipc_release,
2624 .bind = tipc_bind,
2625 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002626 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002627 .accept = tipc_accept,
2628 .getname = tipc_getname,
2629 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002630 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002631 .listen = tipc_listen,
2632 .shutdown = tipc_shutdown,
2633 .setsockopt = tipc_setsockopt,
2634 .getsockopt = tipc_getsockopt,
2635 .sendmsg = tipc_send_stream,
2636 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002637 .mmap = sock_no_mmap,
2638 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002639};
2640
Florian Westphalbca65ea2008-02-07 18:18:01 -08002641static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002642 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002643 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002644 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002645};
2646
2647static struct proto tipc_proto = {
2648 .name = "TIPC",
2649 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002650 .obj_size = sizeof(struct tipc_sock),
2651 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002652};
2653
Ying Xuec5fa7b32013-06-17 10:54:39 -04002654static struct proto tipc_proto_kern = {
2655 .name = "TIPC",
2656 .obj_size = sizeof(struct tipc_sock),
2657 .sysctl_rmem = sysctl_tipc_rmem
2658};
2659
Per Lidenb97bf3f2006-01-02 19:04:38 +01002660/**
Per Liden4323add2006-01-18 00:38:21 +01002661 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002662 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002663 * Returns 0 on success, errno otherwise
2664 */
Per Liden4323add2006-01-18 00:38:21 +01002665int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002666{
2667 int res;
2668
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002669 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002670 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002671 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002672 goto out;
2673 }
2674
2675 res = sock_register(&tipc_family_ops);
2676 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002677 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002678 proto_unregister(&tipc_proto);
2679 goto out;
2680 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002681 out:
2682 return res;
2683}
2684
2685/**
Per Liden4323add2006-01-18 00:38:21 +01002686 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002687 */
Per Liden4323add2006-01-18 00:38:21 +01002688void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002689{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002690 sock_unregister(tipc_family_ops.family);
2691 proto_unregister(&tipc_proto);
2692}
Richard Alpe34b78a122014-11-20 10:29:10 +01002693
2694/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002695static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002696{
2697 u32 peer_node;
2698 u32 peer_port;
2699 struct nlattr *nest;
2700
2701 peer_node = tsk_peer_node(tsk);
2702 peer_port = tsk_peer_port(tsk);
2703
2704 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2705
2706 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2707 goto msg_full;
2708 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2709 goto msg_full;
2710
2711 if (tsk->conn_type != 0) {
2712 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2713 goto msg_full;
2714 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2715 goto msg_full;
2716 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2717 goto msg_full;
2718 }
2719 nla_nest_end(skb, nest);
2720
2721 return 0;
2722
2723msg_full:
2724 nla_nest_cancel(skb, nest);
2725
2726 return -EMSGSIZE;
2727}
2728
2729/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002730static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2731 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002732{
2733 int err;
2734 void *hdr;
2735 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002736 struct net *net = sock_net(skb->sk);
2737 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002738
2739 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2740 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2741 if (!hdr)
2742 goto msg_cancel;
2743
2744 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2745 if (!attrs)
2746 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002747 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002748 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002749 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002750 goto attr_msg_cancel;
2751
2752 if (tsk->connected) {
2753 err = __tipc_nl_add_sk_con(skb, tsk);
2754 if (err)
2755 goto attr_msg_cancel;
2756 } else if (!list_empty(&tsk->publications)) {
2757 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2758 goto attr_msg_cancel;
2759 }
2760 nla_nest_end(skb, attrs);
2761 genlmsg_end(skb, hdr);
2762
2763 return 0;
2764
2765attr_msg_cancel:
2766 nla_nest_cancel(skb, attrs);
2767genlmsg_cancel:
2768 genlmsg_cancel(skb, hdr);
2769msg_cancel:
2770 return -EMSGSIZE;
2771}
2772
2773int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2774{
2775 int err;
2776 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002777 const struct bucket_table *tbl;
2778 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002779 struct net *net = sock_net(skb->sk);
2780 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002781 u32 tbl_id = cb->args[0];
2782 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002783
Ying Xue07f6c4b2015-01-07 13:41:58 +08002784 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002785 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002786 for (; tbl_id < tbl->size; tbl_id++) {
2787 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002788 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002789 if (prev_portid && prev_portid != tsk->portid) {
2790 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2791 continue;
2792 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002793
Richard Alped6e164e2015-01-16 12:30:40 +01002794 err = __tipc_nl_add_sk(skb, cb, tsk);
2795 if (err) {
2796 prev_portid = tsk->portid;
2797 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2798 goto out;
2799 }
2800 prev_portid = 0;
2801 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002802 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002803 }
Richard Alped6e164e2015-01-16 12:30:40 +01002804out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002805 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002806 cb->args[0] = tbl_id;
2807 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002808
2809 return skb->len;
2810}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002811
2812/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002813static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2814 struct netlink_callback *cb,
2815 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002816{
2817 void *hdr;
2818 struct nlattr *attrs;
2819
2820 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2821 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2822 if (!hdr)
2823 goto msg_cancel;
2824
2825 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2826 if (!attrs)
2827 goto genlmsg_cancel;
2828
2829 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2830 goto attr_msg_cancel;
2831 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2832 goto attr_msg_cancel;
2833 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2834 goto attr_msg_cancel;
2835 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2836 goto attr_msg_cancel;
2837
2838 nla_nest_end(skb, attrs);
2839 genlmsg_end(skb, hdr);
2840
2841 return 0;
2842
2843attr_msg_cancel:
2844 nla_nest_cancel(skb, attrs);
2845genlmsg_cancel:
2846 genlmsg_cancel(skb, hdr);
2847msg_cancel:
2848 return -EMSGSIZE;
2849}
2850
2851/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002852static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2853 struct netlink_callback *cb,
2854 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002855{
2856 int err;
2857 struct publication *p;
2858
2859 if (*last_publ) {
2860 list_for_each_entry(p, &tsk->publications, pport_list) {
2861 if (p->key == *last_publ)
2862 break;
2863 }
2864 if (p->key != *last_publ) {
2865 /* We never set seq or call nl_dump_check_consistent()
2866 * this means that setting prev_seq here will cause the
2867 * consistence check to fail in the netlink callback
2868 * handler. Resulting in the last NLMSG_DONE message
2869 * having the NLM_F_DUMP_INTR flag set.
2870 */
2871 cb->prev_seq = 1;
2872 *last_publ = 0;
2873 return -EPIPE;
2874 }
2875 } else {
2876 p = list_first_entry(&tsk->publications, struct publication,
2877 pport_list);
2878 }
2879
2880 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2881 err = __tipc_nl_add_sk_publ(skb, cb, p);
2882 if (err) {
2883 *last_publ = p->key;
2884 return err;
2885 }
2886 }
2887 *last_publ = 0;
2888
2889 return 0;
2890}
2891
2892int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2893{
2894 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002895 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002896 u32 last_publ = cb->args[1];
2897 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002898 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002899 struct tipc_sock *tsk;
2900
Ying Xue07f6c4b2015-01-07 13:41:58 +08002901 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002902 struct nlattr **attrs;
2903 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2904
2905 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2906 if (err)
2907 return err;
2908
2909 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2910 attrs[TIPC_NLA_SOCK],
2911 tipc_nl_sock_policy);
2912 if (err)
2913 return err;
2914
2915 if (!sock[TIPC_NLA_SOCK_REF])
2916 return -EINVAL;
2917
Ying Xue07f6c4b2015-01-07 13:41:58 +08002918 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002919 }
2920
2921 if (done)
2922 return 0;
2923
Ying Xuee05b31f2015-01-09 15:27:08 +08002924 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002925 if (!tsk)
2926 return -EINVAL;
2927
2928 lock_sock(&tsk->sk);
2929 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2930 if (!err)
2931 done = 1;
2932 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002933 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002934
Ying Xue07f6c4b2015-01-07 13:41:58 +08002935 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002936 cb->args[1] = last_publ;
2937 cb->args[2] = done;
2938
2939 return skb->len;
2940}