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