node.c 18.2 KB
Newer Older
P
Per Liden 已提交
1 2
/*
 * net/tipc/node.c: TIPC node management routines
3
 *
P
Per Liden 已提交
4
 * Copyright (c) 2000-2006, Ericsson AB
5
 * Copyright (c) 2005-2006, 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
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "config.h"
#include "node.h"
#include "port.h"
#include "name_distr.h"

43 44
static void node_lost_contact(struct tipc_node *n_ptr);
static void node_established_contact(struct tipc_node *n_ptr);
P
Per Liden 已提交
45

46 47
/* sorted list of nodes within cluster */
static struct tipc_node *tipc_nodes = NULL;
P
Per Liden 已提交
48

49 50
static DEFINE_SPINLOCK(node_create_lock);

P
Per Liden 已提交
51 52
u32 tipc_own_tag = 0;

53 54 55 56 57 58 59 60 61 62
/**
 * tipc_node_create - create neighboring node
 *
 * Currently, this routine is called by neighbor discovery code, which holds
 * net_lock for reading only.  We must take node_create_lock to ensure a node
 * isn't created twice if two different bearers discover the node at the same
 * time.  (It would be preferable to switch to holding net_lock in write mode,
 * but this is a non-trivial change.)
 */

63
struct tipc_node *tipc_node_create(u32 addr)
P
Per Liden 已提交
64 65
{
	struct cluster *c_ptr;
66 67
	struct tipc_node *n_ptr;
	struct tipc_node **curr_node;
P
Per Liden 已提交
68

69 70 71 72 73 74 75 76 77 78 79
	spin_lock_bh(&node_create_lock);

	for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
		if (addr < n_ptr->addr)
			break;
		if (addr == n_ptr->addr) {
			spin_unlock_bh(&node_create_lock);
			return n_ptr;
		}
	}

80
	n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
81
	if (!n_ptr) {
82
		spin_unlock_bh(&node_create_lock);
83 84 85 86 87 88 89 90 91
		warn("Node creation failed, no memory\n");
		return NULL;
	}

	c_ptr = tipc_cltr_find(addr);
	if (!c_ptr) {
		c_ptr = tipc_cltr_create(addr);
	}
	if (!c_ptr) {
92
		spin_unlock_bh(&node_create_lock);
93 94 95
		kfree(n_ptr);
		return NULL;
	}
96

97
	n_ptr->addr = addr;
98
		spin_lock_init(&n_ptr->lock);
99 100 101 102 103 104
	INIT_LIST_HEAD(&n_ptr->nsub);
	n_ptr->owner = c_ptr;
	tipc_cltr_attach_node(c_ptr, n_ptr);
	n_ptr->last_router = -1;

	/* Insert node into ordered list */
105
	for (curr_node = &tipc_nodes; *curr_node;
106 107 108 109 110 111 112
	     curr_node = &(*curr_node)->next) {
		if (addr < (*curr_node)->addr) {
			n_ptr->next = *curr_node;
			break;
		}
	}
	(*curr_node) = n_ptr;
113
	spin_unlock_bh(&node_create_lock);
P
Per Liden 已提交
114 115 116
	return n_ptr;
}

117
void tipc_node_delete(struct tipc_node *n_ptr)
P
Per Liden 已提交
118 119 120 121 122 123 124 125 126 127
{
	if (!n_ptr)
		return;

	dbg("node %x deleted\n", n_ptr->addr);
	kfree(n_ptr);
}


/**
128
 * tipc_node_link_up - handle addition of link
129
 *
P
Per Liden 已提交
130 131 132
 * Link becomes active (alone or shared) or standby, depending on its priority.
 */

