rhashtable: Mark internal/private inline functions as such
[linux-2.6-block.git] / net / tipc / socket.c
CommitLineData
b97bf3fd 1/*
02c00c2a 2 * net/tipc/socket.c: TIPC socket API
c4307285 3 *
3c724acd 4 * Copyright (c) 2001-2007, 2012-2015, Ericsson AB
c5fa7b3c 5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
11 * 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.
b97bf3fd 19 *
9ea1fd3c
PL
20 * 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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
07f6c4bc 37#include <linux/rhashtable.h>
b97bf3fd 38#include "core.h"
e2dafe87 39#include "name_table.h"
78acb1f9 40#include "node.h"
e2dafe87 41#include "link.h"
c637c103 42#include "name_distr.h"
2e84c60b 43#include "socket.h"
2cf8aa19 44
07f6c4bc
YX
45#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
b97bf3fd 47
07f6c4bc 48#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
2f55c437 49#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
07f6c4bc
YX
50#define TIPC_FWD_MSG 1
51#define TIPC_CONN_OK 0
52#define TIPC_CONN_PROBING 1
53#define TIPC_MAX_PORT 0xffffffff
54#define TIPC_MIN_PORT 1
301bae56
JPM
55
56/**
57 * struct tipc_sock - TIPC socket structure
58 * @sk: socket - interacts with 'port' and with user via the socket API
59 * @connected: non-zero if port is currently connected to a peer port
60 * @conn_type: TIPC type used when connection was established
61 * @conn_instance: TIPC instance used when connection was established
62 * @published: non-zero if port has one or more associated names
63 * @max_pkt: maximum packet size "hint" used when building messages sent by port
07f6c4bc 64 * @portid: unique port identity in TIPC socket hash table
301bae56
JPM
65 * @phdr: preformatted message header used when sending messages
66 * @port_list: adjacent ports in TIPC's global list of ports
67 * @publications: list of publications for port
68 * @pub_count: total # of publications port has made during its lifetime
69 * @probing_state:
2f55c437 70 * @probing_intv:
301bae56
JPM
71 * @conn_timeout: the time we can wait for an unresponded setup request
72 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
73 * @link_cong: non-zero if owner must sleep because of link congestion
74 * @sent_unacked: # messages sent by socket, and not yet acked by peer
75 * @rcv_unacked: # messages read by user, but not yet acked back to peer
f2f8036e 76 * @remote: 'connected' peer for dgram/rdm
07f6c4bc
YX
77 * @node: hash table node
78 * @rcu: rcu struct for tipc_sock
301bae56
JPM
79 */
80struct tipc_sock {
81 struct sock sk;
82 int connected;
83 u32 conn_type;
84 u32 conn_instance;
85 int published;
86 u32 max_pkt;
07f6c4bc 87 u32 portid;
301bae56
JPM
88 struct tipc_msg phdr;
89 struct list_head sock_list;
90 struct list_head publications;
91 u32 pub_count;
92 u32 probing_state;
2f55c437 93 unsigned long probing_intv;
301bae56
JPM
94 uint conn_timeout;
95 atomic_t dupl_rcvcnt;
96 bool link_cong;
97 uint sent_unacked;
98 uint rcv_unacked;
f2f8036e 99 struct sockaddr_tipc remote;
07f6c4bc
YX
100 struct rhash_head node;
101 struct rcu_head rcu;
301bae56 102};
b97bf3fd 103
4f4482dc 104static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
676d2369 105static void tipc_data_ready(struct sock *sk);
f288bef4 106static void tipc_write_space(struct sock *sk);
247f0f3c
YX
107static int tipc_release(struct socket *sock);
108static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
0abd8ff2 109static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
f2f2a96a 110static void tipc_sk_timeout(unsigned long data);
301bae56 111static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
0fc87aae 112 struct tipc_name_seq const *seq);
301bae56 113static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
0fc87aae 114 struct tipc_name_seq const *seq);
e05b31f4 115static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
07f6c4bc
YX
116static int tipc_sk_insert(struct tipc_sock *tsk);
117static void tipc_sk_remove(struct tipc_sock *tsk);
39a0295f
YX
118static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
119 size_t dsz);
120static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
b97bf3fd 121
bca65eae
FW
122static const struct proto_ops packet_ops;
123static const struct proto_ops stream_ops;
124static const struct proto_ops msg_ops;
b97bf3fd
PL
125static struct proto tipc_proto;
126
1a1a143d
RA
127static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
128 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
129 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
130 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
131 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
132 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
133};
134
6cca7289
HX
135static const struct rhashtable_params tsk_rht_params;
136
c4307285 137/*
0c3141e9
AS
138 * Revised TIPC socket locking policy:
139 *
140 * Most socket operations take the standard socket lock when they start
141 * and hold it until they finish (or until they need to sleep). Acquiring
142 * this lock grants the owner exclusive access to the fields of the socket
143 * data structures, with the exception of the backlog queue. A few socket
144 * operations can be done without taking the socket lock because they only
145 * read socket information that never changes during the life of the socket.
146 *
147 * Socket operations may acquire the lock for the associated TIPC port if they
148 * need to perform an operation on the port. If any routine needs to acquire
149 * both the socket lock and the port lock it must take the socket lock first
150 * to avoid the risk of deadlock.
151 *
152 * The dispatcher handling incoming messages cannot grab the socket lock in
153 * the standard fashion, since invoked it runs at the BH level and cannot block.
154 * Instead, it checks to see if the socket lock is currently owned by someone,
155 * and either handles the message itself or adds it to the socket's backlog
156 * queue; in the latter case the queued message is processed once the process
157 * owning the socket lock releases it.
158 *
159 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
160 * the problem of a blocked socket operation preventing any other operations
161 * from occurring. However, applications must be careful if they have
162 * multiple threads trying to send (or receive) on the same socket, as these
163 * operations might interfere with each other. For example, doing a connect
164 * and a receive at the same time might allow the receive to consume the
165 * ACK message meant for the connect. While additional work could be done
166 * to try and overcome this, it doesn't seem to be worthwhile at the present.
167 *
168 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
169 * that another operation that must be performed in a non-blocking manner is
170 * not delayed for very long because the lock has already been taken.
171 *
172 * NOTE: This code assumes that certain fields of a port/socket pair are
173 * constant over its lifetime; such fields can be examined without taking
174 * the socket lock and/or port lock, and do not need to be re-read even
175 * after resuming processing after waiting. These fields include:
176 * - socket type
177 * - pointer to socket sk structure (aka tipc_sock structure)
178 * - pointer to port structure
179 * - port reference
180 */
181
c5898636
JPM
182static u32 tsk_own_node(struct tipc_sock *tsk)
183{
184 return msg_prevnode(&tsk->phdr);
185}
186
301bae56 187static u32 tsk_peer_node(struct tipc_sock *tsk)
2e84c60b 188{
301bae56 189 return msg_destnode(&tsk->phdr);
2e84c60b
JPM
190}
191
301bae56 192static u32 tsk_peer_port(struct tipc_sock *tsk)
2e84c60b 193{
301bae56 194 return msg_destport(&tsk->phdr);
2e84c60b
JPM
195}
196
301bae56 197static bool tsk_unreliable(struct tipc_sock *tsk)
2e84c60b 198{
301bae56 199 return msg_src_droppable(&tsk->phdr) != 0;
2e84c60b
JPM
200}
201
301bae56 202static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
2e84c60b 203{
301bae56 204 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
2e84c60b
JPM
205}
206
301bae56 207static bool tsk_unreturnable(struct tipc_sock *tsk)
2e84c60b 208{
301bae56 209 return msg_dest_droppable(&tsk->phdr) != 0;
2e84c60b
JPM
210}
211
301bae56 212static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
2e84c60b 213{
301bae56 214 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
2e84c60b
JPM
215}
216
301bae56 217static int tsk_importance(struct tipc_sock *tsk)
2e84c60b 218{
301bae56 219 return msg_importance(&tsk->phdr);
2e84c60b
JPM
220}
221
301bae56 222static int tsk_set_importance(struct tipc_sock *tsk, int imp)
2e84c60b
JPM
223{
224 if (imp > TIPC_CRITICAL_IMPORTANCE)
225 return -EINVAL;
301bae56 226 msg_set_importance(&tsk->phdr, (u32)imp);
2e84c60b
JPM
227 return 0;
228}
8826cde6 229
301bae56
JPM
230static struct tipc_sock *tipc_sk(const struct sock *sk)
231{
232 return container_of(sk, struct tipc_sock, sk);
233}
234
235static int tsk_conn_cong(struct tipc_sock *tsk)
236{
237 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
238}
239
0c3141e9 240/**
2e84c60b 241 * tsk_advance_rx_queue - discard first buffer in socket receive queue
0c3141e9
AS
242 *
243 * Caller must hold socket lock
b97bf3fd 244 */
2e84c60b 245static void tsk_advance_rx_queue(struct sock *sk)
b97bf3fd 246{
5f6d9123 247 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
b97bf3fd
PL
248}
249
b97bf3fd 250/**
2e84c60b 251 * tsk_rej_rx_queue - reject all buffers in socket receive queue
0c3141e9
AS
252 *
253 * Caller must hold socket lock
b97bf3fd 254 */
2e84c60b 255static void tsk_rej_rx_queue(struct sock *sk)
b97bf3fd 256{
a6ca1094 257 struct sk_buff *skb;
8db1bae3 258 u32 dnode;
c5898636 259 u32 own_node = tsk_own_node(tipc_sk(sk));
0c3141e9 260
a6ca1094 261 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) {
c5898636
JPM
262 if (tipc_msg_reverse(own_node, skb, &dnode, TIPC_ERR_NO_PORT))
263 tipc_link_xmit_skb(sock_net(sk), skb, dnode, 0);
8db1bae3 264 }
b97bf3fd
PL
265}
266
2e84c60b 267/* tsk_peer_msg - verify if message was sent by connected port's peer
0fc87aae
JPM
268 *
269 * Handles cases where the node's network address has changed from
270 * the default of <0.0.0> to its configured setting.
271 */
2e84c60b 272static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
0fc87aae 273{
34747539 274 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
301bae56 275 u32 peer_port = tsk_peer_port(tsk);
0fc87aae
JPM
276 u32 orig_node;
277 u32 peer_node;
278
301bae56 279 if (unlikely(!tsk->connected))
0fc87aae
JPM
280 return false;
281
282 if (unlikely(msg_origport(msg) != peer_port))
283 return false;
284
285 orig_node = msg_orignode(msg);
301bae56 286 peer_node = tsk_peer_node(tsk);
0fc87aae
JPM
287
288 if (likely(orig_node == peer_node))
289 return true;
290
34747539 291 if (!orig_node && (peer_node == tn->own_addr))
0fc87aae
JPM
292 return true;
293
34747539 294 if (!peer_node && (orig_node == tn->own_addr))
0fc87aae
JPM
295 return true;
296
297 return false;
298}
299
b97bf3fd 300/**
c5fa7b3c 301 * tipc_sk_create - create a TIPC socket
0c3141e9 302 * @net: network namespace (must be default network)
b97bf3fd
PL
303 * @sock: pre-allocated socket structure
304 * @protocol: protocol indicator (must be 0)
3f378b68 305 * @kern: caused by kernel or by userspace?
c4307285 306 *
0c3141e9
AS
307 * This routine creates additional data structures used by the TIPC socket,
308 * initializes them, and links them together.
b97bf3fd
PL
309 *
310 * Returns 0 on success, errno otherwise
311 */
58ed9442
JPM
312static int tipc_sk_create(struct net *net, struct socket *sock,
313 int protocol, int kern)
b97bf3fd 314{
c5898636 315 struct tipc_net *tn;
0c3141e9
AS
316 const struct proto_ops *ops;
317 socket_state state;
b97bf3fd 318 struct sock *sk;
58ed9442 319 struct tipc_sock *tsk;
5b8fa7ce 320 struct tipc_msg *msg;
0c3141e9
AS
321
322 /* Validate arguments */
b97bf3fd
PL
323 if (unlikely(protocol != 0))
324 return -EPROTONOSUPPORT;
325
b97bf3fd
PL
326 switch (sock->type) {
327 case SOCK_STREAM:
0c3141e9
AS
328 ops = &stream_ops;
329 state = SS_UNCONNECTED;
b97bf3fd
PL
330 break;
331 case SOCK_SEQPACKET:
0c3141e9
AS
332 ops = &packet_ops;
333 state = SS_UNCONNECTED;
b97bf3fd
PL
334 break;
335 case SOCK_DGRAM:
b97bf3fd 336 case SOCK_RDM:
0c3141e9
AS
337 ops = &msg_ops;
338 state = SS_READY;
b97bf3fd 339 break;
49978651 340 default:
49978651 341 return -EPROTOTYPE;
b97bf3fd
PL
342 }
343
0c3141e9 344 /* Allocate socket's protocol area */
76100a8a 345 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
0c3141e9 346 if (sk == NULL)
b97bf3fd 347 return -ENOMEM;
b97bf3fd 348
58ed9442 349 tsk = tipc_sk(sk);
301bae56 350 tsk->max_pkt = MAX_PKT_DEFAULT;
301bae56
JPM
351 INIT_LIST_HEAD(&tsk->publications);
352 msg = &tsk->phdr;
c5898636
JPM
353 tn = net_generic(sock_net(sk), tipc_net_id);
354 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
5b8fa7ce 355 NAMED_H_SIZE, 0);
b97bf3fd 356
0c3141e9 357 /* Finish initializing socket data structures */
0c3141e9
AS
358 sock->ops = ops;
359 sock->state = state;
0c3141e9 360 sock_init_data(sock, sk);
07f6c4bc
YX
361 if (tipc_sk_insert(tsk)) {
362 pr_warn("Socket create failed; port numbrer exhausted\n");
363 return -EINVAL;
364 }
365 msg_set_origport(msg, tsk->portid);
3721e9c7 366 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
4f4482dc 367 sk->sk_backlog_rcv = tipc_backlog_rcv;
cc79dd1b 368 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
f288bef4
YX
369 sk->sk_data_ready = tipc_data_ready;
370 sk->sk_write_space = tipc_write_space;
4f4482dc 371 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
60120526 372 tsk->sent_unacked = 0;
4f4482dc 373 atomic_set(&tsk->dupl_rcvcnt, 0);
7ef43eba 374
0c3141e9 375 if (sock->state == SS_READY) {
301bae56 376 tsk_set_unreturnable(tsk, true);
0c3141e9 377 if (sock->type == SOCK_DGRAM)
301bae56 378 tsk_set_unreliable(tsk, true);
0c3141e9 379 }
b97bf3fd
PL
380 return 0;
381}
382
07f6c4bc
YX
383static void tipc_sk_callback(struct rcu_head *head)
384{
385 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
386
387 sock_put(&tsk->sk);
388}
389
b97bf3fd 390/**
247f0f3c 391 * tipc_release - destroy a TIPC socket
b97bf3fd
PL
392 * @sock: socket to destroy
393 *
394 * This routine cleans up any messages that are still queued on the socket.
395 * For DGRAM and RDM socket types, all queued messages are rejected.
396 * For SEQPACKET and STREAM socket types, the first message is rejected
397 * and any others are discarded. (If the first message on a STREAM socket
398 * is partially-read, it is discarded and the next one is rejected instead.)
c4307285 399 *
b97bf3fd
PL
400 * NOTE: Rejected messages are not necessarily returned to the sender! They
401 * are returned or discarded according to the "destination droppable" setting
402 * specified for the message by the sender.
403 *
404 * Returns 0 on success, errno otherwise
405 */
247f0f3c 406static int tipc_release(struct socket *sock)
b97bf3fd 407{
b97bf3fd 408 struct sock *sk = sock->sk;
357c4774 409 struct net *net;
58ed9442 410 struct tipc_sock *tsk;
a6ca1094 411 struct sk_buff *skb;
f2f2a96a 412 u32 dnode, probing_state;
b97bf3fd 413
0c3141e9
AS
414 /*
415 * Exit if socket isn't fully initialized (occurs when a failed accept()
416 * releases a pre-allocated child socket that was never used)
417 */
0c3141e9 418 if (sk == NULL)
b97bf3fd 419 return 0;
c4307285 420
357c4774 421 net = sock_net(sk);
58ed9442 422 tsk = tipc_sk(sk);
0c3141e9
AS
423 lock_sock(sk);
424
425 /*
426 * Reject all unreceived messages, except on an active connection
427 * (which disconnects locally & sends a 'FIN+' to peer)
428 */
301bae56 429 dnode = tsk_peer_node(tsk);
b97bf3fd 430 while (sock->state != SS_DISCONNECTING) {
a6ca1094
YX
431 skb = __skb_dequeue(&sk->sk_receive_queue);
432 if (skb == NULL)
b97bf3fd 433 break;
a6ca1094
YX
434 if (TIPC_SKB_CB(skb)->handle != NULL)
435 kfree_skb(skb);
0c3141e9
AS
436 else {
437 if ((sock->state == SS_CONNECTING) ||
438 (sock->state == SS_CONNECTED)) {
439 sock->state = SS_DISCONNECTING;
301bae56 440 tsk->connected = 0;
f2f9800d 441 tipc_node_remove_conn(net, dnode, tsk->portid);
0c3141e9 442 }
c5898636 443 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
34747539 444 TIPC_ERR_NO_PORT))
f2f9800d 445 tipc_link_xmit_skb(net, skb, dnode, 0);
0c3141e9 446 }
b97bf3fd
PL
447 }
448
301bae56 449 tipc_sk_withdraw(tsk, 0, NULL);
f2f2a96a 450 probing_state = tsk->probing_state;
3721e9c7
YX
451 if (del_timer_sync(&sk->sk_timer) &&
452 probing_state != TIPC_CONN_PROBING)
f2f2a96a 453 sock_put(sk);
07f6c4bc 454 tipc_sk_remove(tsk);
301bae56 455 if (tsk->connected) {
c5898636 456 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
34747539 457 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
c5898636 458 tsk_own_node(tsk), tsk_peer_port(tsk),
07f6c4bc 459 tsk->portid, TIPC_ERR_NO_PORT);
a6ca1094 460 if (skb)
f2f9800d
YX
461 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
462 tipc_node_remove_conn(net, dnode, tsk->portid);
5b8fa7ce 463 }
b97bf3fd 464
0c3141e9 465 /* Discard any remaining (connection-based) messages in receive queue */
57467e56 466 __skb_queue_purge(&sk->sk_receive_queue);
b97bf3fd 467
0c3141e9 468 /* Reject any messages that accumulated in backlog queue */
0c3141e9
AS
469 sock->state = SS_DISCONNECTING;
470 release_sock(sk);
07f6c4bc
YX
471
472 call_rcu(&tsk->rcu, tipc_sk_callback);
0c3141e9 473 sock->sk = NULL;
b97bf3fd 474
065d7e39 475 return 0;
b97bf3fd
PL
476}
477
478/**
247f0f3c 479 * tipc_bind - associate or disassocate TIPC name(s) with a socket
b97bf3fd
PL
480 * @sock: socket structure
481 * @uaddr: socket address describing name(s) and desired operation
482 * @uaddr_len: size of socket address data structure
c4307285 483 *
b97bf3fd
PL
484 * Name and name sequence binding is indicated using a positive scope value;
485 * a negative scope value unbinds the specified name. Specifying no name
486 * (i.e. a socket address length of 0) unbinds all names from the socket.
c4307285 487 *
b97bf3fd 488 * Returns 0 on success, errno otherwise
0c3141e9
AS
489 *
490 * NOTE: This routine doesn't need to take the socket lock since it doesn't
491 * access any non-constant socket information.
b97bf3fd 492 */
247f0f3c
YX
493static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
494 int uaddr_len)
b97bf3fd 495{
84602761 496 struct sock *sk = sock->sk;
b97bf3fd 497 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
58ed9442 498 struct tipc_sock *tsk = tipc_sk(sk);
84602761 499 int res = -EINVAL;
b97bf3fd 500
84602761
YX
501 lock_sock(sk);
502 if (unlikely(!uaddr_len)) {
301bae56 503 res = tipc_sk_withdraw(tsk, 0, NULL);
84602761
YX
504 goto exit;
505 }
c4307285 506
84602761
YX
507 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
508 res = -EINVAL;
509 goto exit;
510 }
511 if (addr->family != AF_TIPC) {
512 res = -EAFNOSUPPORT;
513 goto exit;
514 }
b97bf3fd 515
b97bf3fd
PL
516 if (addr->addrtype == TIPC_ADDR_NAME)
517 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
84602761
YX
518 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
519 res = -EAFNOSUPPORT;
520 goto exit;
521 }
c4307285 522
13a2e898 523 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
7d0ab17b 524 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
84602761
YX
525 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
526 res = -EACCES;
527 goto exit;
528 }
c422f1bd 529
84602761 530 res = (addr->scope > 0) ?
301bae56
JPM
531 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
532 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
84602761
YX
533exit:
534 release_sock(sk);
535 return res;
b97bf3fd
PL
536}
537
c4307285 538/**
247f0f3c 539 * tipc_getname - get port ID of socket or peer socket
b97bf3fd
PL
540 * @sock: socket structure
541 * @uaddr: area for returned socket address
542 * @uaddr_len: area for returned length of socket address
2da59918 543 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
c4307285 544 *
b97bf3fd 545 * Returns 0 on success, errno otherwise
0c3141e9 546 *
2da59918
AS
547 * NOTE: This routine doesn't need to take the socket lock since it only
548 * accesses socket information that is unchanging (or which changes in
0e65967e 549 * a completely predictable manner).
b97bf3fd 550 */
247f0f3c
YX
551static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
552 int *uaddr_len, int peer)
b97bf3fd 553{
b97bf3fd 554 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
58ed9442 555 struct tipc_sock *tsk = tipc_sk(sock->sk);
34747539 556 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
b97bf3fd 557
88f8a5e3 558 memset(addr, 0, sizeof(*addr));
0c3141e9 559 if (peer) {
2da59918
AS
560 if ((sock->state != SS_CONNECTED) &&
561 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
562 return -ENOTCONN;
301bae56
JPM
563 addr->addr.id.ref = tsk_peer_port(tsk);
564 addr->addr.id.node = tsk_peer_node(tsk);
0c3141e9 565 } else {
07f6c4bc 566 addr->addr.id.ref = tsk->portid;
34747539 567 addr->addr.id.node = tn->own_addr;
0c3141e9 568 }
b97bf3fd
PL
569
570 *uaddr_len = sizeof(*addr);
571 addr->addrtype = TIPC_ADDR_ID;
572 addr->family = AF_TIPC;
573 addr->scope = 0;
b97bf3fd
PL
574 addr->addr.name.domain = 0;
575
0c3141e9 576 return 0;
b97bf3fd
PL
577}
578
579/**
247f0f3c 580 * tipc_poll - read and possibly block on pollmask
b97bf3fd
PL
581 * @file: file structure associated with the socket
582 * @sock: socket for which to calculate the poll bits
583 * @wait: ???
584 *
9b674e82
AS
585 * Returns pollmask value
586 *
587 * COMMENTARY:
588 * It appears that the usual socket locking mechanisms are not useful here
589 * since the pollmask info is potentially out-of-date the moment this routine
590 * exits. TCP and other protocols seem to rely on higher level poll routines
591 * to handle any preventable race conditions, so TIPC will do the same ...
592 *
593 * TIPC sets the returned events as follows:
f662c070
AS
594 *
595 * socket state flags set
596 * ------------ ---------
597 * unconnected no read flags
c4fc298a 598 * POLLOUT if port is not congested
f662c070
AS
599 *
600 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
601 * no write flags
602 *
603 * connected POLLIN/POLLRDNORM if data in rx queue
604 * POLLOUT if port is not congested
605 *
606 * disconnecting POLLIN/POLLRDNORM/POLLHUP
607 * no write flags
608 *
609 * listening POLLIN if SYN in rx queue
610 * no write flags
611 *
612 * ready POLLIN/POLLRDNORM if data in rx queue
613 * [connectionless] POLLOUT (since port cannot be congested)
614 *
615 * IMPORTANT: The fact that a read or write operation is indicated does NOT
616 * imply that the operation will succeed, merely that it should be performed
617 * and will not block.
b97bf3fd 618 */
247f0f3c
YX
619static unsigned int tipc_poll(struct file *file, struct socket *sock,
620 poll_table *wait)
b97bf3fd 621{
9b674e82 622 struct sock *sk = sock->sk;
58ed9442 623 struct tipc_sock *tsk = tipc_sk(sk);
f662c070 624 u32 mask = 0;
9b674e82 625
f288bef4 626 sock_poll_wait(file, sk_sleep(sk), wait);
9b674e82 627
f662c070 628 switch ((int)sock->state) {
c4fc298a 629 case SS_UNCONNECTED:
60120526 630 if (!tsk->link_cong)
c4fc298a
EH
631 mask |= POLLOUT;
632 break;
f662c070
AS
633 case SS_READY:
634 case SS_CONNECTED:
301bae56 635 if (!tsk->link_cong && !tsk_conn_cong(tsk))
f662c070
AS
636 mask |= POLLOUT;
637 /* fall thru' */
638 case SS_CONNECTING:
639 case SS_LISTENING:
640 if (!skb_queue_empty(&sk->sk_receive_queue))
641 mask |= (POLLIN | POLLRDNORM);
642 break;
643 case SS_DISCONNECTING:
644 mask = (POLLIN | POLLRDNORM | POLLHUP);
645 break;
646 }
9b674e82
AS
647
648 return mask;
b97bf3fd
PL
649}
650
0abd8ff2
JPM
651/**
652 * tipc_sendmcast - send multicast message
653 * @sock: socket structure
654 * @seq: destination address
562640f3 655 * @msg: message to send
0abd8ff2
JPM
656 * @dsz: total length of message data
657 * @timeo: timeout to wait for wakeup
658 *
659 * Called from function tipc_sendmsg(), which has done all sanity checks
660 * Returns the number of bytes sent on success, or errno
661 */
662static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
562640f3 663 struct msghdr *msg, size_t dsz, long timeo)
0abd8ff2
JPM
664{
665 struct sock *sk = sock->sk;
c5898636 666 struct tipc_sock *tsk = tipc_sk(sk);
f2f9800d 667 struct net *net = sock_net(sk);
c5898636 668 struct tipc_msg *mhdr = &tsk->phdr;
94153e36 669 struct sk_buff_head *pktchain = &sk->sk_write_queue;
f25dcc76 670 struct iov_iter save = msg->msg_iter;
0abd8ff2
JPM
671 uint mtu;
672 int rc;
673
674 msg_set_type(mhdr, TIPC_MCAST_MSG);
675 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
676 msg_set_destport(mhdr, 0);
677 msg_set_destnode(mhdr, 0);
678 msg_set_nametype(mhdr, seq->type);
679 msg_set_namelower(mhdr, seq->lower);
680 msg_set_nameupper(mhdr, seq->upper);
681 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
682
683new_mtu:
684 mtu = tipc_bclink_get_mtu();
94153e36 685 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, pktchain);
0abd8ff2
JPM
686 if (unlikely(rc < 0))
687 return rc;
688
689 do {
94153e36 690 rc = tipc_bclink_xmit(net, pktchain);
0abd8ff2
JPM
691 if (likely(rc >= 0)) {
692 rc = dsz;
693 break;
694 }
f25dcc76
AV
695 if (rc == -EMSGSIZE) {
696 msg->msg_iter = save;
0abd8ff2 697 goto new_mtu;
f25dcc76 698 }
0abd8ff2
JPM
699 if (rc != -ELINKCONG)
700 break;
50100a5e 701 tipc_sk(sk)->link_cong = 1;
0abd8ff2
JPM
702 rc = tipc_wait_for_sndmsg(sock, &timeo);
703 if (rc)
94153e36 704 __skb_queue_purge(pktchain);
0abd8ff2
JPM
705 } while (!rc);
706 return rc;
707}
708
cb1b7280
JPM
709/**
710 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
711 * @arrvq: queue with arriving messages, to be cloned after destination lookup
712 * @inputq: queue with cloned messages, delivered to socket after dest lookup
713 *
714 * Multi-threaded: parallel calls with reference to same queues may occur
078bec82 715 */
cb1b7280
JPM
716void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
717 struct sk_buff_head *inputq)
078bec82 718{
cb1b7280 719 struct tipc_msg *msg;
3c724acd 720 struct tipc_plist dports;
3c724acd 721 u32 portid;
078bec82 722 u32 scope = TIPC_CLUSTER_SCOPE;
cb1b7280
JPM
723 struct sk_buff_head tmpq;
724 uint hsz;
725 struct sk_buff *skb, *_skb;
3c724acd 726
cb1b7280 727 __skb_queue_head_init(&tmpq);
3c724acd 728 tipc_plist_init(&dports);
078bec82 729
cb1b7280
JPM
730 skb = tipc_skb_peek(arrvq, &inputq->lock);
731 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
732 msg = buf_msg(skb);
733 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
734
735 if (in_own_node(net, msg_orignode(msg)))
736 scope = TIPC_NODE_SCOPE;
737
738 /* Create destination port list and message clones: */
739 tipc_nametbl_mc_translate(net,
740 msg_nametype(msg), msg_namelower(msg),
741 msg_nameupper(msg), scope, &dports);
742 portid = tipc_plist_pop(&dports);
743 for (; portid; portid = tipc_plist_pop(&dports)) {
744 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
745 if (_skb) {
746 msg_set_destport(buf_msg(_skb), portid);
747 __skb_queue_tail(&tmpq, _skb);
748 continue;
749 }
750 pr_warn("Failed to clone mcast rcv buffer\n");
078bec82 751 }
cb1b7280
JPM
752 /* Append to inputq if not already done by other thread */
753 spin_lock_bh(&inputq->lock);
754 if (skb_peek(arrvq) == skb) {
755 skb_queue_splice_tail_init(&tmpq, inputq);
756 kfree_skb(__skb_dequeue(arrvq));
757 }
758 spin_unlock_bh(&inputq->lock);
759 __skb_queue_purge(&tmpq);
760 kfree_skb(skb);
078bec82 761 }
cb1b7280 762 tipc_sk_rcv(net, inputq);
078bec82
JPM
763}
764
ac0074ee
JPM
765/**
766 * tipc_sk_proto_rcv - receive a connection mng protocol message
767 * @tsk: receiving socket
1186adf7 768 * @skb: pointer to message buffer. Set to NULL if buffer is consumed.
ac0074ee 769 */
1186adf7 770static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff **skb)
ac0074ee 771{
1186adf7 772 struct tipc_msg *msg = buf_msg(*skb);
60120526 773 int conn_cong;
1186adf7
JPM
774 u32 dnode;
775 u32 own_node = tsk_own_node(tsk);
ac0074ee 776 /* Ignore if connection cannot be validated: */
2e84c60b 777 if (!tsk_peer_msg(tsk, msg))
ac0074ee
JPM
778 goto exit;
779
301bae56 780 tsk->probing_state = TIPC_CONN_OK;
ac0074ee
JPM
781
782 if (msg_type(msg) == CONN_ACK) {
301bae56 783 conn_cong = tsk_conn_cong(tsk);
60120526
JPM
784 tsk->sent_unacked -= msg_msgcnt(msg);
785 if (conn_cong)
50100a5e 786 tsk->sk.sk_write_space(&tsk->sk);
ac0074ee 787 } else if (msg_type(msg) == CONN_PROBE) {
1186adf7
JPM
788 if (tipc_msg_reverse(own_node, *skb, &dnode, TIPC_OK)) {
789 msg_set_type(msg, CONN_PROBE_REPLY);
790 return;
791 }
ac0074ee
JPM
792 }
793 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
794exit:
1186adf7
JPM
795 kfree_skb(*skb);
796 *skb = NULL;
ac0074ee
JPM
797}
798
3f40504f
YX
799static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
800{
801 struct sock *sk = sock->sk;
58ed9442 802 struct tipc_sock *tsk = tipc_sk(sk);
3f40504f
YX
803 DEFINE_WAIT(wait);
804 int done;
805
806 do {
807 int err = sock_error(sk);
808 if (err)
809 return err;
810 if (sock->state == SS_DISCONNECTING)
811 return -EPIPE;
812 if (!*timeo_p)
813 return -EAGAIN;
814 if (signal_pending(current))
815 return sock_intr_errno(*timeo_p);
816
817 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
60120526 818 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
3f40504f
YX
819 finish_wait(sk_sleep(sk), &wait);
820 } while (!done);
821 return 0;
822}
823
b97bf3fd 824/**
247f0f3c 825 * tipc_sendmsg - send message in connectionless manner
b97bf3fd
PL
826 * @sock: socket structure
827 * @m: message to send
e2dafe87 828 * @dsz: amount of user data to be sent
c4307285 829 *
b97bf3fd 830 * Message must have an destination specified explicitly.
c4307285 831 * Used for SOCK_RDM and SOCK_DGRAM messages,
b97bf3fd
PL
832 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
833 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
c4307285 834 *
b97bf3fd
PL
835 * Returns the number of bytes sent on success, or errno otherwise
836 */
1b784140 837static int tipc_sendmsg(struct socket *sock,
e2dafe87 838 struct msghdr *m, size_t dsz)
39a0295f
YX
839{
840 struct sock *sk = sock->sk;
841 int ret;
842
843 lock_sock(sk);
844 ret = __tipc_sendmsg(sock, m, dsz);
845 release_sock(sk);
846
847 return ret;
848}
849
850static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
b97bf3fd 851{
e2dafe87 852 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
0c3141e9 853 struct sock *sk = sock->sk;
58ed9442 854 struct tipc_sock *tsk = tipc_sk(sk);
f2f9800d 855 struct net *net = sock_net(sk);
301bae56 856 struct tipc_msg *mhdr = &tsk->phdr;
e2dafe87 857 u32 dnode, dport;
94153e36 858 struct sk_buff_head *pktchain = &sk->sk_write_queue;
a6ca1094 859 struct sk_buff *skb;
f2f8036e 860 struct tipc_name_seq *seq;
f25dcc76 861 struct iov_iter save;
e2dafe87 862 u32 mtu;
3f40504f 863 long timeo;
88b17b6a 864 int rc;
b97bf3fd 865
e2dafe87 866 if (dsz > TIPC_MAX_USER_MSG_SIZE)
c29c3f70 867 return -EMSGSIZE;
f2f8036e
EH
868 if (unlikely(!dest)) {
869 if (tsk->connected && sock->state == SS_READY)
870 dest = &tsk->remote;
871 else
872 return -EDESTADDRREQ;
873 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
874 dest->family != AF_TIPC) {
875 return -EINVAL;
876 }
e2dafe87 877 if (unlikely(sock->state != SS_READY)) {
39a0295f
YX
878 if (sock->state == SS_LISTENING)
879 return -EPIPE;
880 if (sock->state != SS_UNCONNECTED)
881 return -EISCONN;
882 if (tsk->published)
883 return -EOPNOTSUPP;
3388007b 884 if (dest->addrtype == TIPC_ADDR_NAME) {
301bae56
JPM
885 tsk->conn_type = dest->addr.name.name.type;
886 tsk->conn_instance = dest->addr.name.name.instance;
3388007b 887 }
b97bf3fd 888 }
f2f8036e 889 seq = &dest->addr.nameseq;
3f40504f 890 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
e2dafe87
JPM
891
892 if (dest->addrtype == TIPC_ADDR_MCAST) {
39a0295f 893 return tipc_sendmcast(sock, seq, m, dsz, timeo);
e2dafe87
JPM
894 } else if (dest->addrtype == TIPC_ADDR_NAME) {
895 u32 type = dest->addr.name.name.type;
896 u32 inst = dest->addr.name.name.instance;
897 u32 domain = dest->addr.name.domain;
898
899 dnode = domain;
900 msg_set_type(mhdr, TIPC_NAMED_MSG);
901 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
902 msg_set_nametype(mhdr, type);
903 msg_set_nameinst(mhdr, inst);
904 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
4ac1c8d0 905 dport = tipc_nametbl_translate(net, type, inst, &dnode);
e2dafe87
JPM
906 msg_set_destnode(mhdr, dnode);
907 msg_set_destport(mhdr, dport);
39a0295f
YX
908 if (unlikely(!dport && !dnode))
909 return -EHOSTUNREACH;
e2dafe87
JPM
910 } else if (dest->addrtype == TIPC_ADDR_ID) {
911 dnode = dest->addr.id.node;
912 msg_set_type(mhdr, TIPC_DIRECT_MSG);
913 msg_set_lookup_scope(mhdr, 0);
914 msg_set_destnode(mhdr, dnode);
915 msg_set_destport(mhdr, dest->addr.id.ref);
916 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
917 }
918
f25dcc76 919 save = m->msg_iter;
e2dafe87 920new_mtu:
f2f9800d 921 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
94153e36 922 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, pktchain);
e2dafe87 923 if (rc < 0)
39a0295f 924 return rc;
e2dafe87
JPM
925
926 do {
94153e36 927 skb = skb_peek(pktchain);
a6ca1094 928 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
94153e36 929 rc = tipc_link_xmit(net, pktchain, dnode, tsk->portid);
e2dafe87
JPM
930 if (likely(rc >= 0)) {
931 if (sock->state != SS_READY)
0c3141e9 932 sock->state = SS_CONNECTING;
e2dafe87 933 rc = dsz;
0c3141e9 934 break;
c4307285 935 }
f25dcc76
AV
936 if (rc == -EMSGSIZE) {
937 m->msg_iter = save;
e2dafe87 938 goto new_mtu;
f25dcc76 939 }
e2dafe87 940 if (rc != -ELINKCONG)
0c3141e9 941 break;
50100a5e 942 tsk->link_cong = 1;
e2dafe87 943 rc = tipc_wait_for_sndmsg(sock, &timeo);
70452dcb 944 if (rc)
94153e36 945 __skb_queue_purge(pktchain);
e2dafe87 946 } while (!rc);
e2dafe87
JPM
947
948 return rc;
b97bf3fd
PL
949}
950
391a6dd1
YX
951static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
952{
953 struct sock *sk = sock->sk;
58ed9442 954 struct tipc_sock *tsk = tipc_sk(sk);
391a6dd1
YX
955 DEFINE_WAIT(wait);
956 int done;
957
958 do {
959 int err = sock_error(sk);
960 if (err)
961 return err;
962 if (sock->state == SS_DISCONNECTING)
963 return -EPIPE;
964 else if (sock->state != SS_CONNECTED)
965 return -ENOTCONN;
966 if (!*timeo_p)
967 return -EAGAIN;
968 if (signal_pending(current))
969 return sock_intr_errno(*timeo_p);
970
971 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
972 done = sk_wait_event(sk, timeo_p,
60120526 973 (!tsk->link_cong &&
301bae56
JPM
974 !tsk_conn_cong(tsk)) ||
975 !tsk->connected);
391a6dd1
YX
976 finish_wait(sk_sleep(sk), &wait);
977 } while (!done);
978 return 0;
979}
980
c4307285 981/**
4ccfe5e0 982 * tipc_send_stream - send stream-oriented data
b97bf3fd 983 * @sock: socket structure
4ccfe5e0
JPM
984 * @m: data to send
985 * @dsz: total length of data to be transmitted
c4307285 986 *
4ccfe5e0 987 * Used for SOCK_STREAM data.
c4307285 988 *
4ccfe5e0
JPM
989 * Returns the number of bytes sent on success (or partial success),
990 * or errno if no data sent
b97bf3fd 991 */
1b784140 992static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
39a0295f
YX
993{
994 struct sock *sk = sock->sk;
995 int ret;
996
997 lock_sock(sk);
998 ret = __tipc_send_stream(sock, m, dsz);
999 release_sock(sk);
1000
1001 return ret;
1002}
1003
1004static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
b97bf3fd 1005{
0c3141e9 1006 struct sock *sk = sock->sk;
f2f9800d 1007 struct net *net = sock_net(sk);
58ed9442 1008 struct tipc_sock *tsk = tipc_sk(sk);
301bae56 1009 struct tipc_msg *mhdr = &tsk->phdr;
94153e36 1010 struct sk_buff_head *pktchain = &sk->sk_write_queue;
342dfc30 1011 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
07f6c4bc 1012 u32 portid = tsk->portid;
4ccfe5e0 1013 int rc = -EINVAL;
391a6dd1 1014 long timeo;
4ccfe5e0
JPM
1015 u32 dnode;
1016 uint mtu, send, sent = 0;
f25dcc76 1017 struct iov_iter save;
b97bf3fd
PL
1018
1019 /* Handle implied connection establishment */
4ccfe5e0 1020 if (unlikely(dest)) {
39a0295f 1021 rc = __tipc_sendmsg(sock, m, dsz);
4ccfe5e0 1022 if (dsz && (dsz == rc))
60120526 1023 tsk->sent_unacked = 1;
4ccfe5e0
JPM
1024 return rc;
1025 }
1026 if (dsz > (uint)INT_MAX)
c29c3f70
AS
1027 return -EMSGSIZE;
1028
391a6dd1
YX
1029 if (unlikely(sock->state != SS_CONNECTED)) {
1030 if (sock->state == SS_DISCONNECTING)
39a0295f 1031 return -EPIPE;
391a6dd1 1032 else
39a0295f 1033 return -ENOTCONN;
391a6dd1 1034 }
1d835874 1035
391a6dd1 1036 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
301bae56 1037 dnode = tsk_peer_node(tsk);
4ccfe5e0
JPM
1038
1039next:
f25dcc76 1040 save = m->msg_iter;
301bae56 1041 mtu = tsk->max_pkt;
4ccfe5e0 1042 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
94153e36 1043 rc = tipc_msg_build(mhdr, m, sent, send, mtu, pktchain);
4ccfe5e0 1044 if (unlikely(rc < 0))
39a0295f 1045 return rc;
c4307285 1046 do {
301bae56 1047 if (likely(!tsk_conn_cong(tsk))) {
94153e36 1048 rc = tipc_link_xmit(net, pktchain, dnode, portid);
4ccfe5e0 1049 if (likely(!rc)) {
60120526 1050 tsk->sent_unacked++;
4ccfe5e0
JPM
1051 sent += send;
1052 if (sent == dsz)
1053 break;
1054 goto next;
1055 }
1056 if (rc == -EMSGSIZE) {
f2f9800d
YX
1057 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1058 portid);
f25dcc76 1059 m->msg_iter = save;
4ccfe5e0
JPM
1060 goto next;
1061 }
1062 if (rc != -ELINKCONG)
1063 break;
50100a5e 1064 tsk->link_cong = 1;
4ccfe5e0
JPM
1065 }
1066 rc = tipc_wait_for_sndpkt(sock, &timeo);
70452dcb 1067 if (rc)
94153e36 1068 __skb_queue_purge(pktchain);
4ccfe5e0 1069 } while (!rc);
39a0295f 1070
4ccfe5e0 1071 return sent ? sent : rc;
b97bf3fd
PL
1072}
1073
c4307285 1074/**
4ccfe5e0 1075 * tipc_send_packet - send a connection-oriented message
b97bf3fd 1076 * @sock: socket structure
4ccfe5e0
JPM
1077 * @m: message to send
1078 * @dsz: length of data to be transmitted
c4307285 1079 *
4ccfe5e0 1080 * Used for SOCK_SEQPACKET messages.
c4307285 1081 *
4ccfe5e0 1082 * Returns the number of bytes sent on success, or errno otherwise
b97bf3fd 1083 */
1b784140 1084static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
b97bf3fd 1085{
4ccfe5e0
JPM
1086 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1087 return -EMSGSIZE;
b97bf3fd 1088
1b784140 1089 return tipc_send_stream(sock, m, dsz);
b97bf3fd
PL
1090}
1091
dadebc00 1092/* tipc_sk_finish_conn - complete the setup of a connection
b97bf3fd 1093 */
301bae56 1094static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
dadebc00 1095 u32 peer_node)
b97bf3fd 1096{
3721e9c7
YX
1097 struct sock *sk = &tsk->sk;
1098 struct net *net = sock_net(sk);
301bae56 1099 struct tipc_msg *msg = &tsk->phdr;
b97bf3fd 1100
dadebc00
JPM
1101 msg_set_destnode(msg, peer_node);
1102 msg_set_destport(msg, peer_port);
1103 msg_set_type(msg, TIPC_CONN_MSG);
1104 msg_set_lookup_scope(msg, 0);
1105 msg_set_hdr_sz(msg, SHORT_H_SIZE);
584d24b3 1106
2f55c437 1107 tsk->probing_intv = CONN_PROBING_INTERVAL;
301bae56
JPM
1108 tsk->probing_state = TIPC_CONN_OK;
1109 tsk->connected = 1;
3721e9c7 1110 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
f2f9800d
YX
1111 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1112 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
b97bf3fd
PL
1113}
1114
1115/**
1116 * set_orig_addr - capture sender's address for received message
1117 * @m: descriptor for message info
1118 * @msg: received message header
c4307285 1119 *
b97bf3fd
PL
1120 * Note: Address is not captured if not requested by receiver.
1121 */
05790c64 1122static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
b97bf3fd 1123{
342dfc30 1124 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
b97bf3fd 1125
c4307285 1126 if (addr) {
b97bf3fd
PL
1127 addr->family = AF_TIPC;
1128 addr->addrtype = TIPC_ADDR_ID;
60085c3d 1129 memset(&addr->addr, 0, sizeof(addr->addr));
b97bf3fd
PL
1130 addr->addr.id.ref = msg_origport(msg);
1131 addr->addr.id.node = msg_orignode(msg);
0e65967e
AS
1132 addr->addr.name.domain = 0; /* could leave uninitialized */
1133 addr->scope = 0; /* could leave uninitialized */
b97bf3fd
PL
1134 m->msg_namelen = sizeof(struct sockaddr_tipc);
1135 }
1136}
1137
1138/**
301bae56 1139 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
b97bf3fd
PL
1140 * @m: descriptor for message info
1141 * @msg: received message header
301bae56 1142 * @tsk: TIPC port associated with message
c4307285 1143 *
b97bf3fd 1144 * Note: Ancillary data is not captured if not requested by receiver.
c4307285 1145 *
b97bf3fd
PL
1146 * Returns 0 if successful, otherwise errno
1147 */
301bae56
JPM
1148static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1149 struct tipc_sock *tsk)
b97bf3fd
PL
1150{
1151 u32 anc_data[3];
1152 u32 err;
1153 u32 dest_type;
3546c750 1154 int has_name;
b97bf3fd
PL
1155 int res;
1156
1157 if (likely(m->msg_controllen == 0))
1158 return 0;
1159
1160 /* Optionally capture errored message object(s) */
b97bf3fd
PL
1161 err = msg ? msg_errcode(msg) : 0;
1162 if (unlikely(err)) {
1163 anc_data[0] = err;
1164 anc_data[1] = msg_data_sz(msg);
2db9983a
AS
1165 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1166 if (res)
b97bf3fd 1167 return res;
2db9983a
AS
1168 if (anc_data[1]) {
1169 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1170 msg_data(msg));
1171 if (res)
1172 return res;
1173 }
b97bf3fd
PL
1174 }
1175
1176 /* Optionally capture message destination object */
b97bf3fd
PL
1177 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1178 switch (dest_type) {
1179 case TIPC_NAMED_MSG:
3546c750 1180 has_name = 1;
b97bf3fd
PL
1181 anc_data[0] = msg_nametype(msg);
1182 anc_data[1] = msg_namelower(msg);
1183 anc_data[2] = msg_namelower(msg);
1184 break;
1185 case TIPC_MCAST_MSG:
3546c750 1186 has_name = 1;
b97bf3fd
PL
1187 anc_data[0] = msg_nametype(msg);
1188 anc_data[1] = msg_namelower(msg);
1189 anc_data[2] = msg_nameupper(msg);
1190 break;
1191 case TIPC_CONN_MSG:
301bae56
JPM
1192 has_name = (tsk->conn_type != 0);
1193 anc_data[0] = tsk->conn_type;
1194 anc_data[1] = tsk->conn_instance;
1195 anc_data[2] = tsk->conn_instance;
b97bf3fd
PL
1196 break;
1197 default:
3546c750 1198 has_name = 0;
b97bf3fd 1199 }
2db9983a
AS
1200 if (has_name) {
1201 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1202 if (res)
1203 return res;
1204 }
b97bf3fd
PL
1205
1206 return 0;
1207}
1208
301bae56 1209static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
739f5e4e 1210{
f2f9800d 1211 struct net *net = sock_net(&tsk->sk);
a6ca1094 1212 struct sk_buff *skb = NULL;
739f5e4e 1213 struct tipc_msg *msg;
301bae56
JPM
1214 u32 peer_port = tsk_peer_port(tsk);
1215 u32 dnode = tsk_peer_node(tsk);
739f5e4e 1216
301bae56 1217 if (!tsk->connected)
739f5e4e 1218 return;
c5898636
JPM
1219 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1220 dnode, tsk_own_node(tsk), peer_port,
1221 tsk->portid, TIPC_OK);
a6ca1094 1222 if (!skb)
739f5e4e 1223 return;
a6ca1094 1224 msg = buf_msg(skb);
739f5e4e 1225 msg_set_msgcnt(msg, ack);
f2f9800d 1226 tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg));
739f5e4e
JPM
1227}
1228
85d3fc94 1229static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
9bbb4ecc
YX
1230{
1231 struct sock *sk = sock->sk;
1232 DEFINE_WAIT(wait);
85d3fc94 1233 long timeo = *timeop;
9bbb4ecc
YX
1234 int err;
1235
1236 for (;;) {
1237 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
fe8e4649 1238 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
9bbb4ecc
YX
1239 if (sock->state == SS_DISCONNECTING) {
1240 err = -ENOTCONN;
1241 break;
1242 }
1243 release_sock(sk);
1244 timeo = schedule_timeout(timeo);
1245 lock_sock(sk);
1246 }
1247 err = 0;
1248 if (!skb_queue_empty(&sk->sk_receive_queue))
1249 break;
9bbb4ecc
YX
1250 err = -EAGAIN;
1251 if (!timeo)
1252 break;
143fe22f
EH
1253 err = sock_intr_errno(timeo);
1254 if (signal_pending(current))
1255 break;
9bbb4ecc
YX
1256 }
1257 finish_wait(sk_sleep(sk), &wait);
85d3fc94 1258 *timeop = timeo;
9bbb4ecc
YX
1259 return err;
1260}
1261
c4307285 1262/**
247f0f3c 1263 * tipc_recvmsg - receive packet-oriented message
b97bf3fd
PL
1264 * @m: descriptor for message info
1265 * @buf_len: total size of user buffer area
1266 * @flags: receive flags
c4307285 1267 *
b97bf3fd
PL
1268 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1269 * If the complete message doesn't fit in user area, truncate it.
1270 *
1271 * Returns size of returned message data, errno otherwise
1272 */
1b784140
YX
1273static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1274 int flags)
b97bf3fd 1275{
0c3141e9 1276 struct sock *sk = sock->sk;
58ed9442 1277 struct tipc_sock *tsk = tipc_sk(sk);
b97bf3fd
PL
1278 struct sk_buff *buf;
1279 struct tipc_msg *msg;
9bbb4ecc 1280 long timeo;
b97bf3fd
PL
1281 unsigned int sz;
1282 u32 err;
1283 int res;
1284
0c3141e9 1285 /* Catch invalid receive requests */
b97bf3fd
PL
1286 if (unlikely(!buf_len))
1287 return -EINVAL;
1288
0c3141e9 1289 lock_sock(sk);
b97bf3fd 1290
0c3141e9
AS
1291 if (unlikely(sock->state == SS_UNCONNECTED)) {
1292 res = -ENOTCONN;
b97bf3fd
PL
1293 goto exit;
1294 }
1295
9bbb4ecc 1296 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
0c3141e9 1297restart:
b97bf3fd 1298
0c3141e9 1299 /* Look for a message in receive queue; wait if necessary */
85d3fc94 1300 res = tipc_wait_for_rcvmsg(sock, &timeo);
9bbb4ecc
YX
1301 if (res)
1302 goto exit;
b97bf3fd 1303
0c3141e9 1304 /* Look at first message in receive queue */
0c3141e9 1305 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1306 msg = buf_msg(buf);
1307 sz = msg_data_sz(msg);
1308 err = msg_errcode(msg);
1309
b97bf3fd 1310 /* Discard an empty non-errored message & try again */
b97bf3fd 1311 if ((!sz) && (!err)) {
2e84c60b 1312 tsk_advance_rx_queue(sk);
b97bf3fd
PL
1313 goto restart;
1314 }
1315
1316 /* Capture sender's address (optional) */
b97bf3fd
PL
1317 set_orig_addr(m, msg);
1318
1319 /* Capture ancillary data (optional) */
301bae56 1320 res = tipc_sk_anc_data_recv(m, msg, tsk);
0c3141e9 1321 if (res)
b97bf3fd
PL
1322 goto exit;
1323
1324 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd
PL
1325 if (!err) {
1326 if (unlikely(buf_len < sz)) {
1327 sz = buf_len;
1328 m->msg_flags |= MSG_TRUNC;
1329 }
51f3d02b 1330 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
0232fd0a 1331 if (res)
b97bf3fd 1332 goto exit;
b97bf3fd
PL
1333 res = sz;
1334 } else {
1335 if ((sock->state == SS_READY) ||
1336 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1337 res = 0;
1338 else
1339 res = -ECONNRESET;
1340 }
1341
1342 /* Consume received message (optional) */
b97bf3fd 1343 if (likely(!(flags & MSG_PEEK))) {
99009806 1344 if ((sock->state != SS_READY) &&
60120526 1345 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
301bae56 1346 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
60120526
JPM
1347 tsk->rcv_unacked = 0;
1348 }
2e84c60b 1349 tsk_advance_rx_queue(sk);
c4307285 1350 }
b97bf3fd 1351exit:
0c3141e9 1352 release_sock(sk);
b97bf3fd
PL
1353 return res;
1354}
1355
c4307285 1356/**
247f0f3c 1357 * tipc_recv_stream - receive stream-oriented data
b97bf3fd
PL
1358 * @m: descriptor for message info
1359 * @buf_len: total size of user buffer area
1360 * @flags: receive flags
c4307285
YH
1361 *
1362 * Used for SOCK_STREAM messages only. If not enough data is available
b97bf3fd
PL
1363 * will optionally wait for more; never truncates data.
1364 *
1365 * Returns size of returned message data, errno otherwise
1366 */
1b784140
YX
1367static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1368 size_t buf_len, int flags)
b97bf3fd 1369{
0c3141e9 1370 struct sock *sk = sock->sk;
58ed9442 1371 struct tipc_sock *tsk = tipc_sk(sk);
b97bf3fd
PL
1372 struct sk_buff *buf;
1373 struct tipc_msg *msg;
9bbb4ecc 1374 long timeo;
b97bf3fd 1375 unsigned int sz;
3720d40b 1376 int sz_to_copy, target, needed;
b97bf3fd 1377 int sz_copied = 0;
b97bf3fd 1378 u32 err;
0c3141e9 1379 int res = 0;
b97bf3fd 1380
0c3141e9 1381 /* Catch invalid receive attempts */
b97bf3fd
PL
1382 if (unlikely(!buf_len))
1383 return -EINVAL;
1384
0c3141e9 1385 lock_sock(sk);
b97bf3fd 1386
9bbb4ecc 1387 if (unlikely(sock->state == SS_UNCONNECTED)) {
0c3141e9 1388 res = -ENOTCONN;
b97bf3fd
PL
1389 goto exit;
1390 }
1391
3720d40b 1392 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
9bbb4ecc 1393 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
b97bf3fd 1394
617d3c7a 1395restart:
0c3141e9 1396 /* Look for a message in receive queue; wait if necessary */
85d3fc94 1397 res = tipc_wait_for_rcvmsg(sock, &timeo);
9bbb4ecc
YX
1398 if (res)
1399 goto exit;
b97bf3fd 1400
0c3141e9 1401 /* Look at first message in receive queue */
0c3141e9 1402 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1403 msg = buf_msg(buf);
1404 sz = msg_data_sz(msg);
1405 err = msg_errcode(msg);
1406
1407 /* Discard an empty non-errored message & try again */
b97bf3fd 1408 if ((!sz) && (!err)) {
2e84c60b 1409 tsk_advance_rx_queue(sk);
b97bf3fd
PL
1410 goto restart;
1411 }
1412
1413 /* Optionally capture sender's address & ancillary data of first msg */
b97bf3fd
PL
1414 if (sz_copied == 0) {
1415 set_orig_addr(m, msg);
301bae56 1416 res = tipc_sk_anc_data_recv(m, msg, tsk);
0c3141e9 1417 if (res)
b97bf3fd
PL
1418 goto exit;
1419 }
1420
1421 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd 1422 if (!err) {
0232fd0a 1423 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
b97bf3fd 1424
0232fd0a 1425 sz -= offset;
b97bf3fd
PL
1426 needed = (buf_len - sz_copied);
1427 sz_to_copy = (sz <= needed) ? sz : needed;
0232fd0a 1428
51f3d02b
DM
1429 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1430 m, sz_to_copy);
0232fd0a 1431 if (res)
b97bf3fd 1432 goto exit;
0232fd0a 1433
b97bf3fd
PL
1434 sz_copied += sz_to_copy;
1435
1436 if (sz_to_copy < sz) {
1437 if (!(flags & MSG_PEEK))
0232fd0a
AS
1438 TIPC_SKB_CB(buf)->handle =
1439 (void *)(unsigned long)(offset + sz_to_copy);
b97bf3fd
PL
1440 goto exit;
1441 }
b97bf3fd
PL
1442 } else {
1443 if (sz_copied != 0)
1444 goto exit; /* can't add error msg to valid data */
1445
1446 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1447 res = 0;
1448 else
1449 res = -ECONNRESET;
1450 }
1451
1452 /* Consume received message (optional) */
b97bf3fd 1453 if (likely(!(flags & MSG_PEEK))) {
60120526 1454 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
301bae56 1455 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
60120526
JPM
1456 tsk->rcv_unacked = 0;
1457 }
2e84c60b 1458 tsk_advance_rx_queue(sk);
c4307285 1459 }
b97bf3fd
PL
1460
1461 /* Loop around if more data is required */
f64f9e71
JP
1462 if ((sz_copied < buf_len) && /* didn't get all requested data */
1463 (!skb_queue_empty(&sk->sk_receive_queue) ||
3720d40b 1464 (sz_copied < target)) && /* and more is ready or required */
f64f9e71
JP
1465 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1466 (!err)) /* and haven't reached a FIN */
b97bf3fd
PL
1467 goto restart;
1468
1469exit:
0c3141e9 1470 release_sock(sk);
a3b0a5a9 1471 return sz_copied ? sz_copied : res;
b97bf3fd
PL
1472}
1473
f288bef4
YX
1474/**
1475 * tipc_write_space - wake up thread if port congestion is released
1476 * @sk: socket
1477 */
1478static void tipc_write_space(struct sock *sk)
1479{
1480 struct socket_wq *wq;
1481
1482 rcu_read_lock();
1483 wq = rcu_dereference(sk->sk_wq);
1484 if (wq_has_sleeper(wq))
1485 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1486 POLLWRNORM | POLLWRBAND);
1487 rcu_read_unlock();
1488}
1489
1490/**
1491 * tipc_data_ready - wake up threads to indicate messages have been received
1492 * @sk: socket
1493 * @len: the length of messages
1494 */
676d2369 1495static void tipc_data_ready(struct sock *sk)
f288bef4
YX
1496{
1497 struct socket_wq *wq;
1498
1499 rcu_read_lock();
1500 wq = rcu_dereference(sk->sk_wq);
1501 if (wq_has_sleeper(wq))
1502 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1503 POLLRDNORM | POLLRDBAND);
1504 rcu_read_unlock();
1505}
1506
7e6c131e
YX
1507/**
1508 * filter_connect - Handle all incoming messages for a connection-based socket
58ed9442 1509 * @tsk: TIPC socket
1186adf7 1510 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
7e6c131e 1511 *
b2ad5e5f 1512 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
7e6c131e 1513 */
1186adf7 1514static int filter_connect(struct tipc_sock *tsk, struct sk_buff **skb)
7e6c131e 1515{
58ed9442 1516 struct sock *sk = &tsk->sk;
f2f9800d 1517 struct net *net = sock_net(sk);
8826cde6 1518 struct socket *sock = sk->sk_socket;
1186adf7 1519 struct tipc_msg *msg = buf_msg(*skb);
e4de5fab 1520 int retval = -TIPC_ERR_NO_PORT;
7e6c131e
YX
1521
1522 if (msg_mcast(msg))
1523 return retval;
1524
1525 switch ((int)sock->state) {
1526 case SS_CONNECTED:
1527 /* Accept only connection-based messages sent by peer */
2e84c60b 1528 if (tsk_peer_msg(tsk, msg)) {
7e6c131e
YX
1529 if (unlikely(msg_errcode(msg))) {
1530 sock->state = SS_DISCONNECTING;
301bae56 1531 tsk->connected = 0;
dadebc00 1532 /* let timer expire on it's own */
f2f9800d 1533 tipc_node_remove_conn(net, tsk_peer_node(tsk),
07f6c4bc 1534 tsk->portid);
7e6c131e
YX
1535 }
1536 retval = TIPC_OK;
1537 }
1538 break;
1539 case SS_CONNECTING:
1540 /* Accept only ACK or NACK message */
dadebc00
JPM
1541
1542 if (unlikely(!msg_connected(msg)))
1543 break;
1544
584d24b3
YX
1545 if (unlikely(msg_errcode(msg))) {
1546 sock->state = SS_DISCONNECTING;
2c8d8518 1547 sk->sk_err = ECONNREFUSED;
584d24b3
YX
1548 retval = TIPC_OK;
1549 break;
1550 }
1551
dadebc00 1552 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
584d24b3 1553 sock->state = SS_DISCONNECTING;
dadebc00 1554 sk->sk_err = EINVAL;
7e6c131e 1555 retval = TIPC_OK;
584d24b3
YX
1556 break;
1557 }
1558
301bae56
JPM
1559 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1560 msg_set_importance(&tsk->phdr, msg_importance(msg));
dadebc00
JPM
1561 sock->state = SS_CONNECTED;
1562
584d24b3
YX
1563 /* If an incoming message is an 'ACK-', it should be
1564 * discarded here because it doesn't contain useful
1565 * data. In addition, we should try to wake up
1566 * connect() routine if sleeping.
1567 */
1568 if (msg_data_sz(msg) == 0) {
1186adf7
JPM
1569 kfree_skb(*skb);
1570 *skb = NULL;
584d24b3
YX
1571 if (waitqueue_active(sk_sleep(sk)))
1572 wake_up_interruptible(sk_sleep(sk));
1573 }
1574 retval = TIPC_OK;
7e6c131e
YX
1575 break;
1576 case SS_LISTENING:
1577 case SS_UNCONNECTED:
1578 /* Accept only SYN message */
1579 if (!msg_connected(msg) && !(msg_errcode(msg)))
1580 retval = TIPC_OK;
1581 break;
1582 case SS_DISCONNECTING:
1583 break;
1584 default:
1585 pr_err("Unknown socket state %u\n", sock->state);
1586 }
1587 return retval;
1588}
1589
aba79f33
YX
1590/**
1591 * rcvbuf_limit - get proper overload limit of socket receive queue
1592 * @sk: socket
1593 * @buf: message
1594 *
1595 * For all connection oriented messages, irrespective of importance,
1596 * the default overload value (i.e. 67MB) is set as limit.
1597 *
1598 * For all connectionless messages, by default new queue limits are
1599 * as belows:
1600 *
cc79dd1b
YX
1601 * TIPC_LOW_IMPORTANCE (4 MB)
1602 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1603 * TIPC_HIGH_IMPORTANCE (16 MB)
1604 * TIPC_CRITICAL_IMPORTANCE (32 MB)
aba79f33
YX
1605 *
1606 * Returns overload limit according to corresponding message importance
1607 */
1608static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1609{
1610 struct tipc_msg *msg = buf_msg(buf);
aba79f33
YX
1611
1612 if (msg_connected(msg))
0cee6bbe 1613 return sysctl_tipc_rmem[2];
1614
1615 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1616 msg_importance(msg);
aba79f33
YX
1617}
1618
c4307285 1619/**
0c3141e9
AS
1620 * filter_rcv - validate incoming message
1621 * @sk: socket
1186adf7 1622 * @skb: pointer to message. Set to NULL if buffer is consumed.
c4307285 1623 *
0c3141e9
AS
1624 * Enqueues message on receive queue if acceptable; optionally handles
1625 * disconnect indication for a connected socket.
1626 *
1186adf7 1627 * Called with socket lock already taken
c4307285 1628 *
1186adf7 1629 * Returns 0 (TIPC_OK) if message was ok, -TIPC error code if rejected
b97bf3fd 1630 */
1186adf7 1631static int filter_rcv(struct sock *sk, struct sk_buff **skb)
b97bf3fd 1632{
0c3141e9 1633 struct socket *sock = sk->sk_socket;
58ed9442 1634 struct tipc_sock *tsk = tipc_sk(sk);
1186adf7
JPM
1635 struct tipc_msg *msg = buf_msg(*skb);
1636 unsigned int limit = rcvbuf_limit(sk, *skb);
e4de5fab 1637 int rc = TIPC_OK;
b97bf3fd 1638
1186adf7
JPM
1639 if (unlikely(msg_user(msg) == CONN_MANAGER)) {
1640 tipc_sk_proto_rcv(tsk, skb);
1641 return TIPC_OK;
1642 }
ec8a2e56 1643
50100a5e 1644 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1186adf7 1645 kfree_skb(*skb);
50100a5e
JPM
1646 tsk->link_cong = 0;
1647 sk->sk_write_space(sk);
1186adf7 1648 *skb = NULL;
50100a5e
JPM
1649 return TIPC_OK;
1650 }
1651
b97bf3fd 1652 /* Reject message if it is wrong sort of message for socket */
aad58547 1653 if (msg_type(msg) > TIPC_DIRECT_MSG)
e4de5fab 1654 return -TIPC_ERR_NO_PORT;
0c3141e9 1655
b97bf3fd 1656 if (sock->state == SS_READY) {
b29f1428 1657 if (msg_connected(msg))
e4de5fab 1658 return -TIPC_ERR_NO_PORT;
b97bf3fd 1659 } else {
1186adf7
JPM
1660 rc = filter_connect(tsk, skb);
1661 if (rc != TIPC_OK || !*skb)
e4de5fab 1662 return rc;
b97bf3fd
PL
1663 }
1664
1665 /* Reject message if there isn't room to queue it */
1186adf7 1666 if (sk_rmem_alloc_get(sk) + (*skb)->truesize >= limit)
e4de5fab 1667 return -TIPC_ERR_OVERLOAD;
b97bf3fd 1668
aba79f33 1669 /* Enqueue message */
1186adf7
JPM
1670 TIPC_SKB_CB(*skb)->handle = NULL;
1671 __skb_queue_tail(&sk->sk_receive_queue, *skb);
1672 skb_set_owner_r(*skb, sk);
0c3141e9 1673
676d2369 1674 sk->sk_data_ready(sk);
1186adf7 1675 *skb = NULL;
0c3141e9
AS
1676 return TIPC_OK;
1677}
b97bf3fd 1678
0c3141e9 1679/**
4f4482dc 1680 * tipc_backlog_rcv - handle incoming message from backlog queue
0c3141e9 1681 * @sk: socket
a6ca1094 1682 * @skb: message
0c3141e9 1683 *
e3a77561 1684 * Caller must hold socket lock
0c3141e9
AS
1685 *
1686 * Returns 0
1687 */
a6ca1094 1688static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
0c3141e9 1689{
1186adf7
JPM
1690 int err;
1691 atomic_t *dcnt;
1692 u32 dnode;
4f4482dc 1693 struct tipc_sock *tsk = tipc_sk(sk);
34747539 1694 struct net *net = sock_net(sk);
a6ca1094 1695 uint truesize = skb->truesize;
0c3141e9 1696
1186adf7
JPM
1697 err = filter_rcv(sk, &skb);
1698 if (likely(!skb)) {
1699 dcnt = &tsk->dupl_rcvcnt;
1700 if (atomic_read(dcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1701 atomic_add(truesize, dcnt);
ac0074ee
JPM
1702 return 0;
1703 }
1186adf7
JPM
1704 if (!err || tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode, -err))
1705 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
0c3141e9
AS
1706 return 0;
1707}
1708
d570d864 1709/**
c637c103
JPM
1710 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1711 * inputq and try adding them to socket or backlog queue
1712 * @inputq: list of incoming buffers with potentially different destinations
1713 * @sk: socket where the buffers should be enqueued
1714 * @dport: port number for the socket
1715 * @_skb: returned buffer to be forwarded or rejected, if applicable
d570d864
JPM
1716 *
1717 * Caller must hold socket lock
1718 *
c637c103
JPM
1719 * Returns TIPC_OK if all buffers enqueued, otherwise -TIPC_ERR_OVERLOAD
1720 * or -TIPC_ERR_NO_PORT
d570d864 1721 */
c637c103
JPM
1722static int tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1723 u32 dport, struct sk_buff **_skb)
d570d864
JPM
1724{
1725 unsigned int lim;
1726 atomic_t *dcnt;
c637c103
JPM
1727 int err;
1728 struct sk_buff *skb;
1729 unsigned long time_limit = jiffies + 2;
1730
1731 while (skb_queue_len(inputq)) {
51a00daf
JPM
1732 if (unlikely(time_after_eq(jiffies, time_limit)))
1733 return TIPC_OK;
c637c103
JPM
1734 skb = tipc_skb_dequeue(inputq, dport);
1735 if (unlikely(!skb))
1736 return TIPC_OK;
c637c103
JPM
1737 if (!sock_owned_by_user(sk)) {
1738 err = filter_rcv(sk, &skb);
1739 if (likely(!skb))
1740 continue;
1741 *_skb = skb;
1742 return err;
1743 }
1744 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1745 if (sk->sk_backlog.len)
1746 atomic_set(dcnt, 0);
1747 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1748 if (likely(!sk_add_backlog(sk, skb, lim)))
1749 continue;
1750 *_skb = skb;
d570d864 1751 return -TIPC_ERR_OVERLOAD;
c637c103 1752 }
d570d864
JPM
1753 return TIPC_OK;
1754}
1755
0c3141e9 1756/**
c637c103
JPM
1757 * tipc_sk_rcv - handle a chain of incoming buffers
1758 * @inputq: buffer list containing the buffers
1759 * Consumes all buffers in list until inputq is empty
1760 * Note: may be called in multiple threads referring to the same queue
1761 * Returns 0 if last buffer was accepted, otherwise -EHOSTUNREACH
1762 * Only node local calls check the return value, sending single-buffer queues
0c3141e9 1763 */
c637c103 1764int tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
0c3141e9 1765{
c637c103
JPM
1766 u32 dnode, dport = 0;
1767 int err = -TIPC_ERR_NO_PORT;
1768 struct sk_buff *skb;
9816f061 1769 struct tipc_sock *tsk;
c5898636 1770 struct tipc_net *tn;
9816f061 1771 struct sock *sk;
9816f061 1772
c637c103
JPM
1773 while (skb_queue_len(inputq)) {
1774 skb = NULL;
1775 dport = tipc_skb_peek_port(inputq, dport);
1776 tsk = tipc_sk_lookup(net, dport);
1777 if (likely(tsk)) {
1778 sk = &tsk->sk;
1779 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1780 err = tipc_sk_enqueue(inputq, sk, dport, &skb);
1781 spin_unlock_bh(&sk->sk_lock.slock);
1782 dport = 0;
1783 }
1784 sock_put(sk);
1785 } else {
1786 skb = tipc_skb_dequeue(inputq, dport);
1787 }
1788 if (likely(!skb))
1789 continue;
1790 if (tipc_msg_lookup_dest(net, skb, &dnode, &err))
1791 goto xmit;
1792 if (!err) {
1793 dnode = msg_destnode(buf_msg(skb));
1794 goto xmit;
1795 }
1796 tn = net_generic(net, tipc_net_id);
1797 if (!tipc_msg_reverse(tn->own_addr, skb, &dnode, -err))
1798 continue;
e3a77561 1799xmit:
c637c103
JPM
1800 tipc_link_xmit_skb(net, skb, dnode, dport);
1801 }
1186adf7 1802 return err ? -EHOSTUNREACH : 0;
b97bf3fd
PL
1803}
1804
78eb3a53
YX
1805static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1806{
1807 struct sock *sk = sock->sk;
1808 DEFINE_WAIT(wait);
1809 int done;
1810
1811 do {
1812 int err = sock_error(sk);
1813 if (err)
1814 return err;
1815 if (!*timeo_p)
1816 return -ETIMEDOUT;
1817 if (signal_pending(current))
1818 return sock_intr_errno(*timeo_p);
1819
1820 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1821 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1822 finish_wait(sk_sleep(sk), &wait);
1823 } while (!done);
1824 return 0;
1825}
1826
b97bf3fd 1827/**
247f0f3c 1828 * tipc_connect - establish a connection to another TIPC port
b97bf3fd
PL
1829 * @sock: socket structure
1830 * @dest: socket address for destination port
1831 * @destlen: size of socket address data structure
0c3141e9 1832 * @flags: file-related flags associated with socket
b97bf3fd
PL
1833 *
1834 * Returns 0 on success, errno otherwise
1835 */
247f0f3c
YX
1836static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1837 int destlen, int flags)
b97bf3fd 1838{
0c3141e9 1839 struct sock *sk = sock->sk;
f2f8036e 1840 struct tipc_sock *tsk = tipc_sk(sk);
b89741a0
AS
1841 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1842 struct msghdr m = {NULL,};
f2f8036e 1843 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
78eb3a53 1844 socket_state previous;
f2f8036e 1845 int res = 0;
b89741a0 1846
0c3141e9
AS
1847 lock_sock(sk);
1848
f2f8036e 1849 /* DGRAM/RDM connect(), just save the destaddr */
0c3141e9 1850 if (sock->state == SS_READY) {
f2f8036e
EH
1851 if (dst->family == AF_UNSPEC) {
1852 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1853 tsk->connected = 0;
610600c8
SL
1854 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1855 res = -EINVAL;
f2f8036e
EH
1856 } else {
1857 memcpy(&tsk->remote, dest, destlen);
1858 tsk->connected = 1;
1859 }
0c3141e9
AS
1860 goto exit;
1861 }
b89741a0 1862
b89741a0
AS
1863 /*
1864 * Reject connection attempt using multicast address
1865 *
1866 * Note: send_msg() validates the rest of the address fields,
1867 * so there's no need to do it here
1868 */
0c3141e9
AS
1869 if (dst->addrtype == TIPC_ADDR_MCAST) {
1870 res = -EINVAL;
1871 goto exit;
1872 }
1873
78eb3a53 1874 previous = sock->state;
584d24b3
YX
1875 switch (sock->state) {
1876 case SS_UNCONNECTED:
1877 /* Send a 'SYN-' to destination */
1878 m.msg_name = dest;
1879 m.msg_namelen = destlen;
1880
1881 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1882 * indicate send_msg() is never blocked.
1883 */
1884 if (!timeout)
1885 m.msg_flags = MSG_DONTWAIT;
1886
39a0295f 1887 res = __tipc_sendmsg(sock, &m, 0);
584d24b3
YX
1888 if ((res < 0) && (res != -EWOULDBLOCK))
1889 goto exit;
1890
1891 /* Just entered SS_CONNECTING state; the only
1892 * difference is that return value in non-blocking
1893 * case is EINPROGRESS, rather than EALREADY.
1894 */
1895 res = -EINPROGRESS;
584d24b3 1896 case SS_CONNECTING:
78eb3a53
YX
1897 if (previous == SS_CONNECTING)
1898 res = -EALREADY;
1899 if (!timeout)
1900 goto exit;
1901 timeout = msecs_to_jiffies(timeout);
1902 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1903 res = tipc_wait_for_connect(sock, &timeout);
584d24b3
YX
1904 break;
1905 case SS_CONNECTED:
1906 res = -EISCONN;
1907 break;
1908 default:
1909 res = -EINVAL;
78eb3a53 1910 break;
b89741a0 1911 }
0c3141e9
AS
1912exit:
1913 release_sock(sk);
b89741a0 1914 return res;
b97bf3fd
PL
1915}
1916
c4307285 1917/**
247f0f3c 1918 * tipc_listen - allow socket to listen for incoming connections
b97bf3fd
PL
1919 * @sock: socket structure
1920 * @len: (unused)
c4307285 1921 *
b97bf3fd
PL
1922 * Returns 0 on success, errno otherwise
1923 */
247f0f3c 1924static int tipc_listen(struct socket *sock, int len)
b97bf3fd 1925{
0c3141e9
AS
1926 struct sock *sk = sock->sk;
1927 int res;
1928
1929 lock_sock(sk);
b97bf3fd 1930
245f3d34 1931 if (sock->state != SS_UNCONNECTED)
0c3141e9
AS
1932 res = -EINVAL;
1933 else {
1934 sock->state = SS_LISTENING;
1935 res = 0;
1936 }
1937
1938 release_sock(sk);
1939 return res;
b97bf3fd
PL
1940}
1941
6398e23c
YX
1942static int tipc_wait_for_accept(struct socket *sock, long timeo)
1943{
1944 struct sock *sk = sock->sk;
1945 DEFINE_WAIT(wait);
1946 int err;
1947
1948 /* True wake-one mechanism for incoming connections: only
1949 * one process gets woken up, not the 'whole herd'.
1950 * Since we do not 'race & poll' for established sockets
1951 * anymore, the common case will execute the loop only once.
1952 */
1953 for (;;) {
1954 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1955 TASK_INTERRUPTIBLE);
fe8e4649 1956 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
6398e23c
YX
1957 release_sock(sk);
1958 timeo = schedule_timeout(timeo);
1959 lock_sock(sk);
1960 }
1961 err = 0;
1962 if (!skb_queue_empty(&sk->sk_receive_queue))
1963 break;
1964 err = -EINVAL;
1965 if (sock->state != SS_LISTENING)
1966 break;
6398e23c
YX
1967 err = -EAGAIN;
1968 if (!timeo)
1969 break;
143fe22f
EH
1970 err = sock_intr_errno(timeo);
1971 if (signal_pending(current))
1972 break;
6398e23c
YX
1973 }
1974 finish_wait(sk_sleep(sk), &wait);
1975 return err;
1976}
1977
c4307285 1978/**
247f0f3c 1979 * tipc_accept - wait for connection request
b97bf3fd
PL
1980 * @sock: listening socket
1981 * @newsock: new socket that is to be connected
1982 * @flags: file-related flags associated with socket
c4307285 1983 *
b97bf3fd
PL
1984 * Returns 0 on success, errno otherwise
1985 */
247f0f3c 1986static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
b97bf3fd 1987{
0fef8f20 1988 struct sock *new_sk, *sk = sock->sk;
b97bf3fd 1989 struct sk_buff *buf;
301bae56 1990 struct tipc_sock *new_tsock;
0fef8f20 1991 struct tipc_msg *msg;
6398e23c 1992 long timeo;
0c3141e9 1993 int res;
b97bf3fd 1994
0c3141e9 1995 lock_sock(sk);
b97bf3fd 1996
0c3141e9
AS
1997 if (sock->state != SS_LISTENING) {
1998 res = -EINVAL;
b97bf3fd
PL
1999 goto exit;
2000 }
6398e23c
YX
2001 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2002 res = tipc_wait_for_accept(sock, timeo);
2003 if (res)
2004 goto exit;
0c3141e9
AS
2005
2006 buf = skb_peek(&sk->sk_receive_queue);
2007
c5fa7b3c 2008 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
0fef8f20
PG
2009 if (res)
2010 goto exit;
b97bf3fd 2011
0fef8f20 2012 new_sk = new_sock->sk;
301bae56 2013 new_tsock = tipc_sk(new_sk);
0fef8f20 2014 msg = buf_msg(buf);
b97bf3fd 2015
0fef8f20
PG
2016 /* we lock on new_sk; but lockdep sees the lock on sk */
2017 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2018
2019 /*
2020 * Reject any stray messages received by new socket
2021 * before the socket lock was taken (very, very unlikely)
2022 */
2e84c60b 2023 tsk_rej_rx_queue(new_sk);
0fef8f20
PG
2024
2025 /* Connect new socket to it's peer */
301bae56 2026 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
0fef8f20
PG
2027 new_sock->state = SS_CONNECTED;
2028
301bae56 2029 tsk_set_importance(new_tsock, msg_importance(msg));
0fef8f20 2030 if (msg_named(msg)) {
301bae56
JPM
2031 new_tsock->conn_type = msg_nametype(msg);
2032 new_tsock->conn_instance = msg_nameinst(msg);
b97bf3fd 2033 }
0fef8f20
PG
2034
2035 /*
2036 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2037 * Respond to 'SYN+' by queuing it on new socket.
2038 */
2039 if (!msg_data_sz(msg)) {
2040 struct msghdr m = {NULL,};
2041
2e84c60b 2042 tsk_advance_rx_queue(sk);
39a0295f 2043 __tipc_send_stream(new_sock, &m, 0);
0fef8f20
PG
2044 } else {
2045 __skb_dequeue(&sk->sk_receive_queue);
2046 __skb_queue_head(&new_sk->sk_receive_queue, buf);
aba79f33 2047 skb_set_owner_r(buf, new_sk);
0fef8f20
PG
2048 }
2049 release_sock(new_sk);
b97bf3fd 2050exit:
0c3141e9 2051 release_sock(sk);
b97bf3fd
PL
2052 return res;
2053}
2054
2055/**
247f0f3c 2056 * tipc_shutdown - shutdown socket connection
b97bf3fd 2057 * @sock: socket structure
e247a8f5 2058 * @how: direction to close (must be SHUT_RDWR)
b97bf3fd
PL
2059 *
2060 * Terminates connection (if necessary), then purges socket's receive queue.
c4307285 2061 *
b97bf3fd
PL
2062 * Returns 0 on success, errno otherwise
2063 */
247f0f3c 2064static int tipc_shutdown(struct socket *sock, int how)
b97bf3fd 2065{
0c3141e9 2066 struct sock *sk = sock->sk;
f2f9800d 2067 struct net *net = sock_net(sk);
58ed9442 2068 struct tipc_sock *tsk = tipc_sk(sk);
a6ca1094 2069 struct sk_buff *skb;
80e44c22 2070 u32 dnode;
b97bf3fd
PL
2071 int res;
2072
e247a8f5
AS
2073 if (how != SHUT_RDWR)
2074 return -EINVAL;
b97bf3fd 2075
0c3141e9 2076 lock_sock(sk);
b97bf3fd
PL
2077
2078 switch (sock->state) {
0c3141e9 2079 case SS_CONNECTING:
b97bf3fd
PL
2080 case SS_CONNECTED:
2081
b97bf3fd 2082restart:
617d3c7a 2083 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
a6ca1094
YX
2084 skb = __skb_dequeue(&sk->sk_receive_queue);
2085 if (skb) {
2086 if (TIPC_SKB_CB(skb)->handle != NULL) {
2087 kfree_skb(skb);
b97bf3fd
PL
2088 goto restart;
2089 }
c5898636 2090 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
34747539 2091 TIPC_CONN_SHUTDOWN))
f2f9800d
YX
2092 tipc_link_xmit_skb(net, skb, dnode,
2093 tsk->portid);
0c3141e9 2094 } else {
301bae56 2095 dnode = tsk_peer_node(tsk);
c5898636
JPM
2096
2097 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
80e44c22 2098 TIPC_CONN_MSG, SHORT_H_SIZE,
c5898636 2099 0, dnode, tsk_own_node(tsk),
301bae56 2100 tsk_peer_port(tsk),
07f6c4bc 2101 tsk->portid, TIPC_CONN_SHUTDOWN);
f2f9800d 2102 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
b97bf3fd 2103 }
301bae56 2104 tsk->connected = 0;
0c3141e9 2105 sock->state = SS_DISCONNECTING;
f2f9800d 2106 tipc_node_remove_conn(net, dnode, tsk->portid);
b97bf3fd
PL
2107 /* fall through */
2108
2109 case SS_DISCONNECTING:
2110
75031151 2111 /* Discard any unreceived messages */
57467e56 2112 __skb_queue_purge(&sk->sk_receive_queue);
75031151
YX
2113
2114 /* Wake up anyone sleeping in poll */
2115 sk->sk_state_change(sk);
b97bf3fd
PL
2116 res = 0;
2117 break;
2118
2119 default:
2120 res = -ENOTCONN;
2121 }
2122
0c3141e9 2123 release_sock(sk);
b97bf3fd
PL
2124 return res;
2125}
2126
f2f2a96a 2127static void tipc_sk_timeout(unsigned long data)
57289015 2128{
f2f2a96a
YX
2129 struct tipc_sock *tsk = (struct tipc_sock *)data;
2130 struct sock *sk = &tsk->sk;
a6ca1094 2131 struct sk_buff *skb = NULL;
57289015 2132 u32 peer_port, peer_node;
c5898636 2133 u32 own_node = tsk_own_node(tsk);
57289015 2134
6c9808ce 2135 bh_lock_sock(sk);
301bae56 2136 if (!tsk->connected) {
6c9808ce
JPM
2137 bh_unlock_sock(sk);
2138 goto exit;
57289015 2139 }
301bae56
JPM
2140 peer_port = tsk_peer_port(tsk);
2141 peer_node = tsk_peer_node(tsk);
57289015 2142
301bae56 2143 if (tsk->probing_state == TIPC_CONN_PROBING) {
57289015 2144 /* Previous probe not answered -> self abort */
c5898636 2145 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
34747539 2146 TIPC_CONN_MSG, SHORT_H_SIZE, 0,
c5898636 2147 own_node, peer_node, tsk->portid,
34747539 2148 peer_port, TIPC_ERR_NO_PORT);
57289015 2149 } else {
c5898636
JPM
2150 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2151 INT_H_SIZE, 0, peer_node, own_node,
f2f2a96a 2152 peer_port, tsk->portid, TIPC_OK);
301bae56 2153 tsk->probing_state = TIPC_CONN_PROBING;
3721e9c7 2154 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
57289015
JPM
2155 }
2156 bh_unlock_sock(sk);
a6ca1094 2157 if (skb)
f2f9800d 2158 tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
6c9808ce 2159exit:
07f6c4bc 2160 sock_put(sk);
57289015
JPM
2161}
2162
301bae56 2163static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
0fc87aae
JPM
2164 struct tipc_name_seq const *seq)
2165{
f2f9800d 2166 struct net *net = sock_net(&tsk->sk);
0fc87aae
JPM
2167 struct publication *publ;
2168 u32 key;
2169
301bae56 2170 if (tsk->connected)
0fc87aae 2171 return -EINVAL;
07f6c4bc
YX
2172 key = tsk->portid + tsk->pub_count + 1;
2173 if (key == tsk->portid)
0fc87aae
JPM
2174 return -EADDRINUSE;
2175
f2f9800d 2176 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
07f6c4bc 2177 scope, tsk->portid, key);
0fc87aae
JPM
2178 if (unlikely(!publ))
2179 return -EINVAL;
2180
301bae56
JPM
2181 list_add(&publ->pport_list, &tsk->publications);
2182 tsk->pub_count++;
2183 tsk->published = 1;
0fc87aae
JPM
2184 return 0;
2185}
2186
301bae56 2187static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
0fc87aae
JPM
2188 struct tipc_name_seq const *seq)
2189{
f2f9800d 2190 struct net *net = sock_net(&tsk->sk);
0fc87aae
JPM
2191 struct publication *publ;
2192 struct publication *safe;
2193 int rc = -EINVAL;
2194
301bae56 2195 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
0fc87aae
JPM
2196 if (seq) {
2197 if (publ->scope != scope)
2198 continue;
2199 if (publ->type != seq->type)
2200 continue;
2201 if (publ->lower != seq->lower)
2202 continue;
2203 if (publ->upper != seq->upper)
2204 break;
f2f9800d 2205 tipc_nametbl_withdraw(net, publ->type, publ->lower,
0fc87aae
JPM
2206 publ->ref, publ->key);
2207 rc = 0;
2208 break;
2209 }
f2f9800d 2210 tipc_nametbl_withdraw(net, publ->type, publ->lower,
0fc87aae
JPM
2211 publ->ref, publ->key);
2212 rc = 0;
2213 }
301bae56
JPM
2214 if (list_empty(&tsk->publications))
2215 tsk->published = 0;
0fc87aae
JPM
2216 return rc;
2217}
2218
5a9ee0be
JPM
2219/* tipc_sk_reinit: set non-zero address in all existing sockets
2220 * when we go from standalone to network mode.
2221 */
e05b31f4 2222void tipc_sk_reinit(struct net *net)
5a9ee0be 2223{
e05b31f4 2224 struct tipc_net *tn = net_generic(net, tipc_net_id);
07f6c4bc
YX
2225 const struct bucket_table *tbl;
2226 struct rhash_head *pos;
2227 struct tipc_sock *tsk;
5a9ee0be 2228 struct tipc_msg *msg;
07f6c4bc 2229 int i;
5a9ee0be 2230
07f6c4bc 2231 rcu_read_lock();
e05b31f4 2232 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
07f6c4bc
YX
2233 for (i = 0; i < tbl->size; i++) {
2234 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2235 spin_lock_bh(&tsk->sk.sk_lock.slock);
2236 msg = &tsk->phdr;
34747539
YX
2237 msg_set_prevnode(msg, tn->own_addr);
2238 msg_set_orignode(msg, tn->own_addr);
07f6c4bc
YX
2239 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2240 }
5a9ee0be 2241 }
07f6c4bc 2242 rcu_read_unlock();
5a9ee0be
JPM
2243}
2244
e05b31f4 2245static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
808d90f9 2246{
e05b31f4 2247 struct tipc_net *tn = net_generic(net, tipc_net_id);
07f6c4bc 2248 struct tipc_sock *tsk;
808d90f9 2249
07f6c4bc 2250 rcu_read_lock();
6cca7289 2251 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
07f6c4bc
YX
2252 if (tsk)
2253 sock_hold(&tsk->sk);
2254 rcu_read_unlock();
808d90f9 2255
07f6c4bc 2256 return tsk;
808d90f9
JPM
2257}
2258
07f6c4bc 2259static int tipc_sk_insert(struct tipc_sock *tsk)
808d90f9 2260{
e05b31f4
YX
2261 struct sock *sk = &tsk->sk;
2262 struct net *net = sock_net(sk);
2263 struct tipc_net *tn = net_generic(net, tipc_net_id);
07f6c4bc
YX
2264 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2265 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
808d90f9 2266
07f6c4bc
YX
2267 while (remaining--) {
2268 portid++;
2269 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2270 portid = TIPC_MIN_PORT;
2271 tsk->portid = portid;
2272 sock_hold(&tsk->sk);
6cca7289
HX
2273 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2274 tsk_rht_params))
07f6c4bc
YX
2275 return 0;
2276 sock_put(&tsk->sk);
808d90f9
JPM
2277 }
2278
07f6c4bc 2279 return -1;
808d90f9
JPM
2280}
2281
07f6c4bc 2282static void tipc_sk_remove(struct tipc_sock *tsk)
808d90f9 2283{
07f6c4bc 2284 struct sock *sk = &tsk->sk;
e05b31f4 2285 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
808d90f9 2286
6cca7289 2287 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
07f6c4bc
YX
2288 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2289 __sock_put(sk);
808d90f9 2290 }
808d90f9
JPM
2291}
2292
6cca7289
HX
2293static const struct rhashtable_params tsk_rht_params = {
2294 .nelem_hint = 192,
2295 .head_offset = offsetof(struct tipc_sock, node),
2296 .key_offset = offsetof(struct tipc_sock, portid),
2297 .key_len = sizeof(u32), /* portid */
6cca7289
HX
2298 .max_size = 1048576,
2299 .min_size = 256,
2300};
2301
e05b31f4 2302int tipc_sk_rht_init(struct net *net)
808d90f9 2303{
e05b31f4 2304 struct tipc_net *tn = net_generic(net, tipc_net_id);
6cca7289
HX
2305
2306 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
808d90f9
JPM
2307}
2308
e05b31f4 2309void tipc_sk_rht_destroy(struct net *net)
808d90f9 2310{
e05b31f4
YX
2311 struct tipc_net *tn = net_generic(net, tipc_net_id);
2312
07f6c4bc
YX
2313 /* Wait for socket readers to complete */
2314 synchronize_net();
808d90f9 2315
e05b31f4 2316 rhashtable_destroy(&tn->sk_rht);
808d90f9
JPM
2317}
2318
b97bf3fd 2319/**
247f0f3c 2320 * tipc_setsockopt - set socket option
b97bf3fd
PL
2321 * @sock: socket structure
2322 * @lvl: option level
2323 * @opt: option identifier
2324 * @ov: pointer to new option value
2325 * @ol: length of option value
c4307285
YH
2326 *
2327 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
b97bf3fd 2328 * (to ease compatibility).
c4307285 2329 *
b97bf3fd
PL
2330 * Returns 0 on success, errno otherwise
2331 */
247f0f3c
YX
2332static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2333 char __user *ov, unsigned int ol)
b97bf3fd 2334{
0c3141e9 2335 struct sock *sk = sock->sk;
58ed9442 2336 struct tipc_sock *tsk = tipc_sk(sk);
b97bf3fd
PL
2337 u32 value;
2338 int res;
2339
c4307285
YH
2340 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2341 return 0;
b97bf3fd
PL
2342 if (lvl != SOL_TIPC)
2343 return -ENOPROTOOPT;
2344 if (ol < sizeof(value))
2345 return -EINVAL;
2db9983a
AS
2346 res = get_user(value, (u32 __user *)ov);
2347 if (res)
b97bf3fd
PL
2348 return res;
2349
0c3141e9 2350 lock_sock(sk);
c4307285 2351
b97bf3fd
PL
2352 switch (opt) {
2353 case TIPC_IMPORTANCE:
301bae56 2354 res = tsk_set_importance(tsk, value);
b97bf3fd
PL
2355 break;
2356 case TIPC_SRC_DROPPABLE:
2357 if (sock->type != SOCK_STREAM)
301bae56 2358 tsk_set_unreliable(tsk, value);
c4307285 2359 else
b97bf3fd
PL
2360 res = -ENOPROTOOPT;
2361 break;
2362 case TIPC_DEST_DROPPABLE:
301bae56 2363 tsk_set_unreturnable(tsk, value);
b97bf3fd
PL
2364 break;
2365 case TIPC_CONN_TIMEOUT:
a0f40f02 2366 tipc_sk(sk)->conn_timeout = value;
0c3141e9 2367 /* no need to set "res", since already 0 at this point */
b97bf3fd
PL
2368 break;
2369 default:
2370 res = -EINVAL;
2371 }
2372
0c3141e9
AS
2373 release_sock(sk);
2374
b97bf3fd
PL
2375 return res;
2376}
2377
2378/**
247f0f3c 2379 * tipc_getsockopt - get socket option
b97bf3fd
PL
2380 * @sock: socket structure
2381 * @lvl: option level
2382 * @opt: option identifier
2383 * @ov: receptacle for option value
2384 * @ol: receptacle for length of option value
c4307285
YH
2385 *
2386 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
b97bf3fd 2387 * (to ease compatibility).
c4307285 2388 *
b97bf3fd
PL
2389 * Returns 0 on success, errno otherwise
2390 */
247f0f3c
YX
2391static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2392 char __user *ov, int __user *ol)
b97bf3fd 2393{
0c3141e9 2394 struct sock *sk = sock->sk;
58ed9442 2395 struct tipc_sock *tsk = tipc_sk(sk);
c4307285 2396 int len;
b97bf3fd 2397 u32 value;
c4307285 2398 int res;
b97bf3fd 2399
c4307285
YH
2400 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2401 return put_user(0, ol);
b97bf3fd
PL
2402 if (lvl != SOL_TIPC)
2403 return -ENOPROTOOPT;
2db9983a
AS
2404 res = get_user(len, ol);
2405 if (res)
c4307285 2406 return res;
b97bf3fd 2407
0c3141e9 2408 lock_sock(sk);
b97bf3fd
PL
2409
2410 switch (opt) {
2411 case TIPC_IMPORTANCE:
301bae56 2412 value = tsk_importance(tsk);
b97bf3fd
PL
2413 break;
2414 case TIPC_SRC_DROPPABLE:
301bae56 2415 value = tsk_unreliable(tsk);
b97bf3fd
PL
2416 break;
2417 case TIPC_DEST_DROPPABLE:
301bae56 2418 value = tsk_unreturnable(tsk);
b97bf3fd
PL
2419 break;
2420 case TIPC_CONN_TIMEOUT:
301bae56 2421 value = tsk->conn_timeout;
0c3141e9 2422 /* no need to set "res", since already 0 at this point */
b97bf3fd 2423 break;
0e65967e 2424 case TIPC_NODE_RECVQ_DEPTH:
9da3d475 2425 value = 0; /* was tipc_queue_size, now obsolete */
6650613d 2426 break;
0e65967e 2427 case TIPC_SOCK_RECVQ_DEPTH:
6650613d 2428 value = skb_queue_len(&sk->sk_receive_queue);
2429 break;
b97bf3fd
PL
2430 default:
2431 res = -EINVAL;
2432 }
2433
0c3141e9
AS
2434 release_sock(sk);
2435
25860c3b
PG
2436 if (res)
2437 return res; /* "get" failed */
b97bf3fd 2438
25860c3b
PG
2439 if (len < sizeof(value))
2440 return -EINVAL;
2441
2442 if (copy_to_user(ov, &value, sizeof(value)))
2443 return -EFAULT;
2444
2445 return put_user(sizeof(value), ol);
b97bf3fd
PL
2446}
2447
f2f9800d 2448static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
78acb1f9 2449{
f2f9800d 2450 struct sock *sk = sock->sk;
78acb1f9
EH
2451 struct tipc_sioc_ln_req lnr;
2452 void __user *argp = (void __user *)arg;
2453
2454 switch (cmd) {
2455 case SIOCGETLINKNAME:
2456 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2457 return -EFAULT;
f2f9800d
YX
2458 if (!tipc_node_get_linkname(sock_net(sk),
2459 lnr.bearer_id & 0xffff, lnr.peer,
78acb1f9
EH
2460 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2461 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2462 return -EFAULT;
2463 return 0;
2464 }
2465 return -EADDRNOTAVAIL;
78acb1f9
EH
2466 default:
2467 return -ENOIOCTLCMD;
2468 }
2469}
2470
ae86b9e3
BH
2471/* Protocol switches for the various types of TIPC sockets */
2472
bca65eae 2473static const struct proto_ops msg_ops = {
0e65967e 2474 .owner = THIS_MODULE,
b97bf3fd 2475 .family = AF_TIPC,
247f0f3c
YX
2476 .release = tipc_release,
2477 .bind = tipc_bind,
2478 .connect = tipc_connect,
5eee6a6d 2479 .socketpair = sock_no_socketpair,
245f3d34 2480 .accept = sock_no_accept,
247f0f3c
YX
2481 .getname = tipc_getname,
2482 .poll = tipc_poll,
78acb1f9 2483 .ioctl = tipc_ioctl,
245f3d34 2484 .listen = sock_no_listen,
247f0f3c
YX
2485 .shutdown = tipc_shutdown,
2486 .setsockopt = tipc_setsockopt,
2487 .getsockopt = tipc_getsockopt,
2488 .sendmsg = tipc_sendmsg,
2489 .recvmsg = tipc_recvmsg,
8238745a
YH
2490 .mmap = sock_no_mmap,
2491 .sendpage = sock_no_sendpage
b97bf3fd
PL
2492};
2493
bca65eae 2494static const struct proto_ops packet_ops = {
0e65967e 2495 .owner = THIS_MODULE,
b97bf3fd 2496 .family = AF_TIPC,
247f0f3c
YX
2497 .release = tipc_release,
2498 .bind = tipc_bind,
2499 .connect = tipc_connect,
5eee6a6d 2500 .socketpair = sock_no_socketpair,
247f0f3c
YX
2501 .accept = tipc_accept,
2502 .getname = tipc_getname,
2503 .poll = tipc_poll,
78acb1f9 2504 .ioctl = tipc_ioctl,
247f0f3c
YX
2505 .listen = tipc_listen,
2506 .shutdown = tipc_shutdown,
2507 .setsockopt = tipc_setsockopt,
2508 .getsockopt = tipc_getsockopt,
2509 .sendmsg = tipc_send_packet,
2510 .recvmsg = tipc_recvmsg,
8238745a
YH
2511 .mmap = sock_no_mmap,
2512 .sendpage = sock_no_sendpage
b97bf3fd
PL
2513};
2514
bca65eae 2515static const struct proto_ops stream_ops = {
0e65967e 2516 .owner = THIS_MODULE,
b97bf3fd 2517 .family = AF_TIPC,
247f0f3c
YX
2518 .release = tipc_release,
2519 .bind = tipc_bind,
2520 .connect = tipc_connect,
5eee6a6d 2521 .socketpair = sock_no_socketpair,
247f0f3c
YX
2522 .accept = tipc_accept,
2523 .getname = tipc_getname,
2524 .poll = tipc_poll,
78acb1f9 2525 .ioctl = tipc_ioctl,
247f0f3c
YX
2526 .listen = tipc_listen,
2527 .shutdown = tipc_shutdown,
2528 .setsockopt = tipc_setsockopt,
2529 .getsockopt = tipc_getsockopt,
2530 .sendmsg = tipc_send_stream,
2531 .recvmsg = tipc_recv_stream,
8238745a
YH
2532 .mmap = sock_no_mmap,
2533 .sendpage = sock_no_sendpage
b97bf3fd
PL
2534};
2535
bca65eae 2536static const struct net_proto_family tipc_family_ops = {
0e65967e 2537 .owner = THIS_MODULE,
b97bf3fd 2538 .family = AF_TIPC,
c5fa7b3c 2539 .create = tipc_sk_create
b97bf3fd
PL
2540};
2541
2542static struct proto tipc_proto = {
2543 .name = "TIPC",
2544 .owner = THIS_MODULE,
cc79dd1b
YX
2545 .obj_size = sizeof(struct tipc_sock),
2546 .sysctl_rmem = sysctl_tipc_rmem
b97bf3fd
PL
2547};
2548
2549/**
4323add6 2550 * tipc_socket_init - initialize TIPC socket interface
c4307285 2551 *
b97bf3fd
PL
2552 * Returns 0 on success, errno otherwise
2553 */
4323add6 2554int tipc_socket_init(void)
b97bf3fd
PL
2555{
2556 int res;
2557
c4307285 2558 res = proto_register(&tipc_proto, 1);
b97bf3fd 2559 if (res) {
2cf8aa19 2560 pr_err("Failed to register TIPC protocol type\n");
b97bf3fd
PL
2561 goto out;
2562 }
2563
2564 res = sock_register(&tipc_family_ops);
2565 if (res) {
2cf8aa19 2566 pr_err("Failed to register TIPC socket type\n");
b97bf3fd
PL
2567 proto_unregister(&tipc_proto);
2568 goto out;
2569 }
b97bf3fd
PL
2570 out:
2571 return res;
2572}
2573
2574/**
4323add6 2575 * tipc_socket_stop - stop TIPC socket interface
b97bf3fd 2576 */
4323add6 2577void tipc_socket_stop(void)
b97bf3fd 2578{
b97bf3fd
PL
2579 sock_unregister(tipc_family_ops.family);
2580 proto_unregister(&tipc_proto);
2581}
34b78a12
RA
2582
2583/* Caller should hold socket lock for the passed tipc socket. */
d8182804 2584static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
34b78a12
RA
2585{
2586 u32 peer_node;
2587 u32 peer_port;
2588 struct nlattr *nest;
2589
2590 peer_node = tsk_peer_node(tsk);
2591 peer_port = tsk_peer_port(tsk);
2592
2593 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2594
2595 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2596 goto msg_full;
2597 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2598 goto msg_full;
2599
2600 if (tsk->conn_type != 0) {
2601 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2602 goto msg_full;
2603 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2604 goto msg_full;
2605 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2606 goto msg_full;
2607 }
2608 nla_nest_end(skb, nest);
2609
2610 return 0;
2611
2612msg_full:
2613 nla_nest_cancel(skb, nest);
2614
2615 return -EMSGSIZE;
2616}
2617
2618/* Caller should hold socket lock for the passed tipc socket. */
d8182804
RA
2619static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2620 struct tipc_sock *tsk)
34b78a12
RA
2621{
2622 int err;
2623 void *hdr;
2624 struct nlattr *attrs;
34747539
YX
2625 struct net *net = sock_net(skb->sk);
2626 struct tipc_net *tn = net_generic(net, tipc_net_id);
34b78a12
RA
2627
2628 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
bfb3e5dd 2629 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
34b78a12
RA
2630 if (!hdr)
2631 goto msg_cancel;
2632
2633 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2634 if (!attrs)
2635 goto genlmsg_cancel;
07f6c4bc 2636 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
34b78a12 2637 goto attr_msg_cancel;
34747539 2638 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
34b78a12
RA
2639 goto attr_msg_cancel;
2640
2641 if (tsk->connected) {
2642 err = __tipc_nl_add_sk_con(skb, tsk);
2643 if (err)
2644 goto attr_msg_cancel;
2645 } else if (!list_empty(&tsk->publications)) {
2646 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2647 goto attr_msg_cancel;
2648 }
2649 nla_nest_end(skb, attrs);
2650 genlmsg_end(skb, hdr);
2651
2652 return 0;
2653
2654attr_msg_cancel:
2655 nla_nest_cancel(skb, attrs);
2656genlmsg_cancel:
2657 genlmsg_cancel(skb, hdr);
2658msg_cancel:
2659 return -EMSGSIZE;
2660}
2661
2662int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2663{
2664 int err;
2665 struct tipc_sock *tsk;
07f6c4bc
YX
2666 const struct bucket_table *tbl;
2667 struct rhash_head *pos;
e05b31f4
YX
2668 struct net *net = sock_net(skb->sk);
2669 struct tipc_net *tn = net_generic(net, tipc_net_id);
d6e164e3
RA
2670 u32 tbl_id = cb->args[0];
2671 u32 prev_portid = cb->args[1];
34b78a12 2672
07f6c4bc 2673 rcu_read_lock();
e05b31f4 2674 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
d6e164e3
RA
2675 for (; tbl_id < tbl->size; tbl_id++) {
2676 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
07f6c4bc 2677 spin_lock_bh(&tsk->sk.sk_lock.slock);
d6e164e3
RA
2678 if (prev_portid && prev_portid != tsk->portid) {
2679 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2680 continue;
2681 }
2682
07f6c4bc 2683 err = __tipc_nl_add_sk(skb, cb, tsk);
d6e164e3
RA
2684 if (err) {
2685 prev_portid = tsk->portid;
2686 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2687 goto out;
2688 }
2689 prev_portid = 0;
07f6c4bc 2690 spin_unlock_bh(&tsk->sk.sk_lock.slock);
07f6c4bc 2691 }
34b78a12 2692 }
d6e164e3 2693out:
07f6c4bc 2694 rcu_read_unlock();
d6e164e3
RA
2695 cb->args[0] = tbl_id;
2696 cb->args[1] = prev_portid;
34b78a12
RA
2697
2698 return skb->len;
2699}
1a1a143d
RA
2700
2701/* Caller should hold socket lock for the passed tipc socket. */
d8182804
RA
2702static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2703 struct netlink_callback *cb,
2704 struct publication *publ)
1a1a143d
RA
2705{
2706 void *hdr;
2707 struct nlattr *attrs;
2708
2709 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
bfb3e5dd 2710 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
1a1a143d
RA
2711 if (!hdr)
2712 goto msg_cancel;
2713
2714 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2715 if (!attrs)
2716 goto genlmsg_cancel;
2717
2718 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2719 goto attr_msg_cancel;
2720 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2721 goto attr_msg_cancel;
2722 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2723 goto attr_msg_cancel;
2724 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2725 goto attr_msg_cancel;
2726
2727 nla_nest_end(skb, attrs);
2728 genlmsg_end(skb, hdr);
2729
2730 return 0;
2731
2732attr_msg_cancel:
2733 nla_nest_cancel(skb, attrs);
2734genlmsg_cancel:
2735 genlmsg_cancel(skb, hdr);
2736msg_cancel:
2737 return -EMSGSIZE;
2738}
2739
2740/* Caller should hold socket lock for the passed tipc socket. */
d8182804
RA
2741static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2742 struct netlink_callback *cb,
2743 struct tipc_sock *tsk, u32 *last_publ)
1a1a143d
RA
2744{
2745 int err;
2746 struct publication *p;
2747
2748 if (*last_publ) {
2749 list_for_each_entry(p, &tsk->publications, pport_list) {
2750 if (p->key == *last_publ)
2751 break;
2752 }
2753 if (p->key != *last_publ) {
2754 /* We never set seq or call nl_dump_check_consistent()
2755 * this means that setting prev_seq here will cause the
2756 * consistence check to fail in the netlink callback
2757 * handler. Resulting in the last NLMSG_DONE message
2758 * having the NLM_F_DUMP_INTR flag set.
2759 */
2760 cb->prev_seq = 1;
2761 *last_publ = 0;
2762 return -EPIPE;
2763 }
2764 } else {
2765 p = list_first_entry(&tsk->publications, struct publication,
2766 pport_list);
2767 }
2768
2769 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2770 err = __tipc_nl_add_sk_publ(skb, cb, p);
2771 if (err) {
2772 *last_publ = p->key;
2773 return err;
2774 }
2775 }
2776 *last_publ = 0;
2777
2778 return 0;
2779}
2780
2781int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2782{
2783 int err;
07f6c4bc 2784 u32 tsk_portid = cb->args[0];
1a1a143d
RA
2785 u32 last_publ = cb->args[1];
2786 u32 done = cb->args[2];
e05b31f4 2787 struct net *net = sock_net(skb->sk);
1a1a143d
RA
2788 struct tipc_sock *tsk;
2789
07f6c4bc 2790 if (!tsk_portid) {
1a1a143d
RA
2791 struct nlattr **attrs;
2792 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2793
2794 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2795 if (err)
2796 return err;
2797
2798 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2799 attrs[TIPC_NLA_SOCK],
2800 tipc_nl_sock_policy);
2801 if (err)
2802 return err;
2803
2804 if (!sock[TIPC_NLA_SOCK_REF])
2805 return -EINVAL;
2806
07f6c4bc 2807 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1a1a143d
RA
2808 }
2809
2810 if (done)
2811 return 0;
2812
e05b31f4 2813 tsk = tipc_sk_lookup(net, tsk_portid);
1a1a143d
RA
2814 if (!tsk)
2815 return -EINVAL;
2816
2817 lock_sock(&tsk->sk);
2818 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2819 if (!err)
2820 done = 1;
2821 release_sock(&tsk->sk);
07f6c4bc 2822 sock_put(&tsk->sk);
1a1a143d 2823
07f6c4bc 2824 cb->args[0] = tsk_portid;
1a1a143d
RA
2825 cb->args[1] = last_publ;
2826 cb->args[2] = done;
2827
2828 return skb->len;
2829}