blob: b384e658dfebd4c62b6515ac6e541bdb39b0ec25 [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
821 * @dnode: node to send response message to, if any
822 * @buf: buffer containing protocol message
823 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
824 * (CONN_PROBE_REPLY) message should be forwarded.
825 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800826static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
827 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500828{
829 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500830 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500831
832 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400833 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500834 goto exit;
835
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400836 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500837
838 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400839 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500840 tsk->sent_unacked -= msg_msgcnt(msg);
841 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400842 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500843 } else if (msg_type(msg) == CONN_PROBE) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500844 if (!tipc_msg_reverse(tsk_own_node(tsk), buf, dnode, TIPC_OK))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500845 return TIPC_OK;
846 msg_set_type(msg, CONN_PROBE_REPLY);
847 return TIPC_FWD_MSG;
848 }
849 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
850exit:
851 kfree_skb(buf);
852 return TIPC_OK;
853}
854
Ying Xue3f405042014-01-17 09:50:05 +0800855static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
856{
857 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400858 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800859 DEFINE_WAIT(wait);
860 int done;
861
862 do {
863 int err = sock_error(sk);
864 if (err)
865 return err;
866 if (sock->state == SS_DISCONNECTING)
867 return -EPIPE;
868 if (!*timeo_p)
869 return -EAGAIN;
870 if (signal_pending(current))
871 return sock_intr_errno(*timeo_p);
872
873 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500874 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800875 finish_wait(sk_sleep(sk), &wait);
876 } while (!done);
877 return 0;
878}
879
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500880/**
Ying Xue247f0f32014-02-18 16:06:46 +0800881 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700882 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883 * @sock: socket structure
884 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500885 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900886 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900888 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
890 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900891 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 * Returns the number of bytes sent on success, or errno otherwise
893 */
Ying Xue247f0f32014-02-18 16:06:46 +0800894static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500895 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500897 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700898 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400899 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800900 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400901 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500902 u32 dnode, dport;
Ying Xuea6ca1092014-11-26 11:41:55 +0800903 struct sk_buff_head head;
904 struct sk_buff *skb;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500905 struct tipc_name_seq *seq = &dest->addr.nameseq;
Al Virof25dcc72014-11-28 15:52:29 -0500906 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500907 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800908 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100909 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910
911 if (unlikely(!dest))
912 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500913
Allan Stephens51f9cc12006-06-25 23:49:06 -0700914 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
915 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500917
918 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400919 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920
Allan Stephens0c3141e2008-04-15 00:22:02 -0700921 if (iocb)
922 lock_sock(sk);
923
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500924 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700925 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500926 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700927 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700928 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700929 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500930 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700931 goto exit;
932 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400933 if (tsk->published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500934 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700935 goto exit;
936 }
937 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400938 tsk->conn_type = dest->addr.name.name.type;
939 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700940 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 }
942
Ying Xue3f405042014-01-17 09:50:05 +0800943 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500944
945 if (dest->addrtype == TIPC_ADDR_MCAST) {
Al Viro562640f2014-11-15 01:13:43 -0500946 rc = tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500947 goto exit;
948 } else if (dest->addrtype == TIPC_ADDR_NAME) {
949 u32 type = dest->addr.name.name.type;
950 u32 inst = dest->addr.name.name.instance;
951 u32 domain = dest->addr.name.domain;
952
953 dnode = domain;
954 msg_set_type(mhdr, TIPC_NAMED_MSG);
955 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
956 msg_set_nametype(mhdr, type);
957 msg_set_nameinst(mhdr, inst);
958 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800959 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500960 msg_set_destnode(mhdr, dnode);
961 msg_set_destport(mhdr, dport);
962 if (unlikely(!dport && !dnode)) {
963 rc = -EHOSTUNREACH;
964 goto exit;
965 }
966 } else if (dest->addrtype == TIPC_ADDR_ID) {
967 dnode = dest->addr.id.node;
968 msg_set_type(mhdr, TIPC_DIRECT_MSG);
969 msg_set_lookup_scope(mhdr, 0);
970 msg_set_destnode(mhdr, dnode);
971 msg_set_destport(mhdr, dest->addr.id.ref);
972 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
973 }
974
Al Virof25dcc72014-11-28 15:52:29 -0500975 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500976new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800977 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Ying Xuea6ca1092014-11-26 11:41:55 +0800978 __skb_queue_head_init(&head);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500979 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500980 if (rc < 0)
981 goto exit;
982
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900983 do {
Ying Xuea6ca1092014-11-26 11:41:55 +0800984 skb = skb_peek(&head);
985 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Ying Xuef2f98002015-01-09 15:27:05 +0800986 rc = tipc_link_xmit(net, &head, dnode, tsk->portid);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500987 if (likely(rc >= 0)) {
988 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700989 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500990 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700991 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900992 }
Al Virof25dcc72014-11-28 15:52:29 -0500993 if (rc == -EMSGSIZE) {
994 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500995 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500996 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500997 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700998 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400999 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001000 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001001 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +08001002 __skb_queue_purge(&head);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001003 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001004exit:
1005 if (iocb)
1006 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001007
1008 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009}
1010
Ying Xue391a6dd2014-01-17 09:50:06 +08001011static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
1012{
1013 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001014 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +08001015 DEFINE_WAIT(wait);
1016 int done;
1017
1018 do {
1019 int err = sock_error(sk);
1020 if (err)
1021 return err;
1022 if (sock->state == SS_DISCONNECTING)
1023 return -EPIPE;
1024 else if (sock->state != SS_CONNECTED)
1025 return -ENOTCONN;
1026 if (!*timeo_p)
1027 return -EAGAIN;
1028 if (signal_pending(current))
1029 return sock_intr_errno(*timeo_p);
1030
1031 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1032 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001033 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001034 !tsk_conn_cong(tsk)) ||
1035 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +08001036 finish_wait(sk_sleep(sk), &wait);
1037 } while (!done);
1038 return 0;
1039}
1040
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001041/**
Ying Xue247f0f32014-02-18 16:06:46 +08001042 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 * @iocb: (unused)
1044 * @sock: socket structure
1045 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001046 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001047 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001049 *
1050 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001051 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 */
Ying Xue247f0f32014-02-18 16:06:46 +08001053static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001054 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001056 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001057 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001058 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001059 struct tipc_msg *mhdr = &tsk->phdr;
Ying Xuea6ca1092014-11-26 11:41:55 +08001060 struct sk_buff_head head;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001061 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001062 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001063 int rc = -EINVAL;
1064 long timeo;
1065 u32 dnode;
1066 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001067 struct iov_iter save;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001068
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001069 /* Handle implied connection establishment */
1070 if (unlikely(dest)) {
1071 rc = tipc_sendmsg(iocb, sock, m, dsz);
1072 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001073 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001074 return rc;
1075 }
1076 if (dsz > (uint)INT_MAX)
1077 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001078
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001079 if (iocb)
1080 lock_sock(sk);
1081
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001082 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001083 if (sock->state == SS_DISCONNECTING)
1084 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001085 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001086 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +08001087 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001088 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001090 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001091 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001092
1093next:
Al Virof25dcc72014-11-28 15:52:29 -05001094 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001095 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001096 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Ying Xuea6ca1092014-11-26 11:41:55 +08001097 __skb_queue_head_init(&head);
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001098 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001099 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001100 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001101 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001102 if (likely(!tsk_conn_cong(tsk))) {
Ying Xuef2f98002015-01-09 15:27:05 +08001103 rc = tipc_link_xmit(net, &head, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001104 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001105 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001106 sent += send;
1107 if (sent == dsz)
1108 break;
1109 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001110 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001111 if (rc == -EMSGSIZE) {
Ying Xuef2f98002015-01-09 15:27:05 +08001112 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1113 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001114 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001115 goto next;
1116 }
1117 if (rc != -ELINKCONG)
1118 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001119 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001121 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001122 if (rc)
Ying Xuea6ca1092014-11-26 11:41:55 +08001123 __skb_queue_purge(&head);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001124 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001125exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001126 if (iocb)
1127 release_sock(sk);
1128 return sent ? sent : rc;
1129}
1130
1131/**
1132 * tipc_send_packet - send a connection-oriented message
1133 * @iocb: if NULL, indicates that socket lock is already held
1134 * @sock: socket structure
1135 * @m: message to send
1136 * @dsz: length of data to be transmitted
1137 *
1138 * Used for SOCK_SEQPACKET messages.
1139 *
1140 * Returns the number of bytes sent on success, or errno otherwise
1141 */
1142static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1143 struct msghdr *m, size_t dsz)
1144{
1145 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1146 return -EMSGSIZE;
1147
1148 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149}
1150
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001151/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001153static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001154 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155{
Ying Xue3721e9c2015-01-13 17:07:48 +08001156 struct sock *sk = &tsk->sk;
1157 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001158 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001160 msg_set_destnode(msg, peer_node);
1161 msg_set_destport(msg, peer_port);
1162 msg_set_type(msg, TIPC_CONN_MSG);
1163 msg_set_lookup_scope(msg, 0);
1164 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001165
Ying Xue2f55c432015-01-09 15:27:00 +08001166 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001167 tsk->probing_state = TIPC_CONN_OK;
1168 tsk->connected = 1;
Ying Xue3721e9c2015-01-13 17:07:48 +08001169 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001170 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1171 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172}
1173
1174/**
1175 * set_orig_addr - capture sender's address for received message
1176 * @m: descriptor for message info
1177 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001178 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 * Note: Address is not captured if not requested by receiver.
1180 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001181static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001183 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001185 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 addr->family = AF_TIPC;
1187 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001188 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 addr->addr.id.ref = msg_origport(msg);
1190 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001191 addr->addr.name.domain = 0; /* could leave uninitialized */
1192 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 m->msg_namelen = sizeof(struct sockaddr_tipc);
1194 }
1195}
1196
1197/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001198 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 * @m: descriptor for message info
1200 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001201 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001202 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001204 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 * Returns 0 if successful, otherwise errno
1206 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001207static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1208 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209{
1210 u32 anc_data[3];
1211 u32 err;
1212 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001213 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 int res;
1215
1216 if (likely(m->msg_controllen == 0))
1217 return 0;
1218
1219 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 err = msg ? msg_errcode(msg) : 0;
1221 if (unlikely(err)) {
1222 anc_data[0] = err;
1223 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001224 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1225 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001227 if (anc_data[1]) {
1228 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1229 msg_data(msg));
1230 if (res)
1231 return res;
1232 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 }
1234
1235 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1237 switch (dest_type) {
1238 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001239 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 anc_data[0] = msg_nametype(msg);
1241 anc_data[1] = msg_namelower(msg);
1242 anc_data[2] = msg_namelower(msg);
1243 break;
1244 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001245 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 anc_data[0] = msg_nametype(msg);
1247 anc_data[1] = msg_namelower(msg);
1248 anc_data[2] = msg_nameupper(msg);
1249 break;
1250 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001251 has_name = (tsk->conn_type != 0);
1252 anc_data[0] = tsk->conn_type;
1253 anc_data[1] = tsk->conn_instance;
1254 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255 break;
1256 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001257 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258 }
Allan Stephens2db99832010-12-31 18:59:33 +00001259 if (has_name) {
1260 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1261 if (res)
1262 return res;
1263 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264
1265 return 0;
1266}
1267
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001268static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001269{
Ying Xuef2f98002015-01-09 15:27:05 +08001270 struct net *net = sock_net(&tsk->sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001271 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001272 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001273 u32 peer_port = tsk_peer_port(tsk);
1274 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001275
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001276 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001277 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001278 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1279 dnode, tsk_own_node(tsk), peer_port,
1280 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001281 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001282 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001283 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001284 msg_set_msgcnt(msg, ack);
Ying Xuef2f98002015-01-09 15:27:05 +08001285 tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001286}
1287
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001288static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001289{
1290 struct sock *sk = sock->sk;
1291 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001292 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001293 int err;
1294
1295 for (;;) {
1296 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001297 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001298 if (sock->state == SS_DISCONNECTING) {
1299 err = -ENOTCONN;
1300 break;
1301 }
1302 release_sock(sk);
1303 timeo = schedule_timeout(timeo);
1304 lock_sock(sk);
1305 }
1306 err = 0;
1307 if (!skb_queue_empty(&sk->sk_receive_queue))
1308 break;
1309 err = sock_intr_errno(timeo);
1310 if (signal_pending(current))
1311 break;
1312 err = -EAGAIN;
1313 if (!timeo)
1314 break;
1315 }
1316 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001317 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001318 return err;
1319}
1320
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001321/**
Ying Xue247f0f32014-02-18 16:06:46 +08001322 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 * @iocb: (unused)
1324 * @m: descriptor for message info
1325 * @buf_len: total size of user buffer area
1326 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001327 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1329 * If the complete message doesn't fit in user area, truncate it.
1330 *
1331 * Returns size of returned message data, errno otherwise
1332 */
Ying Xue247f0f32014-02-18 16:06:46 +08001333static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1334 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001336 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001337 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 struct sk_buff *buf;
1339 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001340 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 unsigned int sz;
1342 u32 err;
1343 int res;
1344
Allan Stephens0c3141e2008-04-15 00:22:02 -07001345 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 if (unlikely(!buf_len))
1347 return -EINVAL;
1348
Allan Stephens0c3141e2008-04-15 00:22:02 -07001349 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350
Allan Stephens0c3141e2008-04-15 00:22:02 -07001351 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 res = -ENOTCONN;
1353 goto exit;
1354 }
1355
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001356 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001357restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358
Allan Stephens0c3141e2008-04-15 00:22:02 -07001359 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001360 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001361 if (res)
1362 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001363
1364 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001365 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 msg = buf_msg(buf);
1367 sz = msg_data_sz(msg);
1368 err = msg_errcode(msg);
1369
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001372 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373 goto restart;
1374 }
1375
1376 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 set_orig_addr(m, msg);
1378
1379 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001380 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001381 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 goto exit;
1383
1384 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 if (!err) {
1386 if (unlikely(buf_len < sz)) {
1387 sz = buf_len;
1388 m->msg_flags |= MSG_TRUNC;
1389 }
David S. Miller51f3d022014-11-05 16:46:40 -05001390 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001391 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 res = sz;
1394 } else {
1395 if ((sock->state == SS_READY) ||
1396 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1397 res = 0;
1398 else
1399 res = -ECONNRESET;
1400 }
1401
1402 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001404 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001405 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001406 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001407 tsk->rcv_unacked = 0;
1408 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001409 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001410 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001411exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001412 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 return res;
1414}
1415
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001416/**
Ying Xue247f0f32014-02-18 16:06:46 +08001417 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 * @iocb: (unused)
1419 * @m: descriptor for message info
1420 * @buf_len: total size of user buffer area
1421 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001422 *
1423 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 * will optionally wait for more; never truncates data.
1425 *
1426 * Returns size of returned message data, errno otherwise
1427 */
Ying Xue247f0f32014-02-18 16:06:46 +08001428static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1429 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001431 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001432 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 struct sk_buff *buf;
1434 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001435 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001437 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001440 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441
1442 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001443 if (unlikely(!buf_len))
1444 return -EINVAL;
1445
Allan Stephens0c3141e2008-04-15 00:22:02 -07001446 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001448 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 res = -ENOTCONN;
1450 goto exit;
1451 }
1452
Florian Westphal3720d402010-08-17 11:00:04 +00001453 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001454 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001455
Allan Stephens0c3141e2008-04-15 00:22:02 -07001456restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001457 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001458 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001459 if (res)
1460 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001461
1462 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001463 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464 msg = buf_msg(buf);
1465 sz = msg_data_sz(msg);
1466 err = msg_errcode(msg);
1467
1468 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001470 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 goto restart;
1472 }
1473
1474 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 if (sz_copied == 0) {
1476 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001477 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001478 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 goto exit;
1480 }
1481
1482 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001484 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485
Allan Stephens0232fd02011-02-21 09:45:40 -05001486 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487 needed = (buf_len - sz_copied);
1488 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001489
David S. Miller51f3d022014-11-05 16:46:40 -05001490 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1491 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001492 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001494
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 sz_copied += sz_to_copy;
1496
1497 if (sz_to_copy < sz) {
1498 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001499 TIPC_SKB_CB(buf)->handle =
1500 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501 goto exit;
1502 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503 } else {
1504 if (sz_copied != 0)
1505 goto exit; /* can't add error msg to valid data */
1506
1507 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1508 res = 0;
1509 else
1510 res = -ECONNRESET;
1511 }
1512
1513 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001515 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001516 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001517 tsk->rcv_unacked = 0;
1518 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001519 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001520 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521
1522 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001523 if ((sz_copied < buf_len) && /* didn't get all requested data */
1524 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001525 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001526 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1527 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528 goto restart;
1529
1530exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001531 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001532 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001533}
1534
1535/**
Ying Xuef288bef2012-08-21 11:16:57 +08001536 * tipc_write_space - wake up thread if port congestion is released
1537 * @sk: socket
1538 */
1539static void tipc_write_space(struct sock *sk)
1540{
1541 struct socket_wq *wq;
1542
1543 rcu_read_lock();
1544 wq = rcu_dereference(sk->sk_wq);
1545 if (wq_has_sleeper(wq))
1546 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1547 POLLWRNORM | POLLWRBAND);
1548 rcu_read_unlock();
1549}
1550
1551/**
1552 * tipc_data_ready - wake up threads to indicate messages have been received
1553 * @sk: socket
1554 * @len: the length of messages
1555 */
David S. Miller676d2362014-04-11 16:15:36 -04001556static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001557{
1558 struct socket_wq *wq;
1559
1560 rcu_read_lock();
1561 wq = rcu_dereference(sk->sk_wq);
1562 if (wq_has_sleeper(wq))
1563 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1564 POLLRDNORM | POLLRDBAND);
1565 rcu_read_unlock();
1566}
1567
1568/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001569 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001570 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001571 * @msg: message
1572 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001573 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001574 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001575static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001576{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001577 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001578 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001579 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001580 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001581 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001582
1583 if (msg_mcast(msg))
1584 return retval;
1585
1586 switch ((int)sock->state) {
1587 case SS_CONNECTED:
1588 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001589 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001590 if (unlikely(msg_errcode(msg))) {
1591 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001592 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001593 /* let timer expire on it's own */
Ying Xuef2f98002015-01-09 15:27:05 +08001594 tipc_node_remove_conn(net, tsk_peer_node(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08001595 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001596 }
1597 retval = TIPC_OK;
1598 }
1599 break;
1600 case SS_CONNECTING:
1601 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001602
1603 if (unlikely(!msg_connected(msg)))
1604 break;
1605
Ying Xue584d24b2012-11-29 18:51:19 -05001606 if (unlikely(msg_errcode(msg))) {
1607 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001608 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001609 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001610 break;
1611 }
1612
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001613 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001614 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001615 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001616 retval = TIPC_OK;
1617 break;
1618 }
1619
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001620 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1621 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001622 sock->state = SS_CONNECTED;
1623
Ying Xue584d24b2012-11-29 18:51:19 -05001624 /* If an incoming message is an 'ACK-', it should be
1625 * discarded here because it doesn't contain useful
1626 * data. In addition, we should try to wake up
1627 * connect() routine if sleeping.
1628 */
1629 if (msg_data_sz(msg) == 0) {
1630 kfree_skb(*buf);
1631 *buf = NULL;
1632 if (waitqueue_active(sk_sleep(sk)))
1633 wake_up_interruptible(sk_sleep(sk));
1634 }
1635 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001636 break;
1637 case SS_LISTENING:
1638 case SS_UNCONNECTED:
1639 /* Accept only SYN message */
1640 if (!msg_connected(msg) && !(msg_errcode(msg)))
1641 retval = TIPC_OK;
1642 break;
1643 case SS_DISCONNECTING:
1644 break;
1645 default:
1646 pr_err("Unknown socket state %u\n", sock->state);
1647 }
1648 return retval;
1649}
1650
1651/**
Ying Xueaba79f32013-01-20 23:30:09 +01001652 * rcvbuf_limit - get proper overload limit of socket receive queue
1653 * @sk: socket
1654 * @buf: message
1655 *
1656 * For all connection oriented messages, irrespective of importance,
1657 * the default overload value (i.e. 67MB) is set as limit.
1658 *
1659 * For all connectionless messages, by default new queue limits are
1660 * as belows:
1661 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001662 * TIPC_LOW_IMPORTANCE (4 MB)
1663 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1664 * TIPC_HIGH_IMPORTANCE (16 MB)
1665 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001666 *
1667 * Returns overload limit according to corresponding message importance
1668 */
1669static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1670{
1671 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001672
1673 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001674 return sysctl_tipc_rmem[2];
1675
1676 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1677 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001678}
1679
1680/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001681 * filter_rcv - validate incoming message
1682 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001684 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 * Enqueues message on receive queue if acceptable; optionally handles
1686 * disconnect indication for a connected socket.
1687 *
1688 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001689 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001690 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001691 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001693static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001695 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001696 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001698 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001699 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001700 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001702 if (unlikely(msg_user(msg) == CONN_MANAGER))
1703 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001704
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001705 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1706 kfree_skb(buf);
1707 tsk->link_cong = 0;
1708 sk->sk_write_space(sk);
1709 return TIPC_OK;
1710 }
1711
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001713 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001714 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001715
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001717 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001718 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001720 rc = filter_connect(tsk, &buf);
1721 if (rc != TIPC_OK || buf == NULL)
1722 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 }
1724
1725 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001726 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001727 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728
Ying Xueaba79f32013-01-20 23:30:09 +01001729 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001730 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001732 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001733
David S. Miller676d2362014-04-11 16:15:36 -04001734 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735 return TIPC_OK;
1736}
1737
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001738/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001739 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001740 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001741 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001742 *
1743 * Caller must hold socket lock, but not port lock.
1744 *
1745 * Returns 0
1746 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001747static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001749 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001750 u32 onode;
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
Ying Xuea6ca1092014-11-26 11:41:55 +08001755 rc = filter_rcv(sk, skb);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001756
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001757 if (likely(!rc)) {
1758 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1759 atomic_add(truesize, &tsk->dupl_rcvcnt);
1760 return 0;
1761 }
1762
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001763 if ((rc < 0) && !tipc_msg_reverse(tsk_own_node(tsk), skb, &onode, -rc))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001764 return 0;
1765
Ying Xue34747532015-01-09 15:27:10 +08001766 tipc_link_xmit_skb(net, skb, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001767
Allan Stephens0c3141e2008-04-15 00:22:02 -07001768 return 0;
1769}
1770
1771/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001772 * tipc_sk_rcv - handle incoming message
Ying Xuea6ca1092014-11-26 11:41:55 +08001773 * @skb: buffer containing arriving message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001774 * Consumes buffer
1775 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001776 */
Ying Xuef2f98002015-01-09 15:27:05 +08001777int tipc_sk_rcv(struct net *net, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001778{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001779 struct tipc_sock *tsk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001780 struct tipc_net *tn;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001781 struct sock *sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001782 u32 dport = msg_destport(buf_msg(skb));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001783 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001784 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001785 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001786
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001787 /* Validate destination and message */
Ying Xuee05b31f2015-01-09 15:27:08 +08001788 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001789 if (unlikely(!tsk)) {
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001790 rc = tipc_msg_eval(net, skb, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001791 goto exit;
1792 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001793 sk = &tsk->sk;
1794
1795 /* Queue message */
Ying Xue1a194c22014-10-20 14:46:35 +08001796 spin_lock_bh(&sk->sk_lock.slock);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001797
Allan Stephens0c3141e2008-04-15 00:22:02 -07001798 if (!sock_owned_by_user(sk)) {
Ying Xuea6ca1092014-11-26 11:41:55 +08001799 rc = filter_rcv(sk, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001800 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001801 if (sk->sk_backlog.len == 0)
1802 atomic_set(&tsk->dupl_rcvcnt, 0);
Ying Xuea6ca1092014-11-26 11:41:55 +08001803 limit = rcvbuf_limit(sk, skb) + atomic_read(&tsk->dupl_rcvcnt);
1804 if (sk_add_backlog(sk, skb, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001805 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001806 }
Ying Xue1a194c22014-10-20 14:46:35 +08001807 spin_unlock_bh(&sk->sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001808 sock_put(sk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001809 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001810 return 0;
1811exit:
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001812 tn = net_generic(net, tipc_net_id);
1813 if ((rc < 0) && !tipc_msg_reverse(tn->own_addr, skb, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001814 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001815
Ying Xuef2f98002015-01-09 15:27:05 +08001816 tipc_link_xmit_skb(net, skb, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001817 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001818}
1819
Ying Xue78eb3a52014-01-17 09:50:03 +08001820static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1821{
1822 struct sock *sk = sock->sk;
1823 DEFINE_WAIT(wait);
1824 int done;
1825
1826 do {
1827 int err = sock_error(sk);
1828 if (err)
1829 return err;
1830 if (!*timeo_p)
1831 return -ETIMEDOUT;
1832 if (signal_pending(current))
1833 return sock_intr_errno(*timeo_p);
1834
1835 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1836 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1837 finish_wait(sk_sleep(sk), &wait);
1838 } while (!done);
1839 return 0;
1840}
1841
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842/**
Ying Xue247f0f32014-02-18 16:06:46 +08001843 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 * @sock: socket structure
1845 * @dest: socket address for destination port
1846 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001847 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848 *
1849 * Returns 0 on success, errno otherwise
1850 */
Ying Xue247f0f32014-02-18 16:06:46 +08001851static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1852 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001854 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001855 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1856 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001857 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1858 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001859 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860
Allan Stephens0c3141e2008-04-15 00:22:02 -07001861 lock_sock(sk);
1862
Allan Stephensb89741a2008-04-15 00:20:37 -07001863 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001864 if (sock->state == SS_READY) {
1865 res = -EOPNOTSUPP;
1866 goto exit;
1867 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868
Allan Stephensb89741a2008-04-15 00:20:37 -07001869 /*
1870 * Reject connection attempt using multicast address
1871 *
1872 * Note: send_msg() validates the rest of the address fields,
1873 * so there's no need to do it here
1874 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001875 if (dst->addrtype == TIPC_ADDR_MCAST) {
1876 res = -EINVAL;
1877 goto exit;
1878 }
1879
Ying Xue78eb3a52014-01-17 09:50:03 +08001880 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001881 switch (sock->state) {
1882 case SS_UNCONNECTED:
1883 /* Send a 'SYN-' to destination */
1884 m.msg_name = dest;
1885 m.msg_namelen = destlen;
1886
1887 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1888 * indicate send_msg() is never blocked.
1889 */
1890 if (!timeout)
1891 m.msg_flags = MSG_DONTWAIT;
1892
Ying Xue247f0f32014-02-18 16:06:46 +08001893 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001894 if ((res < 0) && (res != -EWOULDBLOCK))
1895 goto exit;
1896
1897 /* Just entered SS_CONNECTING state; the only
1898 * difference is that return value in non-blocking
1899 * case is EINPROGRESS, rather than EALREADY.
1900 */
1901 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001902 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001903 if (previous == SS_CONNECTING)
1904 res = -EALREADY;
1905 if (!timeout)
1906 goto exit;
1907 timeout = msecs_to_jiffies(timeout);
1908 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1909 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001910 break;
1911 case SS_CONNECTED:
1912 res = -EISCONN;
1913 break;
1914 default:
1915 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001916 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001917 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001918exit:
1919 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001920 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921}
1922
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001923/**
Ying Xue247f0f32014-02-18 16:06:46 +08001924 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925 * @sock: socket structure
1926 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001927 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928 * Returns 0 on success, errno otherwise
1929 */
Ying Xue247f0f32014-02-18 16:06:46 +08001930static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001931{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001932 struct sock *sk = sock->sk;
1933 int res;
1934
1935 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936
Ying Xue245f3d32011-07-06 06:01:13 -04001937 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001938 res = -EINVAL;
1939 else {
1940 sock->state = SS_LISTENING;
1941 res = 0;
1942 }
1943
1944 release_sock(sk);
1945 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946}
1947
Ying Xue6398e232014-01-17 09:50:04 +08001948static int tipc_wait_for_accept(struct socket *sock, long timeo)
1949{
1950 struct sock *sk = sock->sk;
1951 DEFINE_WAIT(wait);
1952 int err;
1953
1954 /* True wake-one mechanism for incoming connections: only
1955 * one process gets woken up, not the 'whole herd'.
1956 * Since we do not 'race & poll' for established sockets
1957 * anymore, the common case will execute the loop only once.
1958 */
1959 for (;;) {
1960 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1961 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001962 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001963 release_sock(sk);
1964 timeo = schedule_timeout(timeo);
1965 lock_sock(sk);
1966 }
1967 err = 0;
1968 if (!skb_queue_empty(&sk->sk_receive_queue))
1969 break;
1970 err = -EINVAL;
1971 if (sock->state != SS_LISTENING)
1972 break;
1973 err = sock_intr_errno(timeo);
1974 if (signal_pending(current))
1975 break;
1976 err = -EAGAIN;
1977 if (!timeo)
1978 break;
1979 }
1980 finish_wait(sk_sleep(sk), &wait);
1981 return err;
1982}
1983
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001984/**
Ying Xue247f0f32014-02-18 16:06:46 +08001985 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001986 * @sock: listening socket
1987 * @newsock: new socket that is to be connected
1988 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001989 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990 * Returns 0 on success, errno otherwise
1991 */
Ying Xue247f0f32014-02-18 16:06:46 +08001992static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001993{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001994 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001995 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001996 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001997 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001998 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001999 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000
Allan Stephens0c3141e2008-04-15 00:22:02 -07002001 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002002
Allan Stephens0c3141e2008-04-15 00:22:02 -07002003 if (sock->state != SS_LISTENING) {
2004 res = -EINVAL;
2005 goto exit;
2006 }
Ying Xue6398e232014-01-17 09:50:04 +08002007 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2008 res = tipc_wait_for_accept(sock, timeo);
2009 if (res)
2010 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002011
2012 buf = skb_peek(&sk->sk_receive_queue);
2013
Ying Xuec5fa7b32013-06-17 10:54:39 -04002014 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002015 if (res)
2016 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002017
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002018 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002019 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002020 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002021
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002022 /* we lock on new_sk; but lockdep sees the lock on sk */
2023 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002024
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002025 /*
2026 * Reject any stray messages received by new socket
2027 * before the socket lock was taken (very, very unlikely)
2028 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002029 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002031 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002032 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002033 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002035 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002036 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002037 new_tsock->conn_type = msg_nametype(msg);
2038 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002040
2041 /*
2042 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2043 * Respond to 'SYN+' by queuing it on new socket.
2044 */
2045 if (!msg_data_sz(msg)) {
2046 struct msghdr m = {NULL,};
2047
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002048 tsk_advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08002049 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002050 } else {
2051 __skb_dequeue(&sk->sk_receive_queue);
2052 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002053 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002054 }
2055 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002056exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002057 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058 return res;
2059}
2060
2061/**
Ying Xue247f0f32014-02-18 16:06:46 +08002062 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002064 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002065 *
2066 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002067 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068 * Returns 0 on success, errno otherwise
2069 */
Ying Xue247f0f32014-02-18 16:06:46 +08002070static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002072 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002073 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002074 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002075 struct sk_buff *skb;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002076 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077 int res;
2078
Allan Stephense247a8f2008-03-06 15:05:38 -08002079 if (how != SHUT_RDWR)
2080 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081
Allan Stephens0c3141e2008-04-15 00:22:02 -07002082 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083
2084 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002085 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002086 case SS_CONNECTED:
2087
Per Lidenb97bf3f2006-01-02 19:04:38 +01002088restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002089 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002090 skb = __skb_dequeue(&sk->sk_receive_queue);
2091 if (skb) {
2092 if (TIPC_SKB_CB(skb)->handle != NULL) {
2093 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002094 goto restart;
2095 }
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002096 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
Ying Xue34747532015-01-09 15:27:10 +08002097 TIPC_CONN_SHUTDOWN))
Ying Xuef2f98002015-01-09 15:27:05 +08002098 tipc_link_xmit_skb(net, skb, dnode,
2099 tsk->portid);
2100 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002101 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002102 dnode = tsk_peer_node(tsk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002103
2104 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002105 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002106 0, dnode, tsk_own_node(tsk),
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002107 tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002108 tsk->portid, TIPC_CONN_SHUTDOWN);
Ying Xuef2f98002015-01-09 15:27:05 +08002109 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002110 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002111 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002112 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002113 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114 /* fall through */
2115
2116 case SS_DISCONNECTING:
2117
Ying Xue75031152012-10-29 09:38:15 -04002118 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002119 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002120
2121 /* Wake up anyone sleeping in poll */
2122 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123 res = 0;
2124 break;
2125
2126 default:
2127 res = -ENOTCONN;
2128 }
2129
Allan Stephens0c3141e2008-04-15 00:22:02 -07002130 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 return res;
2132}
2133
Ying Xuef2f2a962015-01-09 15:27:02 +08002134static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002135{
Ying Xuef2f2a962015-01-09 15:27:02 +08002136 struct tipc_sock *tsk = (struct tipc_sock *)data;
2137 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002138 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002139 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002140 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002141
Jon Paul Maloy57289012014-08-22 18:09:09 -04002142 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002143 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002144 bh_unlock_sock(sk);
2145 goto exit;
2146 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002147 peer_port = tsk_peer_port(tsk);
2148 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002149
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002150 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002151 /* Previous probe not answered -> self abort */
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002152 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +08002153 TIPC_CONN_MSG, SHORT_H_SIZE, 0,
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002154 own_node, peer_node, tsk->portid,
Ying Xue34747532015-01-09 15:27:10 +08002155 peer_port, TIPC_ERR_NO_PORT);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002156 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002157 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2158 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002159 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002160 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002161 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002162 }
2163 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002164 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +08002165 tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002166exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002167 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002168}
2169
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002170static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002171 struct tipc_name_seq const *seq)
2172{
Ying Xuef2f98002015-01-09 15:27:05 +08002173 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002174 struct publication *publ;
2175 u32 key;
2176
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002177 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002178 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002179 key = tsk->portid + tsk->pub_count + 1;
2180 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002181 return -EADDRINUSE;
2182
Ying Xuef2f98002015-01-09 15:27:05 +08002183 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002184 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002185 if (unlikely(!publ))
2186 return -EINVAL;
2187
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002188 list_add(&publ->pport_list, &tsk->publications);
2189 tsk->pub_count++;
2190 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002191 return 0;
2192}
2193
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002194static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002195 struct tipc_name_seq const *seq)
2196{
Ying Xuef2f98002015-01-09 15:27:05 +08002197 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002198 struct publication *publ;
2199 struct publication *safe;
2200 int rc = -EINVAL;
2201
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002202 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002203 if (seq) {
2204 if (publ->scope != scope)
2205 continue;
2206 if (publ->type != seq->type)
2207 continue;
2208 if (publ->lower != seq->lower)
2209 continue;
2210 if (publ->upper != seq->upper)
2211 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002212 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002213 publ->ref, publ->key);
2214 rc = 0;
2215 break;
2216 }
Ying Xuef2f98002015-01-09 15:27:05 +08002217 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002218 publ->ref, publ->key);
2219 rc = 0;
2220 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002221 if (list_empty(&tsk->publications))
2222 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002223 return rc;
2224}
2225
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002226static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002227 int len, int full_id)
2228{
Ying Xue34747532015-01-09 15:27:10 +08002229 struct net *net = sock_net(&tsk->sk);
2230 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002231 struct publication *publ;
2232 int ret;
2233
2234 if (full_id)
2235 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
Ying Xue34747532015-01-09 15:27:10 +08002236 tipc_zone(tn->own_addr),
2237 tipc_cluster(tn->own_addr),
2238 tipc_node(tn->own_addr), tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002239 else
Ying Xue07f6c4b2015-01-07 13:41:58 +08002240 ret = tipc_snprintf(buf, len, "%-10u:", tsk->portid);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002241
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002242 if (tsk->connected) {
2243 u32 dport = tsk_peer_port(tsk);
2244 u32 destnode = tsk_peer_node(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002245
2246 ret += tipc_snprintf(buf + ret, len - ret,
2247 " connected to <%u.%u.%u:%u>",
2248 tipc_zone(destnode),
2249 tipc_cluster(destnode),
2250 tipc_node(destnode), dport);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002251 if (tsk->conn_type != 0)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002252 ret += tipc_snprintf(buf + ret, len - ret,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002253 " via {%u,%u}", tsk->conn_type,
2254 tsk->conn_instance);
2255 } else if (tsk->published) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002256 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002257 list_for_each_entry(publ, &tsk->publications, pport_list) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002258 if (publ->lower == publ->upper)
2259 ret += tipc_snprintf(buf + ret, len - ret,
2260 " {%u,%u}", publ->type,
2261 publ->lower);
2262 else
2263 ret += tipc_snprintf(buf + ret, len - ret,
2264 " {%u,%u,%u}", publ->type,
2265 publ->lower, publ->upper);
2266 }
2267 }
2268 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2269 return ret;
2270}
2271
Ying Xuee05b31f2015-01-09 15:27:08 +08002272struct sk_buff *tipc_sk_socks_show(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002273{
Ying Xuee05b31f2015-01-09 15:27:08 +08002274 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002275 const struct bucket_table *tbl;
2276 struct rhash_head *pos;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002277 struct sk_buff *buf;
2278 struct tlv_desc *rep_tlv;
2279 char *pb;
2280 int pb_len;
2281 struct tipc_sock *tsk;
2282 int str_len = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002283 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002284
2285 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2286 if (!buf)
2287 return NULL;
2288 rep_tlv = (struct tlv_desc *)buf->data;
2289 pb = TLV_DATA(rep_tlv);
2290 pb_len = ULTRA_STRING_MAX_LEN;
2291
Ying Xue07f6c4b2015-01-07 13:41:58 +08002292 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002293 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002294 for (i = 0; i < tbl->size; i++) {
2295 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2296 spin_lock_bh(&tsk->sk.sk_lock.slock);
2297 str_len += tipc_sk_show(tsk, pb + str_len,
2298 pb_len - str_len, 0);
2299 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2300 }
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002301 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002302 rcu_read_unlock();
2303
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002304 str_len += 1; /* for "\0" */
2305 skb_put(buf, TLV_SPACE(str_len));
2306 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2307
2308 return buf;
2309}
2310
2311/* tipc_sk_reinit: set non-zero address in all existing sockets
2312 * when we go from standalone to network mode.
2313 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002314void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002315{
Ying Xuee05b31f2015-01-09 15:27:08 +08002316 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002317 const struct bucket_table *tbl;
2318 struct rhash_head *pos;
2319 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002320 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002321 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002322
Ying Xue07f6c4b2015-01-07 13:41:58 +08002323 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002324 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002325 for (i = 0; i < tbl->size; i++) {
2326 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2327 spin_lock_bh(&tsk->sk.sk_lock.slock);
2328 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002329 msg_set_prevnode(msg, tn->own_addr);
2330 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002331 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2332 }
2333 }
2334 rcu_read_unlock();
2335}
2336
Ying Xuee05b31f2015-01-09 15:27:08 +08002337static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002338{
Ying Xuee05b31f2015-01-09 15:27:08 +08002339 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002340 struct tipc_sock *tsk;
2341
2342 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002343 tsk = rhashtable_lookup(&tn->sk_rht, &portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002344 if (tsk)
2345 sock_hold(&tsk->sk);
2346 rcu_read_unlock();
2347
2348 return tsk;
2349}
2350
2351static int tipc_sk_insert(struct tipc_sock *tsk)
2352{
Ying Xuee05b31f2015-01-09 15:27:08 +08002353 struct sock *sk = &tsk->sk;
2354 struct net *net = sock_net(sk);
2355 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002356 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2357 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2358
2359 while (remaining--) {
2360 portid++;
2361 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2362 portid = TIPC_MIN_PORT;
2363 tsk->portid = portid;
2364 sock_hold(&tsk->sk);
Ying Xuee05b31f2015-01-09 15:27:08 +08002365 if (rhashtable_lookup_insert(&tn->sk_rht, &tsk->node))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002366 return 0;
2367 sock_put(&tsk->sk);
2368 }
2369
2370 return -1;
2371}
2372
2373static void tipc_sk_remove(struct tipc_sock *tsk)
2374{
2375 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002376 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002377
Ying Xuee05b31f2015-01-09 15:27:08 +08002378 if (rhashtable_remove(&tn->sk_rht, &tsk->node)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002379 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2380 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002381 }
2382}
2383
Ying Xuee05b31f2015-01-09 15:27:08 +08002384int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002385{
Ying Xuee05b31f2015-01-09 15:27:08 +08002386 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002387 struct rhashtable_params rht_params = {
2388 .nelem_hint = 192,
2389 .head_offset = offsetof(struct tipc_sock, node),
2390 .key_offset = offsetof(struct tipc_sock, portid),
2391 .key_len = sizeof(u32), /* portid */
2392 .hashfn = jhash,
2393 .max_shift = 20, /* 1M */
2394 .min_shift = 8, /* 256 */
2395 .grow_decision = rht_grow_above_75,
2396 .shrink_decision = rht_shrink_below_30,
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002397 };
2398
Ying Xuee05b31f2015-01-09 15:27:08 +08002399 return rhashtable_init(&tn->sk_rht, &rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002400}
2401
Ying Xuee05b31f2015-01-09 15:27:08 +08002402void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002403{
Ying Xuee05b31f2015-01-09 15:27:08 +08002404 struct tipc_net *tn = net_generic(net, tipc_net_id);
2405
Ying Xue07f6c4b2015-01-07 13:41:58 +08002406 /* Wait for socket readers to complete */
2407 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002408
Ying Xuee05b31f2015-01-09 15:27:08 +08002409 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002410}
2411
2412/**
Ying Xue247f0f32014-02-18 16:06:46 +08002413 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002414 * @sock: socket structure
2415 * @lvl: option level
2416 * @opt: option identifier
2417 * @ov: pointer to new option value
2418 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002419 *
2420 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002421 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002422 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002423 * Returns 0 on success, errno otherwise
2424 */
Ying Xue247f0f32014-02-18 16:06:46 +08002425static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2426 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002427{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002428 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002429 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002430 u32 value;
2431 int res;
2432
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002433 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2434 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002435 if (lvl != SOL_TIPC)
2436 return -ENOPROTOOPT;
2437 if (ol < sizeof(value))
2438 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002439 res = get_user(value, (u32 __user *)ov);
2440 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002441 return res;
2442
Allan Stephens0c3141e2008-04-15 00:22:02 -07002443 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002444
Per Lidenb97bf3f2006-01-02 19:04:38 +01002445 switch (opt) {
2446 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002447 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448 break;
2449 case TIPC_SRC_DROPPABLE:
2450 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002451 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002452 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002453 res = -ENOPROTOOPT;
2454 break;
2455 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002456 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457 break;
2458 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002459 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002460 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002461 break;
2462 default:
2463 res = -EINVAL;
2464 }
2465
Allan Stephens0c3141e2008-04-15 00:22:02 -07002466 release_sock(sk);
2467
Per Lidenb97bf3f2006-01-02 19:04:38 +01002468 return res;
2469}
2470
2471/**
Ying Xue247f0f32014-02-18 16:06:46 +08002472 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002473 * @sock: socket structure
2474 * @lvl: option level
2475 * @opt: option identifier
2476 * @ov: receptacle for option value
2477 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002478 *
2479 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002480 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002481 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002482 * Returns 0 on success, errno otherwise
2483 */
Ying Xue247f0f32014-02-18 16:06:46 +08002484static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2485 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002486{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002487 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002488 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002489 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002490 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002491 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002492
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002493 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2494 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002495 if (lvl != SOL_TIPC)
2496 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002497 res = get_user(len, ol);
2498 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002499 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002500
Allan Stephens0c3141e2008-04-15 00:22:02 -07002501 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002502
2503 switch (opt) {
2504 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002505 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002506 break;
2507 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002508 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002509 break;
2510 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002511 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002512 break;
2513 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002514 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002515 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002516 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002517 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002518 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002519 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002520 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002521 value = skb_queue_len(&sk->sk_receive_queue);
2522 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002523 default:
2524 res = -EINVAL;
2525 }
2526
Allan Stephens0c3141e2008-04-15 00:22:02 -07002527 release_sock(sk);
2528
Paul Gortmaker25860c32010-12-31 18:59:31 +00002529 if (res)
2530 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002531
Paul Gortmaker25860c32010-12-31 18:59:31 +00002532 if (len < sizeof(value))
2533 return -EINVAL;
2534
2535 if (copy_to_user(ov, &value, sizeof(value)))
2536 return -EFAULT;
2537
2538 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002539}
2540
Ying Xuef2f98002015-01-09 15:27:05 +08002541static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002542{
Ying Xuef2f98002015-01-09 15:27:05 +08002543 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002544 struct tipc_sioc_ln_req lnr;
2545 void __user *argp = (void __user *)arg;
2546
2547 switch (cmd) {
2548 case SIOCGETLINKNAME:
2549 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2550 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002551 if (!tipc_node_get_linkname(sock_net(sk),
2552 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002553 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2554 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2555 return -EFAULT;
2556 return 0;
2557 }
2558 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002559 default:
2560 return -ENOIOCTLCMD;
2561 }
2562}
2563
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002564/* Protocol switches for the various types of TIPC sockets */
2565
Florian Westphalbca65ea2008-02-07 18:18:01 -08002566static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002567 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002568 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002569 .release = tipc_release,
2570 .bind = tipc_bind,
2571 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002572 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002573 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002574 .getname = tipc_getname,
2575 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002576 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002577 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002578 .shutdown = tipc_shutdown,
2579 .setsockopt = tipc_setsockopt,
2580 .getsockopt = tipc_getsockopt,
2581 .sendmsg = tipc_sendmsg,
2582 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002583 .mmap = sock_no_mmap,
2584 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002585};
2586
Florian Westphalbca65ea2008-02-07 18:18:01 -08002587static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002588 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002589 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002590 .release = tipc_release,
2591 .bind = tipc_bind,
2592 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002593 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002594 .accept = tipc_accept,
2595 .getname = tipc_getname,
2596 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002597 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002598 .listen = tipc_listen,
2599 .shutdown = tipc_shutdown,
2600 .setsockopt = tipc_setsockopt,
2601 .getsockopt = tipc_getsockopt,
2602 .sendmsg = tipc_send_packet,
2603 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002604 .mmap = sock_no_mmap,
2605 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002606};
2607
Florian Westphalbca65ea2008-02-07 18:18:01 -08002608static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002609 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002610 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002611 .release = tipc_release,
2612 .bind = tipc_bind,
2613 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002614 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002615 .accept = tipc_accept,
2616 .getname = tipc_getname,
2617 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002618 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002619 .listen = tipc_listen,
2620 .shutdown = tipc_shutdown,
2621 .setsockopt = tipc_setsockopt,
2622 .getsockopt = tipc_getsockopt,
2623 .sendmsg = tipc_send_stream,
2624 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002625 .mmap = sock_no_mmap,
2626 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002627};
2628
Florian Westphalbca65ea2008-02-07 18:18:01 -08002629static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002630 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002631 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002632 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002633};
2634
2635static struct proto tipc_proto = {
2636 .name = "TIPC",
2637 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002638 .obj_size = sizeof(struct tipc_sock),
2639 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002640};
2641
Ying Xuec5fa7b32013-06-17 10:54:39 -04002642static struct proto tipc_proto_kern = {
2643 .name = "TIPC",
2644 .obj_size = sizeof(struct tipc_sock),
2645 .sysctl_rmem = sysctl_tipc_rmem
2646};
2647
Per Lidenb97bf3f2006-01-02 19:04:38 +01002648/**
Per Liden4323add2006-01-18 00:38:21 +01002649 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002650 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002651 * Returns 0 on success, errno otherwise
2652 */
Per Liden4323add2006-01-18 00:38:21 +01002653int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002654{
2655 int res;
2656
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002657 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002658 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002659 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002660 goto out;
2661 }
2662
2663 res = sock_register(&tipc_family_ops);
2664 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002665 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002666 proto_unregister(&tipc_proto);
2667 goto out;
2668 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002669 out:
2670 return res;
2671}
2672
2673/**
Per Liden4323add2006-01-18 00:38:21 +01002674 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002675 */
Per Liden4323add2006-01-18 00:38:21 +01002676void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002677{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002678 sock_unregister(tipc_family_ops.family);
2679 proto_unregister(&tipc_proto);
2680}
Richard Alpe34b78a122014-11-20 10:29:10 +01002681
2682/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002683static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002684{
2685 u32 peer_node;
2686 u32 peer_port;
2687 struct nlattr *nest;
2688
2689 peer_node = tsk_peer_node(tsk);
2690 peer_port = tsk_peer_port(tsk);
2691
2692 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2693
2694 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2695 goto msg_full;
2696 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2697 goto msg_full;
2698
2699 if (tsk->conn_type != 0) {
2700 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2701 goto msg_full;
2702 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2703 goto msg_full;
2704 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2705 goto msg_full;
2706 }
2707 nla_nest_end(skb, nest);
2708
2709 return 0;
2710
2711msg_full:
2712 nla_nest_cancel(skb, nest);
2713
2714 return -EMSGSIZE;
2715}
2716
2717/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002718static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2719 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002720{
2721 int err;
2722 void *hdr;
2723 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002724 struct net *net = sock_net(skb->sk);
2725 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002726
2727 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2728 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2729 if (!hdr)
2730 goto msg_cancel;
2731
2732 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2733 if (!attrs)
2734 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002735 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002736 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002737 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002738 goto attr_msg_cancel;
2739
2740 if (tsk->connected) {
2741 err = __tipc_nl_add_sk_con(skb, tsk);
2742 if (err)
2743 goto attr_msg_cancel;
2744 } else if (!list_empty(&tsk->publications)) {
2745 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2746 goto attr_msg_cancel;
2747 }
2748 nla_nest_end(skb, attrs);
2749 genlmsg_end(skb, hdr);
2750
2751 return 0;
2752
2753attr_msg_cancel:
2754 nla_nest_cancel(skb, attrs);
2755genlmsg_cancel:
2756 genlmsg_cancel(skb, hdr);
2757msg_cancel:
2758 return -EMSGSIZE;
2759}
2760
2761int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2762{
2763 int err;
2764 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002765 const struct bucket_table *tbl;
2766 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002767 struct net *net = sock_net(skb->sk);
2768 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002769 u32 tbl_id = cb->args[0];
2770 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002771
Ying Xue07f6c4b2015-01-07 13:41:58 +08002772 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002773 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002774 for (; tbl_id < tbl->size; tbl_id++) {
2775 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002776 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002777 if (prev_portid && prev_portid != tsk->portid) {
2778 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2779 continue;
2780 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002781
Richard Alped6e164e2015-01-16 12:30:40 +01002782 err = __tipc_nl_add_sk(skb, cb, tsk);
2783 if (err) {
2784 prev_portid = tsk->portid;
2785 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2786 goto out;
2787 }
2788 prev_portid = 0;
2789 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002790 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002791 }
Richard Alped6e164e2015-01-16 12:30:40 +01002792out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002793 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002794 cb->args[0] = tbl_id;
2795 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002796
2797 return skb->len;
2798}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002799
2800/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002801static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2802 struct netlink_callback *cb,
2803 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002804{
2805 void *hdr;
2806 struct nlattr *attrs;
2807
2808 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2809 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2810 if (!hdr)
2811 goto msg_cancel;
2812
2813 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2814 if (!attrs)
2815 goto genlmsg_cancel;
2816
2817 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2818 goto attr_msg_cancel;
2819 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2820 goto attr_msg_cancel;
2821 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2822 goto attr_msg_cancel;
2823 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2824 goto attr_msg_cancel;
2825
2826 nla_nest_end(skb, attrs);
2827 genlmsg_end(skb, hdr);
2828
2829 return 0;
2830
2831attr_msg_cancel:
2832 nla_nest_cancel(skb, attrs);
2833genlmsg_cancel:
2834 genlmsg_cancel(skb, hdr);
2835msg_cancel:
2836 return -EMSGSIZE;
2837}
2838
2839/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002840static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2841 struct netlink_callback *cb,
2842 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002843{
2844 int err;
2845 struct publication *p;
2846
2847 if (*last_publ) {
2848 list_for_each_entry(p, &tsk->publications, pport_list) {
2849 if (p->key == *last_publ)
2850 break;
2851 }
2852 if (p->key != *last_publ) {
2853 /* We never set seq or call nl_dump_check_consistent()
2854 * this means that setting prev_seq here will cause the
2855 * consistence check to fail in the netlink callback
2856 * handler. Resulting in the last NLMSG_DONE message
2857 * having the NLM_F_DUMP_INTR flag set.
2858 */
2859 cb->prev_seq = 1;
2860 *last_publ = 0;
2861 return -EPIPE;
2862 }
2863 } else {
2864 p = list_first_entry(&tsk->publications, struct publication,
2865 pport_list);
2866 }
2867
2868 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2869 err = __tipc_nl_add_sk_publ(skb, cb, p);
2870 if (err) {
2871 *last_publ = p->key;
2872 return err;
2873 }
2874 }
2875 *last_publ = 0;
2876
2877 return 0;
2878}
2879
2880int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2881{
2882 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002883 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002884 u32 last_publ = cb->args[1];
2885 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002886 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002887 struct tipc_sock *tsk;
2888
Ying Xue07f6c4b2015-01-07 13:41:58 +08002889 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002890 struct nlattr **attrs;
2891 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2892
2893 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2894 if (err)
2895 return err;
2896
2897 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2898 attrs[TIPC_NLA_SOCK],
2899 tipc_nl_sock_policy);
2900 if (err)
2901 return err;
2902
2903 if (!sock[TIPC_NLA_SOCK_REF])
2904 return -EINVAL;
2905
Ying Xue07f6c4b2015-01-07 13:41:58 +08002906 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002907 }
2908
2909 if (done)
2910 return 0;
2911
Ying Xuee05b31f2015-01-09 15:27:08 +08002912 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002913 if (!tsk)
2914 return -EINVAL;
2915
2916 lock_sock(&tsk->sk);
2917 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2918 if (!err)
2919 done = 1;
2920 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002921 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002922
Ying Xue07f6c4b2015-01-07 13:41:58 +08002923 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002924 cb->args[1] = last_publ;
2925 cb->args[2] = done;
2926
2927 return skb->len;
2928}