133
void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
P
Per Liden 已提交
134 135 136
{
	struct link **active = &n_ptr->active_links[0];

137 138
	n_ptr->working_links++;

P
Per Liden 已提交
139 140
	info("Established link <%s> on network plane %c\n",
	     l_ptr->name, l_ptr->b_ptr->net_plane);
141

P
Per Liden 已提交
142 143 144 145 146 147
	if (!active[0]) {
		dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
		active[0] = active[1] = l_ptr;
		node_established_contact(n_ptr);
		return;
	}
148
	if (l_ptr->priority < active[0]->priority) {
149
		info("New link <%s> becomes standby\n", l_ptr->name);
P
Per Liden 已提交
150 151
		return;
	}
152
	tipc_link_send_duplicate(active[0], l_ptr);
153
	if (l_ptr->priority == active[0]->priority) {
P
Per Liden 已提交
154 155 156
		active[0] = l_ptr;
		return;
	}
157 158 159
	info("Old link <%s> becomes standby\n", active[0]->name);
	if (active[1] != active[0])
		info("Old link <%s> becomes standby\n", active[1]->name);
P
Per Liden 已提交
160 161 162 163 164 165 166
	active[0] = active[1] = l_ptr;
}

/**
 * node_select_active_links - select active link
 */

167
static void node_select_active_links(struct tipc_node *n_ptr)
P
Per Liden 已提交
168 169 170 171 172
{
	struct link **active = &n_ptr->active_links[0];
	u32 i;
	u32 highest_prio = 0;

173
	active[0] = active[1] = NULL;
P
Per Liden 已提交
174 175

	for (i = 0; i < MAX_BEARERS; i++) {
176
		struct link *l_ptr = n_ptr->links[i];
P
Per Liden 已提交
177

178
		if (!l_ptr || !tipc_link_is_up(l_ptr) ||
P
Per Liden 已提交
179 180 181 182
		    (l_ptr->priority < highest_prio))
			continue;

		if (l_ptr->priority > highest_prio) {
183
			highest_prio = l_ptr->priority;
P
Per Liden 已提交
184 185 186 187 188 189 190 191
			active[0] = active[1] = l_ptr;
		} else {
			active[1] = l_ptr;
		}
	}
}

/**
192
 * tipc_node_link_down - handle loss of link
P
Per Liden 已提交
193 194
 */

195
void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
P
Per Liden 已提交
196 197 198
{
	struct link **active;

199 200
	n_ptr->working_links--;

201
	if (!tipc_link_is_active(l_ptr)) {
P
Per Liden 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215
		info("Lost standby link <%s> on network plane %c\n",
		     l_ptr->name, l_ptr->b_ptr->net_plane);
		return;
	}
	info("Lost link <%s> on network plane %c\n",
		l_ptr->name, l_ptr->b_ptr->net_plane);

	active = &n_ptr->active_links[0];
	if (active[0] == l_ptr)
		active[0] = active[1];
	if (active[1] == l_ptr)
		active[1] = active[0];
	if (active[0] == l_ptr)
		node_select_active_links(n_ptr);
216
	if (tipc_node_is_up(n_ptr))
217
		tipc_link_changeover(l_ptr);
218
	else
P
Per Liden 已提交
219 220 221
		node_lost_contact(n_ptr);
}

222
int tipc_node_has_active_links(struct tipc_node *n_ptr)
P
Per Liden 已提交
223
{
224
	return n_ptr->active_links[0] != NULL;
P
Per Liden 已提交
225 226
}

227
int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
P
Per Liden 已提交
228
{
E
Eric Dumazet 已提交
229
	return n_ptr->working_links > 1;
P
Per Liden 已提交
230 231
}

232
static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
P
Per Liden 已提交
233
{
E
Eric Dumazet 已提交
234
	return n_ptr && (n_ptr->last_router >= 0);
P
Per Liden 已提交
235 236
}

237
int tipc_node_is_up(struct tipc_node *n_ptr)
P
Per Liden 已提交
238
{
E
Eric Dumazet 已提交
239
	return tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr);
P
Per Liden 已提交
240 241
}

