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