port.c 32.1 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 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 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
}

/**
 * tipc_multicast - send a multicast message to local and remote destinations
 */

76
int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
P
Per Liden 已提交
77 78 79 80 81 82
		   u32 num_sect, struct iovec const *msg_sect)
{
	struct tipc_msg *hdr;
	struct sk_buff *buf;
	struct sk_buff *ibuf = NULL;
	struct port_list dports = {0, NULL, };
83
	struct tipc_port *oport = tipc_port_deref(ref);
P
Per Liden 已提交
84 85 86 87 88 89 90 91
	int ext_targets;
	int res;

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

	/* Create multicast message */

92
	hdr = &oport->phdr;
P
Per Liden 已提交
93
	msg_set_type(hdr, TIPC_MCAST_MSG);
94
	msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
P
Per Liden 已提交
95 96 97 98
	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);
99
	res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
P
Per Liden 已提交
100 101 102 103 104 105
			!oport->user_port, &buf);
	if (unlikely(!buf))
		return res;

	/* Figure out where to send multicast message */

106 107
	ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
						TIPC_NODE_SCOPE, &dports);
108 109

	/* Send message to destinations (duplicate it only if necessary) */
P
Per Liden 已提交
110 111 112 113 114

	if (ext_targets) {
		if (dports.count != 0) {
			ibuf = skb_copy(buf, GFP_ATOMIC);
			if (ibuf == NULL) {
115
				tipc_port_list_free(&dports);
P
Per Liden 已提交
116 117 118 119
				buf_discard(buf);
				return -ENOMEM;
			}
		}
120
		res = tipc_bclink_send_msg(buf);
121
		if ((res < 0) && (dports.count != 0))
P
Per Liden 已提交
122 123 124 125 126 127 128
			buf_discard(ibuf);
	} else {
		ibuf = buf;
	}

	if (res >= 0) {
		if (ibuf)
129
			tipc_port_recv_mcast(ibuf, &dports);
P
Per Liden 已提交
130
	} else {
131
		tipc_port_list_free(&dports);
P
Per Liden 已提交
132 133 134 135 136
	}
	return res;
}

/**
137
 * tipc_port_recv_mcast - deliver multicast message to all destination ports
138
 *
P
Per Liden 已提交
139 140 141
 * If there is no port list, perform a lookup to create one
 */

142
void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
P
Per Liden 已提交
143
{
144
	struct tipc_msg *msg;
P
Per Liden 已提交
145 146 147 148 149 150 151 152 153
	struct port_list dports = {0, NULL, };
	struct port_list *item = dp;
	int cnt = 0;

	msg = buf_msg(buf);

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

	if (dp == NULL) {
154
		tipc_nametbl_mc_translate(msg_nametype(msg),
P
Per Liden 已提交
155 156 157 158 159 160 161 162 163 164 165 166
				     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) {
		if (dp->count == 1) {
			msg_set_destport(msg, dp->ports[0]);
167 168
			tipc_port_recv_msg(buf);
			tipc_port_list_free(dp);
P
Per Liden 已提交
169 170 171 172 173 174 175
			return;
		}
		for (; cnt < dp->count; cnt++) {
			int index = cnt % PLSIZE;
			struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);

			if (b == NULL) {
176
				warn("Unable to deliver multicast message(s)\n");
P
Per Liden 已提交
177 178
				goto exit;
			}
179
			if ((index == 0) && (cnt != 0))
P
Per Liden 已提交
180
				item = item->next;
181
			msg_set_destport(buf_msg(b), item->ports[index]);
182
			tipc_port_recv_msg(b);
P
Per Liden 已提交
183 184 185 186
		}
	}
exit:
	buf_discard(buf);
187
	tipc_port_list_free(dp);
P
Per Liden 已提交
188 189 190
}

/**
191
 * tipc_createport_raw - create a generic TIPC port
192
 *
193
 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
P
Per Liden 已提交
194 195
 */

196
struct tipc_port *tipc_createport_raw(void *usr_handle,
P
Per Liden 已提交
197 198
			u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
			void (*wakeup)(struct tipc_port *),
199
			const u32 importance)
P
Per Liden 已提交
200
{
201
	struct tipc_port *p_ptr;
P
Per Liden 已提交
202 203 204
	struct tipc_msg *msg;
	u32 ref;

205
	p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
206 207
	if (!p_ptr) {
		warn("Port creation failed, no memory\n");
208
		return NULL;
P
Per Liden 已提交
209
	}
210
	ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
P
Per Liden 已提交
211
	if (!ref) {
212
		warn("Port creation failed, reference table exhausted\n");
P
Per Liden 已提交
213
		kfree(p_ptr);
214
		return NULL;
P
Per Liden 已提交
215 216
	}

217 218 219 220
	p_ptr->usr_handle = usr_handle;
	p_ptr->max_pkt = MAX_PKT_DEFAULT;
	p_ptr->ref = ref;
	msg = &p_ptr->phdr;
221
	tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
P
Per Liden 已提交
222 223 224 225 226
	msg_set_origport(msg, ref);
	INIT_LIST_HEAD(&p_ptr->wait_list);
	INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
	p_ptr->dispatcher = dispatcher;
	p_ptr->wakeup = wakeup;
227
	p_ptr->user_port = NULL;
P
Per Liden 已提交
228
	k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
229
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
230 231 232
	INIT_LIST_HEAD(&p_ptr->publications);
	INIT_LIST_HEAD(&p_ptr->port_list);
	list_add_tail(&p_ptr->port_list, &ports);
233
	spin_unlock_bh(&tipc_port_list_lock);
234
	return p_ptr;
P
Per Liden 已提交
235 236 237 238
}

