node.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * net/tipc/node.c: TIPC node management routines
  3. *
  4. * Copyright (c) 2000-2006, 2012-2014, Ericsson AB
  5. * Copyright (c) 2005-2006, 2010-2014, 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 "name_distr.h"
  40. #include "socket.h"
  41. static void node_lost_contact(struct tipc_node *n_ptr);
  42. static void node_established_contact(struct tipc_node *n_ptr);
  43. struct tipc_sock_conn {
  44. u32 port;
  45. u32 peer_port;
  46. u32 peer_node;
  47. struct list_head list;
  48. };
  49. static const struct nla_policy tipc_nl_node_policy[TIPC_NLA_NODE_MAX + 1] = {
  50. [TIPC_NLA_NODE_UNSPEC] = { .type = NLA_UNSPEC },
  51. [TIPC_NLA_NODE_ADDR] = { .type = NLA_U32 },
  52. [TIPC_NLA_NODE_UP] = { .type = NLA_FLAG }
  53. };
  54. /*
  55. * A trivial power-of-two bitmask technique is used for speed, since this
  56. * operation is done for every incoming TIPC packet. The number of hash table
  57. * entries has been chosen so that no hash chain exceeds 8 nodes and will
  58. * usually be much smaller (typically only a single node).
  59. */
  60. static unsigned int tipc_hashfn(u32 addr)
  61. {
  62. return addr & (NODE_HTABLE_SIZE - 1);
  63. }
  64. /*
  65. * tipc_node_find - locate specified node object, if it exists
  66. */
  67. struct tipc_node *tipc_node_find(struct net *net, u32 addr)
  68. {
  69. struct tipc_net *tn = net_generic(net, tipc_net_id);
  70. struct tipc_node *node;
  71. if (unlikely(!in_own_cluster_exact(net, addr)))
  72. return NULL;
  73. rcu_read_lock();
  74. hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)],
  75. hash) {
  76. if (node->addr == addr) {
  77. rcu_read_unlock();
  78. return node;
  79. }
  80. }
  81. rcu_read_unlock();
  82. return NULL;
  83. }
  84. struct tipc_node *tipc_node_create(struct net *net, u32 addr)
  85. {
  86. struct tipc_net *tn = net_generic(net, tipc_net_id);
  87. struct tipc_node *n_ptr, *temp_node;
  88. spin_lock_bh(&tn->node_list_lock);
  89. n_ptr = tipc_node_find(net, addr);
  90. if (n_ptr)
  91. goto exit;
  92. n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
  93. if (!n_ptr) {
  94. pr_warn("Node creation failed, no memory\n");
  95. goto exit;
  96. }
  97. n_ptr->addr = addr;
  98. n_ptr->net = net;
  99. spin_lock_init(&n_ptr->lock);
  100. INIT_HLIST_NODE(&n_ptr->hash);
  101. INIT_LIST_HEAD(&n_ptr->list);
  102. INIT_LIST_HEAD(&n_ptr->publ_list);
  103. INIT_LIST_HEAD(&n_ptr->conn_sks);
  104. skb_queue_head_init(&n_ptr->waiting_sks);
  105. __skb_queue_head_init(&n_ptr->bclink.deferred_queue);
  106. hlist_add_head_rcu(&n_ptr->hash, &tn->node_htable[tipc_hashfn(addr)]);
  107. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  108. if (n_ptr->addr < temp_node->addr)
  109. break;
  110. }
  111. list_add_tail_rcu(&n_ptr->list, &temp_node->list);
  112. n_ptr->action_flags = TIPC_WAIT_PEER_LINKS_DOWN;
  113. n_ptr->signature = INVALID_NODE_SIG;
  114. tn->num_nodes++;
  115. exit:
  116. spin_unlock_bh(&tn->node_list_lock);
  117. return n_ptr;
  118. }
  119. static void tipc_node_delete(struct tipc_net *tn, struct tipc_node *n_ptr)
  120. {
  121. list_del_rcu(&n_ptr->list);
  122. hlist_del_rcu(&n_ptr->hash);
  123. kfree_rcu(n_ptr, rcu);
  124. tn->num_nodes--;
  125. }
  126. void tipc_node_stop(struct net *net)
  127. {
  128. struct tipc_net *tn = net_generic(net, tipc_net_id);
  129. struct tipc_node *node, *t_node;
  130. spin_lock_bh(&tn->node_list_lock);
  131. list_for_each_entry_safe(node, t_node, &tn->node_list, list)
  132. tipc_node_delete(tn, node);
  133. spin_unlock_bh(&tn->node_list_lock);
  134. }
  135. int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
  136. {
  137. struct tipc_node *node;
  138. struct tipc_sock_conn *conn;
  139. if (in_own_node(net, dnode))
  140. return 0;
  141. node = tipc_node_find(net, dnode);
  142. if (!node) {
  143. pr_warn("Connecting sock to node 0x%x failed\n", dnode);
  144. return -EHOSTUNREACH;
  145. }
  146. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  147. if (!conn)
  148. return -EHOSTUNREACH;
  149. conn->peer_node = dnode;
  150. conn->port = port;
  151. conn->peer_port = peer_port;
  152. tipc_node_lock(node);
  153. list_add_tail(&conn->list, &node->conn_sks);
  154. tipc_node_unlock(node);
  155. return 0;
  156. }
  157. void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
  158. {
  159. struct tipc_node *node;
  160. struct tipc_sock_conn *conn, *safe;
  161. if (in_own_node(net, dnode))
  162. return;
  163. node = tipc_node_find(net, dnode);
  164. if (!node)
  165. return;
  166. tipc_node_lock(node);
  167. list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
  168. if (port != conn->port)
  169. continue;
  170. list_del(&conn->list);
  171. kfree(conn);
  172. }
  173. tipc_node_unlock(node);
  174. }
  175. void tipc_node_abort_sock_conns(struct net *net, struct list_head *conns)
  176. {
  177. struct tipc_net *tn = net_generic(net, tipc_net_id);
  178. struct tipc_sock_conn *conn, *safe;
  179. struct sk_buff *buf;
  180. list_for_each_entry_safe(conn, safe, conns, list) {
  181. buf = tipc_msg_create(net, TIPC_CRITICAL_IMPORTANCE,
  182. TIPC_CONN_MSG, SHORT_H_SIZE, 0,
  183. tn->own_addr, conn->peer_node,
  184. conn->port, conn->peer_port,
  185. TIPC_ERR_NO_NODE);
  186. if (likely(buf))
  187. tipc_sk_rcv(net, buf);
  188. list_del(&conn->list);
  189. kfree(conn);
  190. }
  191. }
  192. /**
  193. * tipc_node_link_up - handle addition of link
  194. *
  195. * Link becomes active (alone or shared) or standby, depending on its priority.
  196. */
  197. void tipc_node_link_up(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  198. {
  199. struct tipc_link **active = &n_ptr->active_links[0];
  200. n_ptr->working_links++;
  201. n_ptr->action_flags |= TIPC_NOTIFY_LINK_UP;
  202. n_ptr->link_id = l_ptr->peer_bearer_id << 16 | l_ptr->bearer_id;
  203. pr_debug("Established link <%s> on network plane %c\n",
  204. l_ptr->name, l_ptr->net_plane);
  205. if (!active[0]) {
  206. active[0] = active[1] = l_ptr;
  207. node_established_contact(n_ptr);
  208. goto exit;
  209. }
  210. if (l_ptr->priority < active[0]->priority) {
  211. pr_debug("New link <%s> becomes standby\n", l_ptr->name);
  212. goto exit;
  213. }
  214. tipc_link_dup_queue_xmit(active[0], l_ptr);
  215. if (l_ptr->priority == active[0]->priority) {
  216. active[0] = l_ptr;
  217. goto exit;
  218. }
  219. pr_debug("Old link <%s> becomes standby\n", active[0]->name);
  220. if (active[1] != active[0])
  221. pr_debug("Old link <%s> becomes standby\n", active[1]->name);
  222. active[0] = active[1] = l_ptr;
  223. exit:
  224. /* Leave room for changeover header when returning 'mtu' to users: */
  225. n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE;
  226. n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE;
  227. }
  228. /**
  229. * node_select_active_links - select active link
  230. */
  231. static void node_select_active_links(struct tipc_node *n_ptr)
  232. {
  233. struct tipc_link **active = &n_ptr->active_links[0];
  234. u32 i;
  235. u32 highest_prio = 0;
  236. active[0] = active[1] = NULL;
  237. for (i = 0; i < MAX_BEARERS; i++) {
  238. struct tipc_link *l_ptr = n_ptr->links[i];
  239. if (!l_ptr || !tipc_link_is_up(l_ptr) ||
  240. (l_ptr->priority < highest_prio))
  241. continue;
  242. if (l_ptr->priority > highest_prio) {
  243. highest_prio = l_ptr->priority;
  244. active[0] = active[1] = l_ptr;
  245. } else {
  246. active[1] = l_ptr;
  247. }
  248. }
  249. }
  250. /**
  251. * tipc_node_link_down - handle loss of link
  252. */
  253. void tipc_node_link_down(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  254. {
  255. struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
  256. struct tipc_link **active;
  257. n_ptr->working_links--;
  258. n_ptr->action_flags |= TIPC_NOTIFY_LINK_DOWN;
  259. n_ptr->link_id = l_ptr->peer_bearer_id << 16 | l_ptr->bearer_id;
  260. if (!tipc_link_is_active(l_ptr)) {
  261. pr_debug("Lost standby link <%s> on network plane %c\n",
  262. l_ptr->name, l_ptr->net_plane);
  263. return;
  264. }
  265. pr_debug("Lost link <%s> on network plane %c\n",
  266. l_ptr->name, l_ptr->net_plane);
  267. active = &n_ptr->active_links[0];
  268. if (active[0] == l_ptr)
  269. active[0] = active[1];
  270. if (active[1] == l_ptr)
  271. active[1] = active[0];
  272. if (active[0] == l_ptr)
  273. node_select_active_links(n_ptr);
  274. if (tipc_node_is_up(n_ptr))
  275. tipc_link_failover_send_queue(l_ptr);
  276. else
  277. node_lost_contact(n_ptr);
  278. /* Leave room for changeover header when returning 'mtu' to users: */
  279. if (active[0]) {
  280. n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE;
  281. n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE;
  282. return;
  283. }
  284. /* Loopback link went down? No fragmentation needed from now on. */
  285. if (n_ptr->addr == tn->own_addr) {
  286. n_ptr->act_mtus[0] = MAX_MSG_SIZE;
  287. n_ptr->act_mtus[1] = MAX_MSG_SIZE;
  288. }
  289. }
  290. int tipc_node_active_links(struct tipc_node *n_ptr)
  291. {
  292. return n_ptr->active_links[0] != NULL;
  293. }
  294. int tipc_node_is_up(struct tipc_node *n_ptr)
  295. {
  296. return tipc_node_active_links(n_ptr);
  297. }
  298. void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  299. {
  300. struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
  301. n_ptr->links[l_ptr->bearer_id] = l_ptr;
  302. spin_lock_bh(&tn->node_list_lock);
  303. tn->num_links++;
  304. spin_unlock_bh(&tn->node_list_lock);
  305. n_ptr->link_cnt++;
  306. }
  307. void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  308. {
  309. struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
  310. int i;
  311. for (i = 0; i < MAX_BEARERS; i++) {
  312. if (l_ptr != n_ptr->links[i])
  313. continue;
  314. n_ptr->links[i] = NULL;
  315. spin_lock_bh(&tn->node_list_lock);
  316. tn->num_links--;
  317. spin_unlock_bh(&tn->node_list_lock);
  318. n_ptr->link_cnt--;
  319. }
  320. }
  321. static void node_established_contact(struct tipc_node *n_ptr)
  322. {
  323. n_ptr->action_flags |= TIPC_NOTIFY_NODE_UP;
  324. n_ptr->bclink.oos_state = 0;
  325. n_ptr->bclink.acked = tipc_bclink_get_last_sent(n_ptr->net);
  326. tipc_bclink_add_node(n_ptr->net, n_ptr->addr);
  327. }
  328. static void node_lost_contact(struct tipc_node *n_ptr)
  329. {
  330. char addr_string[16];
  331. u32 i;
  332. pr_debug("Lost contact with %s\n",
  333. tipc_addr_string_fill(addr_string, n_ptr->addr));
  334. /* Flush broadcast link info associated with lost node */
  335. if (n_ptr->bclink.recv_permitted) {
  336. __skb_queue_purge(&n_ptr->bclink.deferred_queue);
  337. if (n_ptr->bclink.reasm_buf) {
  338. kfree_skb(n_ptr->bclink.reasm_buf);
  339. n_ptr->bclink.reasm_buf = NULL;
  340. }
  341. tipc_bclink_remove_node(n_ptr->net, n_ptr->addr);
  342. tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ);
  343. n_ptr->bclink.recv_permitted = false;
  344. }
  345. /* Abort link changeover */
  346. for (i = 0; i < MAX_BEARERS; i++) {
  347. struct tipc_link *l_ptr = n_ptr->links[i];
  348. if (!l_ptr)
  349. continue;
  350. l_ptr->reset_checkpoint = l_ptr->next_in_no;
  351. l_ptr->exp_msg_count = 0;
  352. tipc_link_reset_fragments(l_ptr);
  353. /* Link marked for deletion after failover? => do it now */
  354. if (l_ptr->flags & LINK_STOPPED)
  355. tipc_link_delete(l_ptr);
  356. }
  357. n_ptr->action_flags &= ~TIPC_WAIT_OWN_LINKS_DOWN;
  358. /* Notify subscribers and prevent re-contact with node until
  359. * cleanup is done.
  360. */
  361. n_ptr->action_flags |= TIPC_WAIT_PEER_LINKS_DOWN |
  362. TIPC_NOTIFY_NODE_DOWN;
  363. }
  364. struct sk_buff *tipc_node_get_nodes(struct net *net, const void *req_tlv_area,
  365. int req_tlv_space)
  366. {
  367. struct tipc_net *tn = net_generic(net, tipc_net_id);
  368. u32 domain;
  369. struct sk_buff *buf;
  370. struct tipc_node *n_ptr;
  371. struct tipc_node_info node_info;
  372. u32 payload_size;
  373. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  374. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  375. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  376. if (!tipc_addr_domain_valid(domain))
  377. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  378. " (network address)");
  379. spin_lock_bh(&tn->node_list_lock);
  380. if (!tn->num_nodes) {
  381. spin_unlock_bh(&tn->node_list_lock);
  382. return tipc_cfg_reply_none();
  383. }
  384. /* For now, get space for all other nodes */
  385. payload_size = TLV_SPACE(sizeof(node_info)) * tn->num_nodes;
  386. if (payload_size > 32768u) {
  387. spin_unlock_bh(&tn->node_list_lock);
  388. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  389. " (too many nodes)");
  390. }
  391. spin_unlock_bh(&tn->node_list_lock);
  392. buf = tipc_cfg_reply_alloc(payload_size);
  393. if (!buf)
  394. return NULL;
  395. /* Add TLVs for all nodes in scope */
  396. rcu_read_lock();
  397. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  398. if (!tipc_in_scope(domain, n_ptr->addr))
  399. continue;
  400. node_info.addr = htonl(n_ptr->addr);
  401. node_info.up = htonl(tipc_node_is_up(n_ptr));
  402. tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
  403. &node_info, sizeof(node_info));
  404. }
  405. rcu_read_unlock();
  406. return buf;
  407. }
  408. struct sk_buff *tipc_node_get_links(struct net *net, const void *req_tlv_area,
  409. int req_tlv_space)
  410. {
  411. struct tipc_net *tn = net_generic(net, tipc_net_id);
  412. u32 domain;
  413. struct sk_buff *buf;
  414. struct tipc_node *n_ptr;
  415. struct tipc_link_info link_info;
  416. u32 payload_size;
  417. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
  418. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  419. domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  420. if (!tipc_addr_domain_valid(domain))
  421. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  422. " (network address)");
  423. if (!tn->own_addr)
  424. return tipc_cfg_reply_none();
  425. spin_lock_bh(&tn->node_list_lock);
  426. /* Get space for all unicast links + broadcast link */
  427. payload_size = TLV_SPACE((sizeof(link_info)) * (tn->num_links + 1));
  428. if (payload_size > 32768u) {
  429. spin_unlock_bh(&tn->node_list_lock);
  430. return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
  431. " (too many links)");
  432. }
  433. spin_unlock_bh(&tn->node_list_lock);
  434. buf = tipc_cfg_reply_alloc(payload_size);
  435. if (!buf)
  436. return NULL;
  437. /* Add TLV for broadcast link */
  438. link_info.dest = htonl(tipc_cluster_mask(tn->own_addr));
  439. link_info.up = htonl(1);
  440. strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  441. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
  442. /* Add TLVs for any other links in scope */
  443. rcu_read_lock();
  444. list_for_each_entry_rcu(n_ptr, &tn->node_list, list) {
  445. u32 i;
  446. if (!tipc_in_scope(domain, n_ptr->addr))
  447. continue;
  448. tipc_node_lock(n_ptr);
  449. for (i = 0; i < MAX_BEARERS; i++) {
  450. if (!n_ptr->links[i])
  451. continue;
  452. link_info.dest = htonl(n_ptr->addr);
  453. link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
  454. strcpy(link_info.str, n_ptr->links[i]->name);
  455. tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
  456. &link_info, sizeof(link_info));
  457. }
  458. tipc_node_unlock(n_ptr);
  459. }
  460. rcu_read_unlock();
  461. return buf;
  462. }
  463. /**
  464. * tipc_node_get_linkname - get the name of a link
  465. *
  466. * @bearer_id: id of the bearer
  467. * @node: peer node address
  468. * @linkname: link name output buffer
  469. *
  470. * Returns 0 on success
  471. */
  472. int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
  473. char *linkname, size_t len)
  474. {
  475. struct tipc_link *link;
  476. struct tipc_node *node = tipc_node_find(net, addr);
  477. if ((bearer_id >= MAX_BEARERS) || !node)
  478. return -EINVAL;
  479. tipc_node_lock(node);
  480. link = node->links[bearer_id];
  481. if (link) {
  482. strncpy(linkname, link->name, len);
  483. tipc_node_unlock(node);
  484. return 0;
  485. }
  486. tipc_node_unlock(node);
  487. return -EINVAL;
  488. }
  489. void tipc_node_unlock(struct tipc_node *node)
  490. {
  491. struct net *net = node->net;
  492. LIST_HEAD(nsub_list);
  493. LIST_HEAD(conn_sks);
  494. struct sk_buff_head waiting_sks;
  495. u32 addr = 0;
  496. int flags = node->action_flags;
  497. u32 link_id = 0;
  498. if (likely(!flags)) {
  499. spin_unlock_bh(&node->lock);
  500. return;
  501. }
  502. addr = node->addr;
  503. link_id = node->link_id;
  504. __skb_queue_head_init(&waiting_sks);
  505. if (flags & TIPC_WAKEUP_USERS)
  506. skb_queue_splice_init(&node->waiting_sks, &waiting_sks);
  507. if (flags & TIPC_NOTIFY_NODE_DOWN) {
  508. list_replace_init(&node->publ_list, &nsub_list);
  509. list_replace_init(&node->conn_sks, &conn_sks);
  510. }
  511. node->action_flags &= ~(TIPC_WAKEUP_USERS | TIPC_NOTIFY_NODE_DOWN |
  512. TIPC_NOTIFY_NODE_UP | TIPC_NOTIFY_LINK_UP |
  513. TIPC_NOTIFY_LINK_DOWN |
  514. TIPC_WAKEUP_BCAST_USERS);
  515. spin_unlock_bh(&node->lock);
  516. while (!skb_queue_empty(&waiting_sks))
  517. tipc_sk_rcv(net, __skb_dequeue(&waiting_sks));
  518. if (!list_empty(&conn_sks))
  519. tipc_node_abort_sock_conns(net, &conn_sks);
  520. if (!list_empty(&nsub_list))
  521. tipc_publ_notify(net, &nsub_list, addr);
  522. if (flags & TIPC_WAKEUP_BCAST_USERS)
  523. tipc_bclink_wakeup_users(net);
  524. if (flags & TIPC_NOTIFY_NODE_UP)
  525. tipc_named_node_up(net, addr);
  526. if (flags & TIPC_NOTIFY_LINK_UP)
  527. tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
  528. TIPC_NODE_SCOPE, link_id, addr);
  529. if (flags & TIPC_NOTIFY_LINK_DOWN)
  530. tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
  531. link_id, addr);
  532. }
  533. /* Caller should hold node lock for the passed node */
  534. static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
  535. {
  536. void *hdr;
  537. struct nlattr *attrs;
  538. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_v2_family,
  539. NLM_F_MULTI, TIPC_NL_NODE_GET);
  540. if (!hdr)
  541. return -EMSGSIZE;
  542. attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
  543. if (!attrs)
  544. goto msg_full;
  545. if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
  546. goto attr_msg_full;
  547. if (tipc_node_is_up(node))
  548. if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
  549. goto attr_msg_full;
  550. nla_nest_end(msg->skb, attrs);
  551. genlmsg_end(msg->skb, hdr);
  552. return 0;
  553. attr_msg_full:
  554. nla_nest_cancel(msg->skb, attrs);
  555. msg_full:
  556. genlmsg_cancel(msg->skb, hdr);
  557. return -EMSGSIZE;
  558. }
  559. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  560. {
  561. int err;
  562. struct net *net = sock_net(skb->sk);
  563. struct tipc_net *tn = net_generic(net, tipc_net_id);
  564. int done = cb->args[0];
  565. int last_addr = cb->args[1];
  566. struct tipc_node *node;
  567. struct tipc_nl_msg msg;
  568. if (done)
  569. return 0;
  570. msg.skb = skb;
  571. msg.portid = NETLINK_CB(cb->skb).portid;
  572. msg.seq = cb->nlh->nlmsg_seq;
  573. rcu_read_lock();
  574. if (last_addr && !tipc_node_find(net, last_addr)) {
  575. rcu_read_unlock();
  576. /* We never set seq or call nl_dump_check_consistent() this
  577. * means that setting prev_seq here will cause the consistence
  578. * check to fail in the netlink callback handler. Resulting in
  579. * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
  580. * the node state changed while we released the lock.
  581. */
  582. cb->prev_seq = 1;
  583. return -EPIPE;
  584. }
  585. list_for_each_entry_rcu(node, &tn->node_list, list) {
  586. if (last_addr) {
  587. if (node->addr == last_addr)
  588. last_addr = 0;
  589. else
  590. continue;
  591. }
  592. tipc_node_lock(node);
  593. err = __tipc_nl_add_node(&msg, node);
  594. if (err) {
  595. last_addr = node->addr;
  596. tipc_node_unlock(node);
  597. goto out;
  598. }
  599. tipc_node_unlock(node);
  600. }
  601. done = 1;
  602. out:
  603. cb->args[0] = done;
  604. cb->args[1] = last_addr;
  605. rcu_read_unlock();
  606. return skb->len;
  607. }