1 /* 2 * net/tipc/node.c: TIPC node management routines 3 * 4 * Copyright (c) 2000-2006, Ericsson AB 5 * Copyright (c) 2005-2006, Wind River Systems 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the names of the copyright holders nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * Alternatively, this software may be distributed under the terms of the 21 * GNU General Public License ("GPL") version 2 as published by the Free 22 * Software Foundation. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include "core.h" 38 #include "config.h" 39 #include "node.h" 40 #include "cluster.h" 41 #include "net.h" 42 #include "addr.h" 43 #include "node_subscr.h" 44 #include "link.h" 45 #include "port.h" 46 #include "bearer.h" 47 #include "name_distr.h" 48 49 void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str); 50 static void node_lost_contact(struct tipc_node *n_ptr); 51 static void node_established_contact(struct tipc_node *n_ptr); 52 53 struct tipc_node *tipc_nodes = NULL; /* sorted list of nodes within cluster */ 54 55 static DEFINE_SPINLOCK(node_create_lock); 56 57 u32 tipc_own_tag = 0; 58 59 /** 60 * tipc_node_create - create neighboring node 61 * 62 * Currently, this routine is called by neighbor discovery code, which holds 63 * net_lock for reading only. We must take node_create_lock to ensure a node 64 * isn't created twice if two different bearers discover the node at the same 65 * time. (It would be preferable to switch to holding net_lock in write mode, 66 * but this is a non-trivial change.) 67 */ 68 69 struct tipc_node *tipc_node_create(u32 addr) 70 { 71 struct cluster *c_ptr; 72 struct tipc_node *n_ptr; 73 struct tipc_node **curr_node; 74 75 spin_lock_bh(&node_create_lock); 76 77 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) { 78 if (addr < n_ptr->addr) 79 break; 80 if (addr == n_ptr->addr) { 81 spin_unlock_bh(&node_create_lock); 82 return n_ptr; 83 } 84 } 85 86 n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC); 87 if (!n_ptr) { 88 spin_unlock_bh(&node_create_lock); 89 warn("Node creation failed, no memory\n"); 90 return NULL; 91 } 92 93 c_ptr = tipc_cltr_find(addr); 94 if (!c_ptr) { 95 c_ptr = tipc_cltr_create(addr); 96 } 97 if (!c_ptr) { 98 spin_unlock_bh(&node_create_lock); 99 kfree(n_ptr); 100 return NULL; 101 } 102 103 n_ptr->addr = addr; 104 spin_lock_init(&n_ptr->lock); 105 INIT_LIST_HEAD(&n_ptr->nsub); 106 n_ptr->owner = c_ptr; 107 tipc_cltr_attach_node(c_ptr, n_ptr); 108 n_ptr->last_router = -1; 109 110 /* Insert node into ordered list */ 111 for (curr_node = &tipc_nodes; *curr_node; 112 curr_node = &(*curr_node)->next) { 113 if (addr < (*curr_node)->addr) { 114 n_ptr->next = *curr_node; 115 break; 116 } 117 } 118 (*curr_node) = n_ptr; 119 spin_unlock_bh(&node_create_lock); 120 return n_ptr; 121 } 122 123 void tipc_node_delete(struct tipc_node *n_ptr) 124 { 125 if (!n_ptr) 126 return; 127 128 dbg("node %x deleted\n", n_ptr->addr); 129 kfree(n_ptr); 130 } 131 132 133 /** 134 * tipc_node_link_up - handle addition of link 135 * 136 * Link becomes active (alone or shared) or standby, depending on its priority. 137 */ 138 139 void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr) 140 { 141 struct link **active = &n_ptr->active_links[0]; 142 143 n_ptr->working_links++; 144 145 info("Established link <%s> on network plane %c\n", 146 l_ptr->name, l_ptr->b_ptr->net_plane); 147 148 if (!active[0]) { 149 dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]); 150 active[0] = active[1] = l_ptr; 151 node_established_contact(n_ptr); 152 return; 153 } 154 if (l_ptr->priority < active[0]->priority) { 155 info("New link <%s> becomes standby\n", l_ptr->name); 156 return; 157 } 158 tipc_link_send_duplicate(active[0], l_ptr); 159 if (l_ptr->priority == active[0]->priority) { 160 active[0] = l_ptr; 161 return; 162 } 163 info("Old link <%s> becomes standby\n", active[0]->name); 164 if (active[1] != active[0]) 165 info("Old link <%s> becomes standby\n", active[1]->name); 166 active[0] = active[1] = l_ptr; 167 } 168 169 /** 170 * node_select_active_links - select active link 171 */ 172 173 static void node_select_active_links(struct tipc_node *n_ptr) 174 { 175 struct link **active = &n_ptr->active_links[0]; 176 u32 i; 177 u32 highest_prio = 0; 178 179 active[0] = active[1] = NULL; 180 181 for (i = 0; i < MAX_BEARERS; i++) { 182 struct link *l_ptr = n_ptr->links[i]; 183 184 if (!l_ptr || !tipc_link_is_up(l_ptr) || 185 (l_ptr->priority < highest_prio)) 186 continue; 187 188 if (l_ptr->priority > highest_prio) { 189 highest_prio = l_ptr->priority; 190 active[0] = active[1] = l_ptr; 191 } else { 192 active[1] = l_ptr; 193 } 194 } 195 } 196 197 /** 198 * tipc_node_link_down - handle loss of link 199 */ 200 201 void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr) 202 { 203 struct link **active; 204 205 n_ptr->working_links--; 206 207 if (!tipc_link_is_active(l_ptr)) { 208 info("Lost standby link <%s> on network plane %c\n", 209 l_ptr->name, l_ptr->b_ptr->net_plane); 210 return; 211 } 212 info("Lost link <%s> on network plane %c\n", 213 l_ptr->name, l_ptr->b_ptr->net_plane); 214 215 active = &n_ptr->active_links[0]; 216 if (active[0] == l_ptr) 217 active[0] = active[1]; 218 if (active[1] == l_ptr) 219 active[1] = active[0]; 220 if (active[0] == l_ptr) 221 node_select_active_links(n_ptr); 222 if (tipc_node_is_up(n_ptr)) 223 tipc_link_changeover(l_ptr); 224 else 225 node_lost_contact(n_ptr); 226 } 227 228 int tipc_node_has_active_links(struct tipc_node *n_ptr) 229 { 230 return n_ptr->active_links[0] != NULL; 231 } 232 233 int tipc_node_has_redundant_links(struct tipc_node *n_ptr) 234 { 235 return n_ptr->working_links > 1; 236 } 237 238 static int tipc_node_has_active_routes(struct tipc_node *n_ptr) 239 { 240 return n_ptr && (n_ptr->last_router >= 0); 241 } 242 243 int tipc_node_is_up(struct tipc_node *n_ptr) 244 { 245 return tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr); 246 } 247 248 struct tipc_node *tipc_node_attach_link(struct link *l_ptr) 249 { 250 struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr); 251 252 if (!n_ptr) 253 n_ptr = tipc_node_create(l_ptr->addr); 254 if (n_ptr) { 255 u32 bearer_id = l_ptr->b_ptr->identity; 256 char addr_string[16]; 257 258 if (n_ptr->link_cnt >= 2) { 259 err("Attempt to create third link to %s\n", 260 tipc_addr_string_fill(addr_string, n_ptr->addr)); 261 return NULL; 262 } 263 264 if (!n_ptr->links[bearer_id]) { 265 n_ptr->links[bearer_id] = l_ptr; 266 tipc_net.zones[tipc_zone(l_ptr->addr)]->links++; 267 n_ptr->link_cnt++; 268 return n_ptr; 269 } 270 err("Attempt to establish second link on <%s> to %s\n", 271 l_ptr->b_ptr->publ.name, 272 tipc_addr_string_fill(addr_string, l_ptr->addr)); 273 } 274 return NULL; 275 } 276 277 void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr) 278 { 279 n_ptr->links[l_ptr->b_ptr->identity] = NULL; 280 tipc_net.zones[tipc_zone(l_ptr->addr)]->links--; 281 n_ptr->link_cnt--; 282 } 283 284 /* 285 * Routing table management - five cases to handle: 286 * 287 * 1: A link towards a zone/cluster external node comes up. 288 * => Send a multicast message updating routing tables of all 289 * system nodes within own cluster that the new destination 290 * can be reached via this node. 291 * (node.establishedContact()=>cluster.multicastNewRoute()) 292 * 293 * 2: A link towards a slave node comes up. 294 * => Send a multicast message updating routing tables of all 295 * system nodes within own cluster that the new destination 296 * can be reached via this node. 297 * (node.establishedContact()=>cluster.multicastNewRoute()) 298 * => Send a message to the slave node about existence 299 * of all system nodes within cluster: 300 * (node.establishedContact()=>cluster.sendLocalRoutes()) 301 * 302 * 3: A new cluster local system node becomes available. 303 * => Send message(s) to this particular node containing 304 * information about all cluster external and slave 305 * nodes which can be reached via this node. 306 * (node.establishedContact()==>network.sendExternalRoutes()) 307 * (node.establishedContact()==>network.sendSlaveRoutes()) 308 * => Send messages to all directly connected slave nodes 309 * containing information about the existence of the new node 310 * (node.establishedContact()=>cluster.multicastNewRoute()) 311 * 312 * 4: The link towards a zone/cluster external node or slave 313 * node goes down. 314 * => Send a multcast message updating routing tables of all 315 * nodes within cluster that the new destination can not any 316 * longer be reached via this node. 317 * (node.lostAllLinks()=>cluster.bcastLostRoute()) 318 * 319 * 5: A cluster local system node becomes unavailable. 320 * => Remove all references to this node from the local 321 * routing tables. Note: This is a completely node 322 * local operation. 323 * (node.lostAllLinks()=>network.removeAsRouter()) 324 * => Send messages to all directly connected slave nodes 325 * containing information about loss of the node 326 * (node.establishedContact()=>cluster.multicastLostRoute()) 327 * 328 */ 329 330 static void node_established_contact(struct tipc_node *n_ptr) 331 { 332 struct cluster *c_ptr; 333 334 dbg("node_established_contact:-> %x\n", n_ptr->addr); 335 if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) { 336 tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr); 337 } 338 339 /* Syncronize broadcast acks */ 340 n_ptr->bclink.acked = tipc_bclink_get_last_sent(); 341 342 if (is_slave(tipc_own_addr)) 343 return; 344 if (!in_own_cluster(n_ptr->addr)) { 345 /* Usage case 1 (see above) */ 346 c_ptr = tipc_cltr_find(tipc_own_addr); 347 if (!c_ptr) 348 c_ptr = tipc_cltr_create(tipc_own_addr); 349 if (c_ptr) 350 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, 351 tipc_max_nodes); 352 return; 353 } 354 355 c_ptr = n_ptr->owner; 356 if (is_slave(n_ptr->addr)) { 357 /* Usage case 2 (see above) */ 358 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes); 359 tipc_cltr_send_local_routes(c_ptr, n_ptr->addr); 360 return; 361 } 362 363 if (n_ptr->bclink.supported) { 364 tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr); 365 if (n_ptr->addr < tipc_own_addr) 366 tipc_own_tag++; 367 } 368 369 /* Case 3 (see above) */ 370 tipc_net_send_external_routes(n_ptr->addr); 371 tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr); 372 tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE, 373 tipc_highest_allowed_slave); 374 } 375 376 static void node_cleanup_finished(unsigned long node_addr) 377 { 378 struct tipc_node *n_ptr; 379 380 read_lock_bh(&tipc_net_lock); 381 n_ptr = tipc_node_find(node_addr); 382 if (n_ptr) { 383 tipc_node_lock(n_ptr); 384 n_ptr->cleanup_required = 0; 385 tipc_node_unlock(n_ptr); 386 } 387 read_unlock_bh(&tipc_net_lock); 388 } 389 390 static void node_lost_contact(struct tipc_node *n_ptr) 391 { 392 struct cluster *c_ptr; 393 struct tipc_node_subscr *ns, *tns; 394 char addr_string[16]; 395 u32 i; 396 397 /* Clean up broadcast reception remains */ 398 n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0; 399 while (n_ptr->bclink.deferred_head) { 400 struct sk_buff* buf = n_ptr->bclink.deferred_head; 401 n_ptr->bclink.deferred_head = buf->next; 402 buf_discard(buf); 403 } 404 if (n_ptr->bclink.defragm) { 405 buf_discard(n_ptr->bclink.defragm); 406 n_ptr->bclink.defragm = NULL; 407 } 408 if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) { 409 tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000)); 410 } 411 412 /* Update routing tables */ 413 if (is_slave(tipc_own_addr)) { 414 tipc_net_remove_as_router(n_ptr->addr); 415 } else { 416 if (!in_own_cluster(n_ptr->addr)) { 417 /* Case 4 (see above) */ 418 c_ptr = tipc_cltr_find(tipc_own_addr); 419 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1, 420 tipc_max_nodes); 421 } else { 422 /* Case 5 (see above) */ 423 c_ptr = tipc_cltr_find(n_ptr->addr); 424 if (is_slave(n_ptr->addr)) { 425 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1, 426 tipc_max_nodes); 427 } else { 428 if (n_ptr->bclink.supported) { 429 tipc_nmap_remove(&tipc_cltr_bcast_nodes, 430 n_ptr->addr); 431 if (n_ptr->addr < tipc_own_addr) 432 tipc_own_tag--; 433 } 434 tipc_net_remove_as_router(n_ptr->addr); 435 tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 436 LOWEST_SLAVE, 437 tipc_highest_allowed_slave); 438 } 439 } 440 } 441 if (tipc_node_has_active_routes(n_ptr)) 442 return; 443 444 info("Lost contact with %s\n", 445 tipc_addr_string_fill(addr_string, n_ptr->addr)); 446 447 /* Abort link changeover */ 448 for (i = 0; i < MAX_BEARERS; i++) { 449 struct link *l_ptr = n_ptr->links[i]; 450 if (!l_ptr) 451 continue; 452 l_ptr->reset_checkpoint = l_ptr->next_in_no; 453 l_ptr->exp_msg_count = 0; 454 tipc_link_reset_fragments(l_ptr); 455 } 456 457 /* Notify subscribers */ 458 list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) { 459 ns->node = NULL; 460 list_del_init(&ns->nodesub_list); 461 tipc_k_signal((Handler)ns->handle_node_down, 462 (unsigned long)ns->usr_handle); 463 } 464 465 /* Prevent re-contact with node until all cleanup is done */ 466 467 n_ptr->cleanup_required = 1; 468 tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr); 469 } 470 471 /** 472 * tipc_node_select_next_hop - find the next-hop node for a message 473 * 474 * Called by when cluster local lookup has failed. 475 */ 476 477 struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector) 478 { 479 struct tipc_node *n_ptr; 480 u32 router_addr; 481 482 if (!tipc_addr_domain_valid(addr)) 483 return NULL; 484 485 /* Look for direct link to destination processsor */ 486 n_ptr = tipc_node_find(addr); 487 if (n_ptr && tipc_node_has_active_links(n_ptr)) 488 return n_ptr; 489 490 /* Cluster local system nodes *must* have direct links */ 491 if (!is_slave(addr) && in_own_cluster(addr)) 492 return NULL; 493 494 /* Look for cluster local router with direct link to node */ 495 router_addr = tipc_node_select_router(n_ptr, selector); 496 if (router_addr) 497 return tipc_node_select(router_addr, selector); 498 499 /* Slave nodes can only be accessed within own cluster via a 500 known router with direct link -- if no router was found,give up */ 501 if (is_slave(addr)) 502 return NULL; 503 504 /* Inter zone/cluster -- find any direct link to remote cluster */ 505 addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0); 506 n_ptr = tipc_net_select_remote_node(addr, selector); 507 if (n_ptr && tipc_node_has_active_links(n_ptr)) 508 return n_ptr; 509 510 /* Last resort -- look for any router to anywhere in remote zone */ 511 router_addr = tipc_net_select_router(addr, selector); 512 if (router_addr) 513 return tipc_node_select(router_addr, selector); 514 515 return NULL; 516 } 517 518 /** 519 * tipc_node_select_router - select router to reach specified node 520 * 521 * Uses a deterministic and fair algorithm for selecting router node. 522 */ 523 524 u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref) 525 { 526 u32 ulim; 527 u32 mask; 528 u32 start; 529 u32 r; 530 531 if (!n_ptr) 532 return 0; 533 534 if (n_ptr->last_router < 0) 535 return 0; 536 ulim = ((n_ptr->last_router + 1) * 32) - 1; 537 538 /* Start entry must be random */ 539 mask = tipc_max_nodes; 540 while (mask > ulim) 541 mask >>= 1; 542 start = ref & mask; 543 r = start; 544 545 /* Lookup upwards with wrap-around */ 546 do { 547 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1) 548 break; 549 } while (++r <= ulim); 550 if (r > ulim) { 551 r = 1; 552 do { 553 if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1) 554 break; 555 } while (++r < start); 556 assert(r != start); 557 } 558 assert(r && (r <= ulim)); 559 return tipc_addr(own_zone(), own_cluster(), r); 560 } 561 562 void tipc_node_add_router(struct tipc_node *n_ptr, u32 router) 563 { 564 u32 r_num = tipc_node(router); 565 566 n_ptr->routers[r_num / 32] = 567 ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]); 568 n_ptr->last_router = tipc_max_nodes / 32; 569 while ((--n_ptr->last_router >= 0) && 570 !n_ptr->routers[n_ptr->last_router]); 571 } 572 573 void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router) 574 { 575 u32 r_num = tipc_node(router); 576 577 if (n_ptr->last_router < 0) 578 return; /* No routes */ 579 580 n_ptr->routers[r_num / 32] = 581 ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32])); 582 n_ptr->last_router = tipc_max_nodes / 32; 583 while ((--n_ptr->last_router >= 0) && 584 !n_ptr->routers[n_ptr->last_router]); 585 586 if (!tipc_node_is_up(n_ptr)) 587 node_lost_contact(n_ptr); 588 } 589 590 u32 tipc_available_nodes(const u32 domain) 591 { 592 struct tipc_node *n_ptr; 593 u32 cnt = 0; 594 595 read_lock_bh(&tipc_net_lock); 596 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) { 597 if (!tipc_in_scope(domain, n_ptr->addr)) 598 continue; 599 if (tipc_node_is_up(n_ptr)) 600 cnt++; 601 } 602 read_unlock_bh(&tipc_net_lock); 603 return cnt; 604 } 605 606 struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space) 607 { 608 u32 domain; 609 struct sk_buff *buf; 610 struct tipc_node *n_ptr; 611 struct tipc_node_info node_info; 612 u32 payload_size; 613 614 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR)) 615 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); 616 617 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); 618 if (!tipc_addr_domain_valid(domain)) 619 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE 620 " (network address)"); 621 622 read_lock_bh(&tipc_net_lock); 623 if (!tipc_nodes) { 624 read_unlock_bh(&tipc_net_lock); 625 return tipc_cfg_reply_none(); 626 } 627 628 /* For now, get space for all other nodes 629 (will need to modify this when slave nodes are supported */ 630 631 payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1); 632 if (payload_size > 32768u) { 633 read_unlock_bh(&tipc_net_lock); 634 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED 635 " (too many nodes)"); 636 } 637 buf = tipc_cfg_reply_alloc(payload_size); 638 if (!buf) { 639 read_unlock_bh(&tipc_net_lock); 640 return NULL; 641 } 642 643 /* Add TLVs for all nodes in scope */ 644 645 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) { 646 if (!tipc_in_scope(domain, n_ptr->addr)) 647 continue; 648 node_info.addr = htonl(n_ptr->addr); 649 node_info.up = htonl(tipc_node_is_up(n_ptr)); 650 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO, 651 &node_info, sizeof(node_info)); 652 } 653 654 read_unlock_bh(&tipc_net_lock); 655 return buf; 656 } 657 658 struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space) 659 { 660 u32 domain; 661 struct sk_buff *buf; 662 struct tipc_node *n_ptr; 663 struct tipc_link_info link_info; 664 u32 payload_size; 665 666 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR)) 667 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); 668 669 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); 670 if (!tipc_addr_domain_valid(domain)) 671 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE 672 " (network address)"); 673 674 if (tipc_mode != TIPC_NET_MODE) 675 return tipc_cfg_reply_none(); 676 677 read_lock_bh(&tipc_net_lock); 678 679 /* Get space for all unicast links + multicast link */ 680 681 payload_size = TLV_SPACE(sizeof(link_info)) * 682 (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1); 683 if (payload_size > 32768u) { 684 read_unlock_bh(&tipc_net_lock); 685 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED 686 " (too many links)"); 687 } 688 buf = tipc_cfg_reply_alloc(payload_size); 689 if (!buf) { 690 read_unlock_bh(&tipc_net_lock); 691 return NULL; 692 } 693 694 /* Add TLV for broadcast link */ 695 696 link_info.dest = htonl(tipc_own_addr & 0xfffff00); 697 link_info.up = htonl(1); 698 strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME); 699 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info)); 700 701 /* Add TLVs for any other links in scope */ 702 703 for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) { 704 u32 i; 705 706 if (!tipc_in_scope(domain, n_ptr->addr)) 707 continue; 708 tipc_node_lock(n_ptr); 709 for (i = 0; i < MAX_BEARERS; i++) { 710 if (!n_ptr->links[i]) 711 continue; 712 link_info.dest = htonl(n_ptr->addr); 713 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i])); 714 strcpy(link_info.str, n_ptr->links[i]->name); 715 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, 716 &link_info, sizeof(link_info)); 717 } 718 tipc_node_unlock(n_ptr); 719 } 720 721 read_unlock_bh(&tipc_net_lock); 722 return buf; 723 } 724