int tipc_deleteport(u32 ref)
{
239
	struct tipc_port *p_ptr;
240
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
241

242
	tipc_withdraw(ref, 0, NULL);
243
	p_ptr = tipc_port_lock(ref);
244
	if (!p_ptr)
P
Per Liden 已提交
245 246
		return -EINVAL;

247 248
	tipc_ref_discard(ref);
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
249 250

	k_cancel_timer(&p_ptr->timer);
251
	if (p_ptr->connected) {
P
Per Liden 已提交
252
		buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
253
		tipc_nodesub_unsubscribe(&p_ptr->subscription);
P
Per Liden 已提交
254
	}
255
	kfree(p_ptr->user_port);
P
Per Liden 已提交
256

257
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
258 259
	list_del(&p_ptr->port_list);
	list_del(&p_ptr->wait_list);
260
	spin_unlock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
261 262
	k_term_timer(&p_ptr->timer);
	kfree(p_ptr);
263
	tipc_net_route_msg(buf);
264
	return 0;
P
Per Liden 已提交
265 266
}

267
static int port_unreliable(struct tipc_port *p_ptr)
P
Per Liden 已提交
268
{
269
	return msg_src_droppable(&p_ptr->phdr);
P
Per Liden 已提交
270 271 272 273
}

int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
{
274
	struct tipc_port *p_ptr;
275

276
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
277 278 279
	if (!p_ptr)
		return -EINVAL;
	*isunreliable = port_unreliable(p_ptr);
J
Julia Lawall 已提交
280
	tipc_port_unlock(p_ptr);
281
	return 0;
P
Per Liden 已提交
282 283 284 285
}

int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
{
286
	struct tipc_port *p_ptr;
287

288
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
289 290
	if (!p_ptr)
		return -EINVAL;
291
	msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
292
	tipc_port_unlock(p_ptr);
293
	return 0;
P
Per Liden 已提交
294 295
}

296
static int port_unreturnable(struct tipc_port *p_ptr)
P
Per Liden 已提交
297
{
298
	return msg_dest_droppable(&p_ptr->phdr);
P
Per Liden 已提交
299 300 301 302
}

int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
{
303
	struct tipc_port *p_ptr;
304

305
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
306 307 308
	if (!p_ptr)
		return -EINVAL;
	*isunrejectable = port_unreturnable(p_ptr);
J
Julia Lawall 已提交
309
	tipc_port_unlock(p_ptr);
310
	return 0;
P
Per Liden 已提交
311 312 313 314
}

int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
{
315
	struct tipc_port *p_ptr;
316

317
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
318 319
	if (!p_ptr)
		return -EINVAL;
320
	msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
321
	tipc_port_unlock(p_ptr);
322
	return 0;
P
Per Liden 已提交
323 324
}

325 326 327
/*
 * port_build_proto_msg(): build a port level protocol
 * or a connection abortion message. Called with
P
Per Liden 已提交
328 329 330 331
 * tipc_port lock on.
 */
static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
					    u32 origport, u32 orignode,
332
					    u32 usr, u32 type, u32 err,
333
					    u32 ack)
P
Per Liden 已提交
334 335 336
{
	struct sk_buff *buf;
	struct tipc_msg *msg;
337

338
	buf = tipc_buf_acquire(LONG_H_SIZE);
P
Per Liden 已提交
339 340
	if (buf) {
		msg = buf_msg(buf);
341
		tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
342
		msg_set_errcode(msg, err);
P
Per Liden 已提交
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
		msg_set_destport(msg, destport);
		msg_set_origport(msg, origport);
		msg_set_orignode(msg, orignode);
		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;
	u32 imp = msg_importance(msg);
	u32 data_sz = msg_data_sz(msg);

	if (data_sz > MAX_REJECT_SIZE)
		data_sz = MAX_REJECT_SIZE;
	if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
		imp++;

	/* discard rejected message if it shouldn't be returned to sender */
	if (msg_errcode(msg) || msg_dest_droppable(msg)) {
		buf_discard(buf);
		return data_sz;
	}

	/* construct rejected message */
	if (msg_mcast(msg))
		hdr_sz = MCAST_H_SIZE;
	else
		hdr_sz = LONG_H_SIZE;
376
	rbuf = tipc_buf_acquire(data_sz + hdr_sz);
P
Per Liden 已提交
377 378 379 380 381
	if (rbuf == NULL) {
		buf_discard(buf);
		return data_sz;
	}
	rmsg = buf_msg(rbuf);
382
	tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
383
	msg_set_errcode(rmsg, err);
P
Per Liden 已提交
384 385
	msg_set_destport(rmsg, msg_origport(msg));
	msg_set_origport(rmsg, msg_destport(msg));
386
	if (msg_short(msg)) {
P
Per Liden 已提交
387
		msg_set_orignode(rmsg, tipc_own_addr);
388 389
		/* leave name type & instance as zeroes */
	} else {
P
Per Liden 已提交
390
		msg_set_orignode(rmsg, msg_destnode(msg));
391 392 393
		msg_set_nametype(rmsg, msg_nametype(msg));
		msg_set_nameinst(rmsg, msg_nameinst(msg));
	}
394
	msg_set_size(rmsg, data_sz + hdr_sz);
395
	skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
P
Per Liden 已提交
396 397 398

	/* send self-abort message when rejecting on a connected port */
	if (msg_connected(msg)) {
399
		struct sk_buff *abuf = NULL;
400
		struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
P
Per Liden 已提交
401 402

		if (p_ptr) {
403
			if (p_ptr->connected)
P
Per Liden 已提交
404
				abuf = port_build_self_abort_msg(p_ptr, err);
405
			tipc_port_unlock(p_ptr);
P
Per Liden 已提交
406
		}
407
		tipc_net_route_msg(abuf);
P
Per Liden 已提交
408 409 410 411
	}

	/* send rejected message */
	buf_discard(buf);
412
	tipc_net_route_msg(rbuf);
P
Per Liden 已提交
413 414 415
	return data_sz;
}

416
int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
417 418
			      struct iovec const *msg_sect, u32 num_sect,
			      int err)
