socket.c 72.6 KB
Newer Older
Per Liden's avatar
Per Liden committed
1
/*
2
 * net/tipc/socket.c: TIPC socket API
3
 *
4
 * Copyright (c) 2001-2007, 2012-2016, Ericsson AB
5
 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Liden's avatar
Per Liden committed
6 7
 * All rights reserved.
 *
Per Liden's avatar
Per Liden committed
8
 * Redistribution and use in source and binary forms, with or without
Per Liden's avatar
Per Liden committed
9 10
 * modification, are permitted provided that the following conditions are met:
 *
Per Liden's avatar
Per Liden committed
11 12 13 14 15 16 17 18
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
Per Liden's avatar
Per Liden committed
19
 *
Per Liden's avatar
Per Liden committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Liden's avatar
Per Liden committed
34 35 36
 * POSSIBILITY OF SUCH DAMAGE.
 */

37
#include <linux/rhashtable.h>
38 39
#include <linux/sched/signal.h>

Per Liden's avatar
Per Liden committed
40
#include "core.h"
41
#include "name_table.h"
42
#include "node.h"
43
#include "link.h"
44
#include "name_distr.h"
45
#include "socket.h"
46
#include "bcast.h"
47
#include "netlink.h"
48

49
#define CONN_TIMEOUT_DEFAULT	8000	/* default connect timeout = 8s */
50
#define CONN_PROBING_INTERVAL	msecs_to_jiffies(3600000)  /* [ms] => 1 h */
51 52 53
#define TIPC_FWD_MSG		1
#define TIPC_MAX_PORT		0xffffffff
#define TIPC_MIN_PORT		1
54

55 56
enum {
	TIPC_LISTEN = TCP_LISTEN,
57
	TIPC_ESTABLISHED = TCP_ESTABLISHED,
58
	TIPC_OPEN = TCP_CLOSE,
59
	TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
60
	TIPC_CONNECTING = TCP_SYN_SENT,
61 62
};

63 64 65 66 67 68 69
/**
 * struct tipc_sock - TIPC socket structure
 * @sk: socket - interacts with 'port' and with user via the socket API
 * @conn_type: TIPC type used when connection was established
 * @conn_instance: TIPC instance used when connection was established
 * @published: non-zero if port has one or more associated names
 * @max_pkt: maximum packet size "hint" used when building messages sent by port
70
 * @portid: unique port identity in TIPC socket hash table
71
 * @phdr: preformatted message header used when sending messages
72
 * #cong_links: list of congested links
73
 * @publications: list of publications for port
74
 * @blocking_link: address of the congested link we are currently sleeping on
75 76 77 78
 * @pub_count: total # of publications port has made during its lifetime
 * @probing_state:
 * @conn_timeout: the time we can wait for an unresponded setup request
 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
79
 * @cong_link_cnt: number of congested links
80 81
 * @sent_unacked: # messages sent by socket, and not yet acked by peer
 * @rcv_unacked: # messages read by user, but not yet acked back to peer
82
 * @peer: 'connected' peer for dgram/rdm
83
 * @node: hash table node
84
 * @mc_method: cookie for use between socket and broadcast layer
85
 * @rcu: rcu struct for tipc_sock
86 87 88 89 90 91 92
 */
struct tipc_sock {
	struct sock sk;
	u32 conn_type;
	u32 conn_instance;
	int published;
	u32 max_pkt;
93
	u32 portid;
94
	struct tipc_msg phdr;
95
	struct list_head cong_links;
96 97 98 99
	struct list_head publications;
	u32 pub_count;
	uint conn_timeout;
	atomic_t dupl_rcvcnt;
100
	bool probe_unacked;
101
	u16 cong_link_cnt;
102 103
	u16 snt_unacked;
	u16 snd_win;
104
	u16 peer_caps;
105 106
	u16 rcv_unacked;
	u16 rcv_win;
107
	struct sockaddr_tipc peer;
108
	struct rhash_head node;
109
	struct tipc_mc_method mc_method;
110
	struct rcu_head rcu;
111
};
Per Liden's avatar
Per Liden committed
112

113
static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
114
static void tipc_data_ready(struct sock *sk);
115
static void tipc_write_space(struct sock *sk);
116
static void tipc_sock_destruct(struct sock *sk);
117
static int tipc_release(struct socket *sock);
118 119
static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
		       bool kern);
