node.c 18 KB

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