node.c 36 KB

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