120
static void tipc_sk_timeout(unsigned long data);
121
static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
122
			   struct tipc_name_seq const *seq);
123
static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
124
			    struct tipc_name_seq const *seq);
125
static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
126 127
static int tipc_sk_insert(struct tipc_sock *tsk);
static void tipc_sk_remove(struct tipc_sock *tsk);
128
static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
129
static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Liden's avatar
Per Liden committed
130

131 132 133
static const struct proto_ops packet_ops;
static const struct proto_ops stream_ops;
static const struct proto_ops msg_ops;
Per Liden's avatar
Per Liden committed
134
static struct proto tipc_proto;
135 136
static const struct rhashtable_params tsk_rht_params;

137 138 139 140 141
static u32 tsk_own_node(struct tipc_sock *tsk)
{
	return msg_prevnode(&tsk->phdr);
}

142
static u32 tsk_peer_node(struct tipc_sock *tsk)
143
{
144
	return msg_destnode(&tsk->phdr);
145 146
}

147
static u32 tsk_peer_port(struct tipc_sock *tsk)
148
{
149
	return msg_destport(&tsk->phdr);
150 151
}

152
static  bool tsk_unreliable(struct tipc_sock *tsk)
153
{
154
	return msg_src_droppable(&tsk->phdr) != 0;
155 156
}

157
static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
158
{
159
	msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
160 161
}

162
static bool tsk_unreturnable(struct tipc_sock *tsk)
163
{
164
	return msg_dest_droppable(&tsk->phdr) != 0;
165 166
}

167
static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
168
{
169
	msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
170 171
}

172
static int tsk_importance(struct tipc_sock *tsk)
173
{
174
	return msg_importance(&tsk->phdr);
175 176
}

177
static int tsk_set_importance(struct tipc_sock *tsk, int imp)
178 179 180
{
	if (imp > TIPC_CRITICAL_IMPORTANCE)
		return -EINVAL;
181
	msg_set_importance(&tsk->phdr, (u32)imp);
182 183
	return 0;
}
184

185 186 187 188 189
static struct tipc_sock *tipc_sk(const struct sock *sk)
{
	return container_of(sk, struct tipc_sock, sk);
}

190
static bool tsk_conn_cong(struct tipc_sock *tsk)
191
{
192
	return tsk->snt_unacked > tsk->snd_win;
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
}

/* tsk_blocks(): translate a buffer size in bytes to number of
 * advertisable blocks, taking into account the ratio truesize(len)/len
 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
 */
static u16 tsk_adv_blocks(int len)
{
	return len / FLOWCTL_BLK_SZ / 4;
}

/* tsk_inc(): increment counter for sent or received data
 * - If block based flow control is not supported by peer we
 *   fall back to message based ditto, incrementing the counter
 */
static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
{
	if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
		return ((msglen / FLOWCTL_BLK_SZ) + 1);
	return 1;
213 214
}

215
/**
216
 * tsk_advance_rx_queue - discard first buffer in socket receive queue
217 218
 *
 * Caller must hold socket lock
Per Liden's avatar
Per Liden committed
219
 */
220
static void tsk_advance_rx_queue(struct sock *sk)
Per Liden's avatar
Per Liden committed
221
{
222
	kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Liden's avatar
Per Liden committed
223 224
}

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/* tipc_sk_respond() : send response message back to sender
 */
static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
{
	u32 selector;
	u32 dnode;
	u32 onode = tipc_own_addr(sock_net(sk));

	if (!tipc_msg_reverse(onode, &skb, err))
		return;

	dnode = msg_destnode(buf_msg(skb));
	selector = msg_origport(buf_msg(skb));
	tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
}

Per Liden's avatar
Per Liden committed
241
/**
242
 * tsk_rej_rx_queue - reject all buffers in socket receive queue
243 244
 *
 * Caller must hold socket lock
Per Liden's avatar
Per Liden committed
245
 */
246
static void tsk_rej_rx_queue(struct sock *sk)
Per Liden's avatar
Per Liden committed
247
{
248
	struct sk_buff *skb;
249

250 251
	while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
		tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Per Liden's avatar
Per Liden committed
252 253
}

254 255
static bool tipc_sk_connected(struct sock *sk)
{
256
	return sk->sk_state == TIPC_ESTABLISHED;
257 258
}