P
Per Liden 已提交
419 420 421 422
{
	struct sk_buff *buf;
	int res;

423
	res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
P
Per Liden 已提交
424 425 426 427 428 429 430 431 432
			!p_ptr->user_port, &buf);
	if (!buf)
		return res;

	return tipc_reject_msg(buf, err);
}

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

436 437 438
	if (!p_ptr)
		return;

439
	if (!p_ptr->connected) {
440
		tipc_port_unlock(p_ptr);
P
Per Liden 已提交
441
		return;
442
	}
P
Per Liden 已提交
443 444 445 446 447 448 449

	/* Last probe answered ? */
	if (p_ptr->probing_state == PROBING) {
		buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
	} else {
		buf = port_build_proto_msg(port_peerport(p_ptr),
					   port_peernode(p_ptr),
450
					   p_ptr->ref,
P
Per Liden 已提交
451 452 453
					   tipc_own_addr,
					   CONN_MANAGER,
					   CONN_PROBE,
454
					   TIPC_OK,
P
Per Liden 已提交
455 456 457 458
					   0);
		p_ptr->probing_state = PROBING;
		k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
	}
459 460
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
461 462 463 464 465
}


static void port_handle_node_down(unsigned long ref)
{
466
	struct tipc_port *p_ptr = tipc_port_lock(ref);
467
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
468 469 470 471

	if (!p_ptr)
		return;
	buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
472 473
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
474 475 476
}


477
static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
P
Per Liden 已提交
478
{
479
	u32 imp = msg_importance(&p_ptr->phdr);
P
Per Liden 已提交
480

481
	if (!p_ptr->connected)
482
		return NULL;
P
Per Liden 已提交
483 484
	if (imp < TIPC_CRITICAL_IMPORTANCE)
		imp++;
485
	return port_build_proto_msg(p_ptr->ref,
P
Per Liden 已提交
486 487 488 489 490
				    tipc_own_addr,
				    port_peerport(p_ptr),
				    port_peernode(p_ptr),
				    imp,
				    TIPC_CONN_MSG,
491
				    err,
P
Per Liden 已提交
492 493 494 495
				    0);
}


496
static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
P
Per Liden 已提交
497
{
498
	u32 imp = msg_importance(&p_ptr->phdr);
P
Per Liden 已提交
499

500
	if (!p_ptr->connected)
501
		return NULL;
P
Per Liden 已提交
502 503 504 505
	if (imp < TIPC_CRITICAL_IMPORTANCE)
		imp++;
	return port_build_proto_msg(port_peerport(p_ptr),
				    port_peernode(p_ptr),
506
				    p_ptr->ref,
P
Per Liden 已提交
507 508 509
				    tipc_own_addr,
				    imp,
				    TIPC_CONN_MSG,
510
				    err,
P
Per Liden 已提交
511 512 513
				    0);
}

514
void tipc_port_recv_proto_msg(struct sk_buff *buf)
P
Per Liden 已提交
515 516
{
	struct tipc_msg *msg = buf_msg(buf);
517
	struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
P
Per Liden 已提交
518
	u32 err = TIPC_OK;
519 520
	struct sk_buff *r_buf = NULL;
	struct sk_buff *abort_buf = NULL;
P
Per Liden 已提交
521 522 523

	if (!p_ptr) {
		err = TIPC_ERR_NO_PORT;
524
	} else if (p_ptr->connected) {
525 526
		if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
		    (port_peerport(p_ptr) != msg_origport(msg))) {
P
Per Liden 已提交
527
			err = TIPC_ERR_NO_PORT;
528
		} else if (msg_type(msg) == CONN_ACK) {
529
			int wakeup = tipc_port_congested(p_ptr) &&
530
				     p_ptr->congested &&
P
Per Liden 已提交
531 532
				     p_ptr->wakeup;
			p_ptr->acked += msg_msgcnt(msg);
533
			if (tipc_port_congested(p_ptr))
P
Per Liden 已提交
534
				goto exit;
535
			p_ptr->congested = 0;
P
Per Liden 已提交
536 537
			if (!wakeup)
				goto exit;
538
			p_ptr->wakeup(p_ptr);
P
Per Liden 已提交
539 540
			goto exit;
		}
541
	} else if (p_ptr->published) {
P
Per Liden 已提交
542 543 544 545
		err = TIPC_ERR_NO_PORT;
	}
	if (err) {
		r_buf = port_build_proto_msg(msg_origport(msg),
546 547
					     msg_orignode(msg),
					     msg_destport(msg),
P
Per Liden 已提交
548
					     tipc_own_addr,
549
					     TIPC_HIGH_IMPORTANCE,
P
Per Liden 已提交
550 551 552 553 554 555 556 557
					     TIPC_CONN_MSG,
					     err,
					     0);
		goto exit;
	}

	/* All is fine */
	if (msg_type(msg) == CONN_PROBE) {
558 559 560 561
		r_buf = port_build_proto_msg(msg_origport(msg),
					     msg_orignode(msg),
					     msg_destport(msg),
					     tipc_own_addr,
P
Per Liden 已提交
562 563 564 565 566 567 568 569
					     CONN_MANAGER,
					     CONN_PROBE_REPLY,
					     TIPC_OK,
					     0);
	}
	p_ptr->probing_state = CONFIRMED;
