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