259 260 261 262 263 264 265 266 267 268
/* tipc_sk_type_connectionless - check if the socket is datagram socket
 * @sk: socket
 *
 * Returns true if connection less, false otherwise
 */
static bool tipc_sk_type_connectionless(struct sock *sk)
{
	return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
}

269
/* tsk_peer_msg - verify if message was sent by connected port's peer
270 271 272 273
 *
 * Handles cases where the node's network address has changed from
 * the default of <0.0.0> to its configured setting.
 */
274
static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
275
{
276 277
	struct sock *sk = &tsk->sk;
	struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
278
	u32 peer_port = tsk_peer_port(tsk);
279 280 281
	u32 orig_node;
	u32 peer_node;

282
	if (unlikely(!tipc_sk_connected(sk)))
283 284 285 286 287 288
		return false;

	if (unlikely(msg_origport(msg) != peer_port))
		return false;

	orig_node = msg_orignode(msg);
289
	peer_node = tsk_peer_node(tsk);
290 291 292 293

	if (likely(orig_node == peer_node))
		return true;

294
	if (!orig_node && (peer_node == tn->own_addr))
295 296
		return true;

297
	if (!peer_node && (orig_node == tn->own_addr))
298 299 300 301 302
		return true;

	return false;
}

303 304 305 306 307 308 309 310 311
/* tipc_set_sk_state - set the sk_state of the socket
 * @sk: socket
 *
 * Caller must hold socket lock
 *
 * Returns 0 on success, errno otherwise
 */
static int tipc_set_sk_state(struct sock *sk, int state)
{
312
	int oldsk_state = sk->sk_state;
313 314 315
	int res = -EINVAL;

	switch (state) {
316 317 318
	case TIPC_OPEN:
		res = 0;
		break;
319
	case TIPC_LISTEN:
320
	case TIPC_CONNECTING:
321
		if (oldsk_state == TIPC_OPEN)
322 323
			res = 0;
		break;
324
	case TIPC_ESTABLISHED:
325
		if (oldsk_state == TIPC_CONNECTING ||
326
		    oldsk_state == TIPC_OPEN)
327 328
			res = 0;
		break;
329
	case TIPC_DISCONNECTING:
330
		if (oldsk_state == TIPC_CONNECTING ||
331 332 333
		    oldsk_state == TIPC_ESTABLISHED)
			res = 0;
		break;
334 335 336 337 338 339 340 341
	}

	if (!res)
		sk->sk_state = state;

	return res;
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
static int tipc_sk_sock_err(struct socket *sock, long *timeout)
{
	struct sock *sk = sock->sk;
	int err = sock_error(sk);
	int typ = sock->type;

	if (err)
		return err;
	if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
		if (sk->sk_state == TIPC_DISCONNECTING)
			return -EPIPE;
		else if (!tipc_sk_connected(sk))
			return -ENOTCONN;
	}
	if (!*timeout)
		return -EAGAIN;
	if (signal_pending(current))
		return sock_intr_errno(*timeout);

	return 0;
}

#define tipc_wait_for_cond(sock_, timeout_, condition_)			\
({								        \
	int rc_ = 0;							\
	int done_ = 0;							\
									\
	while (!(condition_) && !done_) {				\
		struct sock *sk_ = sock->sk;				\
		DEFINE_WAIT_FUNC(wait_, woken_wake_function);		\
									\
		rc_ = tipc_sk_sock_err(sock_, timeout_);		\
		if (rc_)						\
			break;						\
		prepare_to_wait(sk_sleep(sk_), &wait_,			\
				TASK_INTERRUPTIBLE);			\
		done_ = sk_wait_event(sk_, timeout_,			\
				      (condition_), &wait_);		\
		remove_wait_queue(sk_sleep(sk_), &wait_);		\
	}								\
	rc_;								\
})

Per Liden's avatar
Per Liden committed
385
/**
386
 * tipc_sk_create - create a TIPC socket
387
 * @net: network namespace (must be default network)
Per Liden's avatar
Per Liden committed
388 389
 * @sock: pre-allocated socket structure
 * @protocol: protocol indicator (must be 0)
390
 * @kern: caused by kernel or by userspace?
391
 *
392 393
 * This routine creates additional data structures used by the TIPC socket,
 * initializes them, and links them together.
Per Liden's avatar
Per Liden committed
394 395 396
 *
 * Returns 0 on success, errno otherwise
 */
