node.c 37 KB

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