exit:
	if (p_ptr)
570 571 572
		tipc_port_unlock(p_ptr);
	tipc_net_route_msg(r_buf);
	tipc_net_route_msg(abort_buf);
P
Per Liden 已提交
573 574 575
	buf_discard(buf);
}

576
static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
P
Per Liden 已提交
577
{
578
	struct publication *publ;
P
Per Liden 已提交
579 580

	if (full_id)
581
		tipc_printf(buf, "<%u.%u.%u:%u>:",
P
Per Liden 已提交
582
			    tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
583
			    tipc_node(tipc_own_addr), p_ptr->ref);
P
Per Liden 已提交
584
	else
585
		tipc_printf(buf, "%-10u:", p_ptr->ref);
P
Per Liden 已提交
586

587
	if (p_ptr->connected) {
588 589 590 591 592 593
		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);
594
		if (p_ptr->conn_type != 0)
595
			tipc_printf(buf, " via {%u,%u}",
596 597 598
				    p_ptr->conn_type,
				    p_ptr->conn_instance);
	} else if (p_ptr->published) {
599 600
		tipc_printf(buf, " bound to");
		list_for_each_entry(publ, &p_ptr->publications, pport_list) {
P
Per Liden 已提交
601 602 603 604
			if (publ->lower == publ->upper)
				tipc_printf(buf, " {%u,%u}", publ->type,
					    publ->lower);
			else
605
				tipc_printf(buf, " {%u,%u,%u}", publ->type,
P
Per Liden 已提交
606
					    publ->lower, publ->upper);
607 608 609
		}
	}
	tipc_printf(buf, "\n");
P
Per Liden 已提交
610 611 612 613
}

#define MAX_PORT_QUERY 32768

614
struct sk_buff *tipc_port_get_ports(void)
P
Per Liden 已提交
615 616 617 618
{
	struct sk_buff *buf;
	struct tlv_desc *rep_tlv;
	struct print_buf pb;
619
	struct tipc_port *p_ptr;
P
Per Liden 已提交
620 621
	int str_len;

622
	buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
P
Per Liden 已提交
623 624 625 626
	if (!buf)
		return NULL;
	rep_tlv = (struct tlv_desc *)buf->data;

627 628
	tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
629
	list_for_each_entry(p_ptr, &ports, port_list) {
630
		spin_lock_bh(p_ptr->lock);
P
Per Liden 已提交
631
		port_print(p_ptr, &pb, 0);
632
		spin_unlock_bh(p_ptr->lock);
P
Per Liden 已提交
633
	}
634 635
	spin_unlock_bh(&tipc_port_list_lock);
	str_len = tipc_printbuf_validate(&pb);
P
Per Liden 已提交
636 637 638 639 640 641 642

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

	return buf;
}

643
void tipc_port_reinit(void)
P
Per Liden 已提交
644
{
645
	struct tipc_port *p_ptr;
P
Per Liden 已提交
646 647
	struct tipc_msg *msg;

648
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
649
	list_for_each_entry(p_ptr, &ports, port_list) {
650
		msg = &p_ptr->phdr;
P
Per Liden 已提交
651 652
		if (msg_orignode(msg) == tipc_own_addr)
			break;
653
		msg_set_prevnode(msg, tipc_own_addr);
P
Per Liden 已提交
654 655
		msg_set_orignode(msg, tipc_own_addr);
	}
656
	spin_unlock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670
}


/*
 *  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;
671
	msg_queue_head = NULL;
P
Per Liden 已提交
672 673 674
	spin_unlock_bh(&queue_lock);

	while (buf) {
675
		struct tipc_port *p_ptr;
P
Per Liden 已提交
676 677 678 679 680 681
		struct user_port *up_ptr;
		struct tipc_portid orig;
		struct tipc_name_seq dseq;
		void *usr_handle;
		int connected;
		int published;
682
		u32 message_type;
P
Per Liden 已提交
683 684 685 686

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

688 689 690 691
		message_type = msg_type(msg);
		if (message_type > TIPC_DIRECT_MSG)
			goto reject;	/* Unsupported message type */

692
		p_ptr = tipc_port_lock(dref);
693 694 695
		if (!p_ptr)
			goto reject;	/* Port deleted while msg in queue */

P
Per Liden 已提交
696 697 698 699
		orig.ref = msg_origport(msg);
		orig.node = msg_orignode(msg);
		up_ptr = p_ptr->user_port;
		usr_handle = up_ptr->usr_handle;
