node.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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. /* Node FSM states and events:
  44. */
  45. enum {
  46. SELF_DOWN_PEER_DOWN = 0xdd,
  47. SELF_UP_PEER_UP = 0xaa,
  48. SELF_DOWN_PEER_LEAVING = 0xd1,
  49. SELF_UP_PEER_COMING = 0xac,
  50. SELF_COMING_PEER_UP = 0xca,
  51. SELF_LEAVING_PEER_DOWN = 0x1d,
  52. NODE_FAILINGOVER = 0xf0,
  53. NODE_SYNCHING = 0xcc
  54. };
  55. enum {
  56. SELF_ESTABL_CONTACT_EVT = 0xece,
  57. SELF_LOST_CONTACT_EVT = 0x1ce,
  58. PEER_ESTABL_CONTACT_EVT = 0x9ece,
  59. PEER_LOST_CONTACT_EVT = 0x91ce,
  60. NODE_FAILOVER_BEGIN_EVT = 0xfbe,
  61. NODE_FAILOVER_END_EVT = 0xfee,
  62. NODE_SYNCH_BEGIN_EVT = 0xcbe,
  63. NODE_SYNCH_END_EVT = 0xcee
  64. };
  65. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  66. struct sk_buff_head *xmitq,
  67. struct tipc_media_addr **maddr);
  68. static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
  69. bool delete);
  70. static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
  71. static void tipc_node_delete(struct tipc_node *node);
  72. static void tipc_node_timeout(unsigned long data);
  73. static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
  74. struct tipc_sock_conn {
  75. u32 port;
  76. u32 peer_port;
  77. u32 peer_node;
  78. struct list_head list;
  79. };
  80. static const struct nla_policy tipc_nl_node_policy[TIPC_NLA_NODE_MAX + 1] = {
  81. [TIPC_NLA_NODE_UNSPEC] = { .type = NLA_UNSPEC },
  82. [TIPC_NLA_NODE_ADDR] = { .type = NLA_U32 },
  83. [TIPC_NLA_NODE_UP] = { .type = NLA_FLAG }
  84. };
  85. /*
  86. * A trivial power-of-two bitmask technique is used for speed, since this
  87. * operation is done for every incoming TIPC packet. The number of hash table
  88. * entries has been chosen so that no hash chain exceeds 8 nodes and will
  89. * usually be much smaller (typically only a single node).
  90. */
  91. static unsigned int tipc_hashfn(u32 addr)
  92. {
  93. return addr & (NODE_HTABLE_SIZE - 1);
  94. }
  95. static void tipc_node_kref_release(struct kref *kref)
  96. {
  97. struct tipc_node *node = container_of(kref, struct tipc_node, kref);
  98. tipc_node_delete(node);
  99. }
  100. void tipc_node_put(struct tipc_node *node)
  101. {
  102. kref_put(&node->kref, tipc_node_kref_release);
  103. }
  104. static void tipc_node_get(struct tipc_node *node)
  105. {
  106. kref_get(&node->kref);
  107. }
  108. /*
  109. * tipc_node_find - locate specified node object, if it exists
  110. */
  111. struct tipc_node *tipc_node_find(struct net *net, u32 addr)
  112. {
  113. struct tipc_net *tn = net_generic(net, tipc_net_id);
  114. struct tipc_node *node;
  115. if (unlikely(!in_own_cluster_exact(net, addr)))
  116. return NULL;
  117. rcu_read_lock();
  118. hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)],
  119. hash) {
  120. if (node->addr == addr) {
  121. tipc_node_get(node);
  122. rcu_read_unlock();
  123. return node;
  124. }
  125. }
  126. rcu_read_unlock();
  127. return NULL;
  128. }
  129. void tipc_node_read_lock(struct tipc_node *n)
  130. {
  131. read_lock_bh(&n->lock);
  132. }
  133. void tipc_node_read_unlock(struct tipc_node *n)
  134. {
  135. read_unlock_bh(&n->lock);
  136. }
  137. static void tipc_node_write_lock(struct tipc_node *n)
  138. {
  139. write_lock_bh(&n->lock);
  140. }
  141. static void tipc_node_write_unlock(struct tipc_node *n)
  142. {
  143. struct net *net = n->net;
  144. u32 addr = 0;
  145. u32 flags = n->action_flags;
  146. u32 link_id = 0;
  147. struct list_head *publ_list;
  148. if (likely(!flags)) {
  149. write_unlock_bh(&n->lock);
  150. return;
  151. }
  152. addr = n->addr;
  153. link_id = n->link_id;
  154. publ_list = &n->publ_list;
  155. n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
  156. TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
  157. write_unlock_bh(&n->lock);
  158. if (flags & TIPC_NOTIFY_NODE_DOWN)
  159. tipc_publ_notify(net, publ_list, addr);
  160. if (flags & TIPC_NOTIFY_NODE_UP)
  161. tipc_named_node_up(net, addr);
  162. if (flags & TIPC_NOTIFY_LINK_UP)
  163. tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
  164. TIPC_NODE_SCOPE, link_id, addr);
  165. if (flags & TIPC_NOTIFY_LINK_DOWN)
  166. tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
  167. link_id, addr);
  168. }
  169. struct tipc_node *tipc_node_create(struct net *net, u32 addr, u16 capabilities)
  170. {
  171. struct tipc_net *tn = net_generic(net, tipc_net_id);
  172. struct tipc_node *n_ptr, *temp_node;
  173. int i;
  174. spin_lock_bh(&tn->node_list_lock);
  175. n_ptr = tipc_node_find(net, addr);
  176. if (n_ptr)
  177. goto exit;
  178. n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
  179. if (!n_ptr) {
  180. pr_warn("Node creation failed, no memory\n");
  181. goto exit;
  182. }
  183. n_ptr->addr = addr;
  184. n_ptr->net = net;
  185. n_ptr->capabilities = capabilities;
  186. kref_init(&n_ptr->kref);
  187. rwlock_init(&n_ptr->lock);
  188. INIT_HLIST_NODE(&n_ptr->hash);
  189. INIT_LIST_HEAD(&n_ptr->list);
  190. INIT_LIST_HEAD(&n_ptr->publ_list);
  191. INIT_LIST_HEAD(&n_ptr->conn_sks);
  192. skb_queue_head_init(&n_ptr->bc_entry.namedq);
  193. skb_queue_head_init(&n_ptr->bc_entry.inputq1);
  194. __skb_queue_head_init(&n_ptr->bc_entry.arrvq);
  195. skb_queue_head_init(&n_ptr->bc_entry.inputq2);
  196. for (i = 0; i < MAX_BEARERS; i++)
  197. spin_lock_init(&n_ptr->links[i].lock);
  198. hlist_add_head_rcu(&n_ptr->hash, &tn->node_htable[tipc_hashfn(addr)]);
  199. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  200. if (n_ptr->addr < temp_node->addr)
  201. break;
  202. }
  203. list_add_tail_rcu(&n_ptr->list, &temp_node->list);
  204. n_ptr->state = SELF_DOWN_PEER_LEAVING;
  205. n_ptr->signature = INVALID_NODE_SIG;
  206. n_ptr->active_links[0] = INVALID_BEARER_ID;
  207. n_ptr->active_links[1] = INVALID_BEARER_ID;
  208. if (!tipc_link_bc_create(net, tipc_own_addr(net), n_ptr->addr,
  209. U16_MAX, tipc_bc_sndlink(net)->window,
  210. n_ptr->capabilities,
  211. &n_ptr->bc_entry.inputq1,
  212. &n_ptr->bc_entry.namedq,
  213. tipc_bc_sndlink(net),
  214. &n_ptr->bc_entry.link)) {
  215. pr_warn("Broadcast rcv link creation failed, no memory\n");
  216. kfree(n_ptr);
  217. n_ptr = NULL;
  218. goto exit;
  219. }
  220. tipc_node_get(n_ptr);
  221. setup_timer(&n_ptr->timer, tipc_node_timeout, (unsigned long)n_ptr);
  222. n_ptr->keepalive_intv = U32_MAX;
  223. exit:
  224. spin_unlock_bh(&tn->node_list_lock);
  225. return n_ptr;
  226. }
  227. static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
  228. {
  229. unsigned long tol = l->tolerance;
  230. unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
  231. unsigned long keepalive_intv = msecs_to_jiffies(intv);
  232. /* Link with lowest tolerance determines timer interval */
  233. if (keepalive_intv < n->keepalive_intv)
  234. n->keepalive_intv = keepalive_intv;
  235. /* Ensure link's abort limit corresponds to current interval */
  236. l->abort_limit = l->tolerance / jiffies_to_msecs(n->keepalive_intv);
  237. }
  238. static void tipc_node_delete(struct tipc_node *node)
  239. {
  240. list_del_rcu(&node->list);
  241. hlist_del_rcu(&node->hash);
  242. kfree(node->bc_entry.link);
  243. kfree_rcu(node, rcu);
  244. }
  245. void tipc_node_stop(struct net *net)
  246. {
  247. struct tipc_net *tn = net_generic(net, tipc_net_id);
  248. struct tipc_node *node, *t_node;
  249. spin_lock_bh(&tn->node_list_lock);
  250. list_for_each_entry_safe(node, t_node, &tn->node_list, list) {
  251. if (del_timer(&node->timer))
  252. tipc_node_put(node);
  253. tipc_node_put(node);
  254. }
  255. spin_unlock_bh(&tn->node_list_lock);
  256. }
  257. void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
  258. {
  259. struct tipc_node *n;
  260. if (in_own_node(net, addr))
  261. return;
  262. n = tipc_node_find(net, addr);
  263. if (!n) {
  264. pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
  265. return;
  266. }
  267. tipc_node_write_lock(n);
  268. list_add_tail(subscr, &n->publ_list);
  269. tipc_node_write_unlock(n);
  270. tipc_node_put(n);
  271. }
  272. void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
  273. {
  274. struct tipc_node *n;
  275. if (in_own_node(net, addr))
  276. return;
  277. n = tipc_node_find(net, addr);
  278. if (!n) {
  279. pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
  280. return;
  281. }
  282. tipc_node_write_lock(n);
  283. list_del_init(subscr);
  284. tipc_node_write_unlock(n);
  285. tipc_node_put(n);
  286. }
  287. int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
  288. {
  289. struct tipc_node *node;
  290. struct tipc_sock_conn *conn;
  291. int err = 0;
  292. if (in_own_node(net, dnode))
  293. return 0;
  294. node = tipc_node_find(net, dnode);
  295. if (!node) {
  296. pr_warn("Connecting sock to node 0x%x failed\n", dnode);
  297. return -EHOSTUNREACH;
  298. }
  299. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  300. if (!conn) {
  301. err = -EHOSTUNREACH;
  302. goto exit;
  303. }
  304. conn->peer_node = dnode;
  305. conn->port = port;
  306. conn->peer_port = peer_port;
  307. tipc_node_write_lock(node);
  308. list_add_tail(&conn->list, &node->conn_sks);
  309. tipc_node_write_unlock(node);
  310. exit:
  311. tipc_node_put(node);
  312. return err;
  313. }
  314. void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
  315. {
  316. struct tipc_node *node;
  317. struct tipc_sock_conn *conn, *safe;
  318. if (in_own_node(net, dnode))
  319. return;
  320. node = tipc_node_find(net, dnode);
  321. if (!node)
  322. return;
  323. tipc_node_write_lock(node);
  324. list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
  325. if (port != conn->port)
  326. continue;
  327. list_del(&conn->list);
  328. kfree(conn);
  329. }
  330. tipc_node_write_unlock(node);
  331. tipc_node_put(node);
  332. }
  333. /* tipc_node_timeout - handle expiration of node timer
  334. */
  335. static void tipc_node_timeout(unsigned long data)
  336. {
  337. struct tipc_node *n = (struct tipc_node *)data;
  338. struct tipc_link_entry *le;
  339. struct sk_buff_head xmitq;
  340. int bearer_id;
  341. int rc = 0;
  342. __skb_queue_head_init(&xmitq);
  343. for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
  344. tipc_node_read_lock(n);
  345. le = &n->links[bearer_id];
  346. spin_lock_bh(&le->lock);
  347. if (le->link) {
  348. /* Link tolerance may change asynchronously: */
  349. tipc_node_calculate_timer(n, le->link);
  350. rc = tipc_link_timeout(le->link, &xmitq);
  351. }
  352. spin_unlock_bh(&le->lock);
  353. tipc_node_read_unlock(n);
  354. tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
  355. if (rc & TIPC_LINK_DOWN_EVT)
  356. tipc_node_link_down(n, bearer_id, false);
  357. }
  358. if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
  359. tipc_node_get(n);
  360. tipc_node_put(n);
  361. }
  362. /**
  363. * __tipc_node_link_up - handle addition of link
  364. * Node lock must be held by caller
  365. * Link becomes active (alone or shared) or standby, depending on its priority.
  366. */
  367. static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
  368. struct sk_buff_head *xmitq)
  369. {
  370. int *slot0 = &n->active_links[0];
  371. int *slot1 = &n->active_links[1];
  372. struct tipc_link *ol = node_active_link(n, 0);
  373. struct tipc_link *nl = n->links[bearer_id].link;
  374. if (!nl)
  375. return;
  376. tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
  377. if (!tipc_link_is_up(nl))
  378. return;
  379. n->working_links++;
  380. n->action_flags |= TIPC_NOTIFY_LINK_UP;
  381. n->link_id = nl->peer_bearer_id << 16 | bearer_id;
  382. /* Leave room for tunnel header when returning 'mtu' to users: */
  383. n->links[bearer_id].mtu = nl->mtu - INT_H_SIZE;
  384. tipc_bearer_add_dest(n->net, bearer_id, n->addr);
  385. tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
  386. pr_debug("Established link <%s> on network plane %c\n",
  387. nl->name, nl->net_plane);
  388. /* First link? => give it both slots */
  389. if (!ol) {
  390. *slot0 = bearer_id;
  391. *slot1 = bearer_id;
  392. tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
  393. n->action_flags |= TIPC_NOTIFY_NODE_UP;
  394. tipc_bcast_add_peer(n->net, nl, xmitq);
  395. return;
  396. }
  397. /* Second link => redistribute slots */
  398. if (nl->priority > ol->priority) {
  399. pr_debug("Old link <%s> becomes standby\n", ol->name);
  400. *slot0 = bearer_id;
  401. *slot1 = bearer_id;
  402. tipc_link_set_active(nl, true);
  403. tipc_link_set_active(ol, false);
  404. } else if (nl->priority == ol->priority) {
  405. tipc_link_set_active(nl, true);
  406. *slot1 = bearer_id;
  407. } else {
  408. pr_debug("New link <%s> is standby\n", nl->name);
  409. }
  410. /* Prepare synchronization with first link */
  411. tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
  412. }
  413. /**
  414. * tipc_node_link_up - handle addition of link
  415. *
  416. * Link becomes active (alone or shared) or standby, depending on its priority.
  417. */
  418. static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
  419. struct sk_buff_head *xmitq)
  420. {
  421. tipc_node_write_lock(n);
  422. __tipc_node_link_up(n, bearer_id, xmitq);
  423. tipc_node_write_unlock(n);
  424. }
  425. /**
  426. * __tipc_node_link_down - handle loss of link
  427. */
  428. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  429. struct sk_buff_head *xmitq,
  430. struct tipc_media_addr **maddr)
  431. {
  432. struct tipc_link_entry *le = &n->links[*bearer_id];
  433. int *slot0 = &n->active_links[0];
  434. int *slot1 = &n->active_links[1];
  435. int i, highest = 0;
  436. struct tipc_link *l, *_l, *tnl;
  437. l = n->links[*bearer_id].link;
  438. if (!l || tipc_link_is_reset(l))
  439. return;
  440. n->working_links--;
  441. n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
  442. n->link_id = l->peer_bearer_id << 16 | *bearer_id;
  443. tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
  444. pr_debug("Lost link <%s> on network plane %c\n",
  445. l->name, l->net_plane);
  446. /* Select new active link if any available */
  447. *slot0 = INVALID_BEARER_ID;
  448. *slot1 = INVALID_BEARER_ID;
  449. for (i = 0; i < MAX_BEARERS; i++) {
  450. _l = n->links[i].link;
  451. if (!_l || !tipc_link_is_up(_l))
  452. continue;
  453. if (_l == l)
  454. continue;
  455. if (_l->priority < highest)
  456. continue;
  457. if (_l->priority > highest) {
  458. highest = _l->priority;
  459. *slot0 = i;
  460. *slot1 = i;
  461. continue;
  462. }
  463. *slot1 = i;
  464. }
  465. if (!tipc_node_is_up(n)) {
  466. if (tipc_link_peer_is_down(l))
  467. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  468. tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
  469. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  470. tipc_link_reset(l);
  471. tipc_link_build_reset_msg(l, xmitq);
  472. *maddr = &n->links[*bearer_id].maddr;
  473. node_lost_contact(n, &le->inputq);
  474. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  475. return;
  476. }
  477. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  478. /* There is still a working link => initiate failover */
  479. tnl = node_active_link(n, 0);
  480. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  481. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  482. n->sync_point = tnl->rcv_nxt + (U16_MAX / 2 - 1);
  483. tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
  484. tipc_link_reset(l);
  485. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  486. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  487. tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
  488. *maddr = &n->links[tnl->bearer_id].maddr;
  489. *bearer_id = tnl->bearer_id;
  490. }
  491. static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
  492. {
  493. struct tipc_link_entry *le = &n->links[bearer_id];
  494. struct tipc_link *l = le->link;
  495. struct tipc_media_addr *maddr;
  496. struct sk_buff_head xmitq;
  497. if (!l)
  498. return;
  499. __skb_queue_head_init(&xmitq);
  500. tipc_node_write_lock(n);
  501. if (!tipc_link_is_establishing(l)) {
  502. __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
  503. if (delete) {
  504. kfree(l);
  505. le->link = NULL;
  506. n->link_cnt--;
  507. }
  508. } else {
  509. /* Defuse pending tipc_node_link_up() */
  510. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  511. }
  512. tipc_node_write_unlock(n);
  513. tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
  514. tipc_sk_rcv(n->net, &le->inputq);
  515. }
  516. bool tipc_node_is_up(struct tipc_node *n)
  517. {
  518. return n->active_links[0] != INVALID_BEARER_ID;
  519. }
  520. void tipc_node_check_dest(struct net *net, u32 onode,
  521. struct tipc_bearer *b,
  522. u16 capabilities, u32 signature,
  523. struct tipc_media_addr *maddr,
  524. bool *respond, bool *dupl_addr)
  525. {
  526. struct tipc_node *n;
  527. struct tipc_link *l;
  528. struct tipc_link_entry *le;
  529. bool addr_match = false;
  530. bool sign_match = false;
  531. bool link_up = false;
  532. bool accept_addr = false;
  533. bool reset = true;
  534. char *if_name;
  535. *dupl_addr = false;
  536. *respond = false;
  537. n = tipc_node_create(net, onode, capabilities);
  538. if (!n)
  539. return;
  540. tipc_node_write_lock(n);
  541. le = &n->links[b->identity];
  542. /* Prepare to validate requesting node's signature and media address */
  543. l = le->link;
  544. link_up = l && tipc_link_is_up(l);
  545. addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
  546. sign_match = (signature == n->signature);
  547. /* These three flags give us eight permutations: */
  548. if (sign_match && addr_match && link_up) {
  549. /* All is fine. Do nothing. */
  550. reset = false;
  551. } else if (sign_match && addr_match && !link_up) {
  552. /* Respond. The link will come up in due time */
  553. *respond = true;
  554. } else if (sign_match && !addr_match && link_up) {
  555. /* Peer has changed i/f address without rebooting.
  556. * If so, the link will reset soon, and the next
  557. * discovery will be accepted. So we can ignore it.
  558. * It may also be an cloned or malicious peer having
  559. * chosen the same node address and signature as an
  560. * existing one.
  561. * Ignore requests until the link goes down, if ever.
  562. */
  563. *dupl_addr = true;
  564. } else if (sign_match && !addr_match && !link_up) {
  565. /* Peer link has changed i/f address without rebooting.
  566. * It may also be a cloned or malicious peer; we can't
  567. * distinguish between the two.
  568. * The signature is correct, so we must accept.
  569. */
  570. accept_addr = true;
  571. *respond = true;
  572. } else if (!sign_match && addr_match && link_up) {
  573. /* Peer node rebooted. Two possibilities:
  574. * - Delayed re-discovery; this link endpoint has already
  575. * reset and re-established contact with the peer, before
  576. * receiving a discovery message from that node.
  577. * (The peer happened to receive one from this node first).
  578. * - The peer came back so fast that our side has not
  579. * discovered it yet. Probing from this side will soon
  580. * reset the link, since there can be no working link
  581. * endpoint at the peer end, and the link will re-establish.
  582. * Accept the signature, since it comes from a known peer.
  583. */
  584. n->signature = signature;
  585. } else if (!sign_match && addr_match && !link_up) {
  586. /* The peer node has rebooted.
  587. * Accept signature, since it is a known peer.
  588. */
  589. n->signature = signature;
  590. *respond = true;
  591. } else if (!sign_match && !addr_match && link_up) {
  592. /* Peer rebooted with new address, or a new/duplicate peer.
  593. * Ignore until the link goes down, if ever.
  594. */
  595. *dupl_addr = true;
  596. } else if (!sign_match && !addr_match && !link_up) {
  597. /* Peer rebooted with new address, or it is a new peer.
  598. * Accept signature and address.
  599. */
  600. n->signature = signature;
  601. accept_addr = true;
  602. *respond = true;
  603. }
  604. if (!accept_addr)
  605. goto exit;
  606. /* Now create new link if not already existing */
  607. if (!l) {
  608. if (n->link_cnt == 2) {
  609. pr_warn("Cannot establish 3rd link to %x\n", n->addr);
  610. goto exit;
  611. }
  612. if_name = strchr(b->name, ':') + 1;
  613. if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
  614. b->net_plane, b->mtu, b->priority,
  615. b->window, mod(tipc_net(net)->random),
  616. tipc_own_addr(net), onode,
  617. n->capabilities,
  618. tipc_bc_sndlink(n->net), n->bc_entry.link,
  619. &le->inputq,
  620. &n->bc_entry.namedq, &l)) {
  621. *respond = false;
  622. goto exit;
  623. }
  624. tipc_link_reset(l);
  625. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  626. if (n->state == NODE_FAILINGOVER)
  627. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  628. le->link = l;
  629. n->link_cnt++;
  630. tipc_node_calculate_timer(n, l);
  631. if (n->link_cnt == 1)
  632. if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
  633. tipc_node_get(n);
  634. }
  635. memcpy(&le->maddr, maddr, sizeof(*maddr));
  636. exit:
  637. tipc_node_write_unlock(n);
  638. if (reset && !tipc_link_is_reset(l))
  639. tipc_node_link_down(n, b->identity, false);
  640. tipc_node_put(n);
  641. }
  642. void tipc_node_delete_links(struct net *net, int bearer_id)
  643. {
  644. struct tipc_net *tn = net_generic(net, tipc_net_id);
  645. struct tipc_node *n;
  646. rcu_read_lock();
  647. list_for_each_entry_rcu(n, &tn->node_list, list) {
  648. tipc_node_link_down(n, bearer_id, true);
  649. }
  650. rcu_read_unlock();
  651. }
  652. static void tipc_node_reset_links(struct tipc_node *n)
  653. {
  654. char addr_string[16];
  655. int i;
  656. pr_warn("Resetting all links to %s\n",
  657. tipc_addr_string_fill(addr_string, n->addr));
  658. for (i = 0; i < MAX_BEARERS; i++) {
  659. tipc_node_link_down(n, i, false);
  660. }
  661. }
  662. /* tipc_node_fsm_evt - node finite state machine
  663. * Determines when contact is allowed with peer node
  664. */
  665. static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
  666. {
  667. int state = n->state;
  668. switch (state) {
  669. case SELF_DOWN_PEER_DOWN:
  670. switch (evt) {
  671. case SELF_ESTABL_CONTACT_EVT:
  672. state = SELF_UP_PEER_COMING;
  673. break;
  674. case PEER_ESTABL_CONTACT_EVT:
  675. state = SELF_COMING_PEER_UP;
  676. break;
  677. case SELF_LOST_CONTACT_EVT:
  678. case PEER_LOST_CONTACT_EVT:
  679. break;
  680. case NODE_SYNCH_END_EVT:
  681. case NODE_SYNCH_BEGIN_EVT:
  682. case NODE_FAILOVER_BEGIN_EVT:
  683. case NODE_FAILOVER_END_EVT:
  684. default:
  685. goto illegal_evt;
  686. }
  687. break;
  688. case SELF_UP_PEER_UP:
  689. switch (evt) {
  690. case SELF_LOST_CONTACT_EVT:
  691. state = SELF_DOWN_PEER_LEAVING;
  692. break;
  693. case PEER_LOST_CONTACT_EVT:
  694. state = SELF_LEAVING_PEER_DOWN;
  695. break;
  696. case NODE_SYNCH_BEGIN_EVT:
  697. state = NODE_SYNCHING;
  698. break;
  699. case NODE_FAILOVER_BEGIN_EVT:
  700. state = NODE_FAILINGOVER;
  701. break;
  702. case SELF_ESTABL_CONTACT_EVT:
  703. case PEER_ESTABL_CONTACT_EVT:
  704. case NODE_SYNCH_END_EVT:
  705. case NODE_FAILOVER_END_EVT:
  706. break;
  707. default:
  708. goto illegal_evt;
  709. }
  710. break;
  711. case SELF_DOWN_PEER_LEAVING:
  712. switch (evt) {
  713. case PEER_LOST_CONTACT_EVT:
  714. state = SELF_DOWN_PEER_DOWN;
  715. break;
  716. case SELF_ESTABL_CONTACT_EVT:
  717. case PEER_ESTABL_CONTACT_EVT:
  718. case SELF_LOST_CONTACT_EVT:
  719. break;
  720. case NODE_SYNCH_END_EVT:
  721. case NODE_SYNCH_BEGIN_EVT:
  722. case NODE_FAILOVER_BEGIN_EVT:
  723. case NODE_FAILOVER_END_EVT:
  724. default:
  725. goto illegal_evt;
  726. }
  727. break;
  728. case SELF_UP_PEER_COMING:
  729. switch (evt) {
  730. case PEER_ESTABL_CONTACT_EVT:
  731. state = SELF_UP_PEER_UP;
  732. break;
  733. case SELF_LOST_CONTACT_EVT:
  734. state = SELF_DOWN_PEER_LEAVING;
  735. break;
  736. case SELF_ESTABL_CONTACT_EVT:
  737. case PEER_LOST_CONTACT_EVT:
  738. case NODE_SYNCH_END_EVT:
  739. case NODE_FAILOVER_BEGIN_EVT:
  740. break;
  741. case NODE_SYNCH_BEGIN_EVT:
  742. case NODE_FAILOVER_END_EVT:
  743. default:
  744. goto illegal_evt;
  745. }
  746. break;
  747. case SELF_COMING_PEER_UP:
  748. switch (evt) {
  749. case SELF_ESTABL_CONTACT_EVT:
  750. state = SELF_UP_PEER_UP;
  751. break;
  752. case PEER_LOST_CONTACT_EVT:
  753. state = SELF_LEAVING_PEER_DOWN;
  754. break;
  755. case SELF_LOST_CONTACT_EVT:
  756. case PEER_ESTABL_CONTACT_EVT:
  757. break;
  758. case NODE_SYNCH_END_EVT:
  759. case NODE_SYNCH_BEGIN_EVT:
  760. case NODE_FAILOVER_BEGIN_EVT:
  761. case NODE_FAILOVER_END_EVT:
  762. default:
  763. goto illegal_evt;
  764. }
  765. break;
  766. case SELF_LEAVING_PEER_DOWN:
  767. switch (evt) {
  768. case SELF_LOST_CONTACT_EVT:
  769. state = SELF_DOWN_PEER_DOWN;
  770. break;
  771. case SELF_ESTABL_CONTACT_EVT:
  772. case PEER_ESTABL_CONTACT_EVT:
  773. case PEER_LOST_CONTACT_EVT:
  774. break;
  775. case NODE_SYNCH_END_EVT:
  776. case NODE_SYNCH_BEGIN_EVT:
  777. case NODE_FAILOVER_BEGIN_EVT:
  778. case NODE_FAILOVER_END_EVT:
  779. default:
  780. goto illegal_evt;
  781. }
  782. break;
  783. case NODE_FAILINGOVER:
  784. switch (evt) {
  785. case SELF_LOST_CONTACT_EVT:
  786. state = SELF_DOWN_PEER_LEAVING;
  787. break;
  788. case PEER_LOST_CONTACT_EVT:
  789. state = SELF_LEAVING_PEER_DOWN;
  790. break;
  791. case NODE_FAILOVER_END_EVT:
  792. state = SELF_UP_PEER_UP;
  793. break;
  794. case NODE_FAILOVER_BEGIN_EVT:
  795. case SELF_ESTABL_CONTACT_EVT:
  796. case PEER_ESTABL_CONTACT_EVT:
  797. break;
  798. case NODE_SYNCH_BEGIN_EVT:
  799. case NODE_SYNCH_END_EVT:
  800. default:
  801. goto illegal_evt;
  802. }
  803. break;
  804. case NODE_SYNCHING:
  805. switch (evt) {
  806. case SELF_LOST_CONTACT_EVT:
  807. state = SELF_DOWN_PEER_LEAVING;
  808. break;
  809. case PEER_LOST_CONTACT_EVT:
  810. state = SELF_LEAVING_PEER_DOWN;
  811. break;
  812. case NODE_SYNCH_END_EVT:
  813. state = SELF_UP_PEER_UP;
  814. break;
  815. case NODE_FAILOVER_BEGIN_EVT:
  816. state = NODE_FAILINGOVER;
  817. break;
  818. case NODE_SYNCH_BEGIN_EVT:
  819. case SELF_ESTABL_CONTACT_EVT:
  820. case PEER_ESTABL_CONTACT_EVT:
  821. break;
  822. case NODE_FAILOVER_END_EVT:
  823. default:
  824. goto illegal_evt;
  825. }
  826. break;
  827. default:
  828. pr_err("Unknown node fsm state %x\n", state);
  829. break;
  830. }
  831. n->state = state;
  832. return;
  833. illegal_evt:
  834. pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
  835. }
  836. static void node_lost_contact(struct tipc_node *n,
  837. struct sk_buff_head *inputq)
  838. {
  839. char addr_string[16];
  840. struct tipc_sock_conn *conn, *safe;
  841. struct tipc_link *l;
  842. struct list_head *conns = &n->conn_sks;
  843. struct sk_buff *skb;
  844. uint i;
  845. pr_debug("Lost contact with %s\n",
  846. tipc_addr_string_fill(addr_string, n->addr));
  847. /* Clean up broadcast state */
  848. tipc_bcast_remove_peer(n->net, n->bc_entry.link);
  849. /* Abort any ongoing link failover */
  850. for (i = 0; i < MAX_BEARERS; i++) {
  851. l = n->links[i].link;
  852. if (l)
  853. tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
  854. }
  855. /* Notify publications from this node */
  856. n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
  857. /* Notify sockets connected to node */
  858. list_for_each_entry_safe(conn, safe, conns, list) {
  859. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
  860. SHORT_H_SIZE, 0, tipc_own_addr(n->net),
  861. conn->peer_node, conn->port,
  862. conn->peer_port, TIPC_ERR_NO_NODE);
  863. if (likely(skb))
  864. skb_queue_tail(inputq, skb);
  865. list_del(&conn->list);
  866. kfree(conn);
  867. }
  868. }
  869. /**
  870. * tipc_node_get_linkname - get the name of a link
  871. *
  872. * @bearer_id: id of the bearer
  873. * @node: peer node address
  874. * @linkname: link name output buffer
  875. *
  876. * Returns 0 on success
  877. */
  878. int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
  879. char *linkname, size_t len)
  880. {
  881. struct tipc_link *link;
  882. int err = -EINVAL;
  883. struct tipc_node *node = tipc_node_find(net, addr);
  884. if (!node)
  885. return err;
  886. if (bearer_id >= MAX_BEARERS)
  887. goto exit;
  888. tipc_node_read_lock(node);
  889. link = node->links[bearer_id].link;
  890. if (link) {
  891. strncpy(linkname, link->name, len);
  892. err = 0;
  893. }
  894. exit:
  895. tipc_node_read_unlock(node);
  896. tipc_node_put(node);
  897. return err;
  898. }
  899. /* Caller should hold node lock for the passed node */
  900. static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
  901. {
  902. void *hdr;
  903. struct nlattr *attrs;
  904. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  905. NLM_F_MULTI, TIPC_NL_NODE_GET);
  906. if (!hdr)
  907. return -EMSGSIZE;
  908. attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
  909. if (!attrs)
  910. goto msg_full;
  911. if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
  912. goto attr_msg_full;
  913. if (tipc_node_is_up(node))
  914. if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
  915. goto attr_msg_full;
  916. nla_nest_end(msg->skb, attrs);
  917. genlmsg_end(msg->skb, hdr);
  918. return 0;
  919. attr_msg_full:
  920. nla_nest_cancel(msg->skb, attrs);
  921. msg_full:
  922. genlmsg_cancel(msg->skb, hdr);
  923. return -EMSGSIZE;
  924. }
  925. /**
  926. * tipc_node_xmit() is the general link level function for message sending
  927. * @net: the applicable net namespace
  928. * @list: chain of buffers containing message
  929. * @dnode: address of destination node
  930. * @selector: a number used for deterministic link selection
  931. * Consumes the buffer chain, except when returning -ELINKCONG
  932. * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
  933. */
  934. int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
  935. u32 dnode, int selector)
  936. {
  937. struct tipc_link_entry *le = NULL;
  938. struct tipc_node *n;
  939. struct sk_buff_head xmitq;
  940. int bearer_id = -1;
  941. int rc = -EHOSTUNREACH;
  942. __skb_queue_head_init(&xmitq);
  943. n = tipc_node_find(net, dnode);
  944. if (likely(n)) {
  945. tipc_node_read_lock(n);
  946. bearer_id = n->active_links[selector & 1];
  947. if (bearer_id >= 0) {
  948. le = &n->links[bearer_id];
  949. spin_lock_bh(&le->lock);
  950. rc = tipc_link_xmit(le->link, list, &xmitq);
  951. spin_unlock_bh(&le->lock);
  952. }
  953. tipc_node_read_unlock(n);
  954. if (likely(!skb_queue_empty(&xmitq))) {
  955. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  956. return 0;
  957. }
  958. if (unlikely(rc == -ENOBUFS))
  959. tipc_node_link_down(n, bearer_id, false);
  960. tipc_node_put(n);
  961. return rc;
  962. }
  963. if (unlikely(!in_own_node(net, dnode)))
  964. return rc;
  965. tipc_sk_rcv(net, list);
  966. return 0;
  967. }
  968. /* tipc_node_xmit_skb(): send single buffer to destination
  969. * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
  970. * messages, which will not be rejected
  971. * The only exception is datagram messages rerouted after secondary
  972. * lookup, which are rare and safe to dispose of anyway.
  973. * TODO: Return real return value, and let callers use
  974. * tipc_wait_for_sendpkt() where applicable
  975. */
  976. int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  977. u32 selector)
  978. {
  979. struct sk_buff_head head;
  980. int rc;
  981. skb_queue_head_init(&head);
  982. __skb_queue_tail(&head, skb);
  983. rc = tipc_node_xmit(net, &head, dnode, selector);
  984. if (rc == -ELINKCONG)
  985. kfree_skb(skb);
  986. return 0;
  987. }
  988. void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
  989. {
  990. struct sk_buff *txskb;
  991. struct tipc_node *n;
  992. u32 dst;
  993. rcu_read_lock();
  994. list_for_each_entry_rcu(n, tipc_nodes(net), list) {
  995. dst = n->addr;
  996. if (in_own_node(net, dst))
  997. continue;
  998. if (!tipc_node_is_up(n))
  999. continue;
  1000. txskb = pskb_copy(skb, GFP_ATOMIC);
  1001. if (!txskb)
  1002. break;
  1003. msg_set_destnode(buf_msg(txskb), dst);
  1004. tipc_node_xmit_skb(net, txskb, dst, 0);
  1005. }
  1006. rcu_read_unlock();
  1007. kfree_skb(skb);
  1008. }
  1009. /**
  1010. * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
  1011. * @net: the applicable net namespace
  1012. * @skb: TIPC packet
  1013. * @bearer_id: id of bearer message arrived on
  1014. *
  1015. * Invoked with no locks held.
  1016. */
  1017. static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
  1018. {
  1019. int rc;
  1020. struct sk_buff_head xmitq;
  1021. struct tipc_bclink_entry *be;
  1022. struct tipc_link_entry *le;
  1023. struct tipc_msg *hdr = buf_msg(skb);
  1024. int usr = msg_user(hdr);
  1025. u32 dnode = msg_destnode(hdr);
  1026. struct tipc_node *n;
  1027. __skb_queue_head_init(&xmitq);
  1028. /* If NACK for other node, let rcv link for that node peek into it */
  1029. if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
  1030. n = tipc_node_find(net, dnode);
  1031. else
  1032. n = tipc_node_find(net, msg_prevnode(hdr));
  1033. if (!n) {
  1034. kfree_skb(skb);
  1035. return;
  1036. }
  1037. be = &n->bc_entry;
  1038. le = &n->links[bearer_id];
  1039. rc = tipc_bcast_rcv(net, be->link, skb);
  1040. /* Broadcast link reset may happen at reassembly failure */
  1041. if (rc & TIPC_LINK_DOWN_EVT)
  1042. tipc_node_reset_links(n);
  1043. /* Broadcast ACKs are sent on a unicast link */
  1044. if (rc & TIPC_LINK_SND_BC_ACK) {
  1045. tipc_node_read_lock(n);
  1046. tipc_link_build_ack_msg(le->link, &xmitq);
  1047. tipc_node_read_unlock(n);
  1048. }
  1049. if (!skb_queue_empty(&xmitq))
  1050. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1051. /* Deliver. 'arrvq' is under inputq2's lock protection */
  1052. if (!skb_queue_empty(&be->inputq1)) {
  1053. spin_lock_bh(&be->inputq2.lock);
  1054. spin_lock_bh(&be->inputq1.lock);
  1055. skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
  1056. spin_unlock_bh(&be->inputq1.lock);
  1057. spin_unlock_bh(&be->inputq2.lock);
  1058. tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
  1059. }
  1060. tipc_node_put(n);
  1061. }
  1062. /**
  1063. * tipc_node_check_state - check and if necessary update node state
  1064. * @skb: TIPC packet
  1065. * @bearer_id: identity of bearer delivering the packet
  1066. * Returns true if state is ok, otherwise consumes buffer and returns false
  1067. */
  1068. static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
  1069. int bearer_id, struct sk_buff_head *xmitq)
  1070. {
  1071. struct tipc_msg *hdr = buf_msg(skb);
  1072. int usr = msg_user(hdr);
  1073. int mtyp = msg_type(hdr);
  1074. u16 oseqno = msg_seqno(hdr);
  1075. u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
  1076. u16 exp_pkts = msg_msgcnt(hdr);
  1077. u16 rcv_nxt, syncpt, dlv_nxt;
  1078. int state = n->state;
  1079. struct tipc_link *l, *tnl, *pl = NULL;
  1080. struct tipc_media_addr *maddr;
  1081. int i, pb_id;
  1082. l = n->links[bearer_id].link;
  1083. if (!l)
  1084. return false;
  1085. rcv_nxt = l->rcv_nxt;
  1086. if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
  1087. return true;
  1088. /* Find parallel link, if any */
  1089. for (i = 0; i < MAX_BEARERS; i++) {
  1090. if ((i != bearer_id) && n->links[i].link) {
  1091. pl = n->links[i].link;
  1092. break;
  1093. }
  1094. }
  1095. /* Check and update node accesibility if applicable */
  1096. if (state == SELF_UP_PEER_COMING) {
  1097. if (!tipc_link_is_up(l))
  1098. return true;
  1099. if (!msg_peer_link_is_up(hdr))
  1100. return true;
  1101. tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
  1102. }
  1103. if (state == SELF_DOWN_PEER_LEAVING) {
  1104. if (msg_peer_node_is_up(hdr))
  1105. return false;
  1106. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  1107. return true;
  1108. }
  1109. if (state == SELF_LEAVING_PEER_DOWN)
  1110. return false;
  1111. /* Ignore duplicate packets */
  1112. if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
  1113. return true;
  1114. /* Initiate or update failover mode if applicable */
  1115. if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
  1116. syncpt = oseqno + exp_pkts - 1;
  1117. if (pl && tipc_link_is_up(pl)) {
  1118. pb_id = pl->bearer_id;
  1119. __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
  1120. tipc_skb_queue_splice_tail_init(pl->inputq, l->inputq);
  1121. }
  1122. /* If pkts arrive out of order, use lowest calculated syncpt */
  1123. if (less(syncpt, n->sync_point))
  1124. n->sync_point = syncpt;
  1125. }
  1126. /* Open parallel link when tunnel link reaches synch point */
  1127. if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
  1128. if (!more(rcv_nxt, n->sync_point))
  1129. return true;
  1130. tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
  1131. if (pl)
  1132. tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
  1133. return true;
  1134. }
  1135. /* No synching needed if only one link */
  1136. if (!pl || !tipc_link_is_up(pl))
  1137. return true;
  1138. /* Initiate synch mode if applicable */
  1139. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
  1140. syncpt = iseqno + exp_pkts - 1;
  1141. if (!tipc_link_is_up(l)) {
  1142. tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1143. __tipc_node_link_up(n, bearer_id, xmitq);
  1144. }
  1145. if (n->state == SELF_UP_PEER_UP) {
  1146. n->sync_point = syncpt;
  1147. tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
  1148. tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
  1149. }
  1150. }
  1151. /* Open tunnel link when parallel link reaches synch point */
  1152. if (n->state == NODE_SYNCHING) {
  1153. if (tipc_link_is_synching(l)) {
  1154. tnl = l;
  1155. } else {
  1156. tnl = pl;
  1157. pl = l;
  1158. }
  1159. dlv_nxt = pl->rcv_nxt - mod(skb_queue_len(pl->inputq));
  1160. if (more(dlv_nxt, n->sync_point)) {
  1161. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  1162. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  1163. return true;
  1164. }
  1165. if (l == pl)
  1166. return true;
  1167. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
  1168. return true;
  1169. if (usr == LINK_PROTOCOL)
  1170. return true;
  1171. return false;
  1172. }
  1173. return true;
  1174. }
  1175. /**
  1176. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1177. * @net: the applicable net namespace
  1178. * @skb: TIPC packet
  1179. * @bearer: pointer to bearer message arrived on
  1180. *
  1181. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1182. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1183. */
  1184. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
  1185. {
  1186. struct sk_buff_head xmitq;
  1187. struct tipc_node *n;
  1188. struct tipc_msg *hdr = buf_msg(skb);
  1189. int usr = msg_user(hdr);
  1190. int bearer_id = b->identity;
  1191. struct tipc_link_entry *le;
  1192. u16 bc_ack = msg_bcast_ack(hdr);
  1193. int rc = 0;
  1194. __skb_queue_head_init(&xmitq);
  1195. /* Ensure message is well-formed */
  1196. if (unlikely(!tipc_msg_validate(skb)))
  1197. goto discard;
  1198. /* Handle arrival of discovery or broadcast packet */
  1199. if (unlikely(msg_non_seq(hdr))) {
  1200. if (unlikely(usr == LINK_CONFIG))
  1201. return tipc_disc_rcv(net, skb, b);
  1202. else
  1203. return tipc_node_bc_rcv(net, skb, bearer_id);
  1204. }
  1205. /* Locate neighboring node that sent packet */
  1206. n = tipc_node_find(net, msg_prevnode(hdr));
  1207. if (unlikely(!n))
  1208. goto discard;
  1209. le = &n->links[bearer_id];
  1210. /* Ensure broadcast reception is in synch with peer's send state */
  1211. if (unlikely(usr == LINK_PROTOCOL))
  1212. tipc_bcast_sync_rcv(net, n->bc_entry.link, hdr);
  1213. else if (unlikely(n->bc_entry.link->acked != bc_ack))
  1214. tipc_bcast_ack_rcv(net, n->bc_entry.link, bc_ack);
  1215. /* Receive packet directly if conditions permit */
  1216. tipc_node_read_lock(n);
  1217. if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
  1218. spin_lock_bh(&le->lock);
  1219. if (le->link) {
  1220. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1221. skb = NULL;
  1222. }
  1223. spin_unlock_bh(&le->lock);
  1224. }
  1225. tipc_node_read_unlock(n);
  1226. /* Check/update node state before receiving */
  1227. if (unlikely(skb)) {
  1228. tipc_node_write_lock(n);
  1229. if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
  1230. if (le->link) {
  1231. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1232. skb = NULL;
  1233. }
  1234. }
  1235. tipc_node_write_unlock(n);
  1236. }
  1237. if (unlikely(rc & TIPC_LINK_UP_EVT))
  1238. tipc_node_link_up(n, bearer_id, &xmitq);
  1239. if (unlikely(rc & TIPC_LINK_DOWN_EVT))
  1240. tipc_node_link_down(n, bearer_id, false);
  1241. if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
  1242. tipc_named_rcv(net, &n->bc_entry.namedq);
  1243. if (!skb_queue_empty(&le->inputq))
  1244. tipc_sk_rcv(net, &le->inputq);
  1245. if (!skb_queue_empty(&xmitq))
  1246. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1247. tipc_node_put(n);
  1248. discard:
  1249. kfree_skb(skb);
  1250. }
  1251. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1252. {
  1253. int err;
  1254. struct net *net = sock_net(skb->sk);
  1255. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1256. int done = cb->args[0];
  1257. int last_addr = cb->args[1];
  1258. struct tipc_node *node;
  1259. struct tipc_nl_msg msg;
  1260. if (done)
  1261. return 0;
  1262. msg.skb = skb;
  1263. msg.portid = NETLINK_CB(cb->skb).portid;
  1264. msg.seq = cb->nlh->nlmsg_seq;
  1265. rcu_read_lock();
  1266. if (last_addr) {
  1267. node = tipc_node_find(net, last_addr);
  1268. if (!node) {
  1269. rcu_read_unlock();
  1270. /* We never set seq or call nl_dump_check_consistent()
  1271. * this means that setting prev_seq here will cause the
  1272. * consistence check to fail in the netlink callback
  1273. * handler. Resulting in the NLMSG_DONE message having
  1274. * the NLM_F_DUMP_INTR flag set if the node state
  1275. * changed while we released the lock.
  1276. */
  1277. cb->prev_seq = 1;
  1278. return -EPIPE;
  1279. }
  1280. tipc_node_put(node);
  1281. }
  1282. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1283. if (last_addr) {
  1284. if (node->addr == last_addr)
  1285. last_addr = 0;
  1286. else
  1287. continue;
  1288. }
  1289. tipc_node_read_lock(node);
  1290. err = __tipc_nl_add_node(&msg, node);
  1291. if (err) {
  1292. last_addr = node->addr;
  1293. tipc_node_read_unlock(node);
  1294. goto out;
  1295. }
  1296. tipc_node_read_unlock(node);
  1297. }
  1298. done = 1;
  1299. out:
  1300. cb->args[0] = done;
  1301. cb->args[1] = last_addr;
  1302. rcu_read_unlock();
  1303. return skb->len;
  1304. }