port.c 32.7 KB
Newer Older
P
Per Liden 已提交
1 2
/*
 * net/tipc/port.c: TIPC port code
3
 *
4
 * Copyright (c) 1992-2007, Ericsson AB
5
 * Copyright (c) 2004-2008, 2010-2011, Wind River Systems
P
Per Liden 已提交
6 7
 * All rights reserved.
 *
P
Per Liden 已提交
8
 * Redistribution and use in source and binary forms, with or without
P
Per Liden 已提交
9 10
 * modification, are permitted provided that the following conditions are met:
 *
P
Per Liden 已提交
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.
P
Per Liden 已提交
19
 *
P
Per Liden 已提交
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
P
Per Liden 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "config.h"
#include "port.h"
#include "name_table.h"

/* Connection management: */
#define PROBING_INTERVAL 3600000	/* [ms] => 1 h */
#define CONFIRMED 0
#define PROBING 1

#define MAX_REJECT_SIZE 1024

49 50
static struct sk_buff *msg_queue_head;
static struct sk_buff *msg_queue_tail;
P
Per Liden 已提交
51

I
Ingo Molnar 已提交
52 53
DEFINE_SPINLOCK(tipc_port_list_lock);
static DEFINE_SPINLOCK(queue_lock);
P
Per Liden 已提交
54

55
static LIST_HEAD(ports);
P
Per Liden 已提交
56
static void port_handle_node_down(unsigned long ref);
57 58
static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
P
Per Liden 已提交
59 60 61
static void port_timeout(unsigned long ref);


62
static inline u32 port_peernode(struct tipc_port *p_ptr)
P
Per Liden 已提交
63
{
64
	return msg_destnode(&p_ptr->phdr);
P
Per Liden 已提交
65 66
}

67
static inline u32 port_peerport(struct tipc_port *p_ptr)
P
Per Liden 已提交
68
{
69
	return msg_destport(&p_ptr->phdr);
P
Per Liden 已提交
70 71
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/*
 * tipc_port_peer_msg - verify message was sent by connected port's peer
 *
 * Handles cases where the node's network address has changed from
 * the default of <0.0.0> to its configured setting.
 */

int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
{
	u32 peernode;
	u32 orignode;

	if (msg_origport(msg) != port_peerport(p_ptr))
		return 0;

	orignode = msg_orignode(msg);
	peernode = port_peernode(p_ptr);
	return (orignode == peernode) ||
		(!orignode && (peernode == tipc_own_addr)) ||
		(!peernode && (orignode == tipc_own_addr));
}

P
Per Liden 已提交
94 95 96 97
/**
 * tipc_multicast - send a multicast message to local and remote destinations
 */

98
int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
99 100
		   u32 num_sect, struct iovec const *msg_sect,
		   unsigned int total_len)
P
Per Liden 已提交
101 102 103 104
{
	struct tipc_msg *hdr;
	struct sk_buff *buf;
	struct sk_buff *ibuf = NULL;
105
	struct tipc_port_list dports = {0, NULL, };
106
	struct tipc_port *oport = tipc_port_deref(ref);
P
Per Liden 已提交
107 108 109 110 111 112 113 114
	int ext_targets;
	int res;

	if (unlikely(!oport))
		return -EINVAL;

	/* Create multicast message */

115
	hdr = &oport->phdr;
P
Per Liden 已提交
116
	msg_set_type(hdr, TIPC_MCAST_MSG);
117
	msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
118 119
	msg_set_destport(hdr, 0);
	msg_set_destnode(hdr, 0);
P
Per Liden 已提交
120 121 122 123
	msg_set_nametype(hdr, seq->type);
	msg_set_namelower(hdr, seq->lower);
	msg_set_nameupper(hdr, seq->upper);
	msg_set_hdr_sz(hdr, MCAST_H_SIZE);
124
	res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
P
Per Liden 已提交
125 126 127 128 129 130
			!oport->user_port, &buf);
	if (unlikely(!buf))
		return res;

	/* Figure out where to send multicast message */

131 132
	ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
						TIPC_NODE_SCOPE, &dports);
133 134

	/* Send message to destinations (duplicate it only if necessary) */
P
Per Liden 已提交
135 136 137 138 139

	if (ext_targets) {
		if (dports.count != 0) {
			ibuf = skb_copy(buf, GFP_ATOMIC);
			if (ibuf == NULL) {
140
				tipc_port_list_free(&dports);
141
				kfree_skb(buf);
P
Per Liden 已提交
142 143 144
				return -ENOMEM;
			}
		}
145
		res = tipc_bclink_send_msg(buf);
146
		if ((res < 0) && (dports.count != 0))
147
			kfree_skb(ibuf);
P
Per Liden 已提交
148 149 150 151 152 153
	} else {
		ibuf = buf;
	}

	if (res >= 0) {
		if (ibuf)
154
			tipc_port_recv_mcast(ibuf, &dports);
P
Per Liden 已提交
155
	} else {
156
		tipc_port_list_free(&dports);
P
Per Liden 已提交
157 158 159 160 161
	}
	return res;
}

/**
162
 * tipc_port_recv_mcast - deliver multicast message to all destination ports
163
 *
P
Per Liden 已提交
164 165 166
 * If there is no port list, perform a lookup to create one
 */

167
void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp)
P
Per Liden 已提交
168
{
169
	struct tipc_msg *msg;
170 171
	struct tipc_port_list dports = {0, NULL, };
	struct tipc_port_list *item = dp;
P
Per Liden 已提交
172 173 174 175 176 177 178
	int cnt = 0;

	msg = buf_msg(buf);

	/* Create destination port list, if one wasn't supplied */

	if (dp == NULL) {
179
		tipc_nametbl_mc_translate(msg_nametype(msg),
P
Per Liden 已提交
180 181 182 183 184 185 186 187 188 189
				     msg_namelower(msg),
				     msg_nameupper(msg),
				     TIPC_CLUSTER_SCOPE,
				     &dports);
		item = dp = &dports;
	}

	/* Deliver a copy of message to each destination port */

	if (dp->count != 0) {
190
		msg_set_destnode(msg, tipc_own_addr);
P
Per Liden 已提交
191 192
		if (dp->count == 1) {
			msg_set_destport(msg, dp->ports[0]);
193 194
			tipc_port_recv_msg(buf);
			tipc_port_list_free(dp);
P
Per Liden 已提交
195 196 197 198 199 200 201
			return;
		}
		for (; cnt < dp->count; cnt++) {
			int index = cnt % PLSIZE;
			struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);

			if (b == NULL) {
202
				warn("Unable to deliver multicast message(s)\n");
P
Per Liden 已提交
203 204
				goto exit;
			}
205
			if ((index == 0) && (cnt != 0))
P
Per Liden 已提交
206
				item = item->next;
207
			msg_set_destport(buf_msg(b), item->ports[index]);
208
			tipc_port_recv_msg(b);
P
Per Liden 已提交
209 210 211
		}
	}
