node.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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. *slot0 = 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, &le->maddr,
  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. TIPC_WAKEUP_BCAST_USERS | TIPC_BCAST_MSG_EVT |
  854. TIPC_BCAST_RESET);
  855. spin_unlock_bh(&node->lock);
  856. if (flags & TIPC_NOTIFY_NODE_DOWN)
  857. tipc_publ_notify(net, publ_list, addr);
  858. if (flags & TIPC_WAKEUP_BCAST_USERS)
  859. tipc_bclink_wakeup_users(net);
  860. if (flags & TIPC_NOTIFY_NODE_UP)
  861. tipc_named_node_up(net, addr);
  862. if (flags & TIPC_NOTIFY_LINK_UP)
  863. tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
  864. TIPC_NODE_SCOPE, link_id, addr);
  865. if (flags & TIPC_NOTIFY_LINK_DOWN)
  866. tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
  867. link_id, addr);
  868. if (flags & TIPC_BCAST_MSG_EVT)
  869. tipc_bclink_input(net);
  870. if (flags & TIPC_BCAST_RESET)
  871. tipc_node_reset_links(node);
  872. }
  873. /* Caller should hold node lock for the passed node */
  874. static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
  875. {
  876. void *hdr;
  877. struct nlattr *attrs;
  878. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  879. NLM_F_MULTI, TIPC_NL_NODE_GET);
  880. if (!hdr)
  881. return -EMSGSIZE;
  882. attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
  883. if (!attrs)
  884. goto msg_full;
  885. if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
  886. goto attr_msg_full;
  887. if (tipc_node_is_up(node))
  888. if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
  889. goto attr_msg_full;
  890. nla_nest_end(msg->skb, attrs);
  891. genlmsg_end(msg->skb, hdr);
  892. return 0;
  893. attr_msg_full:
  894. nla_nest_cancel(msg->skb, attrs);
  895. msg_full:
  896. genlmsg_cancel(msg->skb, hdr);
  897. return -EMSGSIZE;
  898. }
  899. static struct tipc_link *tipc_node_select_link(struct tipc_node *n, int sel,
  900. int *bearer_id,
  901. struct tipc_media_addr **maddr)
  902. {
  903. int id = n->active_links[sel & 1];
  904. if (unlikely(id < 0))
  905. return NULL;
  906. *bearer_id = id;
  907. *maddr = &n->links[id].maddr;
  908. return n->links[id].link;
  909. }
  910. /**
  911. * tipc_node_xmit() is the general link level function for message sending
  912. * @net: the applicable net namespace
  913. * @list: chain of buffers containing message
  914. * @dnode: address of destination node
  915. * @selector: a number used for deterministic link selection
  916. * Consumes the buffer chain, except when returning -ELINKCONG
  917. * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
  918. */
  919. int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
  920. u32 dnode, int selector)
  921. {
  922. struct tipc_link *l = NULL;
  923. struct tipc_node *n;
  924. struct sk_buff_head xmitq;
  925. struct tipc_media_addr *maddr;
  926. int bearer_id;
  927. int rc = -EHOSTUNREACH;
  928. __skb_queue_head_init(&xmitq);
  929. n = tipc_node_find(net, dnode);
  930. if (likely(n)) {
  931. tipc_node_lock(n);
  932. l = tipc_node_select_link(n, selector, &bearer_id, &maddr);
  933. if (likely(l))
  934. rc = tipc_link_xmit(l, list, &xmitq);
  935. tipc_node_unlock(n);
  936. if (unlikely(rc == -ENOBUFS))
  937. tipc_node_link_down(n, bearer_id, false);
  938. tipc_node_put(n);
  939. }
  940. if (likely(!rc)) {
  941. tipc_bearer_xmit(net, bearer_id, &xmitq, maddr);
  942. return 0;
  943. }
  944. if (likely(in_own_node(net, dnode))) {
  945. tipc_sk_rcv(net, list);
  946. return 0;
  947. }
  948. return rc;
  949. }
  950. /* tipc_node_xmit_skb(): send single buffer to destination
  951. * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
  952. * messages, which will not be rejected
  953. * The only exception is datagram messages rerouted after secondary
  954. * lookup, which are rare and safe to dispose of anyway.
  955. * TODO: Return real return value, and let callers use
  956. * tipc_wait_for_sendpkt() where applicable
  957. */
  958. int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  959. u32 selector)
  960. {
  961. struct sk_buff_head head;
  962. int rc;
  963. skb_queue_head_init(&head);
  964. __skb_queue_tail(&head, skb);
  965. rc = tipc_node_xmit(net, &head, dnode, selector);
  966. if (rc == -ELINKCONG)
  967. kfree_skb(skb);
  968. return 0;
  969. }
  970. /**
  971. * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
  972. * @net: the applicable net namespace
  973. * @skb: TIPC packet
  974. * @bearer_id: id of bearer message arrived on
  975. *
  976. * Invoked with no locks held.
  977. */
  978. void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
  979. {
  980. int rc;
  981. struct sk_buff_head xmitq;
  982. struct tipc_bclink_entry *be;
  983. struct tipc_link_entry *le;
  984. struct tipc_msg *hdr = buf_msg(skb);
  985. int usr = msg_user(hdr);
  986. u32 dnode = msg_destnode(hdr);
  987. struct tipc_node *n;
  988. __skb_queue_head_init(&xmitq);
  989. /* If NACK for other node, let rcv link for that node peek into it */
  990. if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
  991. n = tipc_node_find(net, dnode);
  992. else
  993. n = tipc_node_find(net, msg_prevnode(hdr));
  994. if (!n) {
  995. kfree_skb(skb);
  996. return;
  997. }
  998. be = &n->bc_entry;
  999. le = &n->links[bearer_id];
  1000. rc = tipc_bcast_rcv(net, be->link, skb);
  1001. /* Broadcast link reset may happen at reassembly failure */
  1002. if (rc & TIPC_LINK_DOWN_EVT)
  1003. tipc_node_reset_links(n);
  1004. /* Broadcast ACKs are sent on a unicast link */
  1005. if (rc & TIPC_LINK_SND_BC_ACK) {
  1006. tipc_node_lock(n);
  1007. tipc_link_build_ack_msg(le->link, &xmitq);
  1008. tipc_node_unlock(n);
  1009. }
  1010. if (!skb_queue_empty(&xmitq))
  1011. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1012. /* Deliver. 'arrvq' is under inputq2's lock protection */
  1013. if (!skb_queue_empty(&be->inputq1)) {
  1014. spin_lock_bh(&be->inputq2.lock);
  1015. spin_lock_bh(&be->inputq1.lock);
  1016. skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
  1017. spin_unlock_bh(&be->inputq1.lock);
  1018. spin_unlock_bh(&be->inputq2.lock);
  1019. tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
  1020. }
  1021. tipc_node_put(n);
  1022. }
  1023. /**
  1024. * tipc_node_check_state - check and if necessary update node state
  1025. * @skb: TIPC packet
  1026. * @bearer_id: identity of bearer delivering the packet
  1027. * Returns true if state is ok, otherwise consumes buffer and returns false
  1028. */
  1029. static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
  1030. int bearer_id, struct sk_buff_head *xmitq)
  1031. {
  1032. struct tipc_msg *hdr = buf_msg(skb);
  1033. int usr = msg_user(hdr);
  1034. int mtyp = msg_type(hdr);
  1035. u16 oseqno = msg_seqno(hdr);
  1036. u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
  1037. u16 exp_pkts = msg_msgcnt(hdr);
  1038. u16 rcv_nxt, syncpt, dlv_nxt;
  1039. int state = n->state;
  1040. struct tipc_link *l, *tnl, *pl = NULL;
  1041. struct tipc_media_addr *maddr;
  1042. int i, pb_id;
  1043. l = n->links[bearer_id].link;
  1044. if (!l)
  1045. return false;
  1046. rcv_nxt = l->rcv_nxt;
  1047. if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
  1048. return true;
  1049. /* Find parallel link, if any */
  1050. for (i = 0; i < MAX_BEARERS; i++) {
  1051. if ((i != bearer_id) && n->links[i].link) {
  1052. pl = n->links[i].link;
  1053. break;
  1054. }
  1055. }
  1056. /* Update node accesibility if applicable */
  1057. if (state == SELF_UP_PEER_COMING) {
  1058. if (!tipc_link_is_up(l))
  1059. return true;
  1060. if (!msg_peer_link_is_up(hdr))
  1061. return true;
  1062. tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
  1063. }
  1064. if (state == SELF_DOWN_PEER_LEAVING) {
  1065. if (msg_peer_node_is_up(hdr))
  1066. return false;
  1067. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  1068. }
  1069. /* Ignore duplicate packets */
  1070. if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
  1071. return true;
  1072. /* Initiate or update failover mode if applicable */
  1073. if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
  1074. syncpt = oseqno + exp_pkts - 1;
  1075. if (pl && tipc_link_is_up(pl)) {
  1076. pb_id = pl->bearer_id;
  1077. __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
  1078. tipc_skb_queue_splice_tail_init(pl->inputq, l->inputq);
  1079. }
  1080. /* If pkts arrive out of order, use lowest calculated syncpt */
  1081. if (less(syncpt, n->sync_point))
  1082. n->sync_point = syncpt;
  1083. }
  1084. /* Open parallel link when tunnel link reaches synch point */
  1085. if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
  1086. if (!more(rcv_nxt, n->sync_point))
  1087. return true;
  1088. tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
  1089. if (pl)
  1090. tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
  1091. return true;
  1092. }
  1093. /* No synching needed if only one link */
  1094. if (!pl || !tipc_link_is_up(pl))
  1095. return true;
  1096. /* Initiate synch mode if applicable */
  1097. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
  1098. syncpt = iseqno + exp_pkts - 1;
  1099. if (!tipc_link_is_up(l)) {
  1100. tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1101. __tipc_node_link_up(n, bearer_id, xmitq);
  1102. }
  1103. if (n->state == SELF_UP_PEER_UP) {
  1104. n->sync_point = syncpt;
  1105. tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
  1106. tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
  1107. }
  1108. if (less(syncpt, n->sync_point))
  1109. n->sync_point = syncpt;
  1110. }
  1111. /* Open tunnel link when parallel link reaches synch point */
  1112. if ((n->state == NODE_SYNCHING) && tipc_link_is_synching(l)) {
  1113. if (tipc_link_is_synching(l)) {
  1114. tnl = l;
  1115. } else {
  1116. tnl = pl;
  1117. pl = l;
  1118. }
  1119. dlv_nxt = pl->rcv_nxt - mod(skb_queue_len(pl->inputq));
  1120. if (more(dlv_nxt, n->sync_point)) {
  1121. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  1122. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  1123. return true;
  1124. }
  1125. if (l == pl)
  1126. return true;
  1127. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
  1128. return true;
  1129. if (usr == LINK_PROTOCOL)
  1130. return true;
  1131. return false;
  1132. }
  1133. return true;
  1134. }
  1135. /**
  1136. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1137. * @net: the applicable net namespace
  1138. * @skb: TIPC packet
  1139. * @bearer: pointer to bearer message arrived on
  1140. *
  1141. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1142. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1143. */
  1144. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
  1145. {
  1146. struct sk_buff_head xmitq;
  1147. struct tipc_node *n;
  1148. struct tipc_msg *hdr = buf_msg(skb);
  1149. int usr = msg_user(hdr);
  1150. int bearer_id = b->identity;
  1151. struct tipc_link_entry *le;
  1152. u16 bc_ack = msg_bcast_ack(hdr);
  1153. int rc = 0;
  1154. __skb_queue_head_init(&xmitq);
  1155. /* Ensure message is well-formed */
  1156. if (unlikely(!tipc_msg_validate(skb)))
  1157. goto discard;
  1158. /* Handle arrival of discovery or broadcast packet */
  1159. if (unlikely(msg_non_seq(hdr))) {
  1160. if (unlikely(usr == LINK_CONFIG))
  1161. return tipc_disc_rcv(net, skb, b);
  1162. else
  1163. return tipc_node_bc_rcv(net, skb, bearer_id);
  1164. }
  1165. /* Locate neighboring node that sent packet */
  1166. n = tipc_node_find(net, msg_prevnode(hdr));
  1167. if (unlikely(!n))
  1168. goto discard;
  1169. le = &n->links[bearer_id];
  1170. /* Ensure broadcast reception is in synch with peer's send state */
  1171. if (unlikely(usr == LINK_PROTOCOL))
  1172. tipc_bcast_sync_rcv(net, n->bc_entry.link, hdr);
  1173. else if (unlikely(n->bc_entry.link->acked != bc_ack))
  1174. tipc_bcast_ack_rcv(net, n->bc_entry.link, bc_ack);
  1175. tipc_node_lock(n);
  1176. /* Is reception permitted at the moment ? */
  1177. if (!tipc_node_filter_pkt(n, hdr))
  1178. goto unlock;
  1179. /* Check and if necessary update node state */
  1180. if (likely(tipc_node_check_state(n, skb, bearer_id, &xmitq))) {
  1181. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1182. skb = NULL;
  1183. }
  1184. unlock:
  1185. tipc_node_unlock(n);
  1186. if (unlikely(rc & TIPC_LINK_UP_EVT))
  1187. tipc_node_link_up(n, bearer_id, &xmitq);
  1188. if (unlikely(rc & TIPC_LINK_DOWN_EVT))
  1189. tipc_node_link_down(n, bearer_id, false);
  1190. if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
  1191. tipc_named_rcv(net, &n->bc_entry.namedq);
  1192. if (!skb_queue_empty(&le->inputq))
  1193. tipc_sk_rcv(net, &le->inputq);
  1194. if (!skb_queue_empty(&xmitq))
  1195. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1196. tipc_node_put(n);
  1197. discard:
  1198. kfree_skb(skb);
  1199. }
  1200. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1201. {
  1202. int err;
  1203. struct net *net = sock_net(skb->sk);
  1204. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1205. int done = cb->args[0];
  1206. int last_addr = cb->args[1];
  1207. struct tipc_node *node;
  1208. struct tipc_nl_msg msg;
  1209. if (done)
  1210. return 0;
  1211. msg.skb = skb;
  1212. msg.portid = NETLINK_CB(cb->skb).portid;
  1213. msg.seq = cb->nlh->nlmsg_seq;
  1214. rcu_read_lock();
  1215. if (last_addr) {
  1216. node = tipc_node_find(net, last_addr);
  1217. if (!node) {
  1218. rcu_read_unlock();
  1219. /* We never set seq or call nl_dump_check_consistent()
  1220. * this means that setting prev_seq here will cause the
  1221. * consistence check to fail in the netlink callback
  1222. * handler. Resulting in the NLMSG_DONE message having
  1223. * the NLM_F_DUMP_INTR flag set if the node state
  1224. * changed while we released the lock.
  1225. */
  1226. cb->prev_seq = 1;
  1227. return -EPIPE;
  1228. }
  1229. tipc_node_put(node);
  1230. }
  1231. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1232. if (last_addr) {
  1233. if (node->addr == last_addr)
  1234. last_addr = 0;
  1235. else
  1236. continue;
  1237. }
  1238. tipc_node_lock(node);
  1239. err = __tipc_nl_add_node(&msg, node);
  1240. if (err) {
  1241. last_addr = node->addr;
  1242. tipc_node_unlock(node);
  1243. goto out;
  1244. }
  1245. tipc_node_unlock(node);
  1246. }
  1247. done = 1;
  1248. out:
  1249. cb->args[0] = done;
  1250. cb->args[1] = last_addr;
  1251. rcu_read_unlock();
  1252. return skb->len;
  1253. }