397 398
static int tipc_sk_create(struct net *net, struct socket *sock,
			  int protocol, int kern)
Per Liden's avatar
Per Liden committed
399
{
400
	struct tipc_net *tn;
401
	const struct proto_ops *ops;
Per Liden's avatar
Per Liden committed
402
	struct sock *sk;
403
	struct tipc_sock *tsk;
404
	struct tipc_msg *msg;
405 406

	/* Validate arguments */
Per Liden's avatar
Per Liden committed
407 408 409 410 411
	if (unlikely(protocol != 0))
		return -EPROTONOSUPPORT;

	switch (sock->type) {
	case SOCK_STREAM:
412
		ops = &stream_ops;
Per Liden's avatar
Per Liden committed
413 414
		break;
	case SOCK_SEQPACKET:
415
		ops = &packet_ops;
Per Liden's avatar
Per Liden committed
416 417 418
		break;
	case SOCK_DGRAM:
	case SOCK_RDM:
419
		ops = &msg_ops;
Per Liden's avatar
Per Liden committed
420
		break;
421 422
	default:
		return -EPROTOTYPE;
Per Liden's avatar
Per Liden committed
423 424
	}

425
	/* Allocate socket's protocol area */
426
	sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
427
	if (sk == NULL)
Per Liden's avatar
Per Liden committed
428 429
		return -ENOMEM;

430
	tsk = tipc_sk(sk);
431 432
	tsk->max_pkt = MAX_PKT_DEFAULT;
	INIT_LIST_HEAD(&tsk->publications);
433
	INIT_LIST_HEAD(&tsk->cong_links);
434
	msg = &tsk->phdr;
435
	tn = net_generic(sock_net(sk), tipc_net_id);
Per Liden's avatar
Per Liden committed
436

437 438 439
	/* Finish initializing socket data structures */
	sock->ops = ops;
	sock_init_data(sock, sk);
440
	tipc_set_sk_state(sk, TIPC_OPEN);
441
	if (tipc_sk_insert(tsk)) {
Masanari Iida's avatar
Masanari Iida committed
442
		pr_warn("Socket create failed; port number exhausted\n");
443 444
		return -EINVAL;
	}
445 446 447 448 449 450 451

	/* Ensure tsk is visible before we read own_addr. */
	smp_mb();

	tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
		      NAMED_H_SIZE, 0);

452
	msg_set_origport(msg, tsk->portid);
453
	setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
454
	sk->sk_shutdown = 0;
455
	sk->sk_backlog_rcv = tipc_backlog_rcv;
456
	sk->sk_rcvbuf = sysctl_tipc_rmem[1];
457 458
	sk->sk_data_ready = tipc_data_ready;
	sk->sk_write_space = tipc_write_space;
459
	sk->sk_destruct = tipc_sock_destruct;
460 461
	tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
	atomic_set(&tsk->dupl_rcvcnt, 0);
462

463 464 465 466
	/* Start out with safe limits until we receive an advertised window */
	tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
	tsk->rcv_win = tsk->snd_win;

467
	if (tipc_sk_type_connectionless(sk)) {
468
		tsk_set_unreturnable(tsk, true);
469
		if (sock->type == SOCK_DGRAM)
470
			tsk_set_unreliable(tsk, true);
471
	}
472

Per Liden's avatar
Per Liden committed
473 474 475
	return 0;
}

476 477 478 479 480 481 482
static void tipc_sk_callback(struct rcu_head *head)
{
	struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);

	sock_put(&tsk->sk);
}

483 484 485 486 487 488
/* Caller should hold socket lock for the socket. */
static void __tipc_shutdown(struct socket *sock, int error)
{
	struct sock *sk = sock->sk;
	struct tipc_sock *tsk = tipc_sk(sk);
	struct net *net = sock_net(sk);
489
	long timeout = CONN_TIMEOUT_DEFAULT;
490 491 492
	u32 dnode = tsk_peer_node(tsk);
	struct sk_buff *skb;

493 494 495 496
	/* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
	tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
					    !tsk_conn_cong(tsk)));

497 498 499 500 501 502
	/* Reject all unreceived messages, except on an active connection
	 * (which disconnects locally & sends a 'FIN+' to peer).
	 */
	while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
		if (TIPC_SKB_CB(skb)->bytes_read) {
			kfree_skb(skb);
503
			continue;
504
		}
505 506 507 508 509 510
		if (!tipc_sk_type_connectionless(sk) &&
		    sk->sk_state != TIPC_DISCONNECTING) {
			tipc_set_sk_state(sk, TIPC_DISCONNECTING);
			tipc_node_remove_conn(net, dnode, tsk->portid);
		}
		tipc_sk_respond(sk, skb, error);
511
	}