exit:
212
	kfree_skb(buf);
213
	tipc_port_list_free(dp);
P
Per Liden 已提交
214 215 216
}

/**
217
 * tipc_createport_raw - create a generic TIPC port
218
 *
219
 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
P
Per Liden 已提交
220 221
 */

222
struct tipc_port *tipc_createport_raw(void *usr_handle,
P
Per Liden 已提交
223 224
			u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
			void (*wakeup)(struct tipc_port *),
225
			const u32 importance)
P
Per Liden 已提交
226
{
227
	struct tipc_port *p_ptr;
P
Per Liden 已提交
228 229 230
	struct tipc_msg *msg;
	u32 ref;

231
	p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
232 233
	if (!p_ptr) {
		warn("Port creation failed, no memory\n");
234
		return NULL;
P
Per Liden 已提交
235
	}
236
	ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
P
Per Liden 已提交
237
	if (!ref) {
238
		warn("Port creation failed, reference table exhausted\n");
P
Per Liden 已提交
239
		kfree(p_ptr);
240
		return NULL;
P
Per Liden 已提交
241 242
	}

243 244 245
	p_ptr->usr_handle = usr_handle;
	p_ptr->max_pkt = MAX_PKT_DEFAULT;
	p_ptr->ref = ref;
P
Per Liden 已提交
246 247 248 249
	INIT_LIST_HEAD(&p_ptr->wait_list);
	INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
	p_ptr->dispatcher = dispatcher;
	p_ptr->wakeup = wakeup;
250
	p_ptr->user_port = NULL;
P
Per Liden 已提交
251 252 253
	k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
	INIT_LIST_HEAD(&p_ptr->publications);
	INIT_LIST_HEAD(&p_ptr->port_list);
254 255 256 257 258 259 260 261 262 263 264

	/*
	 * Must hold port list lock while initializing message header template
	 * to ensure a change to node's own network address doesn't result
	 * in template containing out-dated network address information
	 */

	spin_lock_bh(&tipc_port_list_lock);
	msg = &p_ptr->phdr;
	tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
	msg_set_origport(msg, ref);
P
Per Liden 已提交
265
	list_add_tail(&p_ptr->port_list, &ports);
266
	spin_unlock_bh(&tipc_port_list_lock);
267
	return p_ptr;
P
Per Liden 已提交
268 269 270 271
}

int tipc_deleteport(u32 ref)
{
272
	struct tipc_port *p_ptr;
273
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
274

275
	tipc_withdraw(ref, 0, NULL);
276
	p_ptr = tipc_port_lock(ref);
277
	if (!p_ptr)
P
Per Liden 已提交
278 279
		return -EINVAL;

280 281
	tipc_ref_discard(ref);
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
282 283

	k_cancel_timer(&p_ptr->timer);
284
	if (p_ptr->connected) {
P
Per Liden 已提交
285
		buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
286
		tipc_nodesub_unsubscribe(&p_ptr->subscription);
P
Per Liden 已提交
287
	}
288
	kfree(p_ptr->user_port);
P
Per Liden 已提交
289

290
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
291 292
	list_del(&p_ptr->port_list);
	list_del(&p_ptr->wait_list);
293
	spin_unlock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
294 295
	k_term_timer(&p_ptr->timer);
	kfree(p_ptr);
296
	tipc_net_route_msg(buf);
297
	return 0;
P
Per Liden 已提交
298 299
}

300
static int port_unreliable(struct tipc_port *p_ptr)
P
Per Liden 已提交
301
{
302
	return msg_src_droppable(&p_ptr->phdr);
P
Per Liden 已提交
303 304 305 306
}

int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
{
307
	struct tipc_port *p_ptr;
308

309
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
310 311 312
	if (!p_ptr)
		return -EINVAL;
	*isunreliable = port_unreliable(p_ptr);
J
Julia Lawall 已提交
313
	tipc_port_unlock(p_ptr);
314
	return 0;
P
Per Liden 已提交
315 316 317 318
}

int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
{
319
	struct tipc_port *p_ptr;
320

321
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
322 323
	if (!p_ptr)
		return -EINVAL;
324
	msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
325
	tipc_port_unlock(p_ptr);
326
	return 0;
P
Per Liden 已提交
327 328
}

329
static int port_unreturnable(struct tipc_port *p_ptr)
P
Per Liden 已提交
330
{
331
	return msg_dest_droppable(&p_ptr->phdr);
P
Per Liden 已提交
332 333 334 335
}

int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
{
336
	struct tipc_port *p_ptr;
337

338
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
339 340 341
	if (!p_ptr)
		return -EINVAL;
	*isunrejectable = port_unreturnable(p_ptr);
J
Julia Lawall 已提交
342
	tipc_port_unlock(p_ptr);
343
	return 0;
P
Per Liden 已提交
344 345 346 347
}

int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
{
348
	struct tipc_port *p_ptr;
349

350
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
351 352
	if (!p_ptr)
		return -EINVAL;
353
	msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
354
	tipc_port_unlock(p_ptr);
355
	return 0;
P
Per Liden 已提交
356 357
}

358
/*
359 360 361
 * port_build_proto_msg(): create connection protocol message for port
 *
 * On entry the port must be locked and connected.
P
Per Liden 已提交
362
 */
363 364
static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
					    u32 type, u32 ack)
