node.c 18 KB

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