512 513 514 515

	if (tipc_sk_type_connectionless(sk))
		return;

516 517 518 519 520 521 522
	if (sk->sk_state != TIPC_DISCONNECTING) {
		skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
				      TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
				      tsk_own_node(tsk), tsk_peer_port(tsk),
				      tsk->portid, error);
		if (skb)
			tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
523 524
		tipc_node_remove_conn(net, dnode, tsk->portid);
		tipc_set_sk_state(sk, TIPC_DISCONNECTING);
525 526 527
	}
}

Per Liden's avatar
Per Liden committed
528
/**
529
 * tipc_release - destroy a TIPC socket
Per Liden's avatar
Per Liden committed
530 531 532 533 534 535 536
 * @sock: socket to destroy
 *
 * This routine cleans up any messages that are still queued on the socket.
 * For DGRAM and RDM socket types, all queued messages are rejected.
 * For SEQPACKET and STREAM socket types, the first message is rejected
 * and any others are discarded.  (If the first message on a STREAM socket
 * is partially-read, it is discarded and the next one is rejected instead.)
537
 *
Per Liden's avatar
Per Liden committed
538 539 540 541 542 543
 * NOTE: Rejected messages are not necessarily returned to the sender!  They
 * are returned or discarded according to the "destination droppable" setting
 * specified for the message by the sender.
 *
 * Returns 0 on success, errno otherwise
 */
544
static int tipc_release(struct socket *sock)
Per Liden's avatar
Per Liden committed
545 546
{
	struct sock *sk = sock->sk;
547
	struct tipc_sock *tsk;
Per Liden's avatar
Per Liden committed
548

549 550 551 552 553
	/*
	 * Exit if socket isn't fully initialized (occurs when a failed accept()
	 * releases a pre-allocated child socket that was never used)
	 */
	if (sk == NULL)
Per Liden's avatar
Per Liden committed
554
		return 0;
555

556
	tsk = tipc_sk(sk);
557 558
	lock_sock(sk);

559 560
	__tipc_shutdown(sock, TIPC_ERR_NO_PORT);
	sk->sk_shutdown = SHUTDOWN_MASK;
561
	tipc_sk_withdraw(tsk, 0, NULL);
562
	sk_stop_timer(sk, &sk->sk_timer);
563
	tipc_sk_remove(tsk);
Per Liden's avatar
Per Liden committed
564

565 566
	/* Reject any messages that accumulated in backlog queue */
	release_sock(sk);
567 568
	u32_list_purge(&tsk->cong_links);
	tsk->cong_link_cnt = 0;
569
	call_rcu(&tsk->rcu, tipc_sk_callback);
570
	sock->sk = NULL;
Per Liden's avatar
Per Liden committed
571

572
	return 0;
Per Liden's avatar
Per Liden committed
573 574 575
}

/**
576
 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Liden's avatar
Per Liden committed
577 578 579
 * @sock: socket structure
 * @uaddr: socket address describing name(s) and desired operation
 * @uaddr_len: size of socket address data structure
580
 *
Per Liden's avatar
Per Liden committed
581 582 583
 * Name and name sequence binding is indicated using a positive scope value;
 * a negative scope value unbinds the specified name.  Specifying no name
 * (i.e. a socket address length of 0) unbinds all names from the socket.
584
 *
Per Liden's avatar
Per Liden committed
585
 * Returns 0 on success, errno otherwise
586 587 588
 *
 * NOTE: This routine doesn't need to take the socket lock since it doesn't
 *       access any non-constant socket information.
Per Liden's avatar
Per Liden committed
589
 */
590 591
static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
		     int uaddr_len)