P
Per Liden 已提交
365 366 367
{
	struct sk_buff *buf;
	struct tipc_msg *msg;
368

369
	buf = tipc_buf_acquire(INT_H_SIZE);
P
Per Liden 已提交
370 371
	if (buf) {
		msg = buf_msg(buf);
372 373 374 375
		tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
			      port_peernode(p_ptr));
		msg_set_destport(msg, port_peerport(p_ptr));
		msg_set_origport(msg, p_ptr->ref);
P
Per Liden 已提交
376 377 378 379 380 381 382 383 384 385 386
		msg_set_msgcnt(msg, ack);
	}
	return buf;
}

int tipc_reject_msg(struct sk_buff *buf, u32 err)
{
	struct tipc_msg *msg = buf_msg(buf);
	struct sk_buff *rbuf;
	struct tipc_msg *rmsg;
	int hdr_sz;
387
	u32 imp;
P
Per Liden 已提交
388
	u32 data_sz = msg_data_sz(msg);
389
	u32 src_node;
390
	u32 rmsg_sz;
P
Per Liden 已提交
391 392

	/* discard rejected message if it shouldn't be returned to sender */
393 394 395 396 397 398

	if (WARN(!msg_isdata(msg),
		 "attempt to reject message with user=%u", msg_user(msg))) {
		dump_stack();
		goto exit;
	}
399 400
	if (msg_errcode(msg) || msg_dest_droppable(msg))
		goto exit;
P
Per Liden 已提交
401

402 403 404 405 406 407 408 409 410
	/*
	 * construct returned message by copying rejected message header and
	 * data (or subset), then updating header fields that need adjusting
	 */

	hdr_sz = msg_hdr_sz(msg);
	rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);

	rbuf = tipc_buf_acquire(rmsg_sz);
411 412 413
	if (rbuf == NULL)
		goto exit;

P
Per Liden 已提交
414
	rmsg = buf_msg(rbuf);
415 416 417 418 419 420
	skb_copy_to_linear_data(rbuf, msg, rmsg_sz);

	if (msg_connected(rmsg)) {
		imp = msg_importance(rmsg);
		if (imp < TIPC_CRITICAL_IMPORTANCE)
			msg_set_importance(rmsg, ++imp);
421
	}
422 423 424 425 426 427 428
	msg_set_non_seq(rmsg, 0);
	msg_set_size(rmsg, rmsg_sz);
	msg_set_errcode(rmsg, err);
	msg_set_prevnode(rmsg, tipc_own_addr);
	msg_swap_words(rmsg, 4, 5);
	if (!msg_short(rmsg))
		msg_swap_words(rmsg, 6, 7);
P
Per Liden 已提交
429 430 431

	/* send self-abort message when rejecting on a connected port */
	if (msg_connected(msg)) {
432
		struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
P
Per Liden 已提交
433 434

		if (p_ptr) {
435 436
			struct sk_buff *abuf = NULL;

437
			if (p_ptr->connected)
P
Per Liden 已提交
438
				abuf = port_build_self_abort_msg(p_ptr, err);
439
			tipc_port_unlock(p_ptr);
440
			tipc_net_route_msg(abuf);
P
Per Liden 已提交
441 442 443
		}
	}

444 445
	/* send returned message & dispose of rejected message */

446 447 448 449 450
	src_node = msg_prevnode(msg);
	if (src_node == tipc_own_addr)
		tipc_port_recv_msg(rbuf);
	else
		tipc_link_send(rbuf, src_node, msg_link_selector(rmsg));
451
exit:
452
	kfree_skb(buf);
P
Per Liden 已提交
453 454 455
	return data_sz;
}

456
int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
457
			      struct iovec const *msg_sect, u32 num_sect,
458
			      unsigned int total_len, int err)
P
Per Liden 已提交
459 460 461 462
{
	struct sk_buff *buf;
	int res;

463
	res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE,
P
Per Liden 已提交
464 465 466 467 468 469 470 471 472
			!p_ptr->user_port, &buf);
	if (!buf)
		return res;

	return tipc_reject_msg(buf, err);
}

static void port_timeout(unsigned long ref)
{
473
	struct tipc_port *p_ptr = tipc_port_lock(ref);
474
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
475

476 477 478
	if (!p_ptr)
		return;

479
	if (!p_ptr->connected) {
480
		tipc_port_unlock(p_ptr);
P
Per Liden 已提交
481
		return;
482
	}
P
Per Liden 已提交
483 484 485 486 487

	/* Last probe answered ? */
	if (p_ptr->probing_state == PROBING) {
		buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
	} else {
488
		buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
P
Per Liden 已提交
489 490 491
		p_ptr->probing_state = PROBING;
		k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
	}
492 493
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
494 495 496 497 498
}


static void port_handle_node_down(unsigned long ref)
{
499
	struct tipc_port *p_ptr = tipc_port_lock(ref);
500
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
501 502 503 504

	if (!p_ptr)
		return;
	buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
505 506
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
507 508 509
}


510
static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
P
Per Liden 已提交
511
{
512
	struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
P
Per Liden 已提交
513

514 515 516 517 518 519
	if (buf) {
		struct tipc_msg *msg = buf_msg(buf);
		msg_swap_words(msg, 4, 5);
		msg_swap_words(msg, 6, 7);
	}
	return buf;
P
Per Liden 已提交
520 521 522
}


523
static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
P
Per Liden 已提交
524
{
525 526 527
	struct sk_buff *buf;
	struct tipc_msg *msg;
	u32 imp;
P
Per Liden 已提交
528

529
	if (!p_ptr->connected)
530
		return NULL;
531 532 533 534 535 536 537 538 539 540 541 542 543

	buf = tipc_buf_acquire(BASIC_H_SIZE);
	if (buf) {
		msg = buf_msg(buf);
		memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
		msg_set_hdr_sz(msg, BASIC_H_SIZE);
		msg_set_size(msg, BASIC_H_SIZE);
		imp = msg_importance(msg);
		if (imp < TIPC_CRITICAL_IMPORTANCE)
			msg_set_importance(msg, ++imp);
		msg_set_errcode(msg, err);
	}
	return buf;
P
Per Liden 已提交
544 545
}

