node.c 35 KB

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