blob: 73c2f518a7c0b7bd0ca8f095a7eb7df9a51dbb0e [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 Maloy3c724ac2015-02-05 08:36:43 -05004 * Copyright (c) 2001-2007, 2012-2015, 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 Maloyc637c102015-02-05 08:36:41 -050043#include "name_distr.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 * @conn_timeout: the time we can wait for an unresponded setup request
73 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
74 * @link_cong: non-zero if owner must sleep because of link congestion
75 * @sent_unacked: # messages sent by socket, and not yet acked by peer
76 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Erik Hugnef2f80362015-03-19 09:02:19 +010077 * @remote: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080078 * @node: hash table node
79 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040080 */
81struct tipc_sock {
82 struct sock sk;
83 int connected;
84 u32 conn_type;
85 u32 conn_instance;
86 int published;
87 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080088 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040089 struct tipc_msg phdr;
90 struct list_head sock_list;
91 struct list_head publications;
92 u32 pub_count;
93 u32 probing_state;
Ying Xue2f55c432015-01-09 15:27:00 +080094 unsigned long probing_intv;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040095 uint conn_timeout;
96 atomic_t dupl_rcvcnt;
97 bool link_cong;
98 uint sent_unacked;
99 uint rcv_unacked;
Erik Hugnef2f80362015-03-19 09:02:19 +0100100 struct sockaddr_tipc remote;
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);
Ying Xue39a02952015-03-02 15:37:47 +0800119static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
120 size_t dsz);
121static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122
Florian Westphalbca65ea2008-02-07 18:18:01 -0800123static const struct proto_ops packet_ops;
124static const struct proto_ops stream_ops;
125static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126static struct proto tipc_proto;
127
Richard Alpe1a1a1432014-11-20 10:29:11 +0100128static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
129 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
130 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
131 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
132 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
133 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
134};
135
Herbert Xu6cca72892015-03-20 21:57:05 +1100136static const struct rhashtable_params tsk_rht_params;
137
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900138/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700139 * Revised TIPC socket locking policy:
140 *
141 * Most socket operations take the standard socket lock when they start
142 * and hold it until they finish (or until they need to sleep). Acquiring
143 * this lock grants the owner exclusive access to the fields of the socket
144 * data structures, with the exception of the backlog queue. A few socket
145 * operations can be done without taking the socket lock because they only
146 * read socket information that never changes during the life of the socket.
147 *
148 * Socket operations may acquire the lock for the associated TIPC port if they
149 * need to perform an operation on the port. If any routine needs to acquire
150 * both the socket lock and the port lock it must take the socket lock first
151 * to avoid the risk of deadlock.
152 *
153 * The dispatcher handling incoming messages cannot grab the socket lock in
154 * the standard fashion, since invoked it runs at the BH level and cannot block.
155 * Instead, it checks to see if the socket lock is currently owned by someone,
156 * and either handles the message itself or adds it to the socket's backlog
157 * queue; in the latter case the queued message is processed once the process
158 * owning the socket lock releases it.
159 *
160 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
161 * the problem of a blocked socket operation preventing any other operations
162 * from occurring. However, applications must be careful if they have
163 * multiple threads trying to send (or receive) on the same socket, as these
164 * operations might interfere with each other. For example, doing a connect
165 * and a receive at the same time might allow the receive to consume the
166 * ACK message meant for the connect. While additional work could be done
167 * to try and overcome this, it doesn't seem to be worthwhile at the present.
168 *
169 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
170 * that another operation that must be performed in a non-blocking manner is
171 * not delayed for very long because the lock has already been taken.
172 *
173 * NOTE: This code assumes that certain fields of a port/socket pair are
174 * constant over its lifetime; such fields can be examined without taking
175 * the socket lock and/or port lock, and do not need to be re-read even
176 * after resuming processing after waiting. These fields include:
177 * - socket type
178 * - pointer to socket sk structure (aka tipc_sock structure)
179 * - pointer to port structure
180 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500183static u32 tsk_own_node(struct tipc_sock *tsk)
184{
185 return msg_prevnode(&tsk->phdr);
186}
187
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400188static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400189{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400190 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400191}
192
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400193static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400194{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400195 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400196}
197
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400198static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400199{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400200 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400201}
202
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400203static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400204{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400205 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400206}
207
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400208static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400209{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400210 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400211}
212
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400213static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400214{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400215 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400216}
217
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400218static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400219{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400220 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400221}
222
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400223static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400224{
225 if (imp > TIPC_CRITICAL_IMPORTANCE)
226 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400227 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400228 return 0;
229}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400230
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400231static struct tipc_sock *tipc_sk(const struct sock *sk)
232{
233 return container_of(sk, struct tipc_sock, sk);
234}
235
236static int tsk_conn_cong(struct tipc_sock *tsk)
237{
238 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
239}
240
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400242 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700243 *
244 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400246static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400248 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249}
250
251/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400252 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700253 *
254 * Caller must hold socket lock
255 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400256static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700257{
Ying Xuea6ca1092014-11-26 11:41:55 +0800258 struct sk_buff *skb;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500259 u32 dnode;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500260 u32 own_node = tsk_own_node(tipc_sk(sk));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700261
Ying Xuea6ca1092014-11-26 11:41:55 +0800262 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500263 if (tipc_msg_reverse(own_node, skb, &dnode, TIPC_ERR_NO_PORT))
264 tipc_link_xmit_skb(sock_net(sk), skb, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500265 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700266}
267
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400268/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400269 *
270 * Handles cases where the node's network address has changed from
271 * the default of <0.0.0> to its configured setting.
272 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400273static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400274{
Ying Xue34747532015-01-09 15:27:10 +0800275 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400276 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400277 u32 orig_node;
278 u32 peer_node;
279
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400280 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400281 return false;
282
283 if (unlikely(msg_origport(msg) != peer_port))
284 return false;
285
286 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400287 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400288
289 if (likely(orig_node == peer_node))
290 return true;
291
Ying Xue34747532015-01-09 15:27:10 +0800292 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400293 return true;
294
Ying Xue34747532015-01-09 15:27:10 +0800295 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400296 return true;
297
298 return false;
299}
300
Allan Stephens0c3141e2008-04-15 00:22:02 -0700301/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400302 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700303 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 * @sock: pre-allocated socket structure
305 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800306 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900307 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700308 * This routine creates additional data structures used by the TIPC socket,
309 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 *
311 * Returns 0 on success, errno otherwise
312 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400313static int tipc_sk_create(struct net *net, struct socket *sock,
314 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500316 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700317 const struct proto_ops *ops;
318 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400320 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400321 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700322
323 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 if (unlikely(protocol != 0))
325 return -EPROTONOSUPPORT;
326
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 switch (sock->type) {
328 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700329 ops = &stream_ops;
330 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 break;
332 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700333 ops = &packet_ops;
334 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 break;
336 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700338 ops = &msg_ops;
339 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 break;
Allan Stephens49978652006-06-25 23:47:18 -0700341 default:
Allan Stephens49978652006-06-25 23:47:18 -0700342 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 }
344
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 /* Allocate socket's protocol area */
Ying Xue76100a82015-03-18 09:32:57 +0800346 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700347 if (sk == NULL)
348 return -ENOMEM;
349
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400350 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400351 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400352 INIT_LIST_HEAD(&tsk->publications);
353 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500354 tn = net_generic(sock_net(sk), tipc_net_id);
355 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400356 NAMED_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700359 sock->ops = ops;
360 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 sock_init_data(sock, sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800362 if (tipc_sk_insert(tsk)) {
363 pr_warn("Socket create failed; port numbrer exhausted\n");
364 return -EINVAL;
365 }
366 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800367 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400368 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400369 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800370 sk->sk_data_ready = tipc_data_ready;
371 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400372 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500373 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400374 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700375
Allan Stephens0c3141e2008-04-15 00:22:02 -0700376 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400377 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700378 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400379 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700380 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381 return 0;
382}
383
Ying Xue07f6c4b2015-01-07 13:41:58 +0800384static void tipc_sk_callback(struct rcu_head *head)
385{
386 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
387
388 sock_put(&tsk->sk);
389}
390
Ying Xuec5fa7b32013-06-17 10:54:39 -0400391/**
Ying Xue247f0f32014-02-18 16:06:46 +0800392 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 * @sock: socket to destroy
394 *
395 * This routine cleans up any messages that are still queued on the socket.
396 * For DGRAM and RDM socket types, all queued messages are rejected.
397 * For SEQPACKET and STREAM socket types, the first message is rejected
398 * and any others are discarded. (If the first message on a STREAM socket
399 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900400 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 * NOTE: Rejected messages are not necessarily returned to the sender! They
402 * are returned or discarded according to the "destination droppable" setting
403 * specified for the message by the sender.
404 *
405 * Returns 0 on success, errno otherwise
406 */
Ying Xue247f0f32014-02-18 16:06:46 +0800407static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 struct sock *sk = sock->sk;
Sasha Levin357c4772015-01-13 12:46:41 -0500410 struct net *net;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400411 struct tipc_sock *tsk;
Ying Xuea6ca1092014-11-26 11:41:55 +0800412 struct sk_buff *skb;
Ying Xuef2f2a962015-01-09 15:27:02 +0800413 u32 dnode, probing_state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414
Allan Stephens0c3141e2008-04-15 00:22:02 -0700415 /*
416 * Exit if socket isn't fully initialized (occurs when a failed accept()
417 * releases a pre-allocated child socket that was never used)
418 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700419 if (sk == NULL)
420 return 0;
421
Sasha Levin357c4772015-01-13 12:46:41 -0500422 net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400423 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700424 lock_sock(sk);
425
426 /*
427 * Reject all unreceived messages, except on an active connection
428 * (which disconnects locally & sends a 'FIN+' to peer)
429 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400430 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 while (sock->state != SS_DISCONNECTING) {
Ying Xuea6ca1092014-11-26 11:41:55 +0800432 skb = __skb_dequeue(&sk->sk_receive_queue);
433 if (skb == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 break;
Ying Xuea6ca1092014-11-26 11:41:55 +0800435 if (TIPC_SKB_CB(skb)->handle != NULL)
436 kfree_skb(skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700437 else {
438 if ((sock->state == SS_CONNECTING) ||
439 (sock->state == SS_CONNECTED)) {
440 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400441 tsk->connected = 0;
Ying Xuef2f98002015-01-09 15:27:05 +0800442 tipc_node_remove_conn(net, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700443 }
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500444 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
Ying Xue34747532015-01-09 15:27:10 +0800445 TIPC_ERR_NO_PORT))
Ying Xuef2f98002015-01-09 15:27:05 +0800446 tipc_link_xmit_skb(net, skb, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700447 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 }
449
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400450 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xuef2f2a962015-01-09 15:27:02 +0800451 probing_state = tsk->probing_state;
Ying Xue3721e9c2015-01-13 17:07:48 +0800452 if (del_timer_sync(&sk->sk_timer) &&
453 probing_state != TIPC_CONN_PROBING)
Ying Xuef2f2a962015-01-09 15:27:02 +0800454 sock_put(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800455 tipc_sk_remove(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400456 if (tsk->connected) {
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500457 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +0800458 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500459 tsk_own_node(tsk), tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +0800460 tsk->portid, TIPC_ERR_NO_PORT);
Ying Xuea6ca1092014-11-26 11:41:55 +0800461 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +0800462 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
463 tipc_node_remove_conn(net, dnode, tsk->portid);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400464 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465
Allan Stephens0c3141e2008-04-15 00:22:02 -0700466 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100467 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468
Allan Stephens0c3141e2008-04-15 00:22:02 -0700469 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700470 sock->state = SS_DISCONNECTING;
471 release_sock(sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800472
473 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700474 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200476 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477}
478
479/**
Ying Xue247f0f32014-02-18 16:06:46 +0800480 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 * @sock: socket structure
482 * @uaddr: socket address describing name(s) and desired operation
483 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900484 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 * Name and name sequence binding is indicated using a positive scope value;
486 * a negative scope value unbinds the specified name. Specifying no name
487 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900488 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700490 *
491 * NOTE: This routine doesn't need to take the socket lock since it doesn't
492 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 */
Ying Xue247f0f32014-02-18 16:06:46 +0800494static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
495 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496{
Ying Xue84602762013-12-27 10:18:28 +0800497 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400499 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800500 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501
Ying Xue84602762013-12-27 10:18:28 +0800502 lock_sock(sk);
503 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400504 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800505 goto exit;
506 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900507
Ying Xue84602762013-12-27 10:18:28 +0800508 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
509 res = -EINVAL;
510 goto exit;
511 }
512 if (addr->family != AF_TIPC) {
513 res = -EAFNOSUPPORT;
514 goto exit;
515 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 if (addr->addrtype == TIPC_ADDR_NAME)
518 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800519 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
520 res = -EAFNOSUPPORT;
521 goto exit;
522 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900523
Ying Xue13a2e892013-06-17 10:54:40 -0400524 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400525 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800526 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
527 res = -EACCES;
528 goto exit;
529 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400530
Ying Xue84602762013-12-27 10:18:28 +0800531 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400532 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
533 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800534exit:
535 release_sock(sk);
536 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537}
538
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900539/**
Ying Xue247f0f32014-02-18 16:06:46 +0800540 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 * @sock: socket structure
542 * @uaddr: area for returned socket address
543 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700544 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900545 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700547 *
Allan Stephens2da59912008-07-14 22:43:32 -0700548 * NOTE: This routine doesn't need to take the socket lock since it only
549 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000550 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 */
Ying Xue247f0f32014-02-18 16:06:46 +0800552static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
553 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400556 struct tipc_sock *tsk = tipc_sk(sock->sk);
Ying Xue34747532015-01-09 15:27:10 +0800557 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000559 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700560 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700561 if ((sock->state != SS_CONNECTED) &&
562 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
563 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400564 addr->addr.id.ref = tsk_peer_port(tsk);
565 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700566 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800567 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800568 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700569 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570
571 *uaddr_len = sizeof(*addr);
572 addr->addrtype = TIPC_ADDR_ID;
573 addr->family = AF_TIPC;
574 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 addr->addr.name.domain = 0;
576
Allan Stephens0c3141e2008-04-15 00:22:02 -0700577 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578}
579
580/**
Ying Xue247f0f32014-02-18 16:06:46 +0800581 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * @file: file structure associated with the socket
583 * @sock: socket for which to calculate the poll bits
584 * @wait: ???
585 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700586 * Returns pollmask value
587 *
588 * COMMENTARY:
589 * It appears that the usual socket locking mechanisms are not useful here
590 * since the pollmask info is potentially out-of-date the moment this routine
591 * exits. TCP and other protocols seem to rely on higher level poll routines
592 * to handle any preventable race conditions, so TIPC will do the same ...
593 *
594 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700595 *
Allan Stephensf662c072010-08-17 11:00:06 +0000596 * socket state flags set
597 * ------------ ---------
598 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200599 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000600 *
601 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
602 * no write flags
603 *
604 * connected POLLIN/POLLRDNORM if data in rx queue
605 * POLLOUT if port is not congested
606 *
607 * disconnecting POLLIN/POLLRDNORM/POLLHUP
608 * no write flags
609 *
610 * listening POLLIN if SYN in rx queue
611 * no write flags
612 *
613 * ready POLLIN/POLLRDNORM if data in rx queue
614 * [connectionless] POLLOUT (since port cannot be congested)
615 *
616 * IMPORTANT: The fact that a read or write operation is indicated does NOT
617 * imply that the operation will succeed, merely that it should be performed
618 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 */
Ying Xue247f0f32014-02-18 16:06:46 +0800620static unsigned int tipc_poll(struct file *file, struct socket *sock,
621 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622{
Allan Stephens9b674e82008-03-26 16:48:21 -0700623 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400624 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000625 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700626
Ying Xuef288bef2012-08-21 11:16:57 +0800627 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700628
Allan Stephensf662c072010-08-17 11:00:06 +0000629 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200630 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500631 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200632 mask |= POLLOUT;
633 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000634 case SS_READY:
635 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400636 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000637 mask |= POLLOUT;
638 /* fall thru' */
639 case SS_CONNECTING:
640 case SS_LISTENING:
641 if (!skb_queue_empty(&sk->sk_receive_queue))
642 mask |= (POLLIN | POLLRDNORM);
643 break;
644 case SS_DISCONNECTING:
645 mask = (POLLIN | POLLRDNORM | POLLHUP);
646 break;
647 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700648
649 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650}
651
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400652/**
653 * tipc_sendmcast - send multicast message
654 * @sock: socket structure
655 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500656 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400657 * @dsz: total length of message data
658 * @timeo: timeout to wait for wakeup
659 *
660 * Called from function tipc_sendmsg(), which has done all sanity checks
661 * Returns the number of bytes sent on success, or errno
662 */
663static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500664 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400665{
666 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500667 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800668 struct net *net = sock_net(sk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500669 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500670 struct sk_buff_head *pktchain = &sk->sk_write_queue;
Al Virof25dcc72014-11-28 15:52:29 -0500671 struct iov_iter save = msg->msg_iter;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400672 uint mtu;
673 int rc;
674
675 msg_set_type(mhdr, TIPC_MCAST_MSG);
676 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
677 msg_set_destport(mhdr, 0);
678 msg_set_destnode(mhdr, 0);
679 msg_set_nametype(mhdr, seq->type);
680 msg_set_namelower(mhdr, seq->lower);
681 msg_set_nameupper(mhdr, seq->upper);
682 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
683
684new_mtu:
685 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500686 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400687 if (unlikely(rc < 0))
688 return rc;
689
690 do {
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500691 rc = tipc_bclink_xmit(net, pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400692 if (likely(rc >= 0)) {
693 rc = dsz;
694 break;
695 }
Al Virof25dcc72014-11-28 15:52:29 -0500696 if (rc == -EMSGSIZE) {
697 msg->msg_iter = save;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400698 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500699 }
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400700 if (rc != -ELINKCONG)
701 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400702 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400703 rc = tipc_wait_for_sndmsg(sock, &timeo);
704 if (rc)
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500705 __skb_queue_purge(pktchain);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400706 } while (!rc);
707 return rc;
708}
709
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500710/**
711 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
712 * @arrvq: queue with arriving messages, to be cloned after destination lookup
713 * @inputq: queue with cloned messages, delivered to socket after dest lookup
714 *
715 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400716 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500717void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
718 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400719{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500720 struct tipc_msg *msg;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500721 struct tipc_plist dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500722 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400723 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500724 struct sk_buff_head tmpq;
725 uint hsz;
726 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500727
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500728 __skb_queue_head_init(&tmpq);
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500729 tipc_plist_init(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400730
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500731 skb = tipc_skb_peek(arrvq, &inputq->lock);
732 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
733 msg = buf_msg(skb);
734 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400735
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500736 if (in_own_node(net, msg_orignode(msg)))
737 scope = TIPC_NODE_SCOPE;
738
739 /* Create destination port list and message clones: */
740 tipc_nametbl_mc_translate(net,
741 msg_nametype(msg), msg_namelower(msg),
742 msg_nameupper(msg), scope, &dports);
743 portid = tipc_plist_pop(&dports);
744 for (; portid; portid = tipc_plist_pop(&dports)) {
745 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
746 if (_skb) {
747 msg_set_destport(buf_msg(_skb), portid);
748 __skb_queue_tail(&tmpq, _skb);
749 continue;
750 }
751 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400752 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500753 /* Append to inputq if not already done by other thread */
754 spin_lock_bh(&inputq->lock);
755 if (skb_peek(arrvq) == skb) {
756 skb_queue_splice_tail_init(&tmpq, inputq);
757 kfree_skb(__skb_dequeue(arrvq));
758 }
759 spin_unlock_bh(&inputq->lock);
760 __skb_queue_purge(&tmpq);
761 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400762 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500763 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400764}
765
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900766/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500767 * tipc_sk_proto_rcv - receive a connection mng protocol message
768 * @tsk: receiving socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500769 * @skb: pointer to message buffer. Set to NULL if buffer is consumed.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500770 */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500771static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff **skb)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500772{
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500773 struct tipc_msg *msg = buf_msg(*skb);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500774 int conn_cong;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500775 u32 dnode;
776 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500777 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400778 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500779 goto exit;
780
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400781 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500782
783 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400784 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500785 tsk->sent_unacked -= msg_msgcnt(msg);
786 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400787 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500788 } else if (msg_type(msg) == CONN_PROBE) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500789 if (tipc_msg_reverse(own_node, *skb, &dnode, TIPC_OK)) {
790 msg_set_type(msg, CONN_PROBE_REPLY);
791 return;
792 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500793 }
794 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
795exit:
Jon Paul Maloy1186adf2015-02-05 08:36:37 -0500796 kfree_skb(*skb);
797 *skb = NULL;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500798}
799
Ying Xue3f405042014-01-17 09:50:05 +0800800static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
801{
802 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400803 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800804 DEFINE_WAIT(wait);
805 int done;
806
807 do {
808 int err = sock_error(sk);
809 if (err)
810 return err;
811 if (sock->state == SS_DISCONNECTING)
812 return -EPIPE;
813 if (!*timeo_p)
814 return -EAGAIN;
815 if (signal_pending(current))
816 return sock_intr_errno(*timeo_p);
817
818 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500819 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800820 finish_wait(sk_sleep(sk), &wait);
821 } while (!done);
822 return 0;
823}
824
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500825/**
Ying Xue247f0f32014-02-18 16:06:46 +0800826 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 * @sock: socket structure
828 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500829 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900830 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900832 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
834 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 * Returns the number of bytes sent on success, or errno otherwise
837 */
Ying Xue1b784142015-03-02 15:37:48 +0800838static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500839 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840{
Ying Xue39a02952015-03-02 15:37:47 +0800841 struct sock *sk = sock->sk;
842 int ret;
843
844 lock_sock(sk);
845 ret = __tipc_sendmsg(sock, m, dsz);
846 release_sock(sk);
847
848 return ret;
849}
850
851static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
852{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500853 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700854 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400855 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuef2f98002015-01-09 15:27:05 +0800856 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400857 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500858 u32 dnode, dport;
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500859 struct sk_buff_head *pktchain = &sk->sk_write_queue;
Ying Xuea6ca1092014-11-26 11:41:55 +0800860 struct sk_buff *skb;
Erik Hugnef2f80362015-03-19 09:02:19 +0100861 struct tipc_name_seq *seq;
Al Virof25dcc72014-11-28 15:52:29 -0500862 struct iov_iter save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500863 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800864 long timeo;
Erik Hugne88b17b62014-12-03 14:44:44 +0100865 int rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500867 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400868 return -EMSGSIZE;
Erik Hugnef2f80362015-03-19 09:02:19 +0100869 if (unlikely(!dest)) {
870 if (tsk->connected && sock->state == SS_READY)
871 dest = &tsk->remote;
872 else
873 return -EDESTADDRREQ;
874 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
875 dest->family != AF_TIPC) {
876 return -EINVAL;
877 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500878 if (unlikely(sock->state != SS_READY)) {
Ying Xue39a02952015-03-02 15:37:47 +0800879 if (sock->state == SS_LISTENING)
880 return -EPIPE;
881 if (sock->state != SS_UNCONNECTED)
882 return -EISCONN;
883 if (tsk->published)
884 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700885 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400886 tsk->conn_type = dest->addr.name.name.type;
887 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700888 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 }
Erik Hugnef2f80362015-03-19 09:02:19 +0100890 seq = &dest->addr.nameseq;
Ying Xue3f405042014-01-17 09:50:05 +0800891 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500892
893 if (dest->addrtype == TIPC_ADDR_MCAST) {
Ying Xue39a02952015-03-02 15:37:47 +0800894 return tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500895 } else if (dest->addrtype == TIPC_ADDR_NAME) {
896 u32 type = dest->addr.name.name.type;
897 u32 inst = dest->addr.name.name.instance;
898 u32 domain = dest->addr.name.domain;
899
900 dnode = domain;
901 msg_set_type(mhdr, TIPC_NAMED_MSG);
902 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
903 msg_set_nametype(mhdr, type);
904 msg_set_nameinst(mhdr, inst);
905 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800906 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500907 msg_set_destnode(mhdr, dnode);
908 msg_set_destport(mhdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800909 if (unlikely(!dport && !dnode))
910 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500911 } else if (dest->addrtype == TIPC_ADDR_ID) {
912 dnode = dest->addr.id.node;
913 msg_set_type(mhdr, TIPC_DIRECT_MSG);
914 msg_set_lookup_scope(mhdr, 0);
915 msg_set_destnode(mhdr, dnode);
916 msg_set_destport(mhdr, dest->addr.id.ref);
917 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
918 }
919
Al Virof25dcc72014-11-28 15:52:29 -0500920 save = m->msg_iter;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500921new_mtu:
Ying Xuef2f98002015-01-09 15:27:05 +0800922 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500923 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500924 if (rc < 0)
Ying Xue39a02952015-03-02 15:37:47 +0800925 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500926
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900927 do {
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500928 skb = skb_peek(pktchain);
Ying Xuea6ca1092014-11-26 11:41:55 +0800929 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500930 rc = tipc_link_xmit(net, pktchain, dnode, tsk->portid);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500931 if (likely(rc >= 0)) {
932 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700933 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500934 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700935 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900936 }
Al Virof25dcc72014-11-28 15:52:29 -0500937 if (rc == -EMSGSIZE) {
938 m->msg_iter = save;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500939 goto new_mtu;
Al Virof25dcc72014-11-28 15:52:29 -0500940 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500941 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700942 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400943 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500944 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400945 if (rc)
Jon Paul Maloy94153e32015-02-05 08:36:40 -0500946 __skb_queue_purge(pktchain);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500947 } while (!rc);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500948
949 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950}
951
Ying Xue391a6dd2014-01-17 09:50:06 +0800952static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
953{
954 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400955 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800956 DEFINE_WAIT(wait);
957 int done;
958
959 do {
960 int err = sock_error(sk);
961 if (err)
962 return err;
963 if (sock->state == SS_DISCONNECTING)
964 return -EPIPE;
965 else if (sock->state != SS_CONNECTED)
966 return -ENOTCONN;
967 if (!*timeo_p)
968 return -EAGAIN;
969 if (signal_pending(current))
970 return sock_intr_errno(*timeo_p);
971
972 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
973 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500974 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400975 !tsk_conn_cong(tsk)) ||
976 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800977 finish_wait(sk_sleep(sk), &wait);
978 } while (!done);
979 return 0;
980}
981
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900982/**
Ying Xue247f0f32014-02-18 16:06:46 +0800983 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 * @sock: socket structure
985 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500986 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900987 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900989 *
990 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700991 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992 */
Ying Xue1b784142015-03-02 15:37:48 +0800993static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700995 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +0800996 int ret;
997
998 lock_sock(sk);
999 ret = __tipc_send_stream(sock, m, dsz);
1000 release_sock(sk);
1001
1002 return ret;
1003}
1004
1005static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1006{
1007 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001008 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001009 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001010 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001011 struct sk_buff_head *pktchain = &sk->sk_write_queue;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001012 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue07f6c4b2015-01-07 13:41:58 +08001013 u32 portid = tsk->portid;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001014 int rc = -EINVAL;
1015 long timeo;
1016 u32 dnode;
1017 uint mtu, send, sent = 0;
Al Virof25dcc72014-11-28 15:52:29 -05001018 struct iov_iter save;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001019
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001020 /* Handle implied connection establishment */
1021 if (unlikely(dest)) {
Ying Xue39a02952015-03-02 15:37:47 +08001022 rc = __tipc_sendmsg(sock, m, dsz);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001023 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001024 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001025 return rc;
1026 }
1027 if (dsz > (uint)INT_MAX)
1028 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001029
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001030 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001031 if (sock->state == SS_DISCONNECTING)
Ying Xue39a02952015-03-02 15:37:47 +08001032 return -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001033 else
Ying Xue39a02952015-03-02 15:37:47 +08001034 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001035 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001037 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001038 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001039
1040next:
Al Virof25dcc72014-11-28 15:52:29 -05001041 save = m->msg_iter;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001042 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001043 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001044 rc = tipc_msg_build(mhdr, m, sent, send, mtu, pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001045 if (unlikely(rc < 0))
Ying Xue39a02952015-03-02 15:37:47 +08001046 return rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001047 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001048 if (likely(!tsk_conn_cong(tsk))) {
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001049 rc = tipc_link_xmit(net, pktchain, dnode, portid);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001050 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001051 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001052 sent += send;
1053 if (sent == dsz)
1054 break;
1055 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001056 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001057 if (rc == -EMSGSIZE) {
Ying Xuef2f98002015-01-09 15:27:05 +08001058 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1059 portid);
Al Virof25dcc72014-11-28 15:52:29 -05001060 m->msg_iter = save;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001061 goto next;
1062 }
1063 if (rc != -ELINKCONG)
1064 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001065 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001067 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001068 if (rc)
Jon Paul Maloy94153e32015-02-05 08:36:40 -05001069 __skb_queue_purge(pktchain);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001070 } while (!rc);
Ying Xue39a02952015-03-02 15:37:47 +08001071
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001072 return sent ? sent : rc;
1073}
1074
1075/**
1076 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001077 * @sock: socket structure
1078 * @m: message to send
1079 * @dsz: length of data to be transmitted
1080 *
1081 * Used for SOCK_SEQPACKET messages.
1082 *
1083 * Returns the number of bytes sent on success, or errno otherwise
1084 */
Ying Xue1b784142015-03-02 15:37:48 +08001085static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001086{
1087 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1088 return -EMSGSIZE;
1089
Ying Xue1b784142015-03-02 15:37:48 +08001090 return tipc_send_stream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091}
1092
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001093/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001095static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001096 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097{
Ying Xue3721e9c2015-01-13 17:07:48 +08001098 struct sock *sk = &tsk->sk;
1099 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001100 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001102 msg_set_destnode(msg, peer_node);
1103 msg_set_destport(msg, peer_port);
1104 msg_set_type(msg, TIPC_CONN_MSG);
1105 msg_set_lookup_scope(msg, 0);
1106 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001107
Ying Xue2f55c432015-01-09 15:27:00 +08001108 tsk->probing_intv = CONN_PROBING_INTERVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001109 tsk->probing_state = TIPC_CONN_OK;
1110 tsk->connected = 1;
Ying Xue3721e9c2015-01-13 17:07:48 +08001111 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Ying Xuef2f98002015-01-09 15:27:05 +08001112 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1113 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114}
1115
1116/**
1117 * set_orig_addr - capture sender's address for received message
1118 * @m: descriptor for message info
1119 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001120 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 * Note: Address is not captured if not requested by receiver.
1122 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001123static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001125 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001127 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128 addr->family = AF_TIPC;
1129 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001130 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131 addr->addr.id.ref = msg_origport(msg);
1132 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001133 addr->addr.name.domain = 0; /* could leave uninitialized */
1134 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 m->msg_namelen = sizeof(struct sockaddr_tipc);
1136 }
1137}
1138
1139/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001140 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 * @m: descriptor for message info
1142 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001143 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001144 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001146 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 * Returns 0 if successful, otherwise errno
1148 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001149static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1150 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151{
1152 u32 anc_data[3];
1153 u32 err;
1154 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001155 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 int res;
1157
1158 if (likely(m->msg_controllen == 0))
1159 return 0;
1160
1161 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 err = msg ? msg_errcode(msg) : 0;
1163 if (unlikely(err)) {
1164 anc_data[0] = err;
1165 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001166 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1167 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001169 if (anc_data[1]) {
1170 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1171 msg_data(msg));
1172 if (res)
1173 return res;
1174 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 }
1176
1177 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1179 switch (dest_type) {
1180 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001181 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 anc_data[0] = msg_nametype(msg);
1183 anc_data[1] = msg_namelower(msg);
1184 anc_data[2] = msg_namelower(msg);
1185 break;
1186 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001187 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 anc_data[0] = msg_nametype(msg);
1189 anc_data[1] = msg_namelower(msg);
1190 anc_data[2] = msg_nameupper(msg);
1191 break;
1192 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001193 has_name = (tsk->conn_type != 0);
1194 anc_data[0] = tsk->conn_type;
1195 anc_data[1] = tsk->conn_instance;
1196 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 break;
1198 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001199 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 }
Allan Stephens2db99832010-12-31 18:59:33 +00001201 if (has_name) {
1202 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1203 if (res)
1204 return res;
1205 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206
1207 return 0;
1208}
1209
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001210static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001211{
Ying Xuef2f98002015-01-09 15:27:05 +08001212 struct net *net = sock_net(&tsk->sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001213 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001214 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001215 u32 peer_port = tsk_peer_port(tsk);
1216 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001217
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001218 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001219 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001220 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1221 dnode, tsk_own_node(tsk), peer_port,
1222 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001223 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001224 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001225 msg = buf_msg(skb);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001226 msg_set_msgcnt(msg, ack);
Ying Xuef2f98002015-01-09 15:27:05 +08001227 tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001228}
1229
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001230static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001231{
1232 struct sock *sk = sock->sk;
1233 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001234 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001235 int err;
1236
1237 for (;;) {
1238 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001239 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001240 if (sock->state == SS_DISCONNECTING) {
1241 err = -ENOTCONN;
1242 break;
1243 }
1244 release_sock(sk);
1245 timeo = schedule_timeout(timeo);
1246 lock_sock(sk);
1247 }
1248 err = 0;
1249 if (!skb_queue_empty(&sk->sk_receive_queue))
1250 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001251 err = -EAGAIN;
1252 if (!timeo)
1253 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001254 err = sock_intr_errno(timeo);
1255 if (signal_pending(current))
1256 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001257 }
1258 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001259 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001260 return err;
1261}
1262
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001263/**
Ying Xue247f0f32014-02-18 16:06:46 +08001264 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265 * @m: descriptor for message info
1266 * @buf_len: total size of user buffer area
1267 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001268 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1270 * If the complete message doesn't fit in user area, truncate it.
1271 *
1272 * Returns size of returned message data, errno otherwise
1273 */
Ying Xue1b784142015-03-02 15:37:48 +08001274static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1275 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001277 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001278 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 struct sk_buff *buf;
1280 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001281 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282 unsigned int sz;
1283 u32 err;
1284 int res;
1285
Allan Stephens0c3141e2008-04-15 00:22:02 -07001286 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 if (unlikely(!buf_len))
1288 return -EINVAL;
1289
Allan Stephens0c3141e2008-04-15 00:22:02 -07001290 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291
Allan Stephens0c3141e2008-04-15 00:22:02 -07001292 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 res = -ENOTCONN;
1294 goto exit;
1295 }
1296
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001297 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001298restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001299
Allan Stephens0c3141e2008-04-15 00:22:02 -07001300 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001301 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001302 if (res)
1303 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001304
1305 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001306 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 msg = buf_msg(buf);
1308 sz = msg_data_sz(msg);
1309 err = msg_errcode(msg);
1310
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001313 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 goto restart;
1315 }
1316
1317 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 set_orig_addr(m, msg);
1319
1320 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001321 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001322 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 goto exit;
1324
1325 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 if (!err) {
1327 if (unlikely(buf_len < sz)) {
1328 sz = buf_len;
1329 m->msg_flags |= MSG_TRUNC;
1330 }
David S. Miller51f3d022014-11-05 16:46:40 -05001331 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001332 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 res = sz;
1335 } else {
1336 if ((sock->state == SS_READY) ||
1337 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1338 res = 0;
1339 else
1340 res = -ECONNRESET;
1341 }
1342
1343 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001345 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001346 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001347 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001348 tsk->rcv_unacked = 0;
1349 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001350 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001351 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001353 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 return res;
1355}
1356
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001357/**
Ying Xue247f0f32014-02-18 16:06:46 +08001358 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 * @m: descriptor for message info
1360 * @buf_len: total size of user buffer area
1361 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001362 *
1363 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 * will optionally wait for more; never truncates data.
1365 *
1366 * Returns size of returned message data, errno otherwise
1367 */
Ying Xue1b784142015-03-02 15:37:48 +08001368static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1369 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001371 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001372 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373 struct sk_buff *buf;
1374 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001375 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001377 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001380 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381
1382 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 if (unlikely(!buf_len))
1384 return -EINVAL;
1385
Allan Stephens0c3141e2008-04-15 00:22:02 -07001386 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001388 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389 res = -ENOTCONN;
1390 goto exit;
1391 }
1392
Florian Westphal3720d402010-08-17 11:00:04 +00001393 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001394 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001395
Allan Stephens0c3141e2008-04-15 00:22:02 -07001396restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001397 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001398 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001399 if (res)
1400 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001401
1402 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 msg = buf_msg(buf);
1405 sz = msg_data_sz(msg);
1406 err = msg_errcode(msg);
1407
1408 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001410 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001411 goto restart;
1412 }
1413
1414 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415 if (sz_copied == 0) {
1416 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001417 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001418 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419 goto exit;
1420 }
1421
1422 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001424 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425
Allan Stephens0232fd02011-02-21 09:45:40 -05001426 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427 needed = (buf_len - sz_copied);
1428 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001429
David S. Miller51f3d022014-11-05 16:46:40 -05001430 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1431 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001432 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001434
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 sz_copied += sz_to_copy;
1436
1437 if (sz_to_copy < sz) {
1438 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001439 TIPC_SKB_CB(buf)->handle =
1440 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441 goto exit;
1442 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001443 } else {
1444 if (sz_copied != 0)
1445 goto exit; /* can't add error msg to valid data */
1446
1447 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1448 res = 0;
1449 else
1450 res = -ECONNRESET;
1451 }
1452
1453 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001455 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001456 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001457 tsk->rcv_unacked = 0;
1458 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001459 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001460 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461
1462 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001463 if ((sz_copied < buf_len) && /* didn't get all requested data */
1464 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001465 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001466 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1467 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 goto restart;
1469
1470exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001471 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001472 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473}
1474
1475/**
Ying Xuef288bef2012-08-21 11:16:57 +08001476 * tipc_write_space - wake up thread if port congestion is released
1477 * @sk: socket
1478 */
1479static void tipc_write_space(struct sock *sk)
1480{
1481 struct socket_wq *wq;
1482
1483 rcu_read_lock();
1484 wq = rcu_dereference(sk->sk_wq);
1485 if (wq_has_sleeper(wq))
1486 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1487 POLLWRNORM | POLLWRBAND);
1488 rcu_read_unlock();
1489}
1490
1491/**
1492 * tipc_data_ready - wake up threads to indicate messages have been received
1493 * @sk: socket
1494 * @len: the length of messages
1495 */
David S. Miller676d2362014-04-11 16:15:36 -04001496static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001497{
1498 struct socket_wq *wq;
1499
1500 rcu_read_lock();
1501 wq = rcu_dereference(sk->sk_wq);
1502 if (wq_has_sleeper(wq))
1503 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1504 POLLRDNORM | POLLRDBAND);
1505 rcu_read_unlock();
1506}
1507
1508/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001509 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001510 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001511 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001512 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001513 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001514 */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001515static int filter_connect(struct tipc_sock *tsk, struct sk_buff **skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001516{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001517 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001518 struct net *net = sock_net(sk);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001519 struct socket *sock = sk->sk_socket;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001520 struct tipc_msg *msg = buf_msg(*skb);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001521 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001522
1523 if (msg_mcast(msg))
1524 return retval;
1525
1526 switch ((int)sock->state) {
1527 case SS_CONNECTED:
1528 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001529 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001530 if (unlikely(msg_errcode(msg))) {
1531 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001532 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001533 /* let timer expire on it's own */
Ying Xuef2f98002015-01-09 15:27:05 +08001534 tipc_node_remove_conn(net, tsk_peer_node(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08001535 tsk->portid);
Ying Xue7e6c1312012-11-29 18:39:14 -05001536 }
1537 retval = TIPC_OK;
1538 }
1539 break;
1540 case SS_CONNECTING:
1541 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001542
1543 if (unlikely(!msg_connected(msg)))
1544 break;
1545
Ying Xue584d24b2012-11-29 18:51:19 -05001546 if (unlikely(msg_errcode(msg))) {
1547 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001548 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001549 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001550 break;
1551 }
1552
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001553 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001554 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001555 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001556 retval = TIPC_OK;
1557 break;
1558 }
1559
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001560 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1561 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001562 sock->state = SS_CONNECTED;
1563
Ying Xue584d24b2012-11-29 18:51:19 -05001564 /* If an incoming message is an 'ACK-', it should be
1565 * discarded here because it doesn't contain useful
1566 * data. In addition, we should try to wake up
1567 * connect() routine if sleeping.
1568 */
1569 if (msg_data_sz(msg) == 0) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001570 kfree_skb(*skb);
1571 *skb = NULL;
Ying Xue584d24b2012-11-29 18:51:19 -05001572 if (waitqueue_active(sk_sleep(sk)))
1573 wake_up_interruptible(sk_sleep(sk));
1574 }
1575 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001576 break;
1577 case SS_LISTENING:
1578 case SS_UNCONNECTED:
1579 /* Accept only SYN message */
1580 if (!msg_connected(msg) && !(msg_errcode(msg)))
1581 retval = TIPC_OK;
1582 break;
1583 case SS_DISCONNECTING:
1584 break;
1585 default:
1586 pr_err("Unknown socket state %u\n", sock->state);
1587 }
1588 return retval;
1589}
1590
1591/**
Ying Xueaba79f32013-01-20 23:30:09 +01001592 * rcvbuf_limit - get proper overload limit of socket receive queue
1593 * @sk: socket
1594 * @buf: message
1595 *
1596 * For all connection oriented messages, irrespective of importance,
1597 * the default overload value (i.e. 67MB) is set as limit.
1598 *
1599 * For all connectionless messages, by default new queue limits are
1600 * as belows:
1601 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001602 * TIPC_LOW_IMPORTANCE (4 MB)
1603 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1604 * TIPC_HIGH_IMPORTANCE (16 MB)
1605 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001606 *
1607 * Returns overload limit according to corresponding message importance
1608 */
1609static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1610{
1611 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001612
1613 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001614 return sysctl_tipc_rmem[2];
1615
1616 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1617 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001618}
1619
1620/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001621 * filter_rcv - validate incoming message
1622 * @sk: socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001623 * @skb: pointer to message. Set to NULL if buffer is consumed.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001624 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001625 * Enqueues message on receive queue if acceptable; optionally handles
1626 * disconnect indication for a connected socket.
1627 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001628 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001629 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001630 * Returns 0 (TIPC_OK) if message was ok, -TIPC error code if rejected
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631 */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001632static int filter_rcv(struct sock *sk, struct sk_buff **skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001634 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001635 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001636 struct tipc_msg *msg = buf_msg(*skb);
1637 unsigned int limit = rcvbuf_limit(sk, *skb);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001638 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001640 if (unlikely(msg_user(msg) == CONN_MANAGER)) {
1641 tipc_sk_proto_rcv(tsk, skb);
1642 return TIPC_OK;
1643 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001644
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001645 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001646 kfree_skb(*skb);
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001647 tsk->link_cong = 0;
1648 sk->sk_write_space(sk);
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001649 *skb = NULL;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001650 return TIPC_OK;
1651 }
1652
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001654 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001655 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001656
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001658 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001659 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660 } else {
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001661 rc = filter_connect(tsk, skb);
1662 if (rc != TIPC_OK || !*skb)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001663 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001664 }
1665
1666 /* Reject message if there isn't room to queue it */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001667 if (sk_rmem_alloc_get(sk) + (*skb)->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001668 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669
Ying Xueaba79f32013-01-20 23:30:09 +01001670 /* Enqueue message */
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001671 TIPC_SKB_CB(*skb)->handle = NULL;
1672 __skb_queue_tail(&sk->sk_receive_queue, *skb);
1673 skb_set_owner_r(*skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001674
David S. Miller676d2362014-04-11 16:15:36 -04001675 sk->sk_data_ready(sk);
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001676 *skb = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 return TIPC_OK;
1678}
1679
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001680/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001681 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001682 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001683 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001684 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001685 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001686 *
1687 * Returns 0
1688 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001689static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001690{
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001691 int err;
1692 atomic_t *dcnt;
1693 u32 dnode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001694 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +08001695 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001696 uint truesize = skb->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001697
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001698 err = filter_rcv(sk, &skb);
1699 if (likely(!skb)) {
1700 dcnt = &tsk->dupl_rcvcnt;
1701 if (atomic_read(dcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1702 atomic_add(truesize, dcnt);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001703 return 0;
1704 }
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001705 if (!err || tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode, -err))
1706 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001707 return 0;
1708}
1709
1710/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001711 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1712 * inputq and try adding them to socket or backlog queue
1713 * @inputq: list of incoming buffers with potentially different destinations
1714 * @sk: socket where the buffers should be enqueued
1715 * @dport: port number for the socket
1716 * @_skb: returned buffer to be forwarded or rejected, if applicable
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001717 *
1718 * Caller must hold socket lock
1719 *
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001720 * Returns TIPC_OK if all buffers enqueued, otherwise -TIPC_ERR_OVERLOAD
1721 * or -TIPC_ERR_NO_PORT
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001722 */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001723static int tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1724 u32 dport, struct sk_buff **_skb)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001725{
1726 unsigned int lim;
1727 atomic_t *dcnt;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001728 int err;
1729 struct sk_buff *skb;
1730 unsigned long time_limit = jiffies + 2;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001731
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001732 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001733 if (unlikely(time_after_eq(jiffies, time_limit)))
1734 return TIPC_OK;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001735 skb = tipc_skb_dequeue(inputq, dport);
1736 if (unlikely(!skb))
1737 return TIPC_OK;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001738 if (!sock_owned_by_user(sk)) {
1739 err = filter_rcv(sk, &skb);
1740 if (likely(!skb))
1741 continue;
1742 *_skb = skb;
1743 return err;
1744 }
1745 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1746 if (sk->sk_backlog.len)
1747 atomic_set(dcnt, 0);
1748 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1749 if (likely(!sk_add_backlog(sk, skb, lim)))
1750 continue;
1751 *_skb = skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001752 return -TIPC_ERR_OVERLOAD;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001753 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001754 return TIPC_OK;
1755}
1756
1757/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001758 * tipc_sk_rcv - handle a chain of incoming buffers
1759 * @inputq: buffer list containing the buffers
1760 * Consumes all buffers in list until inputq is empty
1761 * Note: may be called in multiple threads referring to the same queue
1762 * Returns 0 if last buffer was accepted, otherwise -EHOSTUNREACH
1763 * Only node local calls check the return value, sending single-buffer queues
Allan Stephens0c3141e2008-04-15 00:22:02 -07001764 */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001765int tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001766{
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001767 u32 dnode, dport = 0;
1768 int err = -TIPC_ERR_NO_PORT;
1769 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001770 struct tipc_sock *tsk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001771 struct tipc_net *tn;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001772 struct sock *sk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001773
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001774 while (skb_queue_len(inputq)) {
1775 skb = NULL;
1776 dport = tipc_skb_peek_port(inputq, dport);
1777 tsk = tipc_sk_lookup(net, dport);
1778 if (likely(tsk)) {
1779 sk = &tsk->sk;
1780 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1781 err = tipc_sk_enqueue(inputq, sk, dport, &skb);
1782 spin_unlock_bh(&sk->sk_lock.slock);
1783 dport = 0;
1784 }
1785 sock_put(sk);
1786 } else {
1787 skb = tipc_skb_dequeue(inputq, dport);
1788 }
1789 if (likely(!skb))
1790 continue;
1791 if (tipc_msg_lookup_dest(net, skb, &dnode, &err))
1792 goto xmit;
1793 if (!err) {
1794 dnode = msg_destnode(buf_msg(skb));
1795 goto xmit;
1796 }
1797 tn = net_generic(net, tipc_net_id);
1798 if (!tipc_msg_reverse(tn->own_addr, skb, &dnode, -err))
1799 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001800xmit:
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001801 tipc_link_xmit_skb(net, skb, dnode, dport);
1802 }
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001803 return err ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001804}
1805
Ying Xue78eb3a52014-01-17 09:50:03 +08001806static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1807{
1808 struct sock *sk = sock->sk;
1809 DEFINE_WAIT(wait);
1810 int done;
1811
1812 do {
1813 int err = sock_error(sk);
1814 if (err)
1815 return err;
1816 if (!*timeo_p)
1817 return -ETIMEDOUT;
1818 if (signal_pending(current))
1819 return sock_intr_errno(*timeo_p);
1820
1821 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1822 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1823 finish_wait(sk_sleep(sk), &wait);
1824 } while (!done);
1825 return 0;
1826}
1827
Per Lidenb97bf3f2006-01-02 19:04:38 +01001828/**
Ying Xue247f0f32014-02-18 16:06:46 +08001829 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 * @sock: socket structure
1831 * @dest: socket address for destination port
1832 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001833 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 *
1835 * Returns 0 on success, errno otherwise
1836 */
Ying Xue247f0f32014-02-18 16:06:46 +08001837static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1838 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001840 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001841 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001842 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1843 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001844 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Ying Xue78eb3a52014-01-17 09:50:03 +08001845 socket_state previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001846 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847
Allan Stephens0c3141e2008-04-15 00:22:02 -07001848 lock_sock(sk);
1849
Erik Hugnef2f80362015-03-19 09:02:19 +01001850 /* DGRAM/RDM connect(), just save the destaddr */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001851 if (sock->state == SS_READY) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001852 if (dst->family == AF_UNSPEC) {
1853 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1854 tsk->connected = 0;
1855 } else {
1856 memcpy(&tsk->remote, dest, destlen);
1857 tsk->connected = 1;
1858 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001859 goto exit;
1860 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861
Allan Stephensb89741a2008-04-15 00:20:37 -07001862 /*
1863 * Reject connection attempt using multicast address
1864 *
1865 * Note: send_msg() validates the rest of the address fields,
1866 * so there's no need to do it here
1867 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001868 if (dst->addrtype == TIPC_ADDR_MCAST) {
1869 res = -EINVAL;
1870 goto exit;
1871 }
1872
Ying Xue78eb3a52014-01-17 09:50:03 +08001873 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001874 switch (sock->state) {
1875 case SS_UNCONNECTED:
1876 /* Send a 'SYN-' to destination */
1877 m.msg_name = dest;
1878 m.msg_namelen = destlen;
1879
1880 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1881 * indicate send_msg() is never blocked.
1882 */
1883 if (!timeout)
1884 m.msg_flags = MSG_DONTWAIT;
1885
Ying Xue39a02952015-03-02 15:37:47 +08001886 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001887 if ((res < 0) && (res != -EWOULDBLOCK))
1888 goto exit;
1889
1890 /* Just entered SS_CONNECTING state; the only
1891 * difference is that return value in non-blocking
1892 * case is EINPROGRESS, rather than EALREADY.
1893 */
1894 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001895 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001896 if (previous == SS_CONNECTING)
1897 res = -EALREADY;
1898 if (!timeout)
1899 goto exit;
1900 timeout = msecs_to_jiffies(timeout);
1901 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1902 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001903 break;
1904 case SS_CONNECTED:
1905 res = -EISCONN;
1906 break;
1907 default:
1908 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001909 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001910 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001911exit:
1912 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001913 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914}
1915
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001916/**
Ying Xue247f0f32014-02-18 16:06:46 +08001917 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918 * @sock: socket structure
1919 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001920 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921 * Returns 0 on success, errno otherwise
1922 */
Ying Xue247f0f32014-02-18 16:06:46 +08001923static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001925 struct sock *sk = sock->sk;
1926 int res;
1927
1928 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929
Ying Xue245f3d32011-07-06 06:01:13 -04001930 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001931 res = -EINVAL;
1932 else {
1933 sock->state = SS_LISTENING;
1934 res = 0;
1935 }
1936
1937 release_sock(sk);
1938 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939}
1940
Ying Xue6398e232014-01-17 09:50:04 +08001941static int tipc_wait_for_accept(struct socket *sock, long timeo)
1942{
1943 struct sock *sk = sock->sk;
1944 DEFINE_WAIT(wait);
1945 int err;
1946
1947 /* True wake-one mechanism for incoming connections: only
1948 * one process gets woken up, not the 'whole herd'.
1949 * Since we do not 'race & poll' for established sockets
1950 * anymore, the common case will execute the loop only once.
1951 */
1952 for (;;) {
1953 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1954 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001955 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001956 release_sock(sk);
1957 timeo = schedule_timeout(timeo);
1958 lock_sock(sk);
1959 }
1960 err = 0;
1961 if (!skb_queue_empty(&sk->sk_receive_queue))
1962 break;
1963 err = -EINVAL;
1964 if (sock->state != SS_LISTENING)
1965 break;
Ying Xue6398e232014-01-17 09:50:04 +08001966 err = -EAGAIN;
1967 if (!timeo)
1968 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001969 err = sock_intr_errno(timeo);
1970 if (signal_pending(current))
1971 break;
Ying Xue6398e232014-01-17 09:50:04 +08001972 }
1973 finish_wait(sk_sleep(sk), &wait);
1974 return err;
1975}
1976
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001977/**
Ying Xue247f0f32014-02-18 16:06:46 +08001978 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979 * @sock: listening socket
1980 * @newsock: new socket that is to be connected
1981 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001982 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983 * Returns 0 on success, errno otherwise
1984 */
Ying Xue247f0f32014-02-18 16:06:46 +08001985static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001986{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001987 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001989 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001990 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001991 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001992 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001993
Allan Stephens0c3141e2008-04-15 00:22:02 -07001994 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001995
Allan Stephens0c3141e2008-04-15 00:22:02 -07001996 if (sock->state != SS_LISTENING) {
1997 res = -EINVAL;
1998 goto exit;
1999 }
Ying Xue6398e232014-01-17 09:50:04 +08002000 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2001 res = tipc_wait_for_accept(sock, timeo);
2002 if (res)
2003 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002004
2005 buf = skb_peek(&sk->sk_receive_queue);
2006
Ying Xuec5fa7b32013-06-17 10:54:39 -04002007 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002008 if (res)
2009 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002010
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002011 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002012 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002013 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002014
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002015 /* we lock on new_sk; but lockdep sees the lock on sk */
2016 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002017
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002018 /*
2019 * Reject any stray messages received by new socket
2020 * before the socket lock was taken (very, very unlikely)
2021 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002022 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002023
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002024 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002025 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002026 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002028 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002029 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002030 new_tsock->conn_type = msg_nametype(msg);
2031 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002032 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002033
2034 /*
2035 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2036 * Respond to 'SYN+' by queuing it on new socket.
2037 */
2038 if (!msg_data_sz(msg)) {
2039 struct msghdr m = {NULL,};
2040
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002041 tsk_advance_rx_queue(sk);
Ying Xue39a02952015-03-02 15:37:47 +08002042 __tipc_send_stream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002043 } else {
2044 __skb_dequeue(&sk->sk_receive_queue);
2045 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002046 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002047 }
2048 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002049exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002050 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051 return res;
2052}
2053
2054/**
Ying Xue247f0f32014-02-18 16:06:46 +08002055 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002056 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002057 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058 *
2059 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002060 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002061 * Returns 0 on success, errno otherwise
2062 */
Ying Xue247f0f32014-02-18 16:06:46 +08002063static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002064{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002065 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002066 struct net *net = sock_net(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002067 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002068 struct sk_buff *skb;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002069 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002070 int res;
2071
Allan Stephense247a8f2008-03-06 15:05:38 -08002072 if (how != SHUT_RDWR)
2073 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002074
Allan Stephens0c3141e2008-04-15 00:22:02 -07002075 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076
2077 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002078 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002079 case SS_CONNECTED:
2080
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002082 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Ying Xuea6ca1092014-11-26 11:41:55 +08002083 skb = __skb_dequeue(&sk->sk_receive_queue);
2084 if (skb) {
2085 if (TIPC_SKB_CB(skb)->handle != NULL) {
2086 kfree_skb(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002087 goto restart;
2088 }
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002089 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
Ying Xue34747532015-01-09 15:27:10 +08002090 TIPC_CONN_SHUTDOWN))
Ying Xuef2f98002015-01-09 15:27:05 +08002091 tipc_link_xmit_skb(net, skb, dnode,
2092 tsk->portid);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002093 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002094 dnode = tsk_peer_node(tsk);
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002095
2096 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002097 TIPC_CONN_MSG, SHORT_H_SIZE,
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002098 0, dnode, tsk_own_node(tsk),
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002099 tsk_peer_port(tsk),
Ying Xue07f6c4b2015-01-07 13:41:58 +08002100 tsk->portid, TIPC_CONN_SHUTDOWN);
Ying Xuef2f98002015-01-09 15:27:05 +08002101 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002102 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002103 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002104 sock->state = SS_DISCONNECTING;
Ying Xuef2f98002015-01-09 15:27:05 +08002105 tipc_node_remove_conn(net, dnode, tsk->portid);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 /* fall through */
2107
2108 case SS_DISCONNECTING:
2109
Ying Xue75031152012-10-29 09:38:15 -04002110 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002111 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002112
2113 /* Wake up anyone sleeping in poll */
2114 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 res = 0;
2116 break;
2117
2118 default:
2119 res = -ENOTCONN;
2120 }
2121
Allan Stephens0c3141e2008-04-15 00:22:02 -07002122 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123 return res;
2124}
2125
Ying Xuef2f2a962015-01-09 15:27:02 +08002126static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002127{
Ying Xuef2f2a962015-01-09 15:27:02 +08002128 struct tipc_sock *tsk = (struct tipc_sock *)data;
2129 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002130 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002131 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002132 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002133
Jon Paul Maloy57289012014-08-22 18:09:09 -04002134 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002135 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002136 bh_unlock_sock(sk);
2137 goto exit;
2138 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002139 peer_port = tsk_peer_port(tsk);
2140 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002141
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002142 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002143 /* Previous probe not answered -> self abort */
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002144 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
Ying Xue34747532015-01-09 15:27:10 +08002145 TIPC_CONN_MSG, SHORT_H_SIZE, 0,
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002146 own_node, peer_node, tsk->portid,
Ying Xue34747532015-01-09 15:27:10 +08002147 peer_port, TIPC_ERR_NO_PORT);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002148 } else {
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002149 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2150 INT_H_SIZE, 0, peer_node, own_node,
Ying Xuef2f2a962015-01-09 15:27:02 +08002151 peer_port, tsk->portid, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002152 tsk->probing_state = TIPC_CONN_PROBING;
Ying Xue3721e9c2015-01-13 17:07:48 +08002153 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002154 }
2155 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002156 if (skb)
Ying Xuef2f98002015-01-09 15:27:05 +08002157 tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002158exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002159 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002160}
2161
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002162static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002163 struct tipc_name_seq const *seq)
2164{
Ying Xuef2f98002015-01-09 15:27:05 +08002165 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002166 struct publication *publ;
2167 u32 key;
2168
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002169 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002170 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002171 key = tsk->portid + tsk->pub_count + 1;
2172 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002173 return -EADDRINUSE;
2174
Ying Xuef2f98002015-01-09 15:27:05 +08002175 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002176 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002177 if (unlikely(!publ))
2178 return -EINVAL;
2179
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002180 list_add(&publ->pport_list, &tsk->publications);
2181 tsk->pub_count++;
2182 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002183 return 0;
2184}
2185
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002186static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002187 struct tipc_name_seq const *seq)
2188{
Ying Xuef2f98002015-01-09 15:27:05 +08002189 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002190 struct publication *publ;
2191 struct publication *safe;
2192 int rc = -EINVAL;
2193
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002194 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002195 if (seq) {
2196 if (publ->scope != scope)
2197 continue;
2198 if (publ->type != seq->type)
2199 continue;
2200 if (publ->lower != seq->lower)
2201 continue;
2202 if (publ->upper != seq->upper)
2203 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002204 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002205 publ->ref, publ->key);
2206 rc = 0;
2207 break;
2208 }
Ying Xuef2f98002015-01-09 15:27:05 +08002209 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002210 publ->ref, publ->key);
2211 rc = 0;
2212 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002213 if (list_empty(&tsk->publications))
2214 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002215 return rc;
2216}
2217
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002218/* tipc_sk_reinit: set non-zero address in all existing sockets
2219 * when we go from standalone to network mode.
2220 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002221void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002222{
Ying Xuee05b31f2015-01-09 15:27:08 +08002223 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002224 const struct bucket_table *tbl;
2225 struct rhash_head *pos;
2226 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002227 struct tipc_msg *msg;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002228 int i;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002229
Ying Xue07f6c4b2015-01-07 13:41:58 +08002230 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002231 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002232 for (i = 0; i < tbl->size; i++) {
2233 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2234 spin_lock_bh(&tsk->sk.sk_lock.slock);
2235 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002236 msg_set_prevnode(msg, tn->own_addr);
2237 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002238 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2239 }
2240 }
2241 rcu_read_unlock();
2242}
2243
Ying Xuee05b31f2015-01-09 15:27:08 +08002244static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002245{
Ying Xuee05b31f2015-01-09 15:27:08 +08002246 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002247 struct tipc_sock *tsk;
2248
2249 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002250 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002251 if (tsk)
2252 sock_hold(&tsk->sk);
2253 rcu_read_unlock();
2254
2255 return tsk;
2256}
2257
2258static int tipc_sk_insert(struct tipc_sock *tsk)
2259{
Ying Xuee05b31f2015-01-09 15:27:08 +08002260 struct sock *sk = &tsk->sk;
2261 struct net *net = sock_net(sk);
2262 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002263 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2264 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2265
2266 while (remaining--) {
2267 portid++;
2268 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2269 portid = TIPC_MIN_PORT;
2270 tsk->portid = portid;
2271 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002272 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2273 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002274 return 0;
2275 sock_put(&tsk->sk);
2276 }
2277
2278 return -1;
2279}
2280
2281static void tipc_sk_remove(struct tipc_sock *tsk)
2282{
2283 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002284 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002285
Herbert Xu6cca72892015-03-20 21:57:05 +11002286 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002287 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2288 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002289 }
2290}
2291
Herbert Xu6cca72892015-03-20 21:57:05 +11002292static const struct rhashtable_params tsk_rht_params = {
2293 .nelem_hint = 192,
2294 .head_offset = offsetof(struct tipc_sock, node),
2295 .key_offset = offsetof(struct tipc_sock, portid),
2296 .key_len = sizeof(u32), /* portid */
2297 .hashfn = jhash,
2298 .max_size = 1048576,
2299 .min_size = 256,
2300};
2301
Ying Xuee05b31f2015-01-09 15:27:08 +08002302int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002303{
Ying Xuee05b31f2015-01-09 15:27:08 +08002304 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002305
Herbert Xu6cca72892015-03-20 21:57:05 +11002306 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002307}
2308
Ying Xuee05b31f2015-01-09 15:27:08 +08002309void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002310{
Ying Xuee05b31f2015-01-09 15:27:08 +08002311 struct tipc_net *tn = net_generic(net, tipc_net_id);
2312
Ying Xue07f6c4b2015-01-07 13:41:58 +08002313 /* Wait for socket readers to complete */
2314 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002315
Ying Xuee05b31f2015-01-09 15:27:08 +08002316 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002317}
2318
2319/**
Ying Xue247f0f32014-02-18 16:06:46 +08002320 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002321 * @sock: socket structure
2322 * @lvl: option level
2323 * @opt: option identifier
2324 * @ov: pointer to new option value
2325 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002326 *
2327 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002328 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002329 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002330 * Returns 0 on success, errno otherwise
2331 */
Ying Xue247f0f32014-02-18 16:06:46 +08002332static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2333 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002334{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002335 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002336 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337 u32 value;
2338 int res;
2339
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002340 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2341 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002342 if (lvl != SOL_TIPC)
2343 return -ENOPROTOOPT;
2344 if (ol < sizeof(value))
2345 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002346 res = get_user(value, (u32 __user *)ov);
2347 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002348 return res;
2349
Allan Stephens0c3141e2008-04-15 00:22:02 -07002350 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002351
Per Lidenb97bf3f2006-01-02 19:04:38 +01002352 switch (opt) {
2353 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002354 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002355 break;
2356 case TIPC_SRC_DROPPABLE:
2357 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002358 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002359 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002360 res = -ENOPROTOOPT;
2361 break;
2362 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002363 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002364 break;
2365 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002366 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002367 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002368 break;
2369 default:
2370 res = -EINVAL;
2371 }
2372
Allan Stephens0c3141e2008-04-15 00:22:02 -07002373 release_sock(sk);
2374
Per Lidenb97bf3f2006-01-02 19:04:38 +01002375 return res;
2376}
2377
2378/**
Ying Xue247f0f32014-02-18 16:06:46 +08002379 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002380 * @sock: socket structure
2381 * @lvl: option level
2382 * @opt: option identifier
2383 * @ov: receptacle for option value
2384 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002385 *
2386 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002388 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002389 * Returns 0 on success, errno otherwise
2390 */
Ying Xue247f0f32014-02-18 16:06:46 +08002391static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2392 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002393{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002394 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002395 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002396 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002397 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002398 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002399
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002400 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2401 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002402 if (lvl != SOL_TIPC)
2403 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002404 res = get_user(len, ol);
2405 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002406 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002407
Allan Stephens0c3141e2008-04-15 00:22:02 -07002408 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409
2410 switch (opt) {
2411 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002412 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002413 break;
2414 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002415 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002416 break;
2417 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002418 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002419 break;
2420 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002421 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002422 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002423 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002424 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002425 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002426 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002427 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002428 value = skb_queue_len(&sk->sk_receive_queue);
2429 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002430 default:
2431 res = -EINVAL;
2432 }
2433
Allan Stephens0c3141e2008-04-15 00:22:02 -07002434 release_sock(sk);
2435
Paul Gortmaker25860c32010-12-31 18:59:31 +00002436 if (res)
2437 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002438
Paul Gortmaker25860c32010-12-31 18:59:31 +00002439 if (len < sizeof(value))
2440 return -EINVAL;
2441
2442 if (copy_to_user(ov, &value, sizeof(value)))
2443 return -EFAULT;
2444
2445 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002446}
2447
Ying Xuef2f98002015-01-09 15:27:05 +08002448static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002449{
Ying Xuef2f98002015-01-09 15:27:05 +08002450 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002451 struct tipc_sioc_ln_req lnr;
2452 void __user *argp = (void __user *)arg;
2453
2454 switch (cmd) {
2455 case SIOCGETLINKNAME:
2456 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2457 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002458 if (!tipc_node_get_linkname(sock_net(sk),
2459 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002460 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2461 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2462 return -EFAULT;
2463 return 0;
2464 }
2465 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002466 default:
2467 return -ENOIOCTLCMD;
2468 }
2469}
2470
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002471/* Protocol switches for the various types of TIPC sockets */
2472
Florian Westphalbca65ea2008-02-07 18:18:01 -08002473static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002474 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002475 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002476 .release = tipc_release,
2477 .bind = tipc_bind,
2478 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002479 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002480 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002481 .getname = tipc_getname,
2482 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002483 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002484 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002485 .shutdown = tipc_shutdown,
2486 .setsockopt = tipc_setsockopt,
2487 .getsockopt = tipc_getsockopt,
2488 .sendmsg = tipc_sendmsg,
2489 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002490 .mmap = sock_no_mmap,
2491 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002492};
2493
Florian Westphalbca65ea2008-02-07 18:18:01 -08002494static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002495 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002496 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002497 .release = tipc_release,
2498 .bind = tipc_bind,
2499 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002500 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002501 .accept = tipc_accept,
2502 .getname = tipc_getname,
2503 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002504 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002505 .listen = tipc_listen,
2506 .shutdown = tipc_shutdown,
2507 .setsockopt = tipc_setsockopt,
2508 .getsockopt = tipc_getsockopt,
2509 .sendmsg = tipc_send_packet,
2510 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002511 .mmap = sock_no_mmap,
2512 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002513};
2514
Florian Westphalbca65ea2008-02-07 18:18:01 -08002515static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002516 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002517 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002518 .release = tipc_release,
2519 .bind = tipc_bind,
2520 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002521 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002522 .accept = tipc_accept,
2523 .getname = tipc_getname,
2524 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002525 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002526 .listen = tipc_listen,
2527 .shutdown = tipc_shutdown,
2528 .setsockopt = tipc_setsockopt,
2529 .getsockopt = tipc_getsockopt,
2530 .sendmsg = tipc_send_stream,
2531 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002532 .mmap = sock_no_mmap,
2533 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002534};
2535
Florian Westphalbca65ea2008-02-07 18:18:01 -08002536static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002537 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002538 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002539 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002540};
2541
2542static struct proto tipc_proto = {
2543 .name = "TIPC",
2544 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002545 .obj_size = sizeof(struct tipc_sock),
2546 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002547};
2548
2549/**
Per Liden4323add2006-01-18 00:38:21 +01002550 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002551 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002552 * Returns 0 on success, errno otherwise
2553 */
Per Liden4323add2006-01-18 00:38:21 +01002554int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002555{
2556 int res;
2557
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002558 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002559 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002560 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002561 goto out;
2562 }
2563
2564 res = sock_register(&tipc_family_ops);
2565 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002566 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002567 proto_unregister(&tipc_proto);
2568 goto out;
2569 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002570 out:
2571 return res;
2572}
2573
2574/**
Per Liden4323add2006-01-18 00:38:21 +01002575 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002576 */
Per Liden4323add2006-01-18 00:38:21 +01002577void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002578{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002579 sock_unregister(tipc_family_ops.family);
2580 proto_unregister(&tipc_proto);
2581}
Richard Alpe34b78a122014-11-20 10:29:10 +01002582
2583/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002584static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002585{
2586 u32 peer_node;
2587 u32 peer_port;
2588 struct nlattr *nest;
2589
2590 peer_node = tsk_peer_node(tsk);
2591 peer_port = tsk_peer_port(tsk);
2592
2593 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2594
2595 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2596 goto msg_full;
2597 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2598 goto msg_full;
2599
2600 if (tsk->conn_type != 0) {
2601 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2602 goto msg_full;
2603 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2604 goto msg_full;
2605 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2606 goto msg_full;
2607 }
2608 nla_nest_end(skb, nest);
2609
2610 return 0;
2611
2612msg_full:
2613 nla_nest_cancel(skb, nest);
2614
2615 return -EMSGSIZE;
2616}
2617
2618/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002619static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2620 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002621{
2622 int err;
2623 void *hdr;
2624 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002625 struct net *net = sock_net(skb->sk);
2626 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alpe34b78a122014-11-20 10:29:10 +01002627
2628 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002629 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002630 if (!hdr)
2631 goto msg_cancel;
2632
2633 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2634 if (!attrs)
2635 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002636 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002637 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002638 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002639 goto attr_msg_cancel;
2640
2641 if (tsk->connected) {
2642 err = __tipc_nl_add_sk_con(skb, tsk);
2643 if (err)
2644 goto attr_msg_cancel;
2645 } else if (!list_empty(&tsk->publications)) {
2646 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2647 goto attr_msg_cancel;
2648 }
2649 nla_nest_end(skb, attrs);
2650 genlmsg_end(skb, hdr);
2651
2652 return 0;
2653
2654attr_msg_cancel:
2655 nla_nest_cancel(skb, attrs);
2656genlmsg_cancel:
2657 genlmsg_cancel(skb, hdr);
2658msg_cancel:
2659 return -EMSGSIZE;
2660}
2661
2662int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2663{
2664 int err;
2665 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002666 const struct bucket_table *tbl;
2667 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002668 struct net *net = sock_net(skb->sk);
2669 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002670 u32 tbl_id = cb->args[0];
2671 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002672
Ying Xue07f6c4b2015-01-07 13:41:58 +08002673 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002674 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002675 for (; tbl_id < tbl->size; tbl_id++) {
2676 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002677 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002678 if (prev_portid && prev_portid != tsk->portid) {
2679 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2680 continue;
2681 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002682
Richard Alped6e164e2015-01-16 12:30:40 +01002683 err = __tipc_nl_add_sk(skb, cb, tsk);
2684 if (err) {
2685 prev_portid = tsk->portid;
2686 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2687 goto out;
2688 }
2689 prev_portid = 0;
2690 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002691 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002692 }
Richard Alped6e164e2015-01-16 12:30:40 +01002693out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002694 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002695 cb->args[0] = tbl_id;
2696 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002697
2698 return skb->len;
2699}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002700
2701/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002702static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2703 struct netlink_callback *cb,
2704 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002705{
2706 void *hdr;
2707 struct nlattr *attrs;
2708
2709 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002710 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002711 if (!hdr)
2712 goto msg_cancel;
2713
2714 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2715 if (!attrs)
2716 goto genlmsg_cancel;
2717
2718 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2719 goto attr_msg_cancel;
2720 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2721 goto attr_msg_cancel;
2722 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2723 goto attr_msg_cancel;
2724 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2725 goto attr_msg_cancel;
2726
2727 nla_nest_end(skb, attrs);
2728 genlmsg_end(skb, hdr);
2729
2730 return 0;
2731
2732attr_msg_cancel:
2733 nla_nest_cancel(skb, attrs);
2734genlmsg_cancel:
2735 genlmsg_cancel(skb, hdr);
2736msg_cancel:
2737 return -EMSGSIZE;
2738}
2739
2740/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002741static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2742 struct netlink_callback *cb,
2743 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002744{
2745 int err;
2746 struct publication *p;
2747
2748 if (*last_publ) {
2749 list_for_each_entry(p, &tsk->publications, pport_list) {
2750 if (p->key == *last_publ)
2751 break;
2752 }
2753 if (p->key != *last_publ) {
2754 /* We never set seq or call nl_dump_check_consistent()
2755 * this means that setting prev_seq here will cause the
2756 * consistence check to fail in the netlink callback
2757 * handler. Resulting in the last NLMSG_DONE message
2758 * having the NLM_F_DUMP_INTR flag set.
2759 */
2760 cb->prev_seq = 1;
2761 *last_publ = 0;
2762 return -EPIPE;
2763 }
2764 } else {
2765 p = list_first_entry(&tsk->publications, struct publication,
2766 pport_list);
2767 }
2768
2769 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2770 err = __tipc_nl_add_sk_publ(skb, cb, p);
2771 if (err) {
2772 *last_publ = p->key;
2773 return err;
2774 }
2775 }
2776 *last_publ = 0;
2777
2778 return 0;
2779}
2780
2781int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2782{
2783 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002784 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002785 u32 last_publ = cb->args[1];
2786 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002787 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002788 struct tipc_sock *tsk;
2789
Ying Xue07f6c4b2015-01-07 13:41:58 +08002790 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002791 struct nlattr **attrs;
2792 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2793
2794 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2795 if (err)
2796 return err;
2797
2798 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2799 attrs[TIPC_NLA_SOCK],
2800 tipc_nl_sock_policy);
2801 if (err)
2802 return err;
2803
2804 if (!sock[TIPC_NLA_SOCK_REF])
2805 return -EINVAL;
2806
Ying Xue07f6c4b2015-01-07 13:41:58 +08002807 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002808 }
2809
2810 if (done)
2811 return 0;
2812
Ying Xuee05b31f2015-01-09 15:27:08 +08002813 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002814 if (!tsk)
2815 return -EINVAL;
2816
2817 lock_sock(&tsk->sk);
2818 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2819 if (!err)
2820 done = 1;
2821 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002822 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002823
Ying Xue07f6c4b2015-01-07 13:41:58 +08002824 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002825 cb->args[1] = last_publ;
2826 cb->args[2] = done;
2827
2828 return skb->len;
2829}