546
void tipc_port_recv_proto_msg(struct sk_buff *buf)
P
Per Liden 已提交
547 548
{
	struct tipc_msg *msg = buf_msg(buf);
549
	struct tipc_port *p_ptr;
550
	struct sk_buff *r_buf = NULL;
551 552 553 554 555 556
	u32 destport = msg_destport(msg);
	int wakeable;

	/* Validate connection */

	p_ptr = tipc_port_lock(destport);
557
	if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
558 559 560 561
		r_buf = tipc_buf_acquire(BASIC_H_SIZE);
		if (r_buf) {
			msg = buf_msg(r_buf);
			tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
562
				      BASIC_H_SIZE, msg_orignode(msg));
563 564
			msg_set_errcode(msg, TIPC_ERR_NO_PORT);
			msg_set_origport(msg, destport);
565
			msg_set_destport(msg, msg_origport(msg));
566
		}
567 568
		if (p_ptr)
			tipc_port_unlock(p_ptr);
P
Per Liden 已提交
569 570 571
		goto exit;
	}

572 573 574 575 576 577 578 579 580 581 582 583 584 585
	/* Process protocol message sent by peer */

	switch (msg_type(msg)) {
	case CONN_ACK:
		wakeable = tipc_port_congested(p_ptr) && p_ptr->congested &&
			p_ptr->wakeup;
		p_ptr->acked += msg_msgcnt(msg);
		if (!tipc_port_congested(p_ptr)) {
			p_ptr->congested = 0;
			if (wakeable)
				p_ptr->wakeup(p_ptr);
		}
		break;
	case CONN_PROBE:
586
		r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
587 588 589 590
		break;
	default:
		/* CONN_PROBE_REPLY or unrecognized - no action required */
		break;
P
Per Liden 已提交
591 592
	}
	p_ptr->probing_state = CONFIRMED;
593
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
594
exit:
595
	tipc_net_route_msg(r_buf);
596
	kfree_skb(buf);
P
Per Liden 已提交
597 598
}

599
static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
P
Per Liden 已提交
600
{
601
	struct publication *publ;
P
Per Liden 已提交
602 603

	if (full_id)
604
		tipc_printf(buf, "<%u.%u.%u:%u>:",
P
Per Liden 已提交
605
			    tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
606
			    tipc_node(tipc_own_addr), p_ptr->ref);
P
Per Liden 已提交
607
	else
608
		tipc_printf(buf, "%-10u:", p_ptr->ref);
P
Per Liden 已提交
609

610
	if (p_ptr->connected) {
611 612 613 614 615 616
		u32 dport = port_peerport(p_ptr);
		u32 destnode = port_peernode(p_ptr);

		tipc_printf(buf, " connected to <%u.%u.%u:%u>",
			    tipc_zone(destnode), tipc_cluster(destnode),
			    tipc_node(destnode), dport);
617
		if (p_ptr->conn_type != 0)
618
			tipc_printf(buf, " via {%u,%u}",
619 620 621
				    p_ptr->conn_type,
				    p_ptr->conn_instance);
	} else if (p_ptr->published) {
622 623
		tipc_printf(buf, " bound to");
		list_for_each_entry(publ, &p_ptr->publications, pport_list) {
P
Per Liden 已提交
624 625 626 627
			if (publ->lower == publ->upper)
				tipc_printf(buf, " {%u,%u}", publ->type,
					    publ->lower);
			else
628
				tipc_printf(buf, " {%u,%u,%u}", publ->type,
P
Per Liden 已提交
629
					    publ->lower, publ->upper);
630 631 632
		}
	}
	tipc_printf(buf, "\n");
P
Per Liden 已提交
633 634 635 636
}

#define MAX_PORT_QUERY 32768

637
struct sk_buff *tipc_port_get_ports(void)
P
Per Liden 已提交
638 639 640 641
{
	struct sk_buff *buf;
	struct tlv_desc *rep_tlv;
	struct print_buf pb;
642
	struct tipc_port *p_ptr;
P
Per Liden 已提交
643 644
	int str_len;

645
	buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
P
Per Liden 已提交
646 647 648 649
	if (!buf)
		return NULL;
	rep_tlv = (struct tlv_desc *)buf->data;

650 651
	tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
652
	list_for_each_entry(p_ptr, &ports, port_list) {
653
		spin_lock_bh(p_ptr->lock);
P
Per Liden 已提交
654
		port_print(p_ptr, &pb, 0);
655
		spin_unlock_bh(p_ptr->lock);
P
Per Liden 已提交
656
	}
657 658
	spin_unlock_bh(&tipc_port_list_lock);
	str_len = tipc_printbuf_validate(&pb);
P
Per Liden 已提交
659 660 661 662 663 664 665

	skb_put(buf, TLV_SPACE(str_len));
	TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);

	return buf;
}

666
void tipc_port_reinit(void)
P
Per Liden 已提交
667
{
668
	struct tipc_port *p_ptr;
P
Per Liden 已提交
669 670
	struct tipc_msg *msg;

671
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
672
	list_for_each_entry(p_ptr, &ports, port_list) {
673
		msg = &p_ptr->phdr;
674
		msg_set_prevnode(msg, tipc_own_addr);
P
Per Liden 已提交
675 676
		msg_set_orignode(msg, tipc_own_addr);
	}
677
	spin_unlock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691
}


/*
 *  port_dispatcher_sigh(): Signal handler for messages destinated
 *                          to the tipc_port interface.
 */

