node.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. #include "core.h"
  37. #include "config.h"
  38. #include "node.h"
  39. #include "cluster.h"
  40. #include "net.h"
  41. #include "addr.h"
  42. #include "node_subscr.h"
  43. #include "link.h"
  44. #include "port.h"
  45. #include "bearer.h"
  46. #include "name_distr.h"
  47. void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str);
  48. static void node_lost_contact(struct tipc_node *n_ptr);
  49. static void node_established_contact(struct tipc_node *n_ptr);
  50. /* sorted list of nodes within cluster */
  51. static struct tipc_node *tipc_nodes = NULL;
  52. static DEFINE_SPINLOCK(node_create_lock);
  53. u32 tipc_own_tag = 0;
  54. /**
  55. * tipc_node_create - create neighboring node
  56. *
  57. * Currently, this routine is called by neighbor discovery code, which holds
  58. * net_lock for reading only. We must take node_create_lock to ensure a node
  59. * isn't created twice if two different bearers discover the node at the same
  60. * time. (It would be preferable to switch to holding net_lock in write mode,
  61. * but this is a non-trivial change.)
  62. */
  63. struct tipc_node *tipc_node_create(u32 addr)
  64. {
  65. struct cluster *c_ptr;
  66. struct tipc_node *n_ptr;
  67. struct tipc_node **curr_node;
  68. spin_lock_bh(&node_create_lock);
  69. for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
  70. if (addr < n_ptr->addr)
  71. break;
  72. if (addr == n_ptr->addr) {
  73. spin_unlock_bh(&node_create_lock);
  74. return n_ptr;
  75. }
  76. }
  77. n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
  78. if (!n_ptr) {
  79. spin_unlock_bh(&node_create_lock);
  80. warn("Node creation failed, no memory\n");
  81. return NULL;
  82. }
  83. c_ptr = tipc_cltr_find(addr);
  84. if (!c_ptr) {
  85. c_ptr = tipc_cltr_create(addr);
  86. }
  87. if (!c_ptr) {
  88. spin_unlock_bh(&node_create_lock);
  89. kfree(n_ptr);
  90. return NULL;
  91. }
  92. n_ptr->addr = addr;
  93. spin_lock_init(&n_ptr->lock);
  94. INIT_LIST_HEAD(&n_ptr->nsub);
  95. n_ptr->owner = c_ptr;
  96. tipc_cltr_attach_node(c_ptr, n_ptr);
  97. n_ptr->last_router = -1;
  98. /* Insert node into ordered list */
  99. for (curr_node = &tipc_nodes; *curr_node;
  100. curr_node = &(*curr_node)->next) {
  101. if (addr < (*curr_node)->addr) {
  102. n_ptr->next = *curr_node;
  103. break;
  104. }
  105. }
  106. (*curr_node) = n_ptr;
  107. spin_unlock_bh(&node_create_lock);
  108. return n_ptr;
  109. }
  110. void tipc_node_delete(struct tipc_node *n_ptr)
  111. {
  112. if (!n_ptr)
  113. return;
  114. dbg("node %x deleted\n", n_ptr->addr);
  115. kfree(n_ptr);
  116. }
  117. /**
  118. * tipc_node_link_up - handle addition of link
  119. *
  120. * Link becomes active (alone or shared) or standby, depending on its priority.
  121. */
  122. void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
  123. {
  124. struct link **active = &n_ptr->active_links[0];
  125. n_ptr->working_links++;
  126. info("Established link <%s> on network plane %c\n",
  127. l_ptr->name, l_ptr->b_ptr->net_plane);
  128. if (!active[0]) {
  129. dbg(" link %x into %x/%x\n", l_ptr, &active[0], &active[1]);
  130. active[0] = active[1] = l_ptr;
  131. node_established_contact(n_ptr);
  132. return;
  133. }
  134. if (l_ptr->priority < active[0]->priority) {
  135. info("New link <%s> becomes standby\n", l_ptr->name);
  136. return;
  137. }
  138. tipc_link_send_duplicate(active[0], l_ptr);
  139. if (l_ptr->priority == active[0]->priority) {
  140. active[0] = l_ptr;
  141. return;
  142. }
  143. info("Old link <%s> becomes standby\n", active[0]->name);
  144. if (active[1] != active[0])
  145. info("Old link <%s> becomes standby\n", active[1]->name);
  146. active[0] = active[1] = l_ptr;
  147. }
  148. /**
  149. * node_select_active_links - select active link
  150. */
  151. static void node_select_active_links(struct tipc_node *n_ptr)
  152. {
  153. struct link **active = &n_ptr->active_links[0];
  154. u32 i;
  155. u32 highest_prio = 0;
  156. active[0] = active[1] = NULL;
  157. for (i = 0; i < MAX_BEARERS; i++) {
  158. struct link *l_ptr = n_ptr->links[i];
  159. if (!l_ptr || !tipc_link_is_up(l_ptr) ||
  160. (l_ptr->priority < highest_prio))
  161. continue;
  162. if (l_ptr->priority > highest_prio) {
  163. highest_prio = l_ptr->priority;
  164. active[0] = active[1] = l_ptr;
  165. } else {
  166. active[1] = l_ptr;
  167. }
  168. }
  169. }
  170. /**
  171. * tipc_node_link_down - handle loss of link
  172. */
  173. void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
  174. {
  175. struct link **active;
  176. n_ptr->working_links--;
  177. if (!tipc_link_is_active(l_ptr)) {
  178. info("Lost standby link <%s> on network plane %c\n",
  179. l_ptr->name, l_ptr->b_ptr->net_plane);
  180. return;
  181. }
  182. info("Lost link <%s> on network plane %c\n",
  183. l_ptr->name, l_ptr->b_ptr->net_plane);
  184. active = &n_ptr->active_links[0];
  185. if (active[0] == l_ptr)
  186. active[0] = active[1];
  187. if (active[1] == l_ptr)
  188. active[1] = active[0];
  189. if (active[0] == l_ptr)
  190. node_select_active_links(n_ptr);
  191. if (tipc_node_is_up(n_ptr))
  192. tipc_link_changeover(l_ptr);
  193. else
  194. node_lost_contact(n_ptr);
  195. }
  196. int tipc_node_has_active_links(struct tipc_node *n_ptr)
  197. {
  198. return n_ptr->active_links[0] != NULL;
  199. }
  200. int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
  201. {
  202. return n_ptr->working_links > 1;
  203. }
  204. static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
  205. {
  206. return n_ptr && (n_ptr->last_router >= 0);
  207. }
  208. int tipc_node_is_up(struct tipc_node *n_ptr)
  209. {
  210. return tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr);
  211. }
  212. struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
  213. {
  214. struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
  215. if (!n_ptr)
  216. n_ptr = tipc_node_create(l_ptr->addr);
  217. if (n_ptr) {
  218. u32 bearer_id = l_ptr->b_ptr->identity;
  219. char addr_string[16];
  220. if (n_ptr->link_cnt >= 2) {
  221. err("Attempt to create third link to %s\n",
  222. tipc_addr_string_fill(addr_string, n_ptr->addr));
  223. return NULL;
  224. }
  225. if (!n_ptr->links[bearer_id]) {
  226. n_ptr->links[bearer_id] = l_ptr;
  227. tipc_net.zones[tipc_zone(l_ptr->addr)]->links++;
  228. n_ptr->link_cnt++;
  229. return n_ptr;
  230. }
  231. err("Attempt to establish second link on <%s> to %s\n",
  232. l_ptr->b_ptr->publ.name,
  233. tipc_addr_string_fill(addr_string, l_ptr->addr));
  234. }
  235. return NULL;
  236. }
  237. void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
  238. {
  239. n_ptr->links[l_ptr->b_ptr->identity] = NULL;
  240. tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
  241. n_ptr->link_cnt--;
  242. }
  243. /*
  244. * Routing table management - five cases to handle:
  245. *
  246. * 1: A link towards a zone/cluster external node comes up.
  247. * => Send a multicast message updating routing tables of all
  248. * system nodes within own cluster that the new destination
  249. * can be reached via this node.
  250. * (node.establishedContact()=>cluster.multicastNewRoute())
  251. *
  252. * 2: A link towards a slave node comes up.
  253. * => Send a multicast message updating routing tables of all
  254. * system nodes within own cluster that the new destination
  255. * can be reached via this node.
  256. * (node.establishedContact()=>cluster.multicastNewRoute())
  257. * => Send a message to the slave node about existence
  258. * of all system nodes within cluster:
  259. * (node.establishedContact()=>cluster.sendLocalRoutes())
  260. *
  261. * 3: A new cluster local system node becomes available.
  262. * => Send message(s) to this particular node containing
  263. * information about all cluster external and slave
  264. * nodes which can be reached via this node.
  265. * (node.establishedContact()==>network.sendExternalRoutes())
  266. * (node.establishedContact()==>network.sendSlaveRoutes())
  267. * => Send messages to all directly connected slave nodes
  268. * containing information about the existence of the new node
  269. * (node.establishedContact()=>cluster.multicastNewRoute())
  270. *
  271. * 4: The link towards a zone/cluster external node or slave
  272. * node goes down.
  273. * => Send a multcast message updating routing tables of all
  274. * nodes within cluster that the new destination can not any
  275. * longer be reached via this node.
  276. * (node.lostAllLinks()=>cluster.bcastLostRoute())
  277. *
  278. * 5: A cluster local system node becomes unavailable.
  279. * => Remove all references to this node from the local
  280. * routing tables. Note: This is a completely node
  281. * local operation.
  282. * (node.lostAllLinks()=>network.removeAsRouter())
  283. * => Send messages to all directly connected slave nodes
  284. * containing information about loss of the node
  285. * (node.establishedContact()=>cluster.multicastLostRoute())
  286. *
  287. */
  288. static void node_established_contact(struct tipc_node *n_ptr)
  289. {
  290. struct cluster *c_ptr;
  291. dbg("node_established_contact:-> %x\n", n_ptr->addr);
  292. if (!tipc_node_has_active_routes(n_ptr) && in_own_cluster(n_ptr->addr)) {
  293. tipc_k_signal((Handler)tipc_named_node_up, n_ptr->addr);
  294. }
  295. /* Syncronize broadcast acks */
  296. n_ptr->bclink.acked = tipc_bclink_get_last_sent();
  297. if (is_slave(tipc_own_addr))
  298. return;
  299. if (!in_own_cluster(n_ptr->addr)) {
  300. /* Usage case 1 (see above) */
  301. c_ptr = tipc_cltr_find(tipc_own_addr);
  302. if (!c_ptr)
  303. c_ptr = tipc_cltr_create(tipc_own_addr);
  304. if (c_ptr)
  305. tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1,
  306. tipc_max_nodes);
  307. return;
  308. }
  309. c_ptr = n_ptr->owner;
  310. if (is_slave(n_ptr->addr)) {
  311. /* Usage case 2 (see above) */
  312. tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, 1, tipc_max_nodes);
  313. tipc_cltr_send_local_routes(c_ptr, n_ptr->addr);
  314. return;
  315. }
  316. if (n_ptr->bclink.supported) {
  317. tipc_nmap_add(&tipc_cltr_bcast_nodes, n_ptr->addr);
  318. if (n_ptr->addr < tipc_own_addr)
  319. tipc_own_tag++;
  320. }
  321. /* Case 3 (see above) */
  322. tipc_net_send_external_routes(n_ptr->addr);
  323. tipc_cltr_send_slave_routes(c_ptr, n_ptr->addr);
  324. tipc_cltr_bcast_new_route(c_ptr, n_ptr->addr, LOWEST_SLAVE,
  325. tipc_highest_allowed_slave);
  326. }
  327. static void node_cleanup_finished(unsigned long node_addr)
  328. {
  329. struct tipc_node *n_ptr;
  330. read_lock_bh(&tipc_net_lock);
  331. n_ptr = tipc_node_find(node_addr);
  332. if (n_ptr) {
  333. tipc_node_lock(n_ptr);
  334. n_ptr->cleanup_required = 0;
  335. tipc_node_unlock(n_ptr);
  336. }
  337. read_unlock_bh(&tipc_net_lock);
  338. }
  339. static void node_lost_contact(struct tipc_node *n_ptr)
  340. {
  341. struct cluster *c_ptr;
  342. struct tipc_node_subscr *ns, *tns;
  343. char addr_string[16];
  344. u32 i;
  345. /* Clean up broadcast reception remains */
  346. n_ptr->bclink.gap_after = n_ptr->bclink.gap_to = 0;
  347. while (n_ptr->bclink.deferred_head) {
  348. struct sk_buff* buf = n_ptr->bclink.deferred_head;
  349. n_ptr->bclink.deferred_head = buf->next;
  350. buf_discard(buf);
  351. }
  352. if (n_ptr->bclink.defragm) {
  353. buf_discard(n_ptr->bclink.defragm);
  354. n_ptr->bclink.defragm = NULL;
  355. }
  356. if (in_own_cluster(n_ptr->addr) && n_ptr->bclink.supported) {
  357. tipc_bclink_acknowledge(n_ptr, mod(n_ptr->bclink.acked + 10000));
  358. }
  359. /* Update routing tables */
  360. if (is_slave(tipc_own_addr)) {
  361. tipc_net_remove_as_router(n_ptr->addr);
  362. } else {
  363. if (!in_own_cluster(n_ptr->addr)) {
  364. /* Case 4 (see above) */
  365. c_ptr = tipc_cltr_find(tipc_own_addr);
  366. tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
  367. tipc_max_nodes);
  368. } else {
  369. /* Case 5 (see above) */
  370. c_ptr = tipc_cltr_find(n_ptr->addr);
  371. if (is_slave(n_ptr->addr)) {
  372. tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr, 1,
  373. tipc_max_nodes);
  374. } else {
  375. if (n_ptr->bclink.supported) {
  376. tipc_nmap_remove(&tipc_cltr_bcast_nodes,
  377. n_ptr->addr);
  378. if (n_ptr->addr < tipc_own_addr)
  379. tipc_own_tag--;
  380. }
  381. tipc_net_remove_as_router(n_ptr->addr);
  382. tipc_cltr_bcast_lost_route(c_ptr, n_ptr->addr,
  383. LOWEST_SLAVE,
  384. tipc_highest_allowed_slave);
  385. }
  386. }
  387. }
  388. if (tipc_node_has_active_routes(n_ptr))
  389. return;
  390. info("Lost contact with %s\n",
  391. tipc_addr_string_fill(addr_string, n_ptr->addr));
  392. /* Abort link changeover */
  393. for (i = 0; i < MAX_BEARERS; i++) {
  394. struct link *l_ptr = n_ptr->links[i];
  395. if (!l_ptr)
  396. continue;
  397. l_ptr->reset_checkpoint = l_ptr->next_in_no;
  398. l_ptr->exp_msg_count = 0;
  399. tipc_link_reset_fragments(l_ptr);
  400. }
  401. /* Notify subscribers */
  402. list_for_each_entry_safe(ns, tns, &n_ptr->nsub, nodesub_list) {
  403. ns->node = NULL;
  404. list_del_init(&ns->nodesub_list);
  405. tipc_k_signal((Handler)ns->handle_node_down,
  406. (unsigned long)ns->usr_handle);
  407. }
  408. /* Prevent re-contact with node until all cleanup is done */
  409. n_ptr->cleanup_required = 1;
  410. tipc_k_signal((Handler)node_cleanup_finished, n_ptr->addr);
  411. }
  412. /**
  413. * tipc_node_select_next_hop - find the next-hop node for a message
  414. *
  415. * Called by when cluster local lookup has failed.
  416. */
  417. struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
  418. {
  419. struct tipc_node *n_ptr;
  420. u32 router_addr;
  421. if (!tipc_addr_domain_valid(addr))
  422. return NULL;
  423. /* Look for direct link to destination processsor */
  424. n_ptr = tipc_node_find(addr);
  425. if (n_ptr && tipc_node_has_active_links(n_ptr))
  426. return n_ptr;
  427. /* Cluster local system nodes *must* have direct links */
  428. if (!is_slave(addr) && in_own_cluster(addr))
  429. return NULL;
  430. /* Look for cluster local router with direct link to node */
  431. router_addr = tipc_node_select_router(n_ptr, selector);
  432. if (router_addr)
  433. return tipc_node_select(router_addr, selector);
  434. /* Slave nodes can only be accessed within own cluster via a
  435. known router with direct link -- if no router was found,give up */
  436. if (is_slave(addr))
  437. return NULL;
  438. /* Inter zone/cluster -- find any direct link to remote cluster */
  439. addr = tipc_addr(tipc_zone(addr), tipc_cluster(addr), 0);
  440. n_ptr = tipc_net_select_remote_node(addr, selector);
  441. if (n_ptr && tipc_node_has_active_links(n_ptr))
  442. return n_ptr;
  443. /* Last resort -- look for any router to anywhere in remote zone */
  444. router_addr = tipc_net_select_router(addr, selector);
  445. if (router_addr)
  446. return tipc_node_select(router_addr, selector);
  447. return NULL;
  448. }
  449. /**
  450. * tipc_node_select_router - select router to reach specified node
  451. *
  452. * Uses a deterministic and fair algorithm for selecting router node.
  453. */
  454. u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
  455. {
  456. u32 ulim;
  457. u32 mask;
  458. u32 start;
  459. u32 r;
  460. if (!n_ptr)
  461. return 0;
  462. if (n_ptr->last_router < 0)
  463. return 0;
  464. ulim = ((n_ptr->last_router + 1) * 32) - 1;
  465. /* Start entry must be random */
  466. mask = tipc_max_nodes;
  467. while (mask > ulim)
  468. mask >>= 1;
  469. start = ref & mask;
  470. r = start;
  471. /* Lookup upwards with wrap-around */
  472. do {
  473. if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
  474. break;
  475. } while (++r <= ulim);
  476. if (r > ulim) {
  477. r = 1;
  478. do {
  479. if (((n_ptr->routers[r / 32]) >> (r % 32)) & 1)
  480. break;
  481. } while (++r < start);
  482. assert(r != start);
  483. }
  484. assert(r && (r <= ulim));
  485. return tipc_addr(own_zone(), own_cluster(), r);
  486. }
  487. void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
  488. {
  489. u32 r_num = tipc_node(router);
  490. n_ptr->routers[r_num / 32] =
  491. ((1 << (r_num % 32)) | n_ptr->routers[r_num / 32]);
  492. n_ptr->last_router = tipc_max_nodes / 32;
  493. while ((--n_ptr->last_router >= 0) &&
  494. !n_ptr->routers[n_ptr->last_router]);
  495. }
  496. void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
  497. {
  498. u32 r_num = tipc_node(router);
  499. if (n_ptr->last_router < 0)
  500. return; /* No routes */
  501. n_ptr->routers[r_num / 32] =
  502. ((~(1 << (r_num % 32))) & (n_ptr->routers[r_num / 32]));
  503. n_ptr->last_router = tipc_max_nodes / 32;
  504. while ((--n_ptr->last_router >= 0) &&
  505. !n_ptr->routers[n_ptr->last_router]);
  506. if (!tipc_node_is_up(n_ptr))
  507. node_lost_contact(n_ptr);
  508. }
  509. struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
  510. {
  511. u32 domain;
  512. struct sk_buff *buf;
  513. struct tipc_node *n_ptr;
  514. struct tipc_node_info node_info;
  515. u32 payload_size;
  516. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  517. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  518. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  519. if (!tipc_addr_domain_valid(domain))
  520. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  521. " (network address)");
  522. read_lock_bh(&tipc_net_lock);
  523. if (!tipc_nodes) {
  524. read_unlock_bh(&tipc_net_lock);
  525. return tipc_cfg_reply_none();
  526. }
  527. /* For now, get space for all other nodes
  528. (will need to modify this when slave nodes are supported */
  529. payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
  530. if (payload_size > 32768u) {
  531. read_unlock_bh(&tipc_net_lock);
  532. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  533. " (too many nodes)");
  534. }
  535. buf = tipc_cfg_reply_alloc(payload_size);
  536. if (!buf) {
  537. read_unlock_bh(&tipc_net_lock);
  538. return NULL;
  539. }
  540. /* Add TLVs for all nodes in scope */
  541. for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
  542. if (!tipc_in_scope(domain, n_ptr->addr))
  543. continue;
  544. node_info.addr = htonl(n_ptr->addr);
  545. node_info.up = htonl(tipc_node_is_up(n_ptr));
  546. tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
  547. &node_info, sizeof(node_info));
  548. }
  549. read_unlock_bh(&tipc_net_lock);
  550. return buf;
  551. }
  552. struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
  553. {
  554. u32 domain;
  555. struct sk_buff *buf;
  556. struct tipc_node *n_ptr;
  557. struct tipc_link_info link_info;
  558. u32 payload_size;
  559. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  560. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  561. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  562. if (!tipc_addr_domain_valid(domain))
  563. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  564. " (network address)");
  565. if (tipc_mode != TIPC_NET_MODE)
  566. return tipc_cfg_reply_none();
  567. read_lock_bh(&tipc_net_lock);
  568. /* Get space for all unicast links + multicast link */
  569. payload_size = TLV_SPACE(sizeof(link_info)) *
  570. (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
  571. if (payload_size > 32768u) {
  572. read_unlock_bh(&tipc_net_lock);
  573. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  574. " (too many links)");
  575. }
  576. buf = tipc_cfg_reply_alloc(payload_size);
  577. if (!buf) {
  578. read_unlock_bh(&tipc_net_lock);
  579. return NULL;
  580. }
  581. /* Add TLV for broadcast link */
  582. link_info.dest = htonl(tipc_own_addr & 0xfffff00);
  583. link_info.up = htonl(1);
  584. strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  585. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
  586. /* Add TLVs for any other links in scope */
  587. for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
  588. u32 i;
  589. if (!tipc_in_scope(domain, n_ptr->addr))
  590. continue;
  591. tipc_node_lock(n_ptr);
  592. for (i = 0; i < MAX_BEARERS; i++) {
  593. if (!n_ptr->links[i])
  594. continue;
  595. link_info.dest = htonl(n_ptr->addr);
  596. link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
  597. strcpy(link_info.str, n_ptr->links[i]->name);
  598. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
  599. &link_info, sizeof(link_info));
  600. }
  601. tipc_node_unlock(n_ptr);
  602. }
  603. read_unlock_bh(&tipc_net_lock);
  604. return buf;
  605. }