node.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. #include "discover.h"
  43. static void node_lost_contact(struct tipc_node *n_ptr);
  44. static void node_established_contact(struct tipc_node *n_ptr);
  45. static void tipc_node_delete(struct tipc_node *node);
  46. static void tipc_node_timeout(unsigned long data);
  47. static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
  48. struct tipc_sock_conn {
  49. u32 port;
  50. u32 peer_port;
  51. u32 peer_node;
  52. struct list_head list;
  53. };
  54. static const struct nla_policy tipc_nl_node_policy[TIPC_NLA_NODE_MAX + 1] = {
  55. [TIPC_NLA_NODE_UNSPEC] = { .type = NLA_UNSPEC },
  56. [TIPC_NLA_NODE_ADDR] = { .type = NLA_U32 },
  57. [TIPC_NLA_NODE_UP] = { .type = NLA_FLAG }
  58. };
  59. /*
  60. * A trivial power-of-two bitmask technique is used for speed, since this
  61. * operation is done for every incoming TIPC packet. The number of hash table
  62. * entries has been chosen so that no hash chain exceeds 8 nodes and will
  63. * usually be much smaller (typically only a single node).
  64. */
  65. static unsigned int tipc_hashfn(u32 addr)
  66. {
  67. return addr & (NODE_HTABLE_SIZE - 1);
  68. }
  69. static void tipc_node_kref_release(struct kref *kref)
  70. {
  71. struct tipc_node *node = container_of(kref, struct tipc_node, kref);
  72. tipc_node_delete(node);
  73. }
  74. void tipc_node_put(struct tipc_node *node)
  75. {
  76. kref_put(&node->kref, tipc_node_kref_release);
  77. }
  78. static void tipc_node_get(struct tipc_node *node)
  79. {
  80. kref_get(&node->kref);
  81. }
  82. /*
  83. * tipc_node_find - locate specified node object, if it exists
  84. */
  85. struct tipc_node *tipc_node_find(struct net *net, u32 addr)
  86. {
  87. struct tipc_net *tn = net_generic(net, tipc_net_id);
  88. struct tipc_node *node;
  89. if (unlikely(!in_own_cluster_exact(net, addr)))
  90. return NULL;
  91. rcu_read_lock();
  92. hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)],
  93. hash) {
  94. if (node->addr == addr) {
  95. tipc_node_get(node);
  96. rcu_read_unlock();
  97. return node;
  98. }
  99. }
  100. rcu_read_unlock();
  101. return NULL;
  102. }
  103. struct tipc_node *tipc_node_create(struct net *net, u32 addr)
  104. {
  105. struct tipc_net *tn = net_generic(net, tipc_net_id);
  106. struct tipc_node *n_ptr, *temp_node;
  107. spin_lock_bh(&tn->node_list_lock);
  108. n_ptr = tipc_node_find(net, addr);
  109. if (n_ptr)
  110. goto exit;
  111. n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
  112. if (!n_ptr) {
  113. pr_warn("Node creation failed, no memory\n");
  114. goto exit;
  115. }
  116. n_ptr->addr = addr;
  117. n_ptr->net = net;
  118. kref_init(&n_ptr->kref);
  119. spin_lock_init(&n_ptr->lock);
  120. INIT_HLIST_NODE(&n_ptr->hash);
  121. INIT_LIST_HEAD(&n_ptr->list);
  122. INIT_LIST_HEAD(&n_ptr->publ_list);
  123. INIT_LIST_HEAD(&n_ptr->conn_sks);
  124. skb_queue_head_init(&n_ptr->bclink.namedq);
  125. __skb_queue_head_init(&n_ptr->bclink.deferdq);
  126. hlist_add_head_rcu(&n_ptr->hash, &tn->node_htable[tipc_hashfn(addr)]);
  127. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  128. if (n_ptr->addr < temp_node->addr)
  129. break;
  130. }
  131. list_add_tail_rcu(&n_ptr->list, &temp_node->list);
  132. n_ptr->state = SELF_DOWN_PEER_LEAVING;
  133. n_ptr->signature = INVALID_NODE_SIG;
  134. n_ptr->active_links[0] = INVALID_BEARER_ID;
  135. n_ptr->active_links[1] = INVALID_BEARER_ID;
  136. tipc_node_get(n_ptr);
  137. setup_timer(&n_ptr->timer, tipc_node_timeout, (unsigned long)n_ptr);
  138. n_ptr->keepalive_intv = U32_MAX;
  139. exit:
  140. spin_unlock_bh(&tn->node_list_lock);
  141. return n_ptr;
  142. }
  143. static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
  144. {
  145. unsigned long tol = l->tolerance;
  146. unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
  147. unsigned long keepalive_intv = msecs_to_jiffies(intv);
  148. /* Link with lowest tolerance determines timer interval */
  149. if (keepalive_intv < n->keepalive_intv)
  150. n->keepalive_intv = keepalive_intv;
  151. /* Ensure link's abort limit corresponds to current interval */
  152. l->abort_limit = l->tolerance / jiffies_to_msecs(n->keepalive_intv);
  153. }
  154. static void tipc_node_delete(struct tipc_node *node)
  155. {
  156. list_del_rcu(&node->list);
  157. hlist_del_rcu(&node->hash);
  158. kfree_rcu(node, rcu);
  159. }
  160. void tipc_node_stop(struct net *net)
  161. {
  162. struct tipc_net *tn = net_generic(net, tipc_net_id);
  163. struct tipc_node *node, *t_node;
  164. spin_lock_bh(&tn->node_list_lock);
  165. list_for_each_entry_safe(node, t_node, &tn->node_list, list) {
  166. if (del_timer(&node->timer))
  167. tipc_node_put(node);
  168. tipc_node_put(node);
  169. }
  170. spin_unlock_bh(&tn->node_list_lock);
  171. }
  172. int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
  173. {
  174. struct tipc_node *node;
  175. struct tipc_sock_conn *conn;
  176. int err = 0;
  177. if (in_own_node(net, dnode))
  178. return 0;
  179. node = tipc_node_find(net, dnode);
  180. if (!node) {
  181. pr_warn("Connecting sock to node 0x%x failed\n", dnode);
  182. return -EHOSTUNREACH;
  183. }
  184. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  185. if (!conn) {
  186. err = -EHOSTUNREACH;
  187. goto exit;
  188. }
  189. conn->peer_node = dnode;
  190. conn->port = port;
  191. conn->peer_port = peer_port;
  192. tipc_node_lock(node);
  193. list_add_tail(&conn->list, &node->conn_sks);
  194. tipc_node_unlock(node);
  195. exit:
  196. tipc_node_put(node);
  197. return err;
  198. }
  199. void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
  200. {
  201. struct tipc_node *node;
  202. struct tipc_sock_conn *conn, *safe;
  203. if (in_own_node(net, dnode))
  204. return;
  205. node = tipc_node_find(net, dnode);
  206. if (!node)
  207. return;
  208. tipc_node_lock(node);
  209. list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
  210. if (port != conn->port)
  211. continue;
  212. list_del(&conn->list);
  213. kfree(conn);
  214. }
  215. tipc_node_unlock(node);
  216. tipc_node_put(node);
  217. }
  218. /* tipc_node_timeout - handle expiration of node timer
  219. */
  220. static void tipc_node_timeout(unsigned long data)
  221. {
  222. struct tipc_node *n = (struct tipc_node *)data;
  223. struct sk_buff_head xmitq;
  224. struct tipc_link *l;
  225. struct tipc_media_addr *maddr;
  226. int bearer_id;
  227. int rc = 0;
  228. __skb_queue_head_init(&xmitq);
  229. for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
  230. tipc_node_lock(n);
  231. l = n->links[bearer_id].link;
  232. if (l) {
  233. /* Link tolerance may change asynchronously: */
  234. tipc_node_calculate_timer(n, l);
  235. rc = tipc_link_timeout(l, &xmitq);
  236. if (rc & TIPC_LINK_DOWN_EVT)
  237. tipc_link_reset(l);
  238. }
  239. tipc_node_unlock(n);
  240. maddr = &n->links[bearer_id].maddr;
  241. tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
  242. }
  243. if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
  244. tipc_node_get(n);
  245. tipc_node_put(n);
  246. }
  247. /**
  248. * tipc_node_link_up - handle addition of link
  249. *
  250. * Link becomes active (alone or shared) or standby, depending on its priority.
  251. */
  252. void tipc_node_link_up(struct tipc_node *n, int bearer_id)
  253. {
  254. int *slot0 = &n->active_links[0];
  255. int *slot1 = &n->active_links[1];
  256. struct tipc_link_entry *links = n->links;
  257. struct tipc_link *l = n->links[bearer_id].link;
  258. /* Leave room for tunnel header when returning 'mtu' to users: */
  259. links[bearer_id].mtu = l->mtu - INT_H_SIZE;
  260. n->working_links++;
  261. n->action_flags |= TIPC_NOTIFY_LINK_UP;
  262. n->link_id = l->peer_bearer_id << 16 | l->bearer_id;
  263. tipc_bearer_add_dest(n->net, bearer_id, n->addr);
  264. pr_debug("Established link <%s> on network plane %c\n",
  265. l->name, l->net_plane);
  266. /* No active links ? => take both active slots */
  267. if (!tipc_node_is_up(n)) {
  268. *slot0 = bearer_id;
  269. *slot1 = bearer_id;
  270. node_established_contact(n);
  271. return;
  272. }
  273. /* Lower prio than current active ? => no slot */
  274. if (l->priority < links[*slot0].link->priority) {
  275. pr_debug("New link <%s> becomes standby\n", l->name);
  276. return;
  277. }
  278. tipc_link_dup_queue_xmit(links[*slot0].link, l);
  279. /* Same prio as current active ? => take one slot */
  280. if (l->priority == links[*slot0].link->priority) {
  281. *slot0 = bearer_id;
  282. return;
  283. }
  284. /* Higher prio than current active => take both active slots */
  285. pr_debug("Old link <%s> now standby\n", links[*slot0].link->name);
  286. *slot0 = bearer_id;
  287. *slot1 = bearer_id;
  288. }
  289. /**
  290. * tipc_node_link_down - handle loss of link
  291. */
  292. void tipc_node_link_down(struct tipc_node *n, int bearer_id)
  293. {
  294. int *slot0 = &n->active_links[0];
  295. int *slot1 = &n->active_links[1];
  296. int i, highest = 0;
  297. struct tipc_link *l, *_l;
  298. l = n->links[bearer_id].link;
  299. n->working_links--;
  300. n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
  301. n->link_id = l->peer_bearer_id << 16 | l->bearer_id;
  302. pr_debug("Lost link <%s> on network plane %c\n",
  303. l->name, l->net_plane);
  304. /* Select new active link if any available */
  305. *slot0 = INVALID_BEARER_ID;
  306. *slot1 = INVALID_BEARER_ID;
  307. for (i = 0; i < MAX_BEARERS; i++) {
  308. _l = n->links[i].link;
  309. if (!_l || !tipc_link_is_up(_l))
  310. continue;
  311. if (_l->priority < highest)
  312. continue;
  313. if (_l->priority > highest) {
  314. highest = _l->priority;
  315. *slot0 = i;
  316. *slot1 = i;
  317. continue;
  318. }
  319. *slot1 = i;
  320. }
  321. if (tipc_node_is_up(n))
  322. tipc_link_failover_send_queue(l);
  323. else
  324. node_lost_contact(n);
  325. }
  326. bool tipc_node_is_up(struct tipc_node *n)
  327. {
  328. return n->active_links[0] != INVALID_BEARER_ID;
  329. }
  330. void tipc_node_check_dest(struct tipc_node *n, struct tipc_bearer *b,
  331. bool *link_up, bool *addr_match,
  332. struct tipc_media_addr *maddr)
  333. {
  334. struct tipc_link *l = n->links[b->identity].link;
  335. struct tipc_media_addr *curr = &n->links[b->identity].maddr;
  336. *link_up = l && tipc_link_is_up(l);
  337. *addr_match = l && !memcmp(curr, maddr, sizeof(*maddr));
  338. }
  339. bool tipc_node_update_dest(struct tipc_node *n, struct tipc_bearer *b,
  340. struct tipc_media_addr *maddr)
  341. {
  342. struct tipc_link *l = n->links[b->identity].link;
  343. struct tipc_media_addr *curr = &n->links[b->identity].maddr;
  344. struct sk_buff_head *inputq = &n->links[b->identity].inputq;
  345. if (!l) {
  346. l = tipc_link_create(n, b, maddr, inputq, &n->bclink.namedq);
  347. if (!l)
  348. return false;
  349. tipc_node_calculate_timer(n, l);
  350. if (n->link_cnt == 1) {
  351. if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
  352. tipc_node_get(n);
  353. }
  354. }
  355. memcpy(&l->media_addr, maddr, sizeof(*maddr));
  356. memcpy(curr, maddr, sizeof(*maddr));
  357. tipc_link_reset(l);
  358. return true;
  359. }
  360. void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  361. {
  362. n_ptr->links[l_ptr->bearer_id].link = l_ptr;
  363. n_ptr->link_cnt++;
  364. }
  365. void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
  366. {
  367. int i;
  368. for (i = 0; i < MAX_BEARERS; i++) {
  369. if (l_ptr != n_ptr->links[i].link)
  370. continue;
  371. n_ptr->links[i].link = NULL;
  372. n_ptr->link_cnt--;
  373. }
  374. }
  375. /* tipc_node_fsm_evt - node finite state machine
  376. * Determines when contact is allowed with peer node
  377. */
  378. static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
  379. {
  380. int state = n->state;
  381. switch (state) {
  382. case SELF_DOWN_PEER_DOWN:
  383. switch (evt) {
  384. case SELF_ESTABL_CONTACT_EVT:
  385. state = SELF_UP_PEER_COMING;
  386. break;
  387. case PEER_ESTABL_CONTACT_EVT:
  388. state = SELF_COMING_PEER_UP;
  389. break;
  390. case SELF_LOST_CONTACT_EVT:
  391. case PEER_LOST_CONTACT_EVT:
  392. break;
  393. default:
  394. pr_err("Unknown node fsm evt %x/%x\n", state, evt);
  395. }
  396. break;
  397. case SELF_UP_PEER_UP:
  398. switch (evt) {
  399. case SELF_LOST_CONTACT_EVT:
  400. state = SELF_DOWN_PEER_LEAVING;
  401. break;
  402. case PEER_LOST_CONTACT_EVT:
  403. state = SELF_LEAVING_PEER_DOWN;
  404. break;
  405. case SELF_ESTABL_CONTACT_EVT:
  406. case PEER_ESTABL_CONTACT_EVT:
  407. break;
  408. default:
  409. pr_err("Unknown node fsm evt %x/%x\n", state, evt);
  410. }
  411. break;
  412. case SELF_DOWN_PEER_LEAVING:
  413. switch (evt) {
  414. case PEER_LOST_CONTACT_EVT:
  415. state = SELF_DOWN_PEER_DOWN;
  416. break;
  417. case SELF_ESTABL_CONTACT_EVT:
  418. case PEER_ESTABL_CONTACT_EVT:
  419. case SELF_LOST_CONTACT_EVT:
  420. break;
  421. default:
  422. pr_err("Unknown node fsm evt %x/%x\n", state, evt);
  423. }
  424. break;
  425. case SELF_UP_PEER_COMING:
  426. switch (evt) {
  427. case PEER_ESTABL_CONTACT_EVT:
  428. state = SELF_UP_PEER_UP;
  429. break;
  430. case SELF_LOST_CONTACT_EVT:
  431. state = SELF_DOWN_PEER_LEAVING;
  432. break;
  433. case SELF_ESTABL_CONTACT_EVT:
  434. case PEER_LOST_CONTACT_EVT:
  435. break;
  436. default:
  437. pr_err("Unknown node fsm evt %x/%x\n", state, evt);
  438. }
  439. break;
  440. case SELF_COMING_PEER_UP:
  441. switch (evt) {
  442. case SELF_ESTABL_CONTACT_EVT:
  443. state = SELF_UP_PEER_UP;
  444. break;
  445. case PEER_LOST_CONTACT_EVT:
  446. state = SELF_LEAVING_PEER_DOWN;
  447. break;
  448. case SELF_LOST_CONTACT_EVT:
  449. case PEER_ESTABL_CONTACT_EVT:
  450. break;
  451. default:
  452. pr_err("Unknown node fsm evt %x/%x\n", state, evt);
  453. }
  454. break;
  455. case SELF_LEAVING_PEER_DOWN:
  456. switch (evt) {
  457. case SELF_LOST_CONTACT_EVT:
  458. state = SELF_DOWN_PEER_DOWN;
  459. break;
  460. case SELF_ESTABL_CONTACT_EVT:
  461. case PEER_ESTABL_CONTACT_EVT:
  462. case PEER_LOST_CONTACT_EVT:
  463. break;
  464. default:
  465. pr_err("Unknown node fsm evt %x/%x\n", state, evt);
  466. }
  467. break;
  468. default:
  469. pr_err("Unknown node fsm state %x\n", state);
  470. break;
  471. }
  472. n->state = state;
  473. }
  474. bool tipc_node_filter_skb(struct tipc_node *n, struct tipc_link *l,
  475. struct tipc_msg *hdr)
  476. {
  477. int state = n->state;
  478. if (likely(state == SELF_UP_PEER_UP))
  479. return true;
  480. if (state == SELF_DOWN_PEER_DOWN)
  481. return true;
  482. if (state == SELF_UP_PEER_COMING) {
  483. /* If not traffic msg, peer may still be ESTABLISHING */
  484. if (tipc_link_is_up(l) && msg_is_traffic(hdr))
  485. tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
  486. return true;
  487. }
  488. if (state == SELF_COMING_PEER_UP)
  489. return true;
  490. if (state == SELF_LEAVING_PEER_DOWN)
  491. return false;
  492. if (state == SELF_DOWN_PEER_LEAVING) {
  493. if (msg_peer_is_up(hdr))
  494. return false;
  495. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  496. return true;
  497. }
  498. return false;
  499. }
  500. static void node_established_contact(struct tipc_node *n_ptr)
  501. {
  502. tipc_node_fsm_evt(n_ptr, SELF_ESTABL_CONTACT_EVT);
  503. n_ptr->action_flags |= TIPC_NOTIFY_NODE_UP;
  504. n_ptr->bclink.oos_state = 0;
  505. n_ptr->bclink.acked = tipc_bclink_get_last_sent(n_ptr->net);
  506. tipc_bclink_add_node(n_ptr->net, n_ptr->addr);
  507. }
  508. static void node_lost_contact(struct tipc_node *n_ptr)
  509. {
  510. char addr_string[16];
  511. struct tipc_sock_conn *conn, *safe;
  512. struct list_head *conns = &n_ptr->conn_sks;
  513. struct sk_buff *skb;
  514. struct tipc_net *tn = net_generic(n_ptr->net, tipc_net_id);
  515. uint i;
  516. pr_debug("Lost contact with %s\n",
  517. tipc_addr_string_fill(addr_string, n_ptr->addr));
  518. /* Flush broadcast link info associated with lost node */
  519. if (n_ptr->bclink.recv_permitted) {
  520. __skb_queue_purge(&n_ptr->bclink.deferdq);
  521. if (n_ptr->bclink.reasm_buf) {
  522. kfree_skb(n_ptr->bclink.reasm_buf);
  523. n_ptr->bclink.reasm_buf = NULL;
  524. }
  525. tipc_bclink_remove_node(n_ptr->net, n_ptr->addr);
  526. tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ);
  527. n_ptr->bclink.recv_permitted = false;
  528. }
  529. /* Abort any ongoing link failover */
  530. for (i = 0; i < MAX_BEARERS; i++) {
  531. struct tipc_link *l_ptr = n_ptr->links[i].link;
  532. if (!l_ptr)
  533. continue;
  534. l_ptr->exec_mode = TIPC_LINK_OPEN;
  535. l_ptr->failover_checkpt = 0;
  536. l_ptr->failover_pkts = 0;
  537. kfree_skb(l_ptr->failover_skb);
  538. l_ptr->failover_skb = NULL;
  539. tipc_link_reset_fragments(l_ptr);
  540. }
  541. /* Prevent re-contact with node until cleanup is done */
  542. tipc_node_fsm_evt(n_ptr, SELF_LOST_CONTACT_EVT);
  543. /* Notify publications from this node */
  544. n_ptr->action_flags |= TIPC_NOTIFY_NODE_DOWN;
  545. /* Notify sockets connected to node */
  546. list_for_each_entry_safe(conn, safe, conns, list) {
  547. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
  548. SHORT_H_SIZE, 0, tn->own_addr,
  549. conn->peer_node, conn->port,
  550. conn->peer_port, TIPC_ERR_NO_NODE);
  551. if (likely(skb)) {
  552. skb_queue_tail(n_ptr->inputq, skb);
  553. n_ptr->action_flags |= TIPC_MSG_EVT;
  554. }
  555. list_del(&conn->list);
  556. kfree(conn);
  557. }
  558. }
  559. /**
  560. * tipc_node_get_linkname - get the name of a link
  561. *
  562. * @bearer_id: id of the bearer
  563. * @node: peer node address
  564. * @linkname: link name output buffer
  565. *
  566. * Returns 0 on success
  567. */
  568. int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
  569. char *linkname, size_t len)
  570. {
  571. struct tipc_link *link;
  572. int err = -EINVAL;
  573. struct tipc_node *node = tipc_node_find(net, addr);
  574. if (!node)
  575. return err;
  576. if (bearer_id >= MAX_BEARERS)
  577. goto exit;
  578. tipc_node_lock(node);
  579. link = node->links[bearer_id].link;
  580. if (link) {
  581. strncpy(linkname, link->name, len);
  582. err = 0;
  583. }
  584. exit:
  585. tipc_node_unlock(node);
  586. tipc_node_put(node);
  587. return err;
  588. }
  589. void tipc_node_unlock(struct tipc_node *node)
  590. {
  591. struct net *net = node->net;
  592. u32 addr = 0;
  593. u32 flags = node->action_flags;
  594. u32 link_id = 0;
  595. struct list_head *publ_list;
  596. struct sk_buff_head *inputq = node->inputq;
  597. struct sk_buff_head *namedq;
  598. if (likely(!flags || (flags == TIPC_MSG_EVT))) {
  599. node->action_flags = 0;
  600. spin_unlock_bh(&node->lock);
  601. if (flags == TIPC_MSG_EVT)
  602. tipc_sk_rcv(net, inputq);
  603. return;
  604. }
  605. addr = node->addr;
  606. link_id = node->link_id;
  607. namedq = node->namedq;
  608. publ_list = &node->publ_list;
  609. node->action_flags &= ~(TIPC_MSG_EVT |
  610. TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
  611. TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP |
  612. TIPC_WAKEUP_BCAST_USERS | TIPC_BCAST_MSG_EVT |
  613. TIPC_NAMED_MSG_EVT | TIPC_BCAST_RESET);
  614. spin_unlock_bh(&node->lock);
  615. if (flags & TIPC_NOTIFY_NODE_DOWN)
  616. tipc_publ_notify(net, publ_list, addr);
  617. if (flags & TIPC_WAKEUP_BCAST_USERS)
  618. tipc_bclink_wakeup_users(net);
  619. if (flags & TIPC_NOTIFY_NODE_UP)
  620. tipc_named_node_up(net, addr);
  621. if (flags & TIPC_NOTIFY_LINK_UP)
  622. tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
  623. TIPC_NODE_SCOPE, link_id, addr);
  624. if (flags & TIPC_NOTIFY_LINK_DOWN)
  625. tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
  626. link_id, addr);
  627. if (flags & TIPC_MSG_EVT)
  628. tipc_sk_rcv(net, inputq);
  629. if (flags & TIPC_NAMED_MSG_EVT)
  630. tipc_named_rcv(net, namedq);
  631. if (flags & TIPC_BCAST_MSG_EVT)
  632. tipc_bclink_input(net);
  633. if (flags & TIPC_BCAST_RESET)
  634. tipc_link_reset_all(node);
  635. }
  636. /* Caller should hold node lock for the passed node */
  637. static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
  638. {
  639. void *hdr;
  640. struct nlattr *attrs;
  641. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  642. NLM_F_MULTI, TIPC_NL_NODE_GET);
  643. if (!hdr)
  644. return -EMSGSIZE;
  645. attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
  646. if (!attrs)
  647. goto msg_full;
  648. if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
  649. goto attr_msg_full;
  650. if (tipc_node_is_up(node))
  651. if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
  652. goto attr_msg_full;
  653. nla_nest_end(msg->skb, attrs);
  654. genlmsg_end(msg->skb, hdr);
  655. return 0;
  656. attr_msg_full:
  657. nla_nest_cancel(msg->skb, attrs);
  658. msg_full:
  659. genlmsg_cancel(msg->skb, hdr);
  660. return -EMSGSIZE;
  661. }
  662. static struct tipc_link *tipc_node_select_link(struct tipc_node *n, int sel,
  663. int *bearer_id,
  664. struct tipc_media_addr **maddr)
  665. {
  666. int id = n->active_links[sel & 1];
  667. if (unlikely(id < 0))
  668. return NULL;
  669. *bearer_id = id;
  670. *maddr = &n->links[id].maddr;
  671. return n->links[id].link;
  672. }
  673. /**
  674. * tipc_node_xmit() is the general link level function for message sending
  675. * @net: the applicable net namespace
  676. * @list: chain of buffers containing message
  677. * @dnode: address of destination node
  678. * @selector: a number used for deterministic link selection
  679. * Consumes the buffer chain, except when returning -ELINKCONG
  680. * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
  681. */
  682. int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
  683. u32 dnode, int selector)
  684. {
  685. struct tipc_link *l = NULL;
  686. struct tipc_node *n;
  687. struct sk_buff_head xmitq;
  688. struct tipc_media_addr *maddr;
  689. int bearer_id;
  690. int rc = -EHOSTUNREACH;
  691. __skb_queue_head_init(&xmitq);
  692. n = tipc_node_find(net, dnode);
  693. if (likely(n)) {
  694. tipc_node_lock(n);
  695. l = tipc_node_select_link(n, selector, &bearer_id, &maddr);
  696. if (likely(l))
  697. rc = tipc_link_xmit(l, list, &xmitq);
  698. if (unlikely(rc == -ENOBUFS))
  699. tipc_link_reset(l);
  700. tipc_node_unlock(n);
  701. tipc_node_put(n);
  702. }
  703. if (likely(!rc)) {
  704. tipc_bearer_xmit(net, bearer_id, &xmitq, maddr);
  705. return 0;
  706. }
  707. if (likely(in_own_node(net, dnode))) {
  708. tipc_sk_rcv(net, list);
  709. return 0;
  710. }
  711. return rc;
  712. }
  713. /* tipc_node_xmit_skb(): send single buffer to destination
  714. * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
  715. * messages, which will not be rejected
  716. * The only exception is datagram messages rerouted after secondary
  717. * lookup, which are rare and safe to dispose of anyway.
  718. * TODO: Return real return value, and let callers use
  719. * tipc_wait_for_sendpkt() where applicable
  720. */
  721. int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  722. u32 selector)
  723. {
  724. struct sk_buff_head head;
  725. int rc;
  726. skb_queue_head_init(&head);
  727. __skb_queue_tail(&head, skb);
  728. rc = tipc_node_xmit(net, &head, dnode, selector);
  729. if (rc == -ELINKCONG)
  730. kfree_skb(skb);
  731. return 0;
  732. }
  733. /**
  734. * tipc_rcv - process TIPC packets/messages arriving from off-node
  735. * @net: the applicable net namespace
  736. * @skb: TIPC packet
  737. * @bearer: pointer to bearer message arrived on
  738. *
  739. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  740. * structure (i.e. cannot be NULL), but bearer can be inactive.
  741. */
  742. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
  743. {
  744. struct sk_buff_head xmitq;
  745. struct tipc_node *n;
  746. struct tipc_link *l;
  747. struct tipc_msg *hdr;
  748. struct tipc_media_addr *maddr;
  749. int bearer_id = b->identity;
  750. int rc = 0;
  751. __skb_queue_head_init(&xmitq);
  752. /* Ensure message is well-formed */
  753. if (unlikely(!tipc_msg_validate(skb)))
  754. goto discard;
  755. /* Handle arrival of a non-unicast link packet */
  756. hdr = buf_msg(skb);
  757. if (unlikely(msg_non_seq(hdr))) {
  758. if (msg_user(hdr) == LINK_CONFIG)
  759. tipc_disc_rcv(net, skb, b);
  760. else
  761. tipc_bclink_rcv(net, skb);
  762. return;
  763. }
  764. /* Locate neighboring node that sent packet */
  765. n = tipc_node_find(net, msg_prevnode(hdr));
  766. if (unlikely(!n))
  767. goto discard;
  768. tipc_node_lock(n);
  769. /* Locate link endpoint that should handle packet */
  770. l = n->links[bearer_id].link;
  771. if (unlikely(!l))
  772. goto unlock;
  773. /* Is reception of this packet permitted at the moment ? */
  774. if (unlikely(n->state != SELF_UP_PEER_UP))
  775. if (!tipc_node_filter_skb(n, l, hdr))
  776. goto unlock;
  777. if (unlikely(msg_user(hdr) == LINK_PROTOCOL))
  778. tipc_bclink_sync_state(n, hdr);
  779. /* Release acked broadcast messages */
  780. if (unlikely(n->bclink.acked != msg_bcast_ack(hdr)))
  781. tipc_bclink_acknowledge(n, msg_bcast_ack(hdr));
  782. /* Check protocol and update link state */
  783. rc = tipc_link_rcv(l, skb, &xmitq);
  784. if (unlikely(rc & TIPC_LINK_UP_EVT))
  785. tipc_node_link_up(n, bearer_id);
  786. if (unlikely(rc & TIPC_LINK_DOWN_EVT))
  787. tipc_link_reset(l);
  788. skb = NULL;
  789. unlock:
  790. tipc_node_unlock(n);
  791. tipc_sk_rcv(net, &n->links[bearer_id].inputq);
  792. maddr = &n->links[bearer_id].maddr;
  793. tipc_bearer_xmit(net, bearer_id, &xmitq, maddr);
  794. tipc_node_put(n);
  795. discard:
  796. kfree_skb(skb);
  797. }
  798. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  799. {
  800. int err;
  801. struct net *net = sock_net(skb->sk);
  802. struct tipc_net *tn = net_generic(net, tipc_net_id);
  803. int done = cb->args[0];
  804. int last_addr = cb->args[1];
  805. struct tipc_node *node;
  806. struct tipc_nl_msg msg;
  807. if (done)
  808. return 0;
  809. msg.skb = skb;
  810. msg.portid = NETLINK_CB(cb->skb).portid;
  811. msg.seq = cb->nlh->nlmsg_seq;
  812. rcu_read_lock();
  813. if (last_addr) {
  814. node = tipc_node_find(net, last_addr);
  815. if (!node) {
  816. rcu_read_unlock();
  817. /* We never set seq or call nl_dump_check_consistent()
  818. * this means that setting prev_seq here will cause the
  819. * consistence check to fail in the netlink callback
  820. * handler. Resulting in the NLMSG_DONE message having
  821. * the NLM_F_DUMP_INTR flag set if the node state
  822. * changed while we released the lock.
  823. */
  824. cb->prev_seq = 1;
  825. return -EPIPE;
  826. }
  827. tipc_node_put(node);
  828. }
  829. list_for_each_entry_rcu(node, &tn->node_list, list) {
  830. if (last_addr) {
  831. if (node->addr == last_addr)
  832. last_addr = 0;
  833. else
  834. continue;
  835. }
  836. tipc_node_lock(node);
  837. err = __tipc_nl_add_node(&msg, node);
  838. if (err) {
  839. last_addr = node->addr;
  840. tipc_node_unlock(node);
  841. goto out;
  842. }
  843. tipc_node_unlock(node);
  844. }
  845. done = 1;
  846. out:
  847. cb->args[0] = done;
  848. cb->args[1] = last_addr;
  849. rcu_read_unlock();
  850. return skb->len;
  851. }