static void port_dispatcher_sigh(void *dummy)
{
	struct sk_buff *buf;

	spin_lock_bh(&queue_lock);
	buf = msg_queue_head;
692
	msg_queue_head = NULL;
P
Per Liden 已提交
693 694 695
	spin_unlock_bh(&queue_lock);

	while (buf) {
696
		struct tipc_port *p_ptr;
P
Per Liden 已提交
697 698 699 700 701
		struct user_port *up_ptr;
		struct tipc_portid orig;
		struct tipc_name_seq dseq;
		void *usr_handle;
		int connected;
702
		int peer_invalid;
P
Per Liden 已提交
703
		int published;
704
		u32 message_type;
P
Per Liden 已提交
705 706 707 708

		struct sk_buff *next = buf->next;
		struct tipc_msg *msg = buf_msg(buf);
		u32 dref = msg_destport(msg);
709

710 711 712 713
		message_type = msg_type(msg);
		if (message_type > TIPC_DIRECT_MSG)
			goto reject;	/* Unsupported message type */

714
		p_ptr = tipc_port_lock(dref);
715 716 717
		if (!p_ptr)
			goto reject;	/* Port deleted while msg in queue */

P
Per Liden 已提交
718 719 720 721
		orig.ref = msg_origport(msg);
		orig.node = msg_orignode(msg);
		up_ptr = p_ptr->user_port;
		usr_handle = up_ptr->usr_handle;
722
		connected = p_ptr->connected;
723
		peer_invalid = connected && !tipc_port_peer_msg(p_ptr, msg);
724
		published = p_ptr->published;
P
Per Liden 已提交
725 726 727 728

		if (unlikely(msg_errcode(msg)))
			goto err;

729
		switch (message_type) {
730

P
Per Liden 已提交
731 732
		case TIPC_CONN_MSG:{
				tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
733
				u32 dsz;
P
Per Liden 已提交
734

J
Julia Lawall 已提交
735
				tipc_port_unlock(p_ptr);
736 737
				if (unlikely(!cb))
					goto reject;
P
Per Liden 已提交
738
				if (unlikely(!connected)) {
739
					if (tipc_connect2port(dref, &orig))
P
Per Liden 已提交
740
						goto reject;
741
				} else if (peer_invalid)
P
Per Liden 已提交
742
					goto reject;
743 744 745 746
				dsz = msg_data_sz(msg);
				if (unlikely(dsz &&
					     (++p_ptr->conn_unacked >=
					      TIPC_FLOW_CONTROL_WIN)))
747
					tipc_acknowledge(dref,
748
							 p_ptr->conn_unacked);
P
Per Liden 已提交
749
				skb_pull(buf, msg_hdr_sz(msg));
750
				cb(usr_handle, dref, &buf, msg_data(msg), dsz);
P
Per Liden 已提交
751 752 753 754 755
				break;
			}
		case TIPC_DIRECT_MSG:{
				tipc_msg_event cb = up_ptr->msg_cb;

J
Julia Lawall 已提交
756
				tipc_port_unlock(p_ptr);
757
				if (unlikely(!cb || connected))
P
Per Liden 已提交
758 759
					goto reject;
				skb_pull(buf, msg_hdr_sz(msg));
760
				cb(usr_handle, dref, &buf, msg_data(msg),
P
Per Liden 已提交
761 762 763 764
				   msg_data_sz(msg), msg_importance(msg),
				   &orig);
				break;
			}
765
		case TIPC_MCAST_MSG:
P
Per Liden 已提交
766 767 768
		case TIPC_NAMED_MSG:{
				tipc_named_msg_event cb = up_ptr->named_msg_cb;

J
Julia Lawall 已提交
769
				tipc_port_unlock(p_ptr);
770
				if (unlikely(!cb || connected || !published))
P
Per Liden 已提交
771 772 773
					goto reject;
				dseq.type =  msg_nametype(msg);
				dseq.lower = msg_nameinst(msg);
774 775
				dseq.upper = (message_type == TIPC_NAMED_MSG)
					? dseq.lower : msg_nameupper(msg);
P
Per Liden 已提交
776
				skb_pull(buf, msg_hdr_sz(msg));
777
				cb(usr_handle, dref, &buf, msg_data(msg),
P
Per Liden 已提交
778 779 780 781 782 783
				   msg_data_sz(msg), msg_importance(msg),
				   &orig, &dseq);
				break;
			}
		}
		if (buf)
784
			kfree_skb(buf);
P
Per Liden 已提交
785 786 787
		buf = next;
		continue;
err:
788
		switch (message_type) {
789

P
Per Liden 已提交
790
		case TIPC_CONN_MSG:{
791
				tipc_conn_shutdown_event cb =
P
Per Liden 已提交
792 793
					up_ptr->conn_err_cb;

J
Julia Lawall 已提交
794
				tipc_port_unlock(p_ptr);
795
				if (!cb || !connected || peer_invalid)
P
Per Liden 已提交
796 797 798 799 800 801 802 803 804 805
					break;
				tipc_disconnect(dref);
				skb_pull(buf, msg_hdr_sz(msg));
				cb(usr_handle, dref, &buf, msg_data(msg),
				   msg_data_sz(msg), msg_errcode(msg));
				break;
			}
		case TIPC_DIRECT_MSG:{
				tipc_msg_err_event cb = up_ptr->err_cb;

J
Julia Lawall 已提交
806
				tipc_port_unlock(p_ptr);
807
				if (!cb || connected)
P
Per Liden 已提交
808 809 810 811 812 813
					break;
				skb_pull(buf, msg_hdr_sz(msg));
				cb(usr_handle, dref, &buf, msg_data(msg),
				   msg_data_sz(msg), msg_errcode(msg), &orig);
				break;
			}
814
		case TIPC_MCAST_MSG:
P
Per Liden 已提交
815
		case TIPC_NAMED_MSG:{
816
				tipc_named_msg_err_event cb =
P
Per Liden 已提交
817 818
					up_ptr->named_err_cb;

J
Julia Lawall 已提交
819
				tipc_port_unlock(p_ptr);
820
				if (!cb || connected)
P
Per Liden 已提交
821 822 823
					break;
				dseq.type =  msg_nametype(msg);
				dseq.lower = msg_nameinst(msg);
824 825
				dseq.upper = (message_type == TIPC_NAMED_MSG)
					? dseq.lower : msg_nameupper(msg);
P
Per Liden 已提交
826
				skb_pull(buf, msg_hdr_sz(msg));
827
				cb(usr_handle, dref, &buf, msg_data(msg),
P
Per Liden 已提交
828 829 830 831 832
				   msg_data_sz(msg), msg_errcode(msg), &dseq);
				break;
			}
		}
		if (buf)
833
			kfree_skb(buf);
P
Per Liden 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
		buf = next;
		continue;
reject:
		tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
		buf = next;
	}
}

/*
 *  port_dispatcher(): Dispatcher for messages destinated
 *  to the tipc_port interface. Called with port locked.
 */