700 701
		connected = p_ptr->connected;
		published = p_ptr->published;
P
Per Liden 已提交
702 703 704 705

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

706
		switch (message_type) {
707

P
Per Liden 已提交
708 709 710 711
		case TIPC_CONN_MSG:{
				tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
				u32 peer_port = port_peerport(p_ptr);
				u32 peer_node = port_peernode(p_ptr);
712
				u32 dsz;
P
Per Liden 已提交
713

J
Julia Lawall 已提交
714
				tipc_port_unlock(p_ptr);
715 716
				if (unlikely(!cb))
					goto reject;
P
Per Liden 已提交
717
				if (unlikely(!connected)) {
718
					if (tipc_connect2port(dref, &orig))
P
Per Liden 已提交
719
						goto reject;
720 721
				} else if ((msg_origport(msg) != peer_port) ||
					   (msg_orignode(msg) != peer_node))
P
Per Liden 已提交
722
					goto reject;
723 724 725 726
				dsz = msg_data_sz(msg);
				if (unlikely(dsz &&
					     (++p_ptr->conn_unacked >=
					      TIPC_FLOW_CONTROL_WIN)))
727
					tipc_acknowledge(dref,
728
							 p_ptr->conn_unacked);
P
Per Liden 已提交
729
				skb_pull(buf, msg_hdr_sz(msg));
730
				cb(usr_handle, dref, &buf, msg_data(msg), dsz);
P
Per Liden 已提交
731 732 733 734 735
				break;
			}
		case TIPC_DIRECT_MSG:{
				tipc_msg_event cb = up_ptr->msg_cb;

J
Julia Lawall 已提交
736
				tipc_port_unlock(p_ptr);
737
				if (unlikely(!cb || connected))
P
Per Liden 已提交
738 739
					goto reject;
				skb_pull(buf, msg_hdr_sz(msg));
740
				cb(usr_handle, dref, &buf, msg_data(msg),
P
Per Liden 已提交
741 742 743 744
				   msg_data_sz(msg), msg_importance(msg),
				   &orig);
				break;
			}
745
		case TIPC_MCAST_MSG:
P
Per Liden 已提交
746 747 748
		case TIPC_NAMED_MSG:{
				tipc_named_msg_event cb = up_ptr->named_msg_cb;

J
Julia Lawall 已提交
749
				tipc_port_unlock(p_ptr);
750
				if (unlikely(!cb || connected || !published))
P
Per Liden 已提交
751 752 753
					goto reject;
				dseq.type =  msg_nametype(msg);
				dseq.lower = msg_nameinst(msg);
754 755
				dseq.upper = (message_type == TIPC_NAMED_MSG)
					? dseq.lower : msg_nameupper(msg);
P
Per Liden 已提交
756
				skb_pull(buf, msg_hdr_sz(msg));
757
				cb(usr_handle, dref, &buf, msg_data(msg),
P
Per Liden 已提交
758 759 760 761 762 763 764 765 766 767
				   msg_data_sz(msg), msg_importance(msg),
				   &orig, &dseq);
				break;
			}
		}
		if (buf)
			buf_discard(buf);
		buf = next;
		continue;
err:
768
		switch (message_type) {
769

P
Per Liden 已提交
770
		case TIPC_CONN_MSG:{
771
				tipc_conn_shutdown_event cb =
P
Per Liden 已提交
772 773 774 775
					up_ptr->conn_err_cb;
				u32 peer_port = port_peerport(p_ptr);
				u32 peer_node = port_peernode(p_ptr);

J
Julia Lawall 已提交
776
				tipc_port_unlock(p_ptr);
777
				if (!cb || !connected)
P
Per Liden 已提交
778
					break;
779 780
				if ((msg_origport(msg) != peer_port) ||
				    (msg_orignode(msg) != peer_node))
P
Per Liden 已提交
781 782 783 784 785 786 787 788 789 790
					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 已提交
791
				tipc_port_unlock(p_ptr);
792
				if (!cb || connected)
P
Per Liden 已提交
793 794 795 796 797 798
					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;
			}
799
		case TIPC_MCAST_MSG:
P
Per Liden 已提交
800
		case TIPC_NAMED_MSG:{
801
				tipc_named_msg_err_event cb =
P
Per Liden 已提交
802 803
					up_ptr->named_err_cb;

J
Julia Lawall 已提交
804
				tipc_port_unlock(p_ptr);
805
				if (!cb || connected)
P
Per Liden 已提交
806 807 808
					break;
				dseq.type =  msg_nametype(msg);
				dseq.lower = msg_nameinst(msg);
809 810
				dseq.upper = (message_type == TIPC_NAMED_MSG)
					? dseq.lower : msg_nameupper(msg);
P
Per Liden 已提交
811
				skb_pull(buf, msg_hdr_sz(msg));
812
				cb(usr_handle, dref, &buf, msg_data(msg),
P
Per Liden 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
				   msg_data_sz(msg), msg_errcode(msg), &dseq);
				break;
			}
		}
		if (buf)
			buf_discard(buf);
		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;
841
		tipc_k_signal((Handler)port_dispatcher_sigh, 0);
P
Per Liden 已提交
842 843
	}
	spin_unlock_bh(&queue_lock);
844
	return 0;
P
Per Liden 已提交
845 846
}