242
struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
P
Per Liden 已提交
243
{
244
	struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
P
Per Liden 已提交
245 246

	if (!n_ptr)
247
		n_ptr = tipc_node_create(l_ptr->addr);
248
	if (n_ptr) {
P
Per Liden 已提交
249 250 251
		u32 bearer_id = l_ptr->b_ptr->identity;
		char addr_string[16];

252 253
		if (n_ptr->link_cnt >= 2) {
			err("Attempt to create third link to %s\n",
254
			    tipc_addr_string_fill(addr_string, n_ptr->addr));
255 256 257 258 259
			return NULL;
		}

		if (!n_ptr->links[bearer_id]) {
			n_ptr->links[bearer_id] = l_ptr;
260
			tipc_net.links++;
261 262 263
			n_ptr->link_cnt++;
			return n_ptr;
		}
264
		err("Attempt to establish second link on <%s> to %s\n",
265
		    l_ptr->b_ptr->publ.name,
266
		    tipc_addr_string_fill(addr_string, l_ptr->addr));
267
	}
268
	return NULL;
P
Per Liden 已提交
269 270
}

271
void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
P
Per Liden 已提交
272
{
273
	n_ptr->links[l_ptr->b_ptr->identity] = NULL;
274
	tipc_net.links--;
P
Per Liden 已提交
275 276 277 278 279 280 281
	n_ptr->link_cnt--;
}

/*
 * Routing table management - five cases to handle:
 *
 * 1: A link towards a zone/cluster external node comes up.
282 283 284
 *    => Send a multicast message updating routing tables of all
 *    system nodes within own cluster that the new destination
 *    can be reached via this node.
P
Per Liden 已提交
285 286 287
 *    (node.establishedContact()=>cluster.multicastNewRoute())
 *
 * 2: A link towards a slave node comes up.
288 289 290
 *    => Send a multicast message updating routing tables of all
 *    system nodes within own cluster that the new destination
 *    can be reached via this node.
P
Per Liden 已提交
291
 *    (node.establishedContact()=>cluster.multicastNewRoute())
292
 *    => Send a  message to the slave node about existence
P
Per Liden 已提交
293 294 295 296 297 298 299 300 301
 *    of all system nodes within cluster:
 *    (node.establishedContact()=>cluster.sendLocalRoutes())
 *
 * 3: A new cluster local system node becomes available.
 *    => Send message(s) to this particular node containing
 *    information about all cluster external and slave
 *     nodes which can be reached via this node.
 *    (node.establishedContact()==>network.sendExternalRoutes())
 *    (node.establishedContact()==>network.sendSlaveRoutes())
302
 *    => Send messages to all directly connected slave nodes
P
Per Liden 已提交
303 304
 *    containing information about the existence of the new node
 *    (node.establishedContact()=>cluster.multicastNewRoute())
305
 *
P
Per Liden 已提交
306 307
 * 4: The link towards a zone/cluster external node or slave
 *    node goes down.
308
 *    => Send a multcast message updating routing tables of all
P
Per Liden 已提交
309 310 311 312 313 314 315 316 317
 *    nodes within cluster that the new destination can not any
 *    longer be reached via this node.
 *    (node.lostAllLinks()=>cluster.bcastLostRoute())
 *
 * 5: A cluster local system node becomes unavailable.
 *    => Remove all references to this node from the local
 *    routing tables. Note: This is a completely node
 *    local operation.
 *    (node.lostAllLinks()=>network.removeAsRouter())
318
 *    => Send messages to all directly connected slave nodes
P
Per Liden 已提交
319 320 321 322 323
 *    containing information about loss of the node
 *    (node.establishedContact()=>cluster.multicastLostRoute())
 *
 */

324
static void node_established_contact(struct tipc_node *n_ptr)
P
Per Liden 已提交
325 326 327 328
{
	struct cluster *c_ptr;

	dbg("node_established_contact:-> %x\n", n_ptr->addr);
329
	if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
330
		tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
P
Per Liden 已提交
331 332
	}

333 334
	/* Syncronize broadcast acks */
	n_ptr->bclink.acked = tipc_bclink_get_last_sent();