static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
{
	buf->next = NULL;
	spin_lock_bh(&queue_lock);
	if (msg_queue_head) {
		msg_queue_tail->next = buf;
		msg_queue_tail = buf;
	} else {
		msg_queue_tail = msg_queue_head = buf;
856
		tipc_k_signal((Handler)port_dispatcher_sigh, 0);
P
Per Liden 已提交
857 858
	}
	spin_unlock_bh(&queue_lock);
859
	return 0;
P
Per Liden 已提交
860 861
}

862
/*
P
Per Liden 已提交
863
 * Wake up port after congestion: Called with port locked,
864
 *
P
Per Liden 已提交
865 866 867 868
 */

static void port_wakeup_sh(unsigned long ref)
{
869
	struct tipc_port *p_ptr;
P
Per Liden 已提交
870
	struct user_port *up_ptr;
871 872
	tipc_continue_event cb = NULL;
	void *uh = NULL;
P
Per Liden 已提交
873

874
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
875 876 877 878 879 880
	if (p_ptr) {
		up_ptr = p_ptr->user_port;
		if (up_ptr) {
			cb = up_ptr->continue_event_cb;
			uh = up_ptr->usr_handle;
		}
881
		tipc_port_unlock(p_ptr);
P
Per Liden 已提交
882 883 884 885 886 887 888 889
	}
	if (cb)
		cb(uh, ref);
}


static void port_wakeup(struct tipc_port *p_ptr)
{
890
	tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
P
Per Liden 已提交
891 892 893 894
}

void tipc_acknowledge(u32 ref, u32 ack)
{
895
	struct tipc_port *p_ptr;
896
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
897

898
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
899 900
	if (!p_ptr)
		return;
901 902
	if (p_ptr->connected) {
		p_ptr->conn_unacked -= ack;
903
		buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
P
Per Liden 已提交
904
	}
905 906
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
907 908 909
}

/*
910
 * tipc_createport(): user level call.
P
Per Liden 已提交
911 912
 */

913
int tipc_createport(void *usr_handle,
914 915 916 917 918 919 920
		    unsigned int importance,
		    tipc_msg_err_event error_cb,
		    tipc_named_msg_err_event named_error_cb,
		    tipc_conn_shutdown_event conn_error_cb,
		    tipc_msg_event msg_cb,
		    tipc_named_msg_event named_msg_cb,
		    tipc_conn_msg_event conn_msg_cb,
P
Per Liden 已提交
921 922 923 924
		    tipc_continue_event continue_event_cb,/* May be zero */
		    u32 *portref)
{
	struct user_port *up_ptr;
925
	struct tipc_port *p_ptr;
P
Per Liden 已提交
926

927
	up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
928
	if (!up_ptr) {
929
		warn("Port creation failed, no memory\n");
P
Per Liden 已提交
930 931
		return -ENOMEM;
	}
932
	p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
933 934
						   port_wakeup, importance);
	if (!p_ptr) {
P
Per Liden 已提交
935 936 937 938 939 940
		kfree(up_ptr);
		return -ENOMEM;
	}

	p_ptr->user_port = up_ptr;
	up_ptr->usr_handle = usr_handle;
941
	up_ptr->ref = p_ptr->ref;
P
Per Liden 已提交
942 943 944 945 946 947 948
	up_ptr->err_cb = error_cb;
	up_ptr->named_err_cb = named_error_cb;
	up_ptr->conn_err_cb = conn_error_cb;
	up_ptr->msg_cb = msg_cb;
	up_ptr->named_msg_cb = named_msg_cb;
	up_ptr->conn_msg_cb = conn_msg_cb;
	up_ptr->continue_event_cb = continue_event_cb;
949
	*portref = p_ptr->ref;
950
	tipc_port_unlock(p_ptr);
951
	return 0;
P
Per Liden 已提交
952 953 954 955
}

int tipc_portimportance(u32 ref, unsigned int *importance)
{
956
	struct tipc_port *p_ptr;
957

958
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
959 960
	if (!p_ptr)
		return -EINVAL;
961
	*importance = (unsigned int)msg_importance(&p_ptr->phdr);
J
Julia Lawall 已提交
962
	tipc_port_unlock(p_ptr);
963
	return 0;
P
Per Liden 已提交
964 965 966 967
}

int tipc_set_portimportance(u32 ref, unsigned int imp)
{
968
	struct tipc_port *p_ptr;
P
Per Liden 已提交
969 970 971 972

	if (imp > TIPC_CRITICAL_IMPORTANCE)
		return -EINVAL;

973
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
974 975
	if (!p_ptr)
		return -EINVAL;
976
	msg_set_importance(&p_ptr->phdr, (u32)imp);
J
Julia Lawall 已提交
977
	tipc_port_unlock(p_ptr);
978
	return 0;
P
Per Liden 已提交
979 980 981 982 983
}


int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
{
984
	struct tipc_port *p_ptr;
P
Per Liden 已提交
985 986 987 988
	struct publication *publ;
	u32 key;
	int res = -EINVAL;

989
	p_ptr = tipc_port_lock(ref);
990 991 992
	if (!p_ptr)
		return -EINVAL;

993
	if (p_ptr->connected)
P
Per Liden 已提交
994 995 996 997 998 999 1000 1001 1002 1003
		goto exit;
	if (seq->lower > seq->upper)
		goto exit;
	if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
		goto exit;
	key = ref + p_ptr->pub_count + 1;
	if (key == ref) {
		res = -EADDRINUSE;
		goto exit;
	}
1004
	publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1005
				    scope, p_ptr->ref, key);
P
Per Liden 已提交
1006 1007 1008
	if (publ) {
		list_add(&publ->pport_list, &p_ptr->publications);
		p_ptr->pub_count++;
1009
		p_ptr->published = 1;
1010
		res = 0;
P
Per Liden 已提交
1011 1012
	}
exit:
1013
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
1014 1015 1016 1017 1018
	return res;
}