847
/*
P
Per Liden 已提交
848
 * Wake up port after congestion: Called with port locked,
849
 *
P
Per Liden 已提交
850 851 852 853
 */

static void port_wakeup_sh(unsigned long ref)
{
854
	struct tipc_port *p_ptr;
P
Per Liden 已提交
855
	struct user_port *up_ptr;
856 857
	tipc_continue_event cb = NULL;
	void *uh = NULL;
P
Per Liden 已提交
858

859
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
860 861 862 863 864 865
	if (p_ptr) {
		up_ptr = p_ptr->user_port;
		if (up_ptr) {
			cb = up_ptr->continue_event_cb;
			uh = up_ptr->usr_handle;
		}
866
		tipc_port_unlock(p_ptr);
P
Per Liden 已提交
867 868 869 870 871 872 873 874
	}
	if (cb)
		cb(uh, ref);
}


static void port_wakeup(struct tipc_port *p_ptr)
{
875
	tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
P
Per Liden 已提交
876 877 878 879
}

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

883
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
884 885
	if (!p_ptr)
		return;
886 887
	if (p_ptr->connected) {
		p_ptr->conn_unacked -= ack;
P
Per Liden 已提交
888 889 890 891 892 893
		buf = port_build_proto_msg(port_peerport(p_ptr),
					   port_peernode(p_ptr),
					   ref,
					   tipc_own_addr,
					   CONN_MANAGER,
					   CONN_ACK,
894
					   TIPC_OK,
P
Per Liden 已提交
895 896
					   ack);
	}
897 898
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
899 900 901
}

/*
902
 * tipc_createport(): user level call.
P
Per Liden 已提交
903 904
 */

905
int tipc_createport(void *usr_handle,
906 907 908 909 910 911 912
		    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 已提交
913 914 915 916
		    tipc_continue_event continue_event_cb,/* May be zero */
		    u32 *portref)
{
	struct user_port *up_ptr;
917
	struct tipc_port *p_ptr;
P
Per Liden 已提交
918

919
	up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
920
	if (!up_ptr) {
921
		warn("Port creation failed, no memory\n");
P
Per Liden 已提交
922 923
		return -ENOMEM;
	}
924
	p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
925 926
						   port_wakeup, importance);
	if (!p_ptr) {
P
Per Liden 已提交
927 928 929 930 931 932
		kfree(up_ptr);
		return -ENOMEM;
	}

	p_ptr->user_port = up_ptr;
	up_ptr->usr_handle = usr_handle;
933
	up_ptr->ref = p_ptr->ref;
P
Per Liden 已提交
934 935 936 937 938 939 940
	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;
941
	*portref = p_ptr->ref;
942
	tipc_port_unlock(p_ptr);
943
	return 0;
P
Per Liden 已提交
944 945 946 947
}

int tipc_portimportance(u32 ref, unsigned int *importance)
{
948
	struct tipc_port *p_ptr;
949

950
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
951 952
	if (!p_ptr)
		return -EINVAL;
953
	*importance = (unsigned int)msg_importance(&p_ptr->phdr);
J
Julia Lawall 已提交
954
	tipc_port_unlock(p_ptr);
955
	return 0;
P
Per Liden 已提交
956 957 958 959
}

int tipc_set_portimportance(u32 ref, unsigned int imp)
{
960
	struct tipc_port *p_ptr;
P
Per Liden 已提交
961 962 963 964

	if (imp > TIPC_CRITICAL_IMPORTANCE)
		return -EINVAL;

965
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
966 967
	if (!p_ptr)
		return -EINVAL;
968
	msg_set_importance(&p_ptr->phdr, (u32)imp);
J
Julia Lawall 已提交
969
	tipc_port_unlock(p_ptr);
970
	return 0;
P
Per Liden 已提交
971 972 973 974 975
}


int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
{
976
	struct tipc_port *p_ptr;
P
Per Liden 已提交
977 978 979 980
	struct publication *publ;
	u32 key;
	int res = -EINVAL;

981
	p_ptr = tipc_port_lock(ref);
982 983 984
	if (!p_ptr)
		return -EINVAL;

985
	if (p_ptr->connected)
P
Per Liden 已提交
986 987 988 989 990 991 992 993 994 995
		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;
	}
996
	publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
997
				    scope, p_ptr->ref, key);
P
Per Liden 已提交
998 999 1000
	if (publ) {
		list_add(&publ->pport_list, &p_ptr->publications);
		p_ptr->pub_count++;
1001
		p_ptr->published = 1;
1002
		res = 0;
P
Per Liden 已提交
1003 1004
	}
exit:
1005
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
1006 1007 1008 1009 1010
	return res;
}

int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
{
1011
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1012 1013 1014
	struct publication *publ;
	struct publication *tpubl;
	int res = -EINVAL;
1015

1016
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1017 1018 1019
	if (!p_ptr)
		return -EINVAL;
	if (!seq) {
1020
		list_for_each_entry_safe(publ, tpubl,
P
Per Liden 已提交
1021
					 &p_ptr->publications, pport_list) {
1022
			tipc_nametbl_withdraw(publ->type, publ->lower,
1023
					      publ->ref, publ->key);
P
Per Liden 已提交
1024
		}
1025
		res = 0;
P
Per Liden 已提交
1026
	} else {
1027
		list_for_each_entry_safe(publ, tpubl,
P
Per Liden 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036
					 &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;
1037
			tipc_nametbl_withdraw(publ->type, publ->lower,
1038
					      publ->ref, publ->key);
1039
			res = 0;
P
Per Liden 已提交
1040 1041 1042 1043
			break;
		}
	}
	if (list_empty(&p_ptr->publications))
1044
		p_ptr->published = 0;
1045
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
1046 1047 1048 1049 1050
	return res;
}

