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