node.c 34 KB

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