int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
{
1051
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1052 1053 1054
	struct tipc_msg *msg;
	int res = -EINVAL;

1055
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1056 1057
	if (!p_ptr)
		return -EINVAL;
1058
	if (p_ptr->published || p_ptr->connected)
P
Per Liden 已提交
1059 1060 1061 1062
		goto exit;
	if (!peer->ref)
		goto exit;

1063
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1064 1065 1066
	msg_set_destnode(msg, peer->node);
	msg_set_destport(msg, peer->ref);
	msg_set_orignode(msg, tipc_own_addr);
1067
	msg_set_origport(msg, p_ptr->ref);
P
Per Liden 已提交
1068
	msg_set_type(msg, TIPC_CONN_MSG);
1069
	msg_set_lookup_scope(msg, 0);
1070
	msg_set_hdr_sz(msg, SHORT_H_SIZE);
P
Per Liden 已提交
1071 1072 1073

	p_ptr->probing_interval = PROBING_INTERVAL;
	p_ptr->probing_state = CONFIRMED;
1074
	p_ptr->connected = 1;
P
Per Liden 已提交
1075 1076
	k_start_timer(&p_ptr->timer, p_ptr->probing_interval);

1077
	tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
1078
			  (void *)(unsigned long)ref,
P
Per Liden 已提交
1079
			  (net_ev_handler)port_handle_node_down);
1080
	res = 0;
P
Per Liden 已提交
1081
exit:
1082
	tipc_port_unlock(p_ptr);
1083
	p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
P
Per Liden 已提交
1084 1085 1086
	return res;
}

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
/**
 * 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(
1101
			&((struct tipc_port *)tp_ptr)->subscription);
1102
		res = 0;
1103 1104 1105 1106 1107 1108
	} else {
		res = -ENOTCONN;
	}
	return res;
}

P
Per Liden 已提交
1109 1110 1111 1112 1113 1114 1115
/*
 * tipc_disconnect(): Disconnect port form peer.
 *                    This is a node local operation.
 */

int tipc_disconnect(u32 ref)
{
1116
	struct tipc_port *p_ptr;
1117
	int res;
P
Per Liden 已提交
1118

1119
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1120 1121
	if (!p_ptr)
		return -EINVAL;
1122
	res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1123
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
1124 1125 1126 1127 1128 1129 1130 1131
	return res;
}

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

1135
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1136 1137 1138
	if (!p_ptr)
		return -EINVAL;

1139 1140
	if (p_ptr->connected) {
		u32 imp = msg_importance(&p_ptr->phdr);
P
Per Liden 已提交
1141 1142 1143 1144 1145 1146 1147 1148
		if (imp < TIPC_CRITICAL_IMPORTANCE)
			imp++;
		buf = port_build_proto_msg(port_peerport(p_ptr),
					   port_peernode(p_ptr),
					   ref,
					   tipc_own_addr,
					   imp,
					   TIPC_CONN_MSG,
1149
					   TIPC_CONN_SHUTDOWN,
P
Per Liden 已提交
1150 1151
					   0);
	}
1152 1153
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
1154 1155 1156 1157
	return tipc_disconnect(ref);
}

/*
1158
 *  tipc_port_recv_sections(): Concatenate and deliver sectioned
P
Per Liden 已提交
1159 1160 1161
 *                        message for this node.
 */

1162
static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
1163
				   struct iovec const *msg_sect)
P
Per Liden 已提交
1164 1165 1166
{
	struct sk_buff *buf;
	int res;
1167

1168
	res = tipc_msg_build(&sender->phdr, msg_sect, num_sect,
P
Per Liden 已提交
1169 1170
			MAX_MSG_SIZE, !sender->user_port, &buf);
	if (likely(buf))
1171
		tipc_port_recv_msg(buf);
P
Per Liden 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180
	return res;
}

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

int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
{
1181
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1182 1183 1184
	u32 destnode;
	int res;

1185
	p_ptr = tipc_port_deref(ref);
1186
	if (!p_ptr || !p_ptr->connected)
P
Per Liden 已提交
1187 1188
		return -EINVAL;

1189
	p_ptr->congested = 1;
1190
	if (!tipc_port_congested(p_ptr)) {
P
Per Liden 已提交
1191 1192
		destnode = port_peernode(p_ptr);
		if (likely(destnode != tipc_own_addr))
1193 1194
			res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
							   destnode);
P
Per Liden 已提交
1195
		else
1196
			res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
P
Per Liden 已提交
1197 1198

		if (likely(res != -ELINKCONG)) {
1199
			p_ptr->congested = 0;
1200 1201
			if (res > 0)
				p_ptr->sent++;
P
Per Liden 已提交
1202 1203 1204 1205
			return res;
		}
	}
	if (port_unreliable(p_ptr)) {
1206
		p_ptr->congested = 0;
P
Per Liden 已提交
1207
		/* Just calculate msg length and return */
1208
		return tipc_msg_calc_data_size(msg_sect, num_sect);
P
Per Liden 已提交
1209 1210 1211 1212 1213
	}
	return -ELINKCONG;
}

