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