blob: b3415a78da2e6e1de1df88277670cce2aaa60264 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050038#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020039#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050040#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040041#include <linux/export.h>
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -040042#include "config.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040043#include "socket.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040044
Per Lidenb97bf3f2006-01-02 19:04:38 +010045#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
47
Jon Paul Maloy301bae52014-08-22 18:09:20 -040048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloydadebc02014-08-22 18:09:11 -040049#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
Jon Paul Maloy301bae52014-08-22 18:09:20 -040050#define TIPC_FWD_MSG 1
51#define TIPC_CONN_OK 0
52#define TIPC_CONN_PROBING 1
53
54/**
55 * struct tipc_sock - TIPC socket structure
56 * @sk: socket - interacts with 'port' and with user via the socket API
57 * @connected: non-zero if port is currently connected to a peer port
58 * @conn_type: TIPC type used when connection was established
59 * @conn_instance: TIPC instance used when connection was established
60 * @published: non-zero if port has one or more associated names
61 * @max_pkt: maximum packet size "hint" used when building messages sent by port
62 * @ref: unique reference to port in TIPC object registry
63 * @phdr: preformatted message header used when sending messages
64 * @port_list: adjacent ports in TIPC's global list of ports
65 * @publications: list of publications for port
66 * @pub_count: total # of publications port has made during its lifetime
67 * @probing_state:
68 * @probing_interval:
69 * @timer:
70 * @port: port - interacts with 'sk' and with the rest of the TIPC stack
71 * @peer_name: the peer of the connection, if any
72 * @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
77 */
78struct tipc_sock {
79 struct sock sk;
80 int connected;
81 u32 conn_type;
82 u32 conn_instance;
83 int published;
84 u32 max_pkt;
85 u32 ref;
86 struct tipc_msg phdr;
87 struct list_head sock_list;
88 struct list_head publications;
89 u32 pub_count;
90 u32 probing_state;
91 u32 probing_interval;
92 struct timer_list timer;
93 uint conn_timeout;
94 atomic_t dupl_rcvcnt;
95 bool link_cong;
96 uint sent_unacked;
97 uint rcv_unacked;
98};
Per Lidenb97bf3f2006-01-02 19:04:38 +010099
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400100static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400101static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800102static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800103static int tipc_release(struct socket *sock);
104static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400105static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Jon Paul Maloy57289012014-08-22 18:09:09 -0400106static void tipc_sk_timeout(unsigned long ref);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400107static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400108 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400109static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400110 struct tipc_name_seq const *seq);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -0400111static u32 tipc_sk_ref_acquire(struct tipc_sock *tsk);
112static void tipc_sk_ref_discard(u32 ref);
113static struct tipc_sock *tipc_sk_get(u32 ref);
114static struct tipc_sock *tipc_sk_get_next(u32 *ref);
115static void tipc_sk_put(struct tipc_sock *tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116
Florian Westphalbca65ea2008-02-07 18:18:01 -0800117static const struct proto_ops packet_ops;
118static const struct proto_ops stream_ops;
119static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120
121static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400122static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123
Richard Alpe1a1a1432014-11-20 10:29:11 +0100124static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
125 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
126 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
127 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
128 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
129 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
130};
131
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900132/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133 * Revised TIPC socket locking policy:
134 *
135 * Most socket operations take the standard socket lock when they start
136 * and hold it until they finish (or until they need to sleep). Acquiring
137 * this lock grants the owner exclusive access to the fields of the socket
138 * data structures, with the exception of the backlog queue. A few socket
139 * operations can be done without taking the socket lock because they only
140 * read socket information that never changes during the life of the socket.
141 *
142 * Socket operations may acquire the lock for the associated TIPC port if they
143 * need to perform an operation on the port. If any routine needs to acquire
144 * both the socket lock and the port lock it must take the socket lock first
145 * to avoid the risk of deadlock.
146 *
147 * The dispatcher handling incoming messages cannot grab the socket lock in
148 * the standard fashion, since invoked it runs at the BH level and cannot block.
149 * Instead, it checks to see if the socket lock is currently owned by someone,
150 * and either handles the message itself or adds it to the socket's backlog
151 * queue; in the latter case the queued message is processed once the process
152 * owning the socket lock releases it.
153 *
154 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
155 * the problem of a blocked socket operation preventing any other operations
156 * from occurring. However, applications must be careful if they have
157 * multiple threads trying to send (or receive) on the same socket, as these
158 * operations might interfere with each other. For example, doing a connect
159 * and a receive at the same time might allow the receive to consume the
160 * ACK message meant for the connect. While additional work could be done
161 * to try and overcome this, it doesn't seem to be worthwhile at the present.
162 *
163 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
164 * that another operation that must be performed in a non-blocking manner is
165 * not delayed for very long because the lock has already been taken.
166 *
167 * NOTE: This code assumes that certain fields of a port/socket pair are
168 * constant over its lifetime; such fields can be examined without taking
169 * the socket lock and/or port lock, and do not need to be re-read even
170 * after resuming processing after waiting. These fields include:
171 * - socket type
172 * - pointer to socket sk structure (aka tipc_sock structure)
173 * - pointer to port structure
174 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400177static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400178{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400179 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400180}
181
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400182static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400183{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400184 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400185}
186
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400187static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400188{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400190}
191
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400192static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400193{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400194 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400195}
196
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400197static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400198{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400199 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400200}
201
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400202static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400203{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400204 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400205}
206
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400208{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400209 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400210}
211
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400212static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400213{
214 if (imp > TIPC_CRITICAL_IMPORTANCE)
215 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400216 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400217 return 0;
218}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400219
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400220static struct tipc_sock *tipc_sk(const struct sock *sk)
221{
222 return container_of(sk, struct tipc_sock, sk);
223}
224
225static int tsk_conn_cong(struct tipc_sock *tsk)
226{
227 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
228}
229
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400231 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700232 *
233 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400235static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400237 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238}
239
240/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400241 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700242 *
243 * Caller must hold socket lock
244 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400245static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700246{
247 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500248 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500250 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
251 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400252 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500253 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700254}
255
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400256/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400257 *
258 * Handles cases where the node's network address has changed from
259 * the default of <0.0.0> to its configured setting.
260 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400261static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400262{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400263 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400264 u32 orig_node;
265 u32 peer_node;
266
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400267 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400268 return false;
269
270 if (unlikely(msg_origport(msg) != peer_port))
271 return false;
272
273 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400274 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400275
276 if (likely(orig_node == peer_node))
277 return true;
278
279 if (!orig_node && (peer_node == tipc_own_addr))
280 return true;
281
282 if (!peer_node && (orig_node == tipc_own_addr))
283 return true;
284
285 return false;
286}
287
Allan Stephens0c3141e2008-04-15 00:22:02 -0700288/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400289 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700290 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 * @sock: pre-allocated socket structure
292 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800293 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900294 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700295 * This routine creates additional data structures used by the TIPC socket,
296 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 *
298 * Returns 0 on success, errno otherwise
299 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400300static int tipc_sk_create(struct net *net, struct socket *sock,
301 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700303 const struct proto_ops *ops;
304 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400306 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400307 struct tipc_msg *msg;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400308 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700309
310 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 if (unlikely(protocol != 0))
312 return -EPROTONOSUPPORT;
313
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 switch (sock->type) {
315 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 ops = &stream_ops;
317 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 break;
319 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700320 ops = &packet_ops;
321 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 break;
323 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 ops = &msg_ops;
326 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 break;
Allan Stephens49978652006-06-25 23:47:18 -0700328 default:
Allan Stephens49978652006-06-25 23:47:18 -0700329 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 }
331
Allan Stephens0c3141e2008-04-15 00:22:02 -0700332 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400333 if (!kern)
334 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
335 else
336 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
337
Allan Stephens0c3141e2008-04-15 00:22:02 -0700338 if (sk == NULL)
339 return -ENOMEM;
340
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400341 tsk = tipc_sk(sk);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -0400342 ref = tipc_sk_ref_acquire(tsk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400343 if (!ref) {
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400344 pr_warn("Socket create failed; reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 return -ENOMEM;
346 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400347 tsk->max_pkt = MAX_PKT_DEFAULT;
348 tsk->ref = ref;
349 INIT_LIST_HEAD(&tsk->publications);
350 msg = &tsk->phdr;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400351 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
352 NAMED_H_SIZE, 0);
353 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 sock->ops = ops;
357 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 sock_init_data(sock, sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400359 k_init_timer(&tsk->timer, (Handler)tipc_sk_timeout, ref);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400360 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400361 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800362 sk->sk_data_ready = tipc_data_ready;
363 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400364 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500365 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400366 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700367
Allan Stephens0c3141e2008-04-15 00:22:02 -0700368 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400369 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700370 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400371 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700372 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 return 0;
374}
375
376/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400377 * tipc_sock_create_local - create TIPC socket from inside TIPC module
378 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
379 *
380 * We cannot use sock_creat_kern here because it bumps module user count.
381 * Since socket owner and creator is the same module we must make sure
382 * that module count remains zero for module local sockets, otherwise
383 * we cannot do rmmod.
384 *
385 * Returns 0 on success, errno otherwise
386 */
387int tipc_sock_create_local(int type, struct socket **res)
388{
389 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400390
391 rc = sock_create_lite(AF_TIPC, type, 0, res);
392 if (rc < 0) {
393 pr_err("Failed to create kernel socket\n");
394 return rc;
395 }
396 tipc_sk_create(&init_net, *res, 0, 1);
397
Ying Xuec5fa7b32013-06-17 10:54:39 -0400398 return 0;
399}
400
401/**
402 * tipc_sock_release_local - release socket created by tipc_sock_create_local
403 * @sock: the socket to be released.
404 *
405 * Module reference count is not incremented when such sockets are created,
406 * so we must keep it from being decremented when they are released.
407 */
408void tipc_sock_release_local(struct socket *sock)
409{
Ying Xue247f0f32014-02-18 16:06:46 +0800410 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400411 sock->ops = NULL;
412 sock_release(sock);
413}
414
415/**
416 * tipc_sock_accept_local - accept a connection on a socket created
417 * with tipc_sock_create_local. Use this function to avoid that
418 * module reference count is inadvertently incremented.
419 *
420 * @sock: the accepting socket
421 * @newsock: reference to the new socket to be created
422 * @flags: socket flags
423 */
424
425int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400426 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400427{
428 struct sock *sk = sock->sk;
429 int ret;
430
431 ret = sock_create_lite(sk->sk_family, sk->sk_type,
432 sk->sk_protocol, newsock);
433 if (ret < 0)
434 return ret;
435
Ying Xue247f0f32014-02-18 16:06:46 +0800436 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400437 if (ret < 0) {
438 sock_release(*newsock);
439 return ret;
440 }
441 (*newsock)->ops = sock->ops;
442 return ret;
443}
444
445/**
Ying Xue247f0f32014-02-18 16:06:46 +0800446 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 * @sock: socket to destroy
448 *
449 * This routine cleans up any messages that are still queued on the socket.
450 * For DGRAM and RDM socket types, all queued messages are rejected.
451 * For SEQPACKET and STREAM socket types, the first message is rejected
452 * and any others are discarded. (If the first message on a STREAM socket
453 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900454 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 * NOTE: Rejected messages are not necessarily returned to the sender! They
456 * are returned or discarded according to the "destination droppable" setting
457 * specified for the message by the sender.
458 *
459 * Returns 0 on success, errno otherwise
460 */
Ying Xue247f0f32014-02-18 16:06:46 +0800461static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400464 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500466 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468 /*
469 * Exit if socket isn't fully initialized (occurs when a failed accept()
470 * releases a pre-allocated child socket that was never used)
471 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700472 if (sk == NULL)
473 return 0;
474
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400475 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700476 lock_sock(sk);
477
478 /*
479 * Reject all unreceived messages, except on an active connection
480 * (which disconnects locally & sends a 'FIN+' to peer)
481 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400482 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700484 buf = __skb_dequeue(&sk->sk_receive_queue);
485 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 break;
Ying Xue40682432013-10-18 07:23:16 +0200487 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400488 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700489 else {
490 if ((sock->state == SS_CONNECTING) ||
491 (sock->state == SS_CONNECTED)) {
492 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400493 tsk->connected = 0;
494 tipc_node_remove_conn(dnode, tsk->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700495 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500496 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400497 tipc_link_xmit(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700498 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 }
500
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400501 tipc_sk_withdraw(tsk, 0, NULL);
502 tipc_sk_ref_discard(tsk->ref);
503 k_cancel_timer(&tsk->timer);
504 if (tsk->connected) {
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400505 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
506 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400507 tsk_peer_port(tsk),
508 tsk->ref, TIPC_ERR_NO_PORT);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400509 if (buf)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400510 tipc_link_xmit(buf, dnode, tsk->ref);
511 tipc_node_remove_conn(dnode, tsk->ref);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400512 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400513 k_term_timer(&tsk->timer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514
Allan Stephens0c3141e2008-04-15 00:22:02 -0700515 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100516 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517
Allan Stephens0c3141e2008-04-15 00:22:02 -0700518 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700519 sock->state = SS_DISCONNECTING;
520 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700522 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200524 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525}
526
527/**
Ying Xue247f0f32014-02-18 16:06:46 +0800528 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 * @sock: socket structure
530 * @uaddr: socket address describing name(s) and desired operation
531 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900532 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 * Name and name sequence binding is indicated using a positive scope value;
534 * a negative scope value unbinds the specified name. Specifying no name
535 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900536 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700538 *
539 * NOTE: This routine doesn't need to take the socket lock since it doesn't
540 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 */
Ying Xue247f0f32014-02-18 16:06:46 +0800542static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
543 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544{
Ying Xue84602762013-12-27 10:18:28 +0800545 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400547 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800548 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549
Ying Xue84602762013-12-27 10:18:28 +0800550 lock_sock(sk);
551 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400552 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800553 goto exit;
554 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900555
Ying Xue84602762013-12-27 10:18:28 +0800556 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
557 res = -EINVAL;
558 goto exit;
559 }
560 if (addr->family != AF_TIPC) {
561 res = -EAFNOSUPPORT;
562 goto exit;
563 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 if (addr->addrtype == TIPC_ADDR_NAME)
566 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800567 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
568 res = -EAFNOSUPPORT;
569 goto exit;
570 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900571
Ying Xue13a2e892013-06-17 10:54:40 -0400572 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400573 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800574 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
575 res = -EACCES;
576 goto exit;
577 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400578
Ying Xue84602762013-12-27 10:18:28 +0800579 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400580 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
581 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800582exit:
583 release_sock(sk);
584 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585}
586
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587/**
Ying Xue247f0f32014-02-18 16:06:46 +0800588 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 * @sock: socket structure
590 * @uaddr: area for returned socket address
591 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700592 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900593 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700595 *
Allan Stephens2da59912008-07-14 22:43:32 -0700596 * NOTE: This routine doesn't need to take the socket lock since it only
597 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000598 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 */
Ying Xue247f0f32014-02-18 16:06:46 +0800600static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
601 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400604 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000606 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700607 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700608 if ((sock->state != SS_CONNECTED) &&
609 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
610 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400611 addr->addr.id.ref = tsk_peer_port(tsk);
612 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700613 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400614 addr->addr.id.ref = tsk->ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000615 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700616 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617
618 *uaddr_len = sizeof(*addr);
619 addr->addrtype = TIPC_ADDR_ID;
620 addr->family = AF_TIPC;
621 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 addr->addr.name.domain = 0;
623
Allan Stephens0c3141e2008-04-15 00:22:02 -0700624 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625}
626
627/**
Ying Xue247f0f32014-02-18 16:06:46 +0800628 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 * @file: file structure associated with the socket
630 * @sock: socket for which to calculate the poll bits
631 * @wait: ???
632 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700633 * Returns pollmask value
634 *
635 * COMMENTARY:
636 * It appears that the usual socket locking mechanisms are not useful here
637 * since the pollmask info is potentially out-of-date the moment this routine
638 * exits. TCP and other protocols seem to rely on higher level poll routines
639 * to handle any preventable race conditions, so TIPC will do the same ...
640 *
641 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700642 *
Allan Stephensf662c072010-08-17 11:00:06 +0000643 * socket state flags set
644 * ------------ ---------
645 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200646 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000647 *
648 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
649 * no write flags
650 *
651 * connected POLLIN/POLLRDNORM if data in rx queue
652 * POLLOUT if port is not congested
653 *
654 * disconnecting POLLIN/POLLRDNORM/POLLHUP
655 * no write flags
656 *
657 * listening POLLIN if SYN in rx queue
658 * no write flags
659 *
660 * ready POLLIN/POLLRDNORM if data in rx queue
661 * [connectionless] POLLOUT (since port cannot be congested)
662 *
663 * IMPORTANT: The fact that a read or write operation is indicated does NOT
664 * imply that the operation will succeed, merely that it should be performed
665 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 */
Ying Xue247f0f32014-02-18 16:06:46 +0800667static unsigned int tipc_poll(struct file *file, struct socket *sock,
668 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669{
Allan Stephens9b674e82008-03-26 16:48:21 -0700670 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400671 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000672 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700673
Ying Xuef288bef2012-08-21 11:16:57 +0800674 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700675
Allan Stephensf662c072010-08-17 11:00:06 +0000676 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200677 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500678 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200679 mask |= POLLOUT;
680 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000681 case SS_READY:
682 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400683 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000684 mask |= POLLOUT;
685 /* fall thru' */
686 case SS_CONNECTING:
687 case SS_LISTENING:
688 if (!skb_queue_empty(&sk->sk_receive_queue))
689 mask |= (POLLIN | POLLRDNORM);
690 break;
691 case SS_DISCONNECTING:
692 mask = (POLLIN | POLLRDNORM | POLLHUP);
693 break;
694 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700695
696 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697}
698
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400699/**
700 * tipc_sendmcast - send multicast message
701 * @sock: socket structure
702 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500703 * @msg: message to send
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400704 * @dsz: total length of message data
705 * @timeo: timeout to wait for wakeup
706 *
707 * Called from function tipc_sendmsg(), which has done all sanity checks
708 * Returns the number of bytes sent on success, or errno
709 */
710static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Al Viro562640f2014-11-15 01:13:43 -0500711 struct msghdr *msg, size_t dsz, long timeo)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400712{
713 struct sock *sk = sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400714 struct tipc_msg *mhdr = &tipc_sk(sk)->phdr;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400715 struct sk_buff *buf;
716 uint mtu;
717 int rc;
718
719 msg_set_type(mhdr, TIPC_MCAST_MSG);
720 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
721 msg_set_destport(mhdr, 0);
722 msg_set_destnode(mhdr, 0);
723 msg_set_nametype(mhdr, seq->type);
724 msg_set_namelower(mhdr, seq->lower);
725 msg_set_nameupper(mhdr, seq->upper);
726 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
727
728new_mtu:
729 mtu = tipc_bclink_get_mtu();
Al Viro45dcc682014-11-15 01:16:27 -0500730 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400731 if (unlikely(rc < 0))
732 return rc;
733
734 do {
735 rc = tipc_bclink_xmit(buf);
736 if (likely(rc >= 0)) {
737 rc = dsz;
738 break;
739 }
740 if (rc == -EMSGSIZE)
741 goto new_mtu;
742 if (rc != -ELINKCONG)
743 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400744 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400745 rc = tipc_wait_for_sndmsg(sock, &timeo);
746 if (rc)
747 kfree_skb_list(buf);
748 } while (!rc);
749 return rc;
750}
751
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400752/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
753 */
754void tipc_sk_mcast_rcv(struct sk_buff *buf)
755{
756 struct tipc_msg *msg = buf_msg(buf);
757 struct tipc_port_list dports = {0, NULL, };
758 struct tipc_port_list *item;
759 struct sk_buff *b;
760 uint i, last, dst = 0;
761 u32 scope = TIPC_CLUSTER_SCOPE;
762
763 if (in_own_node(msg_orignode(msg)))
764 scope = TIPC_NODE_SCOPE;
765
766 /* Create destination port list: */
767 tipc_nametbl_mc_translate(msg_nametype(msg),
768 msg_namelower(msg),
769 msg_nameupper(msg),
770 scope,
771 &dports);
772 last = dports.count;
773 if (!last) {
774 kfree_skb(buf);
775 return;
776 }
777
778 for (item = &dports; item; item = item->next) {
779 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
780 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
781 if (!b) {
782 pr_warn("Failed do clone mcast rcv buffer\n");
783 continue;
784 }
785 msg_set_destport(msg, item->ports[i]);
786 tipc_sk_rcv(b);
787 }
788 }
789 tipc_port_list_free(&dports);
790}
791
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900792/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500793 * tipc_sk_proto_rcv - receive a connection mng protocol message
794 * @tsk: receiving socket
795 * @dnode: node to send response message to, if any
796 * @buf: buffer containing protocol message
797 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
798 * (CONN_PROBE_REPLY) message should be forwarded.
799 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800800static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
801 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500802{
803 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500804 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500805
806 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400807 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500808 goto exit;
809
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400810 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500811
812 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400813 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500814 tsk->sent_unacked -= msg_msgcnt(msg);
815 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400816 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500817 } else if (msg_type(msg) == CONN_PROBE) {
818 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
819 return TIPC_OK;
820 msg_set_type(msg, CONN_PROBE_REPLY);
821 return TIPC_FWD_MSG;
822 }
823 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
824exit:
825 kfree_skb(buf);
826 return TIPC_OK;
827}
828
829/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 * dest_name_check - verify user is permitted to send to specified port name
831 * @dest: destination address
832 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 * Prevents restricted configuration commands from being issued by
835 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900836 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837 * Returns 0 if permission is granted, otherwise errno
838 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800839static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840{
841 struct tipc_cfg_msg_hdr hdr;
842
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500843 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
844 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900845 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
846 return 0;
847 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
848 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900849 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
850 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851
Allan Stephens3f8dd942011-01-18 13:09:29 -0500852 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
853 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900854 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700856 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900858
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 return 0;
860}
861
Ying Xue3f405042014-01-17 09:50:05 +0800862static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
863{
864 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400865 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800866 DEFINE_WAIT(wait);
867 int done;
868
869 do {
870 int err = sock_error(sk);
871 if (err)
872 return err;
873 if (sock->state == SS_DISCONNECTING)
874 return -EPIPE;
875 if (!*timeo_p)
876 return -EAGAIN;
877 if (signal_pending(current))
878 return sock_intr_errno(*timeo_p);
879
880 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500881 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800882 finish_wait(sk_sleep(sk), &wait);
883 } while (!done);
884 return 0;
885}
886
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500887/**
Ying Xue247f0f32014-02-18 16:06:46 +0800888 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700889 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 * @sock: socket structure
891 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500892 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900893 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900895 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
897 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900898 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 * Returns the number of bytes sent on success, or errno otherwise
900 */
Ying Xue247f0f32014-02-18 16:06:46 +0800901static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500902 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500904 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700905 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400906 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400907 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500908 u32 dnode, dport;
909 struct sk_buff *buf;
910 struct tipc_name_seq *seq = &dest->addr.nameseq;
911 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800912 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500913 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914
915 if (unlikely(!dest))
916 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500917
Allan Stephens51f9cc12006-06-25 23:49:06 -0700918 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
919 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500921
922 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400923 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924
Allan Stephens0c3141e2008-04-15 00:22:02 -0700925 if (iocb)
926 lock_sock(sk);
927
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500928 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700929 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500930 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700931 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700932 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700933 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500934 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700935 goto exit;
936 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400937 if (tsk->published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500938 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700939 goto exit;
940 }
941 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400942 tsk->conn_type = dest->addr.name.name.type;
943 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700944 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500946 rc = dest_name_check(dest, m);
947 if (rc)
948 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949
Ying Xue3f405042014-01-17 09:50:05 +0800950 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500951
952 if (dest->addrtype == TIPC_ADDR_MCAST) {
Al Viro562640f2014-11-15 01:13:43 -0500953 rc = tipc_sendmcast(sock, seq, m, dsz, timeo);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500954 goto exit;
955 } else if (dest->addrtype == TIPC_ADDR_NAME) {
956 u32 type = dest->addr.name.name.type;
957 u32 inst = dest->addr.name.name.instance;
958 u32 domain = dest->addr.name.domain;
959
960 dnode = domain;
961 msg_set_type(mhdr, TIPC_NAMED_MSG);
962 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
963 msg_set_nametype(mhdr, type);
964 msg_set_nameinst(mhdr, inst);
965 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
966 dport = tipc_nametbl_translate(type, inst, &dnode);
967 msg_set_destnode(mhdr, dnode);
968 msg_set_destport(mhdr, dport);
969 if (unlikely(!dport && !dnode)) {
970 rc = -EHOSTUNREACH;
971 goto exit;
972 }
973 } else if (dest->addrtype == TIPC_ADDR_ID) {
974 dnode = dest->addr.id.node;
975 msg_set_type(mhdr, TIPC_DIRECT_MSG);
976 msg_set_lookup_scope(mhdr, 0);
977 msg_set_destnode(mhdr, dnode);
978 msg_set_destport(mhdr, dest->addr.id.ref);
979 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
980 }
981
982new_mtu:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400983 mtu = tipc_node_get_mtu(dnode, tsk->ref);
Al Viro45dcc682014-11-15 01:16:27 -0500984 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500985 if (rc < 0)
986 goto exit;
987
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900988 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400989 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400990 rc = tipc_link_xmit(buf, dnode, tsk->ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500991 if (likely(rc >= 0)) {
992 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700993 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500994 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700995 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900996 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500997 if (rc == -EMSGSIZE)
998 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500999 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001000 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001001 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001002 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001003 if (rc)
1004 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001005 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001006exit:
1007 if (iocb)
1008 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001009
1010 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011}
1012
Ying Xue391a6dd2014-01-17 09:50:06 +08001013static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
1014{
1015 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001016 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +08001017 DEFINE_WAIT(wait);
1018 int done;
1019
1020 do {
1021 int err = sock_error(sk);
1022 if (err)
1023 return err;
1024 if (sock->state == SS_DISCONNECTING)
1025 return -EPIPE;
1026 else if (sock->state != SS_CONNECTED)
1027 return -ENOTCONN;
1028 if (!*timeo_p)
1029 return -EAGAIN;
1030 if (signal_pending(current))
1031 return sock_intr_errno(*timeo_p);
1032
1033 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1034 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001035 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001036 !tsk_conn_cong(tsk)) ||
1037 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +08001038 finish_wait(sk_sleep(sk), &wait);
1039 } while (!done);
1040 return 0;
1041}
1042
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001043/**
Ying Xue247f0f32014-02-18 16:06:46 +08001044 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 * @iocb: (unused)
1046 * @sock: socket structure
1047 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001048 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001049 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001051 *
1052 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001053 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 */
Ying Xue247f0f32014-02-18 16:06:46 +08001055static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001056 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001058 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001059 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001060 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001061 struct sk_buff *buf;
1062 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001063 u32 ref = tsk->ref;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001064 int rc = -EINVAL;
1065 long timeo;
1066 u32 dnode;
1067 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001068
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001069 /* Handle implied connection establishment */
1070 if (unlikely(dest)) {
1071 rc = tipc_sendmsg(iocb, sock, m, dsz);
1072 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001073 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001074 return rc;
1075 }
1076 if (dsz > (uint)INT_MAX)
1077 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001078
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001079 if (iocb)
1080 lock_sock(sk);
1081
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001082 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001083 if (sock->state == SS_DISCONNECTING)
1084 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001085 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001086 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +08001087 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001088 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001090 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001091 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001092
1093next:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001094 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001095 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Al Viro45dcc682014-11-15 01:16:27 -05001096 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001097 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001098 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001099 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001100 if (likely(!tsk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001101 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001102 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001103 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001104 sent += send;
1105 if (sent == dsz)
1106 break;
1107 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001108 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001109 if (rc == -EMSGSIZE) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001110 tsk->max_pkt = tipc_node_get_mtu(dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001111 goto next;
1112 }
1113 if (rc != -ELINKCONG)
1114 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001115 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001117 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001118 if (rc)
1119 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001120 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001121exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001122 if (iocb)
1123 release_sock(sk);
1124 return sent ? sent : rc;
1125}
1126
1127/**
1128 * tipc_send_packet - send a connection-oriented message
1129 * @iocb: if NULL, indicates that socket lock is already held
1130 * @sock: socket structure
1131 * @m: message to send
1132 * @dsz: length of data to be transmitted
1133 *
1134 * Used for SOCK_SEQPACKET messages.
1135 *
1136 * Returns the number of bytes sent on success, or errno otherwise
1137 */
1138static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1139 struct msghdr *m, size_t dsz)
1140{
1141 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1142 return -EMSGSIZE;
1143
1144 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145}
1146
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001147/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001149static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001150 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151{
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001152 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001154 msg_set_destnode(msg, peer_node);
1155 msg_set_destport(msg, peer_port);
1156 msg_set_type(msg, TIPC_CONN_MSG);
1157 msg_set_lookup_scope(msg, 0);
1158 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001159
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001160 tsk->probing_interval = CONN_PROBING_INTERVAL;
1161 tsk->probing_state = TIPC_CONN_OK;
1162 tsk->connected = 1;
1163 k_start_timer(&tsk->timer, tsk->probing_interval);
1164 tipc_node_add_conn(peer_node, tsk->ref, peer_port);
1165 tsk->max_pkt = tipc_node_get_mtu(peer_node, tsk->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166}
1167
1168/**
1169 * set_orig_addr - capture sender's address for received message
1170 * @m: descriptor for message info
1171 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001172 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 * Note: Address is not captured if not requested by receiver.
1174 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001175static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001177 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001179 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 addr->family = AF_TIPC;
1181 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001182 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 addr->addr.id.ref = msg_origport(msg);
1184 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001185 addr->addr.name.domain = 0; /* could leave uninitialized */
1186 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 m->msg_namelen = sizeof(struct sockaddr_tipc);
1188 }
1189}
1190
1191/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001192 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 * @m: descriptor for message info
1194 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001195 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001196 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001198 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 * Returns 0 if successful, otherwise errno
1200 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001201static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1202 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203{
1204 u32 anc_data[3];
1205 u32 err;
1206 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001207 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 int res;
1209
1210 if (likely(m->msg_controllen == 0))
1211 return 0;
1212
1213 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 err = msg ? msg_errcode(msg) : 0;
1215 if (unlikely(err)) {
1216 anc_data[0] = err;
1217 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001218 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1219 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001221 if (anc_data[1]) {
1222 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1223 msg_data(msg));
1224 if (res)
1225 return res;
1226 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227 }
1228
1229 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1231 switch (dest_type) {
1232 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001233 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 anc_data[0] = msg_nametype(msg);
1235 anc_data[1] = msg_namelower(msg);
1236 anc_data[2] = msg_namelower(msg);
1237 break;
1238 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001239 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 anc_data[0] = msg_nametype(msg);
1241 anc_data[1] = msg_namelower(msg);
1242 anc_data[2] = msg_nameupper(msg);
1243 break;
1244 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001245 has_name = (tsk->conn_type != 0);
1246 anc_data[0] = tsk->conn_type;
1247 anc_data[1] = tsk->conn_instance;
1248 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 break;
1250 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001251 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 }
Allan Stephens2db99832010-12-31 18:59:33 +00001253 if (has_name) {
1254 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1255 if (res)
1256 return res;
1257 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258
1259 return 0;
1260}
1261
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001262static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001263{
1264 struct sk_buff *buf = NULL;
1265 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001266 u32 peer_port = tsk_peer_port(tsk);
1267 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001268
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001269 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001270 return;
1271 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001272 tipc_own_addr, peer_port, tsk->ref, TIPC_OK);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001273 if (!buf)
1274 return;
1275 msg = buf_msg(buf);
1276 msg_set_msgcnt(msg, ack);
1277 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1278}
1279
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001280static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001281{
1282 struct sock *sk = sock->sk;
1283 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001284 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001285 int err;
1286
1287 for (;;) {
1288 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001289 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001290 if (sock->state == SS_DISCONNECTING) {
1291 err = -ENOTCONN;
1292 break;
1293 }
1294 release_sock(sk);
1295 timeo = schedule_timeout(timeo);
1296 lock_sock(sk);
1297 }
1298 err = 0;
1299 if (!skb_queue_empty(&sk->sk_receive_queue))
1300 break;
1301 err = sock_intr_errno(timeo);
1302 if (signal_pending(current))
1303 break;
1304 err = -EAGAIN;
1305 if (!timeo)
1306 break;
1307 }
1308 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001309 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001310 return err;
1311}
1312
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001313/**
Ying Xue247f0f32014-02-18 16:06:46 +08001314 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 * @iocb: (unused)
1316 * @m: descriptor for message info
1317 * @buf_len: total size of user buffer area
1318 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001319 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1321 * If the complete message doesn't fit in user area, truncate it.
1322 *
1323 * Returns size of returned message data, errno otherwise
1324 */
Ying Xue247f0f32014-02-18 16:06:46 +08001325static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1326 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001328 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001329 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330 struct sk_buff *buf;
1331 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001332 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 unsigned int sz;
1334 u32 err;
1335 int res;
1336
Allan Stephens0c3141e2008-04-15 00:22:02 -07001337 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 if (unlikely(!buf_len))
1339 return -EINVAL;
1340
Allan Stephens0c3141e2008-04-15 00:22:02 -07001341 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342
Allan Stephens0c3141e2008-04-15 00:22:02 -07001343 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 res = -ENOTCONN;
1345 goto exit;
1346 }
1347
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001348 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001349restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350
Allan Stephens0c3141e2008-04-15 00:22:02 -07001351 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001352 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001353 if (res)
1354 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001355
1356 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001357 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 msg = buf_msg(buf);
1359 sz = msg_data_sz(msg);
1360 err = msg_errcode(msg);
1361
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001364 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 goto restart;
1366 }
1367
1368 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369 set_orig_addr(m, msg);
1370
1371 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001372 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001373 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 goto exit;
1375
1376 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 if (!err) {
1378 if (unlikely(buf_len < sz)) {
1379 sz = buf_len;
1380 m->msg_flags |= MSG_TRUNC;
1381 }
David S. Miller51f3d022014-11-05 16:46:40 -05001382 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001383 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 res = sz;
1386 } else {
1387 if ((sock->state == SS_READY) ||
1388 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1389 res = 0;
1390 else
1391 res = -ECONNRESET;
1392 }
1393
1394 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001396 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001397 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001398 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001399 tsk->rcv_unacked = 0;
1400 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001401 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001402 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001404 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405 return res;
1406}
1407
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001408/**
Ying Xue247f0f32014-02-18 16:06:46 +08001409 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 * @iocb: (unused)
1411 * @m: descriptor for message info
1412 * @buf_len: total size of user buffer area
1413 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001414 *
1415 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 * will optionally wait for more; never truncates data.
1417 *
1418 * Returns size of returned message data, errno otherwise
1419 */
Ying Xue247f0f32014-02-18 16:06:46 +08001420static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1421 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001423 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001424 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425 struct sk_buff *buf;
1426 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001427 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001429 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001432 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433
1434 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 if (unlikely(!buf_len))
1436 return -EINVAL;
1437
Allan Stephens0c3141e2008-04-15 00:22:02 -07001438 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001440 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441 res = -ENOTCONN;
1442 goto exit;
1443 }
1444
Florian Westphal3720d402010-08-17 11:00:04 +00001445 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001446 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001447
Allan Stephens0c3141e2008-04-15 00:22:02 -07001448restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001449 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001450 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001451 if (res)
1452 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001453
1454 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001455 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456 msg = buf_msg(buf);
1457 sz = msg_data_sz(msg);
1458 err = msg_errcode(msg);
1459
1460 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001462 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 goto restart;
1464 }
1465
1466 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 if (sz_copied == 0) {
1468 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001469 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001470 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 goto exit;
1472 }
1473
1474 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001476 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477
Allan Stephens0232fd02011-02-21 09:45:40 -05001478 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 needed = (buf_len - sz_copied);
1480 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001481
David S. Miller51f3d022014-11-05 16:46:40 -05001482 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1483 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001484 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001486
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487 sz_copied += sz_to_copy;
1488
1489 if (sz_to_copy < sz) {
1490 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001491 TIPC_SKB_CB(buf)->handle =
1492 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493 goto exit;
1494 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 } else {
1496 if (sz_copied != 0)
1497 goto exit; /* can't add error msg to valid data */
1498
1499 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1500 res = 0;
1501 else
1502 res = -ECONNRESET;
1503 }
1504
1505 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001507 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001508 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001509 tsk->rcv_unacked = 0;
1510 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001511 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001512 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513
1514 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001515 if ((sz_copied < buf_len) && /* didn't get all requested data */
1516 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001517 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001518 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1519 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520 goto restart;
1521
1522exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001523 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001524 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525}
1526
1527/**
Ying Xuef288bef2012-08-21 11:16:57 +08001528 * tipc_write_space - wake up thread if port congestion is released
1529 * @sk: socket
1530 */
1531static void tipc_write_space(struct sock *sk)
1532{
1533 struct socket_wq *wq;
1534
1535 rcu_read_lock();
1536 wq = rcu_dereference(sk->sk_wq);
1537 if (wq_has_sleeper(wq))
1538 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1539 POLLWRNORM | POLLWRBAND);
1540 rcu_read_unlock();
1541}
1542
1543/**
1544 * tipc_data_ready - wake up threads to indicate messages have been received
1545 * @sk: socket
1546 * @len: the length of messages
1547 */
David S. Miller676d2362014-04-11 16:15:36 -04001548static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001549{
1550 struct socket_wq *wq;
1551
1552 rcu_read_lock();
1553 wq = rcu_dereference(sk->sk_wq);
1554 if (wq_has_sleeper(wq))
1555 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1556 POLLRDNORM | POLLRDBAND);
1557 rcu_read_unlock();
1558}
1559
1560/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001561 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001562 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001563 * @msg: message
1564 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001565 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001566 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001567static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001568{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001569 struct sock *sk = &tsk->sk;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001570 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001571 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001572 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001573
1574 if (msg_mcast(msg))
1575 return retval;
1576
1577 switch ((int)sock->state) {
1578 case SS_CONNECTED:
1579 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001580 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001581 if (unlikely(msg_errcode(msg))) {
1582 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001583 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001584 /* let timer expire on it's own */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001585 tipc_node_remove_conn(tsk_peer_node(tsk),
1586 tsk->ref);
Ying Xue7e6c1312012-11-29 18:39:14 -05001587 }
1588 retval = TIPC_OK;
1589 }
1590 break;
1591 case SS_CONNECTING:
1592 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001593
1594 if (unlikely(!msg_connected(msg)))
1595 break;
1596
Ying Xue584d24b2012-11-29 18:51:19 -05001597 if (unlikely(msg_errcode(msg))) {
1598 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001599 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001600 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001601 break;
1602 }
1603
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001604 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001605 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001606 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001607 retval = TIPC_OK;
1608 break;
1609 }
1610
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001611 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1612 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001613 sock->state = SS_CONNECTED;
1614
Ying Xue584d24b2012-11-29 18:51:19 -05001615 /* If an incoming message is an 'ACK-', it should be
1616 * discarded here because it doesn't contain useful
1617 * data. In addition, we should try to wake up
1618 * connect() routine if sleeping.
1619 */
1620 if (msg_data_sz(msg) == 0) {
1621 kfree_skb(*buf);
1622 *buf = NULL;
1623 if (waitqueue_active(sk_sleep(sk)))
1624 wake_up_interruptible(sk_sleep(sk));
1625 }
1626 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001627 break;
1628 case SS_LISTENING:
1629 case SS_UNCONNECTED:
1630 /* Accept only SYN message */
1631 if (!msg_connected(msg) && !(msg_errcode(msg)))
1632 retval = TIPC_OK;
1633 break;
1634 case SS_DISCONNECTING:
1635 break;
1636 default:
1637 pr_err("Unknown socket state %u\n", sock->state);
1638 }
1639 return retval;
1640}
1641
1642/**
Ying Xueaba79f32013-01-20 23:30:09 +01001643 * rcvbuf_limit - get proper overload limit of socket receive queue
1644 * @sk: socket
1645 * @buf: message
1646 *
1647 * For all connection oriented messages, irrespective of importance,
1648 * the default overload value (i.e. 67MB) is set as limit.
1649 *
1650 * For all connectionless messages, by default new queue limits are
1651 * as belows:
1652 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001653 * TIPC_LOW_IMPORTANCE (4 MB)
1654 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1655 * TIPC_HIGH_IMPORTANCE (16 MB)
1656 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001657 *
1658 * Returns overload limit according to corresponding message importance
1659 */
1660static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1661{
1662 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001663
1664 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001665 return sysctl_tipc_rmem[2];
1666
1667 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1668 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001669}
1670
1671/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001672 * filter_rcv - validate incoming message
1673 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001675 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001676 * Enqueues message on receive queue if acceptable; optionally handles
1677 * disconnect indication for a connected socket.
1678 *
1679 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001680 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001681 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001682 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001684static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001686 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001687 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001689 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001690 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001691 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001693 if (unlikely(msg_user(msg) == CONN_MANAGER))
1694 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001695
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001696 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1697 kfree_skb(buf);
1698 tsk->link_cong = 0;
1699 sk->sk_write_space(sk);
1700 return TIPC_OK;
1701 }
1702
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001704 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001705 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001706
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001708 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001709 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001711 rc = filter_connect(tsk, &buf);
1712 if (rc != TIPC_OK || buf == NULL)
1713 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001714 }
1715
1716 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001717 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001718 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719
Ying Xueaba79f32013-01-20 23:30:09 +01001720 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001721 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001722 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001723 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001724
David S. Miller676d2362014-04-11 16:15:36 -04001725 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 return TIPC_OK;
1727}
1728
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001729/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001730 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731 * @sk: socket
1732 * @buf: message
1733 *
1734 * Caller must hold socket lock, but not port lock.
1735 *
1736 * Returns 0
1737 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001738static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001740 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001741 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001742 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001743 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001744
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001745 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001746
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001747 if (likely(!rc)) {
1748 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1749 atomic_add(truesize, &tsk->dupl_rcvcnt);
1750 return 0;
1751 }
1752
1753 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1754 return 0;
1755
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001756 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001757
Allan Stephens0c3141e2008-04-15 00:22:02 -07001758 return 0;
1759}
1760
1761/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001762 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001763 * @buf: buffer containing arriving message
1764 * Consumes buffer
1765 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001766 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001767int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001768{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001769 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001770 struct sock *sk;
1771 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001772 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001773 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001774 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001775
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001776 /* Validate destination and message */
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001777 tsk = tipc_sk_get(dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001778 if (unlikely(!tsk)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001779 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001780 goto exit;
1781 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001782 sk = &tsk->sk;
1783
1784 /* Queue message */
Ying Xue1a194c22014-10-20 14:46:35 +08001785 spin_lock_bh(&sk->sk_lock.slock);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001786
Allan Stephens0c3141e2008-04-15 00:22:02 -07001787 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001788 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001789 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001790 if (sk->sk_backlog.len == 0)
1791 atomic_set(&tsk->dupl_rcvcnt, 0);
1792 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1793 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001794 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001795 }
Ying Xue1a194c22014-10-20 14:46:35 +08001796 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001797 tipc_sk_put(tsk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001798 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001799 return 0;
1800exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001801 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001802 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001803
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001804 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001805 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001806}
1807
Ying Xue78eb3a52014-01-17 09:50:03 +08001808static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1809{
1810 struct sock *sk = sock->sk;
1811 DEFINE_WAIT(wait);
1812 int done;
1813
1814 do {
1815 int err = sock_error(sk);
1816 if (err)
1817 return err;
1818 if (!*timeo_p)
1819 return -ETIMEDOUT;
1820 if (signal_pending(current))
1821 return sock_intr_errno(*timeo_p);
1822
1823 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1824 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1825 finish_wait(sk_sleep(sk), &wait);
1826 } while (!done);
1827 return 0;
1828}
1829
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830/**
Ying Xue247f0f32014-02-18 16:06:46 +08001831 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832 * @sock: socket structure
1833 * @dest: socket address for destination port
1834 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001835 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001836 *
1837 * Returns 0 on success, errno otherwise
1838 */
Ying Xue247f0f32014-02-18 16:06:46 +08001839static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1840 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001842 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001843 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1844 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001845 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1846 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001847 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848
Allan Stephens0c3141e2008-04-15 00:22:02 -07001849 lock_sock(sk);
1850
Allan Stephensb89741a2008-04-15 00:20:37 -07001851 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001852 if (sock->state == SS_READY) {
1853 res = -EOPNOTSUPP;
1854 goto exit;
1855 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856
Allan Stephensb89741a2008-04-15 00:20:37 -07001857 /*
1858 * Reject connection attempt using multicast address
1859 *
1860 * Note: send_msg() validates the rest of the address fields,
1861 * so there's no need to do it here
1862 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001863 if (dst->addrtype == TIPC_ADDR_MCAST) {
1864 res = -EINVAL;
1865 goto exit;
1866 }
1867
Ying Xue78eb3a52014-01-17 09:50:03 +08001868 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001869 switch (sock->state) {
1870 case SS_UNCONNECTED:
1871 /* Send a 'SYN-' to destination */
1872 m.msg_name = dest;
1873 m.msg_namelen = destlen;
1874
1875 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1876 * indicate send_msg() is never blocked.
1877 */
1878 if (!timeout)
1879 m.msg_flags = MSG_DONTWAIT;
1880
Ying Xue247f0f32014-02-18 16:06:46 +08001881 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001882 if ((res < 0) && (res != -EWOULDBLOCK))
1883 goto exit;
1884
1885 /* Just entered SS_CONNECTING state; the only
1886 * difference is that return value in non-blocking
1887 * case is EINPROGRESS, rather than EALREADY.
1888 */
1889 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001890 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001891 if (previous == SS_CONNECTING)
1892 res = -EALREADY;
1893 if (!timeout)
1894 goto exit;
1895 timeout = msecs_to_jiffies(timeout);
1896 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1897 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001898 break;
1899 case SS_CONNECTED:
1900 res = -EISCONN;
1901 break;
1902 default:
1903 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001904 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001905 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001906exit:
1907 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001908 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909}
1910
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001911/**
Ying Xue247f0f32014-02-18 16:06:46 +08001912 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913 * @sock: socket structure
1914 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001915 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916 * Returns 0 on success, errno otherwise
1917 */
Ying Xue247f0f32014-02-18 16:06:46 +08001918static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001920 struct sock *sk = sock->sk;
1921 int res;
1922
1923 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924
Ying Xue245f3d32011-07-06 06:01:13 -04001925 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001926 res = -EINVAL;
1927 else {
1928 sock->state = SS_LISTENING;
1929 res = 0;
1930 }
1931
1932 release_sock(sk);
1933 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934}
1935
Ying Xue6398e232014-01-17 09:50:04 +08001936static int tipc_wait_for_accept(struct socket *sock, long timeo)
1937{
1938 struct sock *sk = sock->sk;
1939 DEFINE_WAIT(wait);
1940 int err;
1941
1942 /* True wake-one mechanism for incoming connections: only
1943 * one process gets woken up, not the 'whole herd'.
1944 * Since we do not 'race & poll' for established sockets
1945 * anymore, the common case will execute the loop only once.
1946 */
1947 for (;;) {
1948 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1949 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001950 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001951 release_sock(sk);
1952 timeo = schedule_timeout(timeo);
1953 lock_sock(sk);
1954 }
1955 err = 0;
1956 if (!skb_queue_empty(&sk->sk_receive_queue))
1957 break;
1958 err = -EINVAL;
1959 if (sock->state != SS_LISTENING)
1960 break;
1961 err = sock_intr_errno(timeo);
1962 if (signal_pending(current))
1963 break;
1964 err = -EAGAIN;
1965 if (!timeo)
1966 break;
1967 }
1968 finish_wait(sk_sleep(sk), &wait);
1969 return err;
1970}
1971
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001972/**
Ying Xue247f0f32014-02-18 16:06:46 +08001973 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974 * @sock: listening socket
1975 * @newsock: new socket that is to be connected
1976 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001977 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978 * Returns 0 on success, errno otherwise
1979 */
Ying Xue247f0f32014-02-18 16:06:46 +08001980static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001982 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001984 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001985 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001986 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001987 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988
Allan Stephens0c3141e2008-04-15 00:22:02 -07001989 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001990
Allan Stephens0c3141e2008-04-15 00:22:02 -07001991 if (sock->state != SS_LISTENING) {
1992 res = -EINVAL;
1993 goto exit;
1994 }
Ying Xue6398e232014-01-17 09:50:04 +08001995 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1996 res = tipc_wait_for_accept(sock, timeo);
1997 if (res)
1998 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001999
2000 buf = skb_peek(&sk->sk_receive_queue);
2001
Ying Xuec5fa7b32013-06-17 10:54:39 -04002002 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002003 if (res)
2004 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002005
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002006 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002007 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002008 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002009
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002010 /* we lock on new_sk; but lockdep sees the lock on sk */
2011 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002012
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002013 /*
2014 * Reject any stray messages received by new socket
2015 * before the socket lock was taken (very, very unlikely)
2016 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002017 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002018
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002019 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002020 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002021 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002023 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002024 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002025 new_tsock->conn_type = msg_nametype(msg);
2026 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002028
2029 /*
2030 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2031 * Respond to 'SYN+' by queuing it on new socket.
2032 */
2033 if (!msg_data_sz(msg)) {
2034 struct msghdr m = {NULL,};
2035
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002036 tsk_advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08002037 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002038 } else {
2039 __skb_dequeue(&sk->sk_receive_queue);
2040 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002041 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002042 }
2043 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002045 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002046 return res;
2047}
2048
2049/**
Ying Xue247f0f32014-02-18 16:06:46 +08002050 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002052 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053 *
2054 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002055 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002056 * Returns 0 on success, errno otherwise
2057 */
Ying Xue247f0f32014-02-18 16:06:46 +08002058static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002059{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002060 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002061 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002062 struct sk_buff *buf;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002063 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002064 int res;
2065
Allan Stephense247a8f2008-03-06 15:05:38 -08002066 if (how != SHUT_RDWR)
2067 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068
Allan Stephens0c3141e2008-04-15 00:22:02 -07002069 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002070
2071 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002072 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073 case SS_CONNECTED:
2074
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002076 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07002077 buf = __skb_dequeue(&sk->sk_receive_queue);
2078 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02002079 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002080 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081 goto restart;
2082 }
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002083 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002084 tipc_link_xmit(buf, dnode, tsk->ref);
2085 tipc_node_remove_conn(dnode, tsk->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002086 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002087 dnode = tsk_peer_node(tsk);
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002088 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
2089 TIPC_CONN_MSG, SHORT_H_SIZE,
2090 0, dnode, tipc_own_addr,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002091 tsk_peer_port(tsk),
2092 tsk->ref, TIPC_CONN_SHUTDOWN);
2093 tipc_link_xmit(buf, dnode, tsk->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002094 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002095 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002096 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002097 tipc_node_remove_conn(dnode, tsk->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002098 /* fall through */
2099
2100 case SS_DISCONNECTING:
2101
Ying Xue75031152012-10-29 09:38:15 -04002102 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002103 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002104
2105 /* Wake up anyone sleeping in poll */
2106 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002107 res = 0;
2108 break;
2109
2110 default:
2111 res = -ENOTCONN;
2112 }
2113
Allan Stephens0c3141e2008-04-15 00:22:02 -07002114 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 return res;
2116}
2117
Jon Paul Maloy57289012014-08-22 18:09:09 -04002118static void tipc_sk_timeout(unsigned long ref)
2119{
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002120 struct tipc_sock *tsk;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002121 struct sock *sk;
2122 struct sk_buff *buf = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002123 u32 peer_port, peer_node;
2124
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002125 tsk = tipc_sk_get(ref);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04002126 if (!tsk)
Ying Xuecc086fc2014-08-28 10:02:41 +08002127 return;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002128
Ying Xuecc086fc2014-08-28 10:02:41 +08002129 sk = &tsk->sk;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002130 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002131 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002132 bh_unlock_sock(sk);
2133 goto exit;
2134 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002135 peer_port = tsk_peer_port(tsk);
2136 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002137
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002138 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002139 /* Previous probe not answered -> self abort */
2140 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2141 SHORT_H_SIZE, 0, tipc_own_addr,
2142 peer_node, ref, peer_port,
2143 TIPC_ERR_NO_PORT);
2144 } else {
2145 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2146 0, peer_node, tipc_own_addr,
2147 peer_port, ref, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002148 tsk->probing_state = TIPC_CONN_PROBING;
2149 k_start_timer(&tsk->timer, tsk->probing_interval);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002150 }
2151 bh_unlock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002152 if (buf)
2153 tipc_link_xmit(buf, peer_node, ref);
2154exit:
2155 tipc_sk_put(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002156}
2157
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002158static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002159 struct tipc_name_seq const *seq)
2160{
2161 struct publication *publ;
2162 u32 key;
2163
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002164 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002165 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002166 key = tsk->ref + tsk->pub_count + 1;
2167 if (key == tsk->ref)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002168 return -EADDRINUSE;
2169
2170 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002171 scope, tsk->ref, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002172 if (unlikely(!publ))
2173 return -EINVAL;
2174
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002175 list_add(&publ->pport_list, &tsk->publications);
2176 tsk->pub_count++;
2177 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002178 return 0;
2179}
2180
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002181static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002182 struct tipc_name_seq const *seq)
2183{
2184 struct publication *publ;
2185 struct publication *safe;
2186 int rc = -EINVAL;
2187
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002188 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002189 if (seq) {
2190 if (publ->scope != scope)
2191 continue;
2192 if (publ->type != seq->type)
2193 continue;
2194 if (publ->lower != seq->lower)
2195 continue;
2196 if (publ->upper != seq->upper)
2197 break;
2198 tipc_nametbl_withdraw(publ->type, publ->lower,
2199 publ->ref, publ->key);
2200 rc = 0;
2201 break;
2202 }
2203 tipc_nametbl_withdraw(publ->type, publ->lower,
2204 publ->ref, publ->key);
2205 rc = 0;
2206 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002207 if (list_empty(&tsk->publications))
2208 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002209 return rc;
2210}
2211
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002212static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002213 int len, int full_id)
2214{
2215 struct publication *publ;
2216 int ret;
2217
2218 if (full_id)
2219 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2220 tipc_zone(tipc_own_addr),
2221 tipc_cluster(tipc_own_addr),
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002222 tipc_node(tipc_own_addr), tsk->ref);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002223 else
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002224 ret = tipc_snprintf(buf, len, "%-10u:", tsk->ref);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002225
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002226 if (tsk->connected) {
2227 u32 dport = tsk_peer_port(tsk);
2228 u32 destnode = tsk_peer_node(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002229
2230 ret += tipc_snprintf(buf + ret, len - ret,
2231 " connected to <%u.%u.%u:%u>",
2232 tipc_zone(destnode),
2233 tipc_cluster(destnode),
2234 tipc_node(destnode), dport);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002235 if (tsk->conn_type != 0)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002236 ret += tipc_snprintf(buf + ret, len - ret,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002237 " via {%u,%u}", tsk->conn_type,
2238 tsk->conn_instance);
2239 } else if (tsk->published) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002240 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002241 list_for_each_entry(publ, &tsk->publications, pport_list) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002242 if (publ->lower == publ->upper)
2243 ret += tipc_snprintf(buf + ret, len - ret,
2244 " {%u,%u}", publ->type,
2245 publ->lower);
2246 else
2247 ret += tipc_snprintf(buf + ret, len - ret,
2248 " {%u,%u,%u}", publ->type,
2249 publ->lower, publ->upper);
2250 }
2251 }
2252 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2253 return ret;
2254}
2255
2256struct sk_buff *tipc_sk_socks_show(void)
2257{
2258 struct sk_buff *buf;
2259 struct tlv_desc *rep_tlv;
2260 char *pb;
2261 int pb_len;
2262 struct tipc_sock *tsk;
2263 int str_len = 0;
2264 u32 ref = 0;
2265
2266 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2267 if (!buf)
2268 return NULL;
2269 rep_tlv = (struct tlv_desc *)buf->data;
2270 pb = TLV_DATA(rep_tlv);
2271 pb_len = ULTRA_STRING_MAX_LEN;
2272
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002273 tsk = tipc_sk_get_next(&ref);
2274 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2275 lock_sock(&tsk->sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002276 str_len += tipc_sk_show(tsk, pb + str_len,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002277 pb_len - str_len, 0);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002278 release_sock(&tsk->sk);
2279 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002280 }
2281 str_len += 1; /* for "\0" */
2282 skb_put(buf, TLV_SPACE(str_len));
2283 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2284
2285 return buf;
2286}
2287
2288/* tipc_sk_reinit: set non-zero address in all existing sockets
2289 * when we go from standalone to network mode.
2290 */
2291void tipc_sk_reinit(void)
2292{
2293 struct tipc_msg *msg;
2294 u32 ref = 0;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002295 struct tipc_sock *tsk = tipc_sk_get_next(&ref);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002296
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002297 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2298 lock_sock(&tsk->sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002299 msg = &tsk->phdr;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002300 msg_set_prevnode(msg, tipc_own_addr);
2301 msg_set_orignode(msg, tipc_own_addr);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002302 release_sock(&tsk->sk);
2303 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002304 }
2305}
2306
Per Lidenb97bf3f2006-01-02 19:04:38 +01002307/**
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002308 * struct reference - TIPC socket reference entry
2309 * @tsk: pointer to socket associated with reference entry
2310 * @ref: reference value for socket (combines instance & array index info)
2311 */
2312struct reference {
2313 struct tipc_sock *tsk;
2314 u32 ref;
2315};
2316
2317/**
2318 * struct tipc_ref_table - table of TIPC socket reference entries
2319 * @entries: pointer to array of reference entries
2320 * @capacity: array index of first unusable entry
2321 * @init_point: array index of first uninitialized entry
2322 * @first_free: array index of first unused socket reference entry
2323 * @last_free: array index of last unused socket reference entry
2324 * @index_mask: bitmask for array index portion of reference values
2325 * @start_mask: initial value for instance value portion of reference values
2326 */
2327struct ref_table {
2328 struct reference *entries;
2329 u32 capacity;
2330 u32 init_point;
2331 u32 first_free;
2332 u32 last_free;
2333 u32 index_mask;
2334 u32 start_mask;
2335};
2336
2337/* Socket reference table consists of 2**N entries.
2338 *
2339 * State Socket ptr Reference
2340 * ----- ---------- ---------
2341 * In use non-NULL XXXX|own index
2342 * (XXXX changes each time entry is acquired)
2343 * Free NULL YYYY|next free index
2344 * (YYYY is one more than last used XXXX)
2345 * Uninitialized NULL 0
2346 *
2347 * Entry 0 is not used; this allows index 0 to denote the end of the free list.
2348 *
2349 * Note that a reference value of 0 does not necessarily indicate that an
2350 * entry is uninitialized, since the last entry in the free list could also
2351 * have a reference value of 0 (although this is unlikely).
2352 */
2353
2354static struct ref_table tipc_ref_table;
2355
2356static DEFINE_RWLOCK(ref_table_lock);
2357
2358/**
2359 * tipc_ref_table_init - create reference table for sockets
2360 */
2361int tipc_sk_ref_table_init(u32 req_sz, u32 start)
2362{
2363 struct reference *table;
2364 u32 actual_sz;
2365
2366 /* account for unused entry, then round up size to a power of 2 */
2367
2368 req_sz++;
2369 for (actual_sz = 16; actual_sz < req_sz; actual_sz <<= 1) {
2370 /* do nothing */
2371 };
2372
2373 /* allocate table & mark all entries as uninitialized */
2374 table = vzalloc(actual_sz * sizeof(struct reference));
2375 if (table == NULL)
2376 return -ENOMEM;
2377
2378 tipc_ref_table.entries = table;
2379 tipc_ref_table.capacity = req_sz;
2380 tipc_ref_table.init_point = 1;
2381 tipc_ref_table.first_free = 0;
2382 tipc_ref_table.last_free = 0;
2383 tipc_ref_table.index_mask = actual_sz - 1;
2384 tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
2385
2386 return 0;
2387}
2388
2389/**
2390 * tipc_ref_table_stop - destroy reference table for sockets
2391 */
2392void tipc_sk_ref_table_stop(void)
2393{
2394 if (!tipc_ref_table.entries)
2395 return;
2396 vfree(tipc_ref_table.entries);
2397 tipc_ref_table.entries = NULL;
2398}
2399
2400/* tipc_ref_acquire - create reference to a socket
2401 *
2402 * Register an socket pointer in the reference table.
2403 * Returns a unique reference value that is used from then on to retrieve the
2404 * socket pointer, or to determine if the socket has been deregistered.
2405 */
2406u32 tipc_sk_ref_acquire(struct tipc_sock *tsk)
2407{
2408 u32 index;
2409 u32 index_mask;
2410 u32 next_plus_upper;
2411 u32 ref = 0;
2412 struct reference *entry;
2413
2414 if (unlikely(!tsk)) {
2415 pr_err("Attempt to acquire ref. to non-existent obj\n");
2416 return 0;
2417 }
2418 if (unlikely(!tipc_ref_table.entries)) {
2419 pr_err("Ref. table not found in acquisition attempt\n");
2420 return 0;
2421 }
2422
2423 /* Take a free entry, if available; otherwise initialize a new one */
2424 write_lock_bh(&ref_table_lock);
2425 index = tipc_ref_table.first_free;
2426 entry = &tipc_ref_table.entries[index];
2427
2428 if (likely(index)) {
2429 index = tipc_ref_table.first_free;
2430 entry = &tipc_ref_table.entries[index];
2431 index_mask = tipc_ref_table.index_mask;
2432 next_plus_upper = entry->ref;
2433 tipc_ref_table.first_free = next_plus_upper & index_mask;
2434 ref = (next_plus_upper & ~index_mask) + index;
2435 entry->tsk = tsk;
2436 } else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
2437 index = tipc_ref_table.init_point++;
2438 entry = &tipc_ref_table.entries[index];
2439 ref = tipc_ref_table.start_mask + index;
2440 }
2441
2442 if (ref) {
2443 entry->ref = ref;
2444 entry->tsk = tsk;
2445 }
2446 write_unlock_bh(&ref_table_lock);
2447 return ref;
2448}
2449
2450/* tipc_sk_ref_discard - invalidate reference to an socket
2451 *
2452 * Disallow future references to an socket and free up the entry for re-use.
2453 */
2454void tipc_sk_ref_discard(u32 ref)
2455{
2456 struct reference *entry;
2457 u32 index;
2458 u32 index_mask;
2459
2460 if (unlikely(!tipc_ref_table.entries)) {
2461 pr_err("Ref. table not found during discard attempt\n");
2462 return;
2463 }
2464
2465 index_mask = tipc_ref_table.index_mask;
2466 index = ref & index_mask;
2467 entry = &tipc_ref_table.entries[index];
2468
2469 write_lock_bh(&ref_table_lock);
2470
2471 if (unlikely(!entry->tsk)) {
2472 pr_err("Attempt to discard ref. to non-existent socket\n");
2473 goto exit;
2474 }
2475 if (unlikely(entry->ref != ref)) {
2476 pr_err("Attempt to discard non-existent reference\n");
2477 goto exit;
2478 }
2479
2480 /* Mark entry as unused; increment instance part of entry's
2481 * reference to invalidate any subsequent references
2482 */
2483
2484 entry->tsk = NULL;
2485 entry->ref = (ref & ~index_mask) + (index_mask + 1);
2486
2487 /* Append entry to free entry list */
2488 if (unlikely(tipc_ref_table.first_free == 0))
2489 tipc_ref_table.first_free = index;
2490 else
2491 tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index;
2492 tipc_ref_table.last_free = index;
2493exit:
2494 write_unlock_bh(&ref_table_lock);
2495}
2496
2497/* tipc_sk_get - find referenced socket and return pointer to it
2498 */
2499struct tipc_sock *tipc_sk_get(u32 ref)
2500{
2501 struct reference *entry;
2502 struct tipc_sock *tsk;
2503
2504 if (unlikely(!tipc_ref_table.entries))
2505 return NULL;
2506 read_lock_bh(&ref_table_lock);
2507 entry = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
2508 tsk = entry->tsk;
2509 if (likely(tsk && (entry->ref == ref)))
2510 sock_hold(&tsk->sk);
2511 else
2512 tsk = NULL;
2513 read_unlock_bh(&ref_table_lock);
2514 return tsk;
2515}
2516
2517/* tipc_sk_get_next - lock & return next socket after referenced one
2518*/
2519struct tipc_sock *tipc_sk_get_next(u32 *ref)
2520{
2521 struct reference *entry;
2522 struct tipc_sock *tsk = NULL;
2523 uint index = *ref & tipc_ref_table.index_mask;
2524
2525 read_lock_bh(&ref_table_lock);
2526 while (++index < tipc_ref_table.capacity) {
2527 entry = &tipc_ref_table.entries[index];
2528 if (!entry->tsk)
2529 continue;
2530 tsk = entry->tsk;
2531 sock_hold(&tsk->sk);
2532 *ref = entry->ref;
2533 break;
2534 }
2535 read_unlock_bh(&ref_table_lock);
2536 return tsk;
2537}
2538
2539static void tipc_sk_put(struct tipc_sock *tsk)
2540{
2541 sock_put(&tsk->sk);
2542}
2543
2544/**
Ying Xue247f0f32014-02-18 16:06:46 +08002545 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002546 * @sock: socket structure
2547 * @lvl: option level
2548 * @opt: option identifier
2549 * @ov: pointer to new option value
2550 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002551 *
2552 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002553 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002554 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002555 * Returns 0 on success, errno otherwise
2556 */
Ying Xue247f0f32014-02-18 16:06:46 +08002557static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2558 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002559{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002560 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002561 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002562 u32 value;
2563 int res;
2564
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002565 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2566 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002567 if (lvl != SOL_TIPC)
2568 return -ENOPROTOOPT;
2569 if (ol < sizeof(value))
2570 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002571 res = get_user(value, (u32 __user *)ov);
2572 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002573 return res;
2574
Allan Stephens0c3141e2008-04-15 00:22:02 -07002575 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002576
Per Lidenb97bf3f2006-01-02 19:04:38 +01002577 switch (opt) {
2578 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002579 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002580 break;
2581 case TIPC_SRC_DROPPABLE:
2582 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002583 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002584 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002585 res = -ENOPROTOOPT;
2586 break;
2587 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002588 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002589 break;
2590 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002591 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002592 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002593 break;
2594 default:
2595 res = -EINVAL;
2596 }
2597
Allan Stephens0c3141e2008-04-15 00:22:02 -07002598 release_sock(sk);
2599
Per Lidenb97bf3f2006-01-02 19:04:38 +01002600 return res;
2601}
2602
2603/**
Ying Xue247f0f32014-02-18 16:06:46 +08002604 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002605 * @sock: socket structure
2606 * @lvl: option level
2607 * @opt: option identifier
2608 * @ov: receptacle for option value
2609 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002610 *
2611 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002612 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002613 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002614 * Returns 0 on success, errno otherwise
2615 */
Ying Xue247f0f32014-02-18 16:06:46 +08002616static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2617 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002618{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002619 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002620 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002621 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002622 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002623 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002624
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002625 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2626 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002627 if (lvl != SOL_TIPC)
2628 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002629 res = get_user(len, ol);
2630 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002631 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002632
Allan Stephens0c3141e2008-04-15 00:22:02 -07002633 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002634
2635 switch (opt) {
2636 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002637 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002638 break;
2639 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002640 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002641 break;
2642 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002643 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002644 break;
2645 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002646 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002647 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002648 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002649 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002650 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002651 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002652 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002653 value = skb_queue_len(&sk->sk_receive_queue);
2654 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002655 default:
2656 res = -EINVAL;
2657 }
2658
Allan Stephens0c3141e2008-04-15 00:22:02 -07002659 release_sock(sk);
2660
Paul Gortmaker25860c32010-12-31 18:59:31 +00002661 if (res)
2662 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002663
Paul Gortmaker25860c32010-12-31 18:59:31 +00002664 if (len < sizeof(value))
2665 return -EINVAL;
2666
2667 if (copy_to_user(ov, &value, sizeof(value)))
2668 return -EFAULT;
2669
2670 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002671}
2672
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002673static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002674{
2675 struct tipc_sioc_ln_req lnr;
2676 void __user *argp = (void __user *)arg;
2677
2678 switch (cmd) {
2679 case SIOCGETLINKNAME:
2680 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2681 return -EFAULT;
Ying Xue7b8613e2014-10-20 14:44:25 +08002682 if (!tipc_node_get_linkname(lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002683 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2684 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2685 return -EFAULT;
2686 return 0;
2687 }
2688 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002689 default:
2690 return -ENOIOCTLCMD;
2691 }
2692}
2693
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002694/* Protocol switches for the various types of TIPC sockets */
2695
Florian Westphalbca65ea2008-02-07 18:18:01 -08002696static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002697 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002698 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002699 .release = tipc_release,
2700 .bind = tipc_bind,
2701 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002702 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002703 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002704 .getname = tipc_getname,
2705 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002706 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002707 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002708 .shutdown = tipc_shutdown,
2709 .setsockopt = tipc_setsockopt,
2710 .getsockopt = tipc_getsockopt,
2711 .sendmsg = tipc_sendmsg,
2712 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002713 .mmap = sock_no_mmap,
2714 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002715};
2716
Florian Westphalbca65ea2008-02-07 18:18:01 -08002717static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002718 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002719 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002720 .release = tipc_release,
2721 .bind = tipc_bind,
2722 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002723 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002724 .accept = tipc_accept,
2725 .getname = tipc_getname,
2726 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002727 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002728 .listen = tipc_listen,
2729 .shutdown = tipc_shutdown,
2730 .setsockopt = tipc_setsockopt,
2731 .getsockopt = tipc_getsockopt,
2732 .sendmsg = tipc_send_packet,
2733 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002734 .mmap = sock_no_mmap,
2735 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002736};
2737
Florian Westphalbca65ea2008-02-07 18:18:01 -08002738static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002739 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002740 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002741 .release = tipc_release,
2742 .bind = tipc_bind,
2743 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002744 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002745 .accept = tipc_accept,
2746 .getname = tipc_getname,
2747 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002748 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002749 .listen = tipc_listen,
2750 .shutdown = tipc_shutdown,
2751 .setsockopt = tipc_setsockopt,
2752 .getsockopt = tipc_getsockopt,
2753 .sendmsg = tipc_send_stream,
2754 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002755 .mmap = sock_no_mmap,
2756 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002757};
2758
Florian Westphalbca65ea2008-02-07 18:18:01 -08002759static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002760 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002761 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002762 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002763};
2764
2765static struct proto tipc_proto = {
2766 .name = "TIPC",
2767 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002768 .obj_size = sizeof(struct tipc_sock),
2769 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002770};
2771
Ying Xuec5fa7b32013-06-17 10:54:39 -04002772static struct proto tipc_proto_kern = {
2773 .name = "TIPC",
2774 .obj_size = sizeof(struct tipc_sock),
2775 .sysctl_rmem = sysctl_tipc_rmem
2776};
2777
Per Lidenb97bf3f2006-01-02 19:04:38 +01002778/**
Per Liden4323add2006-01-18 00:38:21 +01002779 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002780 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002781 * Returns 0 on success, errno otherwise
2782 */
Per Liden4323add2006-01-18 00:38:21 +01002783int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002784{
2785 int res;
2786
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002787 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002788 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002789 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002790 goto out;
2791 }
2792
2793 res = sock_register(&tipc_family_ops);
2794 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002795 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002796 proto_unregister(&tipc_proto);
2797 goto out;
2798 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002799 out:
2800 return res;
2801}
2802
2803/**
Per Liden4323add2006-01-18 00:38:21 +01002804 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002805 */
Per Liden4323add2006-01-18 00:38:21 +01002806void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002807{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002808 sock_unregister(tipc_family_ops.family);
2809 proto_unregister(&tipc_proto);
2810}
Richard Alpe34b78a122014-11-20 10:29:10 +01002811
2812/* Caller should hold socket lock for the passed tipc socket. */
2813int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2814{
2815 u32 peer_node;
2816 u32 peer_port;
2817 struct nlattr *nest;
2818
2819 peer_node = tsk_peer_node(tsk);
2820 peer_port = tsk_peer_port(tsk);
2821
2822 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2823
2824 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2825 goto msg_full;
2826 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2827 goto msg_full;
2828
2829 if (tsk->conn_type != 0) {
2830 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2831 goto msg_full;
2832 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2833 goto msg_full;
2834 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2835 goto msg_full;
2836 }
2837 nla_nest_end(skb, nest);
2838
2839 return 0;
2840
2841msg_full:
2842 nla_nest_cancel(skb, nest);
2843
2844 return -EMSGSIZE;
2845}
2846
2847/* Caller should hold socket lock for the passed tipc socket. */
2848int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2849 struct tipc_sock *tsk)
2850{
2851 int err;
2852 void *hdr;
2853 struct nlattr *attrs;
2854
2855 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2856 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2857 if (!hdr)
2858 goto msg_cancel;
2859
2860 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2861 if (!attrs)
2862 goto genlmsg_cancel;
2863 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->ref))
2864 goto attr_msg_cancel;
2865 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr))
2866 goto attr_msg_cancel;
2867
2868 if (tsk->connected) {
2869 err = __tipc_nl_add_sk_con(skb, tsk);
2870 if (err)
2871 goto attr_msg_cancel;
2872 } else if (!list_empty(&tsk->publications)) {
2873 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2874 goto attr_msg_cancel;
2875 }
2876 nla_nest_end(skb, attrs);
2877 genlmsg_end(skb, hdr);
2878
2879 return 0;
2880
2881attr_msg_cancel:
2882 nla_nest_cancel(skb, attrs);
2883genlmsg_cancel:
2884 genlmsg_cancel(skb, hdr);
2885msg_cancel:
2886 return -EMSGSIZE;
2887}
2888
2889int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2890{
2891 int err;
2892 struct tipc_sock *tsk;
2893 u32 prev_ref = cb->args[0];
2894 u32 ref = prev_ref;
2895
2896 tsk = tipc_sk_get_next(&ref);
2897 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2898 lock_sock(&tsk->sk);
2899 err = __tipc_nl_add_sk(skb, cb, tsk);
2900 release_sock(&tsk->sk);
2901 tipc_sk_put(tsk);
2902 if (err)
2903 break;
2904
2905 prev_ref = ref;
2906 }
2907
2908 cb->args[0] = prev_ref;
2909
2910 return skb->len;
2911}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002912
2913/* Caller should hold socket lock for the passed tipc socket. */
2914int __tipc_nl_add_sk_publ(struct sk_buff *skb, struct netlink_callback *cb,
2915 struct publication *publ)
2916{
2917 void *hdr;
2918 struct nlattr *attrs;
2919
2920 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2921 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2922 if (!hdr)
2923 goto msg_cancel;
2924
2925 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2926 if (!attrs)
2927 goto genlmsg_cancel;
2928
2929 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2930 goto attr_msg_cancel;
2931 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2932 goto attr_msg_cancel;
2933 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2934 goto attr_msg_cancel;
2935 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2936 goto attr_msg_cancel;
2937
2938 nla_nest_end(skb, attrs);
2939 genlmsg_end(skb, hdr);
2940
2941 return 0;
2942
2943attr_msg_cancel:
2944 nla_nest_cancel(skb, attrs);
2945genlmsg_cancel:
2946 genlmsg_cancel(skb, hdr);
2947msg_cancel:
2948 return -EMSGSIZE;
2949}
2950
2951/* Caller should hold socket lock for the passed tipc socket. */
2952int __tipc_nl_list_sk_publ(struct sk_buff *skb, struct netlink_callback *cb,
2953 struct tipc_sock *tsk, u32 *last_publ)
2954{
2955 int err;
2956 struct publication *p;
2957
2958 if (*last_publ) {
2959 list_for_each_entry(p, &tsk->publications, pport_list) {
2960 if (p->key == *last_publ)
2961 break;
2962 }
2963 if (p->key != *last_publ) {
2964 /* We never set seq or call nl_dump_check_consistent()
2965 * this means that setting prev_seq here will cause the
2966 * consistence check to fail in the netlink callback
2967 * handler. Resulting in the last NLMSG_DONE message
2968 * having the NLM_F_DUMP_INTR flag set.
2969 */
2970 cb->prev_seq = 1;
2971 *last_publ = 0;
2972 return -EPIPE;
2973 }
2974 } else {
2975 p = list_first_entry(&tsk->publications, struct publication,
2976 pport_list);
2977 }
2978
2979 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2980 err = __tipc_nl_add_sk_publ(skb, cb, p);
2981 if (err) {
2982 *last_publ = p->key;
2983 return err;
2984 }
2985 }
2986 *last_publ = 0;
2987
2988 return 0;
2989}
2990
2991int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2992{
2993 int err;
2994 u32 tsk_ref = cb->args[0];
2995 u32 last_publ = cb->args[1];
2996 u32 done = cb->args[2];
2997 struct tipc_sock *tsk;
2998
2999 if (!tsk_ref) {
3000 struct nlattr **attrs;
3001 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3002
3003 err = tipc_nlmsg_parse(cb->nlh, &attrs);
3004 if (err)
3005 return err;
3006
3007 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
3008 attrs[TIPC_NLA_SOCK],
3009 tipc_nl_sock_policy);
3010 if (err)
3011 return err;
3012
3013 if (!sock[TIPC_NLA_SOCK_REF])
3014 return -EINVAL;
3015
3016 tsk_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
3017 }
3018
3019 if (done)
3020 return 0;
3021
3022 tsk = tipc_sk_get(tsk_ref);
3023 if (!tsk)
3024 return -EINVAL;
3025
3026 lock_sock(&tsk->sk);
3027 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
3028 if (!err)
3029 done = 1;
3030 release_sock(&tsk->sk);
3031 tipc_sk_put(tsk);
3032
3033 cb->args[0] = tsk_ref;
3034 cb->args[1] = last_publ;
3035 cb->args[2] = done;
3036
3037 return skb->len;
3038}