/**
1214
 * tipc_send2name - send message sections to port name
P
Per Liden 已提交
1215 1216
 */

1217 1218
int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
	   unsigned int num_sect, struct iovec const *msg_sect)
P
Per Liden 已提交
1219
{
1220
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1221 1222
	struct tipc_msg *msg;
	u32 destnode = domain;
1223
	u32 destport;
P
Per Liden 已提交
1224 1225
	int res;

1226
	p_ptr = tipc_port_deref(ref);
1227
	if (!p_ptr || p_ptr->connected)
P
Per Liden 已提交
1228 1229
		return -EINVAL;

1230
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1231
	msg_set_type(msg, TIPC_NAMED_MSG);
1232 1233
	msg_set_orignode(msg, tipc_own_addr);
	msg_set_origport(msg, ref);
P
Per Liden 已提交
1234 1235 1236
	msg_set_hdr_sz(msg, LONG_H_SIZE);
	msg_set_nametype(msg, name->type);
	msg_set_nameinst(msg, name->instance);
1237
	msg_set_lookup_scope(msg, tipc_addr_scope(domain));
1238
	destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
P
Per Liden 已提交
1239 1240 1241
	msg_set_destnode(msg, destnode);
	msg_set_destport(msg, destport);

1242
	if (likely(destport)) {
P
Per Liden 已提交
1243
		if (likely(destnode == tipc_own_addr))
1244 1245 1246 1247 1248 1249 1250 1251
			res = tipc_port_recv_sections(p_ptr, num_sect,
						      msg_sect);
		else
			res = tipc_link_send_sections_fast(p_ptr, msg_sect,
							   num_sect, destnode);
		if (likely(res != -ELINKCONG)) {
			if (res > 0)
				p_ptr->sent++;
P
Per Liden 已提交
1252
			return res;
1253
		}
P
Per Liden 已提交
1254 1255
		if (port_unreliable(p_ptr)) {
			/* Just calculate msg length and return */
1256
			return tipc_msg_calc_data_size(msg_sect, num_sect);
P
Per Liden 已提交
1257 1258 1259
		}
		return -ELINKCONG;
	}
1260
	return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1261
					 TIPC_ERR_NO_NAME);
P
Per Liden 已提交
1262 1263 1264
}

/**
1265
 * tipc_send2port - send message sections to port identity
P
Per Liden 已提交
1266 1267
 */

1268 1269
int tipc_send2port(u32 ref, struct tipc_portid const *dest,
	   unsigned int num_sect, struct iovec const *msg_sect)
P
Per Liden 已提交
1270
{
1271
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1272 1273 1274
	struct tipc_msg *msg;
	int res;

1275
	p_ptr = tipc_port_deref(ref);
1276
	if (!p_ptr || p_ptr->connected)
P
Per Liden 已提交
1277 1278
		return -EINVAL;

1279
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1280
	msg_set_type(msg, TIPC_DIRECT_MSG);
1281
	msg_set_lookup_scope(msg, 0);
1282 1283
	msg_set_orignode(msg, tipc_own_addr);
	msg_set_origport(msg, ref);
P
Per Liden 已提交
1284 1285 1286
	msg_set_destnode(msg, dest->node);
	msg_set_destport(msg, dest->ref);
	msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1287

P
Per Liden 已提交
1288
	if (dest->node == tipc_own_addr)
1289 1290 1291 1292 1293 1294 1295
		res =  tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
	else
		res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
						   dest->node);
	if (likely(res != -ELINKCONG)) {
		if (res > 0)
			p_ptr->sent++;
P
Per Liden 已提交
1296
		return res;
1297
	}
P
Per Liden 已提交
1298 1299
	if (port_unreliable(p_ptr)) {
		/* Just calculate msg length and return */
1300
		return tipc_msg_calc_data_size(msg_sect, num_sect);
P
Per Liden 已提交
1301 1302 1303 1304
	}
	return -ELINKCONG;
}

1305
/**
1306
 * tipc_send_buf2port - send message buffer to port identity
P
Per Liden 已提交
1307 1308
 */

1309 1310
int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
	       struct sk_buff *buf, unsigned int dsz)
P
Per Liden 已提交
1311
{
1312
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1313 1314 1315
	struct tipc_msg *msg;
	int res;

1316 1317
	p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
	if (!p_ptr || p_ptr->connected)
P
Per Liden 已提交
1318 1319
		return -EINVAL;

1320
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1321
	msg_set_type(msg, TIPC_DIRECT_MSG);
1322 1323
	msg_set_orignode(msg, tipc_own_addr);
	msg_set_origport(msg, ref);
P
Per Liden 已提交
1324 1325 1326 1327 1328 1329 1330 1331
	msg_set_destnode(msg, dest->node);
	msg_set_destport(msg, dest->ref);
	msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
	msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
	if (skb_cow(buf, DIR_MSG_H_SIZE))
		return -ENOMEM;

	skb_push(buf, DIR_MSG_H_SIZE);
1332
	skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
1333

P
Per Liden 已提交
1334
	if (dest->node == tipc_own_addr)
1335 1336 1337 1338 1339 1340
		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 已提交
1341
		return res;
1342
	}
P
Per Liden 已提交
1343 1344 1345 1346 1347
	if (port_unreliable(p_ptr))
		return dsz;
	return -ELINKCONG;
}