P
Per Liden 已提交
335 336 337

	if (!in_own_cluster(n_ptr->addr)) {
		/* Usage case 1 (see above) */
338
		c_ptr = tipc_cltr_find(tipc_own_addr);
P
Per Liden 已提交
339
		if (!c_ptr)
340
			c_ptr = tipc_cltr_create(tipc_own_addr);
341 342
		if (c_ptr)
			tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
343
						  tipc_max_nodes);
P
Per Liden 已提交
344
		return;
345
	}
P
Per Liden 已提交
346 347 348

	c_ptr = n_ptr->owner;
	if (n_ptr->bclink.supported) {
349
		tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
P
Per Liden 已提交
350 351 352 353 354
		if (n_ptr->addr < tipc_own_addr)
			tipc_own_tag++;
	}

	/* Case 3 (see above) */
355
	tipc_net_send_external_routes(n_ptr->addr);
P
Per Liden 已提交
356 357
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371
static void node_cleanup_finished(unsigned long node_addr)
{
	struct tipc_node *n_ptr;

	read_lock_bh(&tipc_net_lock);
	n_ptr = tipc_node_find(node_addr);
	if (n_ptr) {
		tipc_node_lock(n_ptr);
		n_ptr->cleanup_required = 0;
		tipc_node_unlock(n_ptr);
	}
	read_unlock_bh(&tipc_net_lock);
}

372
static void node_lost_contact(struct tipc_node *n_ptr)
P
Per Liden 已提交
373 374
{
	struct cluster *c_ptr;
375
	struct tipc_node_subscr *ns, *tns;
P
Per Liden 已提交
376 377 378
	char addr_string[16];
	u32 i;

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	/* Clean up broadcast reception remains */
	n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
	while (n_ptr->bclink.deferred_head) {
		struct sk_buff* buf = n_ptr->bclink.deferred_head;
		n_ptr->bclink.deferred_head = buf->next;
		buf_discard(buf);
	}
	if (n_ptr->bclink.defragm) {
		buf_discard(n_ptr->bclink.defragm);
		n_ptr->bclink.defragm = NULL;
	}
	if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
		tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
	}

	/* Update routing tables */
395 396 397 398 399
	if (!in_own_cluster(n_ptr->addr)) {
		/* Case 4 (see above) */
		c_ptr = tipc_cltr_find(tipc_own_addr);
		tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
					   tipc_max_nodes);
P
Per Liden 已提交
400
	} else {
401 402 403 404 405 406
		/* Case 5 (see above) */
		c_ptr = tipc_cltr_find(n_ptr->addr);
		if (n_ptr->bclink.supported) {
			tipc_nmap_remove(&tipc_cltr_bcast_nodes, n_ptr->addr);
			if (n_ptr->addr < tipc_own_addr)
				tipc_own_tag--;
P
Per Liden 已提交
407
		}
408
		tipc_net_remove_as_router(n_ptr->addr);
P
Per Liden 已提交
409
	}
410
	if (tipc_node_has_active_routes(n_ptr))
P
Per Liden 已提交
411 412
		return;

413
	info("Lost contact with %s\n",
414
	     tipc_addr_string_fill(addr_string, n_ptr->addr));
P
Per Liden 已提交
415 416 417 418

	/* Abort link changeover */
	for (i = 0; i < MAX_BEARERS; i++) {
		struct link *l_ptr = n_ptr->links[i];
419
		if (!l_ptr)
P
Per Liden 已提交
420 421 422
			continue;
		l_ptr->reset_checkpoint = l_ptr->next_in_no;
		l_ptr->exp_msg_count = 0;
423
		tipc_link_reset_fragments(l_ptr);
P
Per Liden 已提交
424 425 426 427
	}

	/* Notify subscribers */
	list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
428
		ns->node = NULL;
P
Per Liden 已提交
429
		list_del_init(&ns->nodesub_list);
430 431
		tipc_k_signal((Handler)ns->handle_node_down,
			      (unsigned long)ns->usr_handle);
P
Per Liden 已提交
432
	}
433 434 435 436 437

	/* Prevent re-contact with node until all cleanup is done */

	n_ptr->cleanup_required = 1;
	tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