Per Liden's avatar
Per Liden committed
592
{
593
	struct sock *sk = sock->sk;
Per Liden's avatar
Per Liden committed
594
	struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
595
	struct tipc_sock *tsk = tipc_sk(sk);
596
	int res = -EINVAL;
Per Liden's avatar
Per Liden committed
597

598 599
	lock_sock(sk);
	if (unlikely(!uaddr_len)) {
600
		res = tipc_sk_withdraw(tsk, 0, NULL);
601 602
		goto exit;
	}
603

604 605 606 607 608 609 610 611
	if (uaddr_len < sizeof(struct sockaddr_tipc)) {
		res = -EINVAL;
		goto exit;
	}
	if (addr->family != AF_TIPC) {
		res = -EAFNOSUPPORT;
		goto exit;
	}
Per Liden's avatar
Per Liden committed
612 613 614

	if (addr->addrtype == TIPC_ADDR_NAME)
		addr->addr.nameseq.upper = addr->addr.nameseq.lower;
615 616 617 618
	else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
		res = -EAFNOSUPPORT;
		goto exit;
	}
619

620
	if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
621
	    (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
622 623 624 625
	    (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
		res = -EACCES;
		goto exit;
	}
626

627
	res = (addr->scope > 0) ?
628 629
		tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
		tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
630 631 632
exit:
	release_sock(sk);
	return res;
Per Liden's avatar
Per Liden committed
633 634
}

635
/**
636
 * tipc_getname - get port ID of socket or peer socket
Per Liden's avatar
Per Liden committed
637 638 639
 * @sock: socket structure
 * @uaddr: area for returned socket address
 * @uaddr_len: area for returned length of socket address
640
 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
641
 *
Per Liden's avatar
Per Liden committed
642
 * Returns 0 on success, errno otherwise
643
 *
644 645
 * NOTE: This routine doesn't need to take the socket lock since it only
 *       accesses socket information that is unchanging (or which changes in
646
 *       a completely predictable manner).
Per Liden's avatar
Per Liden committed
647
 */
648 649
static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
			int *uaddr_len, int peer)