int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
{
1019
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1020 1021 1022
	struct publication *publ;
	struct publication *tpubl;
	int res = -EINVAL;
1023

1024
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1025 1026 1027
	if (!p_ptr)
		return -EINVAL;
	if (!seq) {
1028
		list_for_each_entry_safe(publ, tpubl,
P
Per Liden 已提交
1029
					 &p_ptr->publications, pport_list) {
1030
			tipc_nametbl_withdraw(publ->type, publ->lower,
1031
					      publ->ref, publ->key);
P
Per Liden 已提交
1032
		}
1033
		res = 0;
P
Per Liden 已提交
1034
	} else {
1035
		list_for_each_entry_safe(publ, tpubl,
P
Per Liden 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044
					 &p_ptr->publications, pport_list) {
			if (publ->scope != scope)
				continue;
			if (publ->type != seq->type)
				continue;
			if (publ->lower != seq->lower)
				continue;
			if (publ->upper != seq->upper)
				break;
1045
			tipc_nametbl_withdraw(publ->type, publ->lower,
1046
					      publ->ref, publ->key);
1047
			res = 0;
P
Per Liden 已提交
1048 1049 1050 1051
			break;
		}
	}
	if (list_empty(&p_ptr->publications))
1052
		p_ptr->published = 0;
1053
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
1054 1055 1056 1057 1058
	return res;
}

int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
{
1059
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1060 1061 1062
	struct tipc_msg *msg;
	int res = -EINVAL;

1063
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1064 1065
	if (!p_ptr)
		return -EINVAL;
1066
	if (p_ptr->published || p_ptr->connected)
P
Per Liden 已提交
1067 1068 1069 1070
		goto exit;
	if (!peer->ref)
		goto exit;

1071
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1072 1073 1074
	msg_set_destnode(msg, peer->node);
	msg_set_destport(msg, peer->ref);
	msg_set_type(msg, TIPC_CONN_MSG);
1075
	msg_set_lookup_scope(msg, 0);
1076
	msg_set_hdr_sz(msg, SHORT_H_SIZE);
P
Per Liden 已提交
1077 1078 1079

	p_ptr->probing_interval = PROBING_INTERVAL;
	p_ptr->probing_state = CONFIRMED;
1080
	p_ptr->connected = 1;
P
Per Liden 已提交
1081 1082
	k_start_timer(&p_ptr->timer, p_ptr->probing_interval);

1083
	tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
1084
			  (void *)(unsigned long)ref,
P
Per Liden 已提交
1085
			  (net_ev_handler)port_handle_node_down);
1086
	res = 0;
P
Per Liden 已提交
1087
exit:
1088
	tipc_port_unlock(p_ptr);
1089
	p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
P
Per Liden 已提交
1090 1091 1092
	return res;
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
/**
 * tipc_disconnect_port - disconnect port from peer
 *
 * Port must be locked.
 */

int tipc_disconnect_port(struct tipc_port *tp_ptr)
{
	int res;

	if (tp_ptr->connected) {
		tp_ptr->connected = 0;
		/* let timer expire on it's own to avoid deadlock! */
		tipc_nodesub_unsubscribe(
1107
			&((struct tipc_port *)tp_ptr)->subscription);
1108
		res = 0;
1109 1110 1111 1112 1113 1114
	} else {
		res = -ENOTCONN;
	}
	return res;
}

P
Per Liden 已提交
1115 1116 1117 1118 1119 1120 1121
/*
 * tipc_disconnect(): Disconnect port form peer.
 *                    This is a node local operation.
 */

int tipc_disconnect(u32 ref)
{
1122
	struct tipc_port *p_ptr;
1123
	int res;
P
Per Liden 已提交
1124

1125
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1126 1127
	if (!p_ptr)
		return -EINVAL;
1128
	res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1129
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
1130 1131 1132 1133 1134 1135 1136 1137
	return res;
}

/*
 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
 */
int tipc_shutdown(u32 ref)
{
1138
	struct tipc_port *p_ptr;
1139
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
1140

1141
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1142 1143 1144
	if (!p_ptr)
		return -EINVAL;

1145
	buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
1146 1147
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
1148 1149 1150
	return tipc_disconnect(ref);
}

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
/**
 * tipc_port_recv_msg - receive message from lower layer and deliver to port user
 */

int tipc_port_recv_msg(struct sk_buff *buf)
{
	struct tipc_port *p_ptr;
	struct tipc_msg *msg = buf_msg(buf);
	u32 destport = msg_destport(msg);
	u32 dsz = msg_data_sz(msg);
	u32 err;

	/* forward unresolved named message */
	if (unlikely(!destport)) {
		tipc_net_route_msg(buf);
		return dsz;
	}

	/* validate destination & pass to port, otherwise reject message */
	p_ptr = tipc_port_lock(destport);
	if (likely(p_ptr)) {
		err = p_ptr->dispatcher(p_ptr, buf);
		tipc_port_unlock(p_ptr);
		if (likely(!err))
			return dsz;
	} else {
		err = TIPC_ERR_NO_PORT;
	}
1179

1180 1181 1182
	return tipc_reject_msg(buf, err);
}

P
Per Liden 已提交
1183
/*
1184
 *  tipc_port_recv_sections(): Concatenate and deliver sectioned
P
Per Liden 已提交
1185 1186 1187
 *                        message for this node.
 */

1188
static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
1189 1190
				   struct iovec const *msg_sect,
				   unsigned int total_len)
P
Per Liden 已提交
1191 1192 1193
{
	struct sk_buff *buf;
	int res;
1194

1195
	res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len,
P
Per Liden 已提交
1196 1197
			MAX_MSG_SIZE, !sender->user_port, &buf);
	if (likely(buf))
1198
		tipc_port_recv_msg(buf);
P
Per Liden 已提交
1199 1200 1201 1202 1203 1204 1205
	return res;
}

/**
 * tipc_send - send message sections on connection
 */

1206 1207
int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
	      unsigned int total_len)
P
Per Liden 已提交
1208
{
1209
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1210 1211 1212
	u32 destnode;
	int res;

1213
	p_ptr = tipc_port_deref(ref);
1214
	if (!p_ptr || !p_ptr->connected)
P
Per Liden 已提交
1215 1216
		return -EINVAL;

1217
	p_ptr->congested = 1;
1218
	if (!tipc_port_congested(p_ptr)) {
P
Per Liden 已提交
1219
		destnode = port_peernode(p_ptr);
1220
		if (likely(!in_own_node(destnode)))
1221
			res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1222
							   total_len, destnode);
P
Per Liden 已提交
1223
		else
1224 1225
			res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
						      total_len);