P
Per Liden 已提交
438 439 440
}

/**
441
 * tipc_node_select_next_hop - find the next-hop node for a message
442
 *
P
Per Liden 已提交
443 444 445
 * Called by when cluster local lookup has failed.
 */

446
struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
P
Per Liden 已提交
447
{
448
	struct tipc_node *n_ptr;
P
Per Liden 已提交
449 450
	u32 router_addr;

451 452
	if (!tipc_addr_domain_valid(addr))
		return NULL;
P
Per Liden 已提交
453 454

	/* Look for direct link to destination processsor */
455 456
	n_ptr = tipc_node_find(addr);
	if (n_ptr && tipc_node_has_active_links(n_ptr))
457
		return n_ptr;
P
Per Liden 已提交
458 459

	/* Cluster local system nodes *must* have direct links */
460
	if (in_own_cluster(addr))
461
		return NULL;
P
Per Liden 已提交
462 463

	/* Look for cluster local router with direct link to node */
464
	router_addr = tipc_node_select_router(n_ptr, selector);
465 466
	if (router_addr)
		return tipc_node_select(router_addr, selector);
P
Per Liden 已提交
467 468 469

	/* Inter zone/cluster -- find any direct link to remote cluster */
	addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
470 471
	n_ptr = tipc_net_select_remote_node(addr, selector);
	if (n_ptr && tipc_node_has_active_links(n_ptr))
472
		return n_ptr;
P
Per Liden 已提交
473 474

	/* Last resort -- look for any router to anywhere in remote zone */
475
	router_addr =  tipc_net_select_router(addr, selector);
476 477
	if (router_addr)
		return tipc_node_select(router_addr, selector);
P
Per Liden 已提交
478

479
	return NULL;
P
Per Liden 已提交
480 481 482
}

/**
483
 * tipc_node_select_router - select router to reach specified node
484 485
 *
 * Uses a deterministic and fair algorithm for selecting router node.
P
Per Liden 已提交
486 487
 */

488
u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
P
Per Liden 已提交
489 490 491 492 493 494
{
	u32 ulim;
	u32 mask;
	u32 start;
	u32 r;

495 496
	if (!n_ptr)
		return 0;
P
Per Liden 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525

	if (n_ptr->last_router < 0)
		return 0;
	ulim = ((n_ptr->last_router + 1) * 32) - 1;

	/* Start entry must be random */
	mask = tipc_max_nodes;
	while (mask > ulim)
		mask >>= 1;
	start = ref & mask;
	r = start;

	/* Lookup upwards with wrap-around */
	do {
		if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
			break;
	} while (++r <= ulim);
	if (r > ulim) {
		r = 1;
		do {
			if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
				break;
		} while (++r < start);
		assert(r != start);
	}
	assert(r && (r <= ulim));
	return tipc_addr(own_zone(), own_cluster(), r);
}