Per Liden's avatar
Per Liden committed
650 651
{
	struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
652 653
	struct sock *sk = sock->sk;
	struct tipc_sock *tsk = tipc_sk(sk);
654
	struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Liden's avatar
Per Liden committed
655

656
	memset(addr, 0, sizeof(*addr));
657
	if (peer) {
658
		if ((!tipc_sk_connected(sk)) &&
659
		    ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
660
			return -ENOTCONN;
661 662
		addr->addr.id.ref = tsk_peer_port(tsk);
		addr->addr.id.node = tsk_peer_node(tsk);
663
	} else {
664
		addr->addr.id.ref = tsk->portid;
665
		addr->addr.id.node = tn->own_addr;
666
	}
Per Liden's avatar
Per Liden committed
667 668 669 670 671 672 673

	*uaddr_len = sizeof(*addr);
	addr->addrtype = TIPC_ADDR_ID;
	addr->family = AF_TIPC;
	addr->scope = 0;
	addr->addr.name.domain = 0;

674
	return 0;
Per Liden's avatar
Per Liden committed
675 676 677
}

/**
678
 * tipc_poll - read and possibly block on pollmask
Per Liden's avatar
Per Liden committed
679 680 681 682
 * @file: file structure associated with the socket
 * @sock: socket for which to calculate the poll bits
 * @wait: ???
 *
683 684 685 686 687 688 689 690
 * Returns pollmask value
 *
 * COMMENTARY:
 * It appears that the usual socket locking mechanisms are not useful here
 * since the pollmask info is potentially out-of-date the moment this routine
 * exits.  TCP and other protocols seem to rely on higher level poll routines
 * to handle any preventable race conditions, so TIPC will do the same ...
 *
691 692 693
 * IMPORTANT: The fact that a read or write operation is indicated does NOT
 * imply that the operation will succeed, merely that it should be performed
 * and will not block.
Per Liden's avatar
Per Liden committed
694
 */
695 696
static unsigned int tipc_poll(struct file *file, struct socket *sock,
			      poll_table *wait)
Per Liden's avatar
Per Liden committed
697
{
698
	struct sock *sk = sock->sk;
699
	struct tipc_sock *tsk = tipc_sk(sk);
700
	u32 mask = 0;
701

702
	sock_poll_wait(file, sk_sleep(sk), wait);
703

704 705 706 707 708
	if (sk->sk_shutdown & RCV_SHUTDOWN)
		mask |= POLLRDHUP | POLLIN | POLLRDNORM;
	if (sk->sk_shutdown == SHUTDOWN_MASK)
		mask |= POLLHUP;

709 710
	switch (sk->sk_state) {
	case TIPC_ESTABLISHED:
711
		if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
712
			mask |= POLLOUT;
713 714 715
		/* fall thru' */
	case TIPC_LISTEN:
	case TIPC_CONNECTING:
716 717
		if (!skb_queue_empty(&sk->sk_receive_queue))
			mask |= (POLLIN | POLLRDNORM);
718 719
		break;
	case TIPC_OPEN:
720
		if (!tsk->cong_link_cnt)
721 722 723 724 725 726 727 728
			mask |= POLLOUT;
		if (tipc_sk_type_connectionless(sk) &&
		    (!skb_queue_empty(&sk->sk_receive_queue)))
			mask |= (POLLIN | POLLRDNORM);
		break;
	case TIPC_DISCONNECTING:
		mask = (POLLIN | POLLRDNORM | POLLHUP);
		break;
729
	}
730 731

	return mask;
Per Liden's avatar
Per Liden committed
732 733
}

734 735 736 737
/**
 * tipc_sendmcast - send multicast message
 * @sock: socket structure
 * @seq: destination address
738
 * @msg: message to send
739 740
 * @dlen: length of data to send
 * @timeout: timeout to wait for wakeup
741 742 743 744 745
 *
 * Called from function tipc_sendmsg(), which has done all sanity checks
 * Returns the number of bytes sent on success, or errno
 */
static int tipc_sendmcast(struct  socket *sock, struct tipc_name_seq *seq,
746
			  struct msghdr *msg, size_t dlen, long timeout)
747 748
{
	struct sock *sk = sock->sk;
749
	struct tipc_sock *tsk = tipc_sk(sk);
750
	struct tipc_msg *hdr = &tsk->phdr;
751
	struct net *net = sock_net(sk);
752
	int mtu = tipc_bcast_get_mtu(net);
753
	struct tipc_mc_method *method = &tsk->mc_method;
754
	u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE);
755
	struct sk_buff_head pkts;
756
	struct tipc_nlist dsts;
757 758
	int rc;

759
	/* Block or return if any destination link is congested */
760 761 762
	rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
	if (unlikely(rc))
		return rc;
763

764 765 766 767 768 769 770 771
	/* Lookup destination nodes */
	tipc_nlist_init(&dsts, tipc_own_addr(net));
	tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
				      seq->upper, domain, &dsts);
	if (!dsts.local && !dsts.remote)
		return -EHOSTUNREACH;

	/* Build message header */
772
	msg_set_type(hdr, TIPC_MCAST_MSG);
773
	msg_set_hdr_sz(hdr, MCAST_H_SIZE);
774 775 776 777 778 779 780
	msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
	msg_set_destport(hdr, 0);
	msg_set_destnode(hdr, 0);
	msg_set_nametype(hdr, seq->type);
	msg_set_namelower(hdr, seq->lower);
	msg_set_nameupper(hdr, seq->upper);

781
	/* Build message as chain of buffers */
782 783
	skb_queue_head_init(&pkts);
	rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
784

785 786
	/* Send message if build was successful */
	if (unlikely(rc == dlen))
787
		rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
788 789 790
				     &tsk->cong_link_cnt);

	tipc_nlist_purge(&dsts);
791 792

	return rc ? rc : dlen;
793 794
}

795 796 797 798 799 800
/**
 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
 * @arrvq: queue with arriving messages, to be cloned after destination lookup
 * @inputq: queue with cloned messages, delivered to socket after dest lookup
 *
 * Multi-threaded: parallel calls with reference to same queues may occur
801
 */
802 803
void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
		       struct sk_buff_head *inputq)
804
{
805
	struct tipc_msg *msg;
806
	struct list_head dports;
807
	u32 portid;
808
	u32 scope = TIPC_CLUSTER_SCOPE;
809 810 811
	struct sk_buff_head tmpq;
	uint hsz;
	struct sk_buff *skb, *_skb;
812

813
	__skb_queue_head_init(&tmpq);
814
	INIT_LIST_HEAD(&dports);
815

816 817 818 819 820 821 822