P
Per Liden 已提交
1226 1227

		if (likely(res != -ELINKCONG)) {
1228
			p_ptr->congested = 0;
1229 1230
			if (res > 0)
				p_ptr->sent++;
P
Per Liden 已提交
1231 1232 1233 1234
			return res;
		}
	}
	if (port_unreliable(p_ptr)) {
1235
		p_ptr->congested = 0;
1236
		return total_len;
P
Per Liden 已提交
1237 1238 1239 1240 1241
	}
	return -ELINKCONG;
}

/**
1242
 * tipc_send2name - send message sections to port name
P
Per Liden 已提交
1243 1244
 */

1245
int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1246 1247
		   unsigned int num_sect, struct iovec const *msg_sect,
		   unsigned int total_len)
P
Per Liden 已提交
1248
{
1249
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1250 1251
	struct tipc_msg *msg;
	u32 destnode = domain;
1252
	u32 destport;
P
Per Liden 已提交
1253 1254
	int res;

1255
	p_ptr = tipc_port_deref(ref);
1256
	if (!p_ptr || p_ptr->connected)
P
Per Liden 已提交
1257 1258
		return -EINVAL;

1259
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1260
	msg_set_type(msg, TIPC_NAMED_MSG);
1261
	msg_set_hdr_sz(msg, NAMED_H_SIZE);
P
Per Liden 已提交
1262 1263
	msg_set_nametype(msg, name->type);
	msg_set_nameinst(msg, name->instance);
1264
	msg_set_lookup_scope(msg, tipc_addr_scope(domain));
1265
	destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
P
Per Liden 已提交
1266 1267 1268
	msg_set_destnode(msg, destnode);
	msg_set_destport(msg, destport);

1269
	if (likely(destport || destnode)) {
1270
		if (likely(in_own_node(destnode)))
1271
			res = tipc_port_recv_sections(p_ptr, num_sect,
1272
						      msg_sect, total_len);
1273
		else if (tipc_own_addr)
1274
			res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1275 1276
							   num_sect, total_len,
							   destnode);
1277 1278 1279 1280
		else
			res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
							num_sect, total_len,
							TIPC_ERR_NO_NODE);
1281 1282 1283
		if (likely(res != -ELINKCONG)) {
			if (res > 0)
				p_ptr->sent++;
P
Per Liden 已提交
1284
			return res;
1285
		}
P
Per Liden 已提交
1286
		if (port_unreliable(p_ptr)) {
1287
			return total_len;
P
Per Liden 已提交
1288 1289 1290
		}
		return -ELINKCONG;
	}
1291
	return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1292
					 total_len, TIPC_ERR_NO_NAME);
P
Per Liden 已提交
1293 1294 1295
}

/**
1296
 * tipc_send2port - send message sections to port identity
P
Per Liden 已提交
1297 1298
 */

1299
int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1300 1301
		   unsigned int num_sect, struct iovec const *msg_sect,
		   unsigned int total_len)
P
Per Liden 已提交
1302
{
1303
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1304 1305 1306
	struct tipc_msg *msg;
	int res;

1307
	p_ptr = tipc_port_deref(ref);
1308
	if (!p_ptr || p_ptr->connected)
P
Per Liden 已提交
1309 1310
		return -EINVAL;

1311
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1312
	msg_set_type(msg, TIPC_DIRECT_MSG);
1313
	msg_set_lookup_scope(msg, 0);
P
Per Liden 已提交
1314 1315
	msg_set_destnode(msg, dest->node);
	msg_set_destport(msg, dest->ref);
1316
	msg_set_hdr_sz(msg, BASIC_H_SIZE);
1317

1318
	if (in_own_node(dest->node))
1319 1320
		res =  tipc_port_recv_sections(p_ptr, num_sect, msg_sect,
					       total_len);
1321
	else if (tipc_own_addr)
1322
		res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1323
						   total_len, dest->node);
1324 1325 1326
	else
		res = tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
						total_len, TIPC_ERR_NO_NODE);
1327 1328 1329
	if (likely(res != -ELINKCONG)) {
		if (res > 0)
			p_ptr->sent++;
P
Per Liden 已提交
1330
		return res;
1331
	}
P
Per Liden 已提交
1332
	if (port_unreliable(p_ptr)) {
1333
		return total_len;
P
Per Liden 已提交
1334 1335 1336 1337
	}
	return -ELINKCONG;
}

1338
/**
1339
 * tipc_send_buf2port - send message buffer to port identity
P
Per Liden 已提交
1340 1341
 */

1342 1343
int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
	       struct sk_buff *buf, unsigned int dsz)
P
Per Liden 已提交
1344
{
1345
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1346 1347 1348
	struct tipc_msg *msg;
	int res;

1349 1350
	p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
	if (!p_ptr || p_ptr->connected)
P
Per Liden 已提交
1351 1352
		return -EINVAL;

1353
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1354 1355 1356
	msg_set_type(msg, TIPC_DIRECT_MSG);
	msg_set_destnode(msg, dest->node);
	msg_set_destport(msg, dest->ref);
1357 1358 1359
	msg_set_hdr_sz(msg, BASIC_H_SIZE);
	msg_set_size(msg, BASIC_H_SIZE + dsz);
	if (skb_cow(buf, BASIC_H_SIZE))
P
Per Liden 已提交
1360 1361
		return -ENOMEM;

1362 1363
	skb_push(buf, BASIC_H_SIZE);
	skb_copy_to_linear_data(buf, msg, BASIC_H_SIZE);
1364

1365
	if (in_own_node(dest->node))
1366 1367 1368 1369 1370 1371
		res = tipc_port_recv_msg(buf);
	else
		res = tipc_send_buf_fast(buf, dest->node);
	if (likely(res != -ELINKCONG)) {
		if (res > 0)
			p_ptr->sent++;
P
Per Liden 已提交
1372
		return res;
1373
	}
P
Per Liden 已提交
1374 1375 1376 1377 1378
	if (port_unreliable(p_ptr))
		return dsz;
	return -ELINKCONG;
}