526
void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
P
Per Liden 已提交
527 528 529
{
	u32 r_num = tipc_node(router);

530
	n_ptr->routers[r_num / 32] =
P
Per Liden 已提交
531 532
		((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
	n_ptr->last_router = tipc_max_nodes / 32;
533
	while ((--n_ptr->last_router >= 0) &&
P
Per Liden 已提交
534 535 536
	       !n_ptr->routers[n_ptr->last_router]);
}

537
void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
P
Per Liden 已提交
538 539 540 541 542 543 544 545 546
{
	u32 r_num = tipc_node(router);

	if (n_ptr->last_router < 0)
		return;		/* No routes */

	n_ptr->routers[r_num / 32] =
		((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
	n_ptr->last_router = tipc_max_nodes / 32;
547
	while ((--n_ptr->last_router >= 0) &&
P
Per Liden 已提交
548 549
	       !n_ptr->routers[n_ptr->last_router]);

550
	if (!tipc_node_is_up(n_ptr))
P
Per Liden 已提交
551 552 553
		node_lost_contact(n_ptr);
}

554
struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
P
Per Liden 已提交
555 556 557
{
	u32 domain;
	struct sk_buff *buf;
558
	struct tipc_node *n_ptr;
559
	struct tipc_node_info node_info;
560
	u32 payload_size;
P
Per Liden 已提交
561 562

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
563
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
564

A
Al Viro 已提交
565
	domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
566 567 568
	if (!tipc_addr_domain_valid(domain))
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (network address)");
P
Per Liden 已提交
569

570 571 572
	read_lock_bh(&tipc_net_lock);
	if (!tipc_nodes) {
		read_unlock_bh(&tipc_net_lock);
573
		return tipc_cfg_reply_none();
574
	}
P
Per Liden 已提交
575

576
	/* For now, get space for all other nodes */
P
Per Liden 已提交
577

578
	payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
579 580
	if (payload_size > 32768u) {
		read_unlock_bh(&tipc_net_lock);
581 582
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
						   " (too many nodes)");
583
	}
584
	buf = tipc_cfg_reply_alloc(payload_size);
585 586
	if (!buf) {
		read_unlock_bh(&tipc_net_lock);
P
Per Liden 已提交
587
		return NULL;
588
	}
P
Per Liden 已提交
589 590 591

	/* Add TLVs for all nodes in scope */

592
	for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
593
		if (!tipc_in_scope(domain, n_ptr->addr))
P
Per Liden 已提交
594
			continue;
595 596 597
		node_info.addr = htonl(n_ptr->addr);
		node_info.up = htonl(tipc_node_is_up(n_ptr));
		tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
598
				    &node_info, sizeof(node_info));
P
Per Liden 已提交
599 600
	}

601
	read_unlock_bh(&tipc_net_lock);
P
Per Liden 已提交
602 603 604
	return buf;
}

605
struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
P
Per Liden 已提交
606 607 608
{
	u32 domain;
	struct sk_buff *buf;
609
	struct tipc_node *n_ptr;
610
	struct tipc_link_info link_info;
611
	u32 payload_size;
P
Per Liden 已提交
612 613

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
614
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
615

A
Al Viro 已提交
616
	domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
617 618 619
	if (!tipc_addr_domain_valid(domain))
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (network address)");
P
Per Liden 已提交
620

621 622 623
	if (tipc_mode != TIPC_NET_MODE)
		return tipc_cfg_reply_none();

624 625
	read_lock_bh(&tipc_net_lock);

626 627
	/* Get space for all unicast links + multicast link */

628
	payload_size = TLV_SPACE(sizeof(link_info)) * (tipc_net.links + 1);
629 630
	if (payload_size > 32768u) {
		read_unlock_bh(&tipc_net_lock);
631 632
		return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
						   " (too many links)");
633
	}
634
	buf = tipc_cfg_reply_alloc(payload_size);
635 636
	if (!buf) {
		read_unlock_bh(&tipc_net_lock);
P
Per Liden 已提交
637
		return NULL;
638
	}
P
Per Liden 已提交
639 640 641

	/* Add TLV for broadcast link */

642 643
	link_info.dest = htonl(tipc_own_addr & 0xfffff00);
	link_info.up = htonl(1);
644
	strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
645
	tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
P
Per Liden 已提交
646 647 648

	/* Add TLVs for any other links in scope */

649
	for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
650
		u32 i;
P
Per Liden 已提交
651

652
		if (!tipc_in_scope(domain, n_ptr->addr))
P
Per Liden 已提交
653
			continue;
654
		tipc_node_lock(n_ptr);
655 656 657 658 659 660 661
		for (i = 0; i < MAX_BEARERS; i++) {
			if (!n_ptr->links[i])
				continue;
			link_info.dest = htonl(n_ptr->addr);
			link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
			strcpy(link_info.str, n_ptr->links[i]->name);
			tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
662
					    &link_info, sizeof(link_info));
663
		}
664
		tipc_node_unlock(n_ptr);
P
Per Liden 已提交
665 666
	}

667
	read_unlock_bh(&tipc_net_lock);
P
Per Liden 已提交
668 669
	return buf;
}