node.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  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. #include "netlink.h"
  44. #define INVALID_NODE_SIG 0x10000
  45. /* Flags used to take different actions according to flag type
  46. * TIPC_NOTIFY_NODE_DOWN: notify node is down
  47. * TIPC_NOTIFY_NODE_UP: notify node is up
  48. * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
  49. */
  50. enum {
  51. TIPC_NOTIFY_NODE_DOWN = (1 << 3),
  52. TIPC_NOTIFY_NODE_UP = (1 << 4),
  53. TIPC_NOTIFY_LINK_UP = (1 << 6),
  54. TIPC_NOTIFY_LINK_DOWN = (1 << 7)
  55. };
  56. struct tipc_link_entry {
  57. struct tipc_link *link;
  58. spinlock_t lock; /* per link */
  59. u32 mtu;
  60. struct sk_buff_head inputq;
  61. struct tipc_media_addr maddr;
  62. };
  63. struct tipc_bclink_entry {
  64. struct tipc_link *link;
  65. struct sk_buff_head inputq1;
  66. struct sk_buff_head arrvq;
  67. struct sk_buff_head inputq2;
  68. struct sk_buff_head namedq;
  69. };
  70. /**
  71. * struct tipc_node - TIPC node structure
  72. * @addr: network address of node
  73. * @ref: reference counter to node object
  74. * @lock: rwlock governing access to structure
  75. * @net: the applicable net namespace
  76. * @hash: links to adjacent nodes in unsorted hash chain
  77. * @inputq: pointer to input queue containing messages for msg event
  78. * @namedq: pointer to name table input queue with name table messages
  79. * @active_links: bearer ids of active links, used as index into links[] array
  80. * @links: array containing references to all links to node
  81. * @action_flags: bit mask of different types of node actions
  82. * @state: connectivity state vs peer node
  83. * @sync_point: sequence number where synch/failover is finished
  84. * @list: links to adjacent nodes in sorted list of cluster's nodes
  85. * @working_links: number of working links to node (both active and standby)
  86. * @link_cnt: number of links to node
  87. * @capabilities: bitmap, indicating peer node's functional capabilities
  88. * @signature: node instance identifier
  89. * @link_id: local and remote bearer ids of changing link, if any
  90. * @publ_list: list of publications
  91. * @rcu: rcu struct for tipc_node
  92. */
  93. struct tipc_node {
  94. u32 addr;
  95. struct kref kref;
  96. rwlock_t lock;
  97. struct net *net;
  98. struct hlist_node hash;
  99. int active_links[2];
  100. struct tipc_link_entry links[MAX_BEARERS];
  101. struct tipc_bclink_entry bc_entry;
  102. int action_flags;
  103. struct list_head list;
  104. int state;
  105. u16 sync_point;
  106. int link_cnt;
  107. u16 working_links;
  108. u16 capabilities;
  109. u32 signature;
  110. u32 link_id;
  111. struct list_head publ_list;
  112. struct list_head conn_sks;
  113. unsigned long keepalive_intv;
  114. struct timer_list timer;
  115. struct rcu_head rcu;
  116. };
  117. /* Node FSM states and events:
  118. */
  119. enum {
  120. SELF_DOWN_PEER_DOWN = 0xdd,
  121. SELF_UP_PEER_UP = 0xaa,
  122. SELF_DOWN_PEER_LEAVING = 0xd1,
  123. SELF_UP_PEER_COMING = 0xac,
  124. SELF_COMING_PEER_UP = 0xca,
  125. SELF_LEAVING_PEER_DOWN = 0x1d,
  126. NODE_FAILINGOVER = 0xf0,
  127. NODE_SYNCHING = 0xcc
  128. };
  129. enum {
  130. SELF_ESTABL_CONTACT_EVT = 0xece,
  131. SELF_LOST_CONTACT_EVT = 0x1ce,
  132. PEER_ESTABL_CONTACT_EVT = 0x9ece,
  133. PEER_LOST_CONTACT_EVT = 0x91ce,
  134. NODE_FAILOVER_BEGIN_EVT = 0xfbe,
  135. NODE_FAILOVER_END_EVT = 0xfee,
  136. NODE_SYNCH_BEGIN_EVT = 0xcbe,
  137. NODE_SYNCH_END_EVT = 0xcee
  138. };
  139. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  140. struct sk_buff_head *xmitq,
  141. struct tipc_media_addr **maddr);
  142. static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
  143. bool delete);
  144. static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
  145. static void tipc_node_delete(struct tipc_node *node);
  146. static void tipc_node_timeout(unsigned long data);
  147. static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
  148. static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
  149. static void tipc_node_put(struct tipc_node *node);
  150. static bool tipc_node_is_up(struct tipc_node *n);
  151. struct tipc_sock_conn {
  152. u32 port;
  153. u32 peer_port;
  154. u32 peer_node;
  155. struct list_head list;
  156. };
  157. static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
  158. {
  159. int bearer_id = n->active_links[sel & 1];
  160. if (unlikely(bearer_id == INVALID_BEARER_ID))
  161. return NULL;
  162. return n->links[bearer_id].link;
  163. }
  164. int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel)
  165. {
  166. struct tipc_node *n;
  167. int bearer_id;
  168. unsigned int mtu = MAX_MSG_SIZE;
  169. n = tipc_node_find(net, addr);
  170. if (unlikely(!n))
  171. return mtu;
  172. bearer_id = n->active_links[sel & 1];
  173. if (likely(bearer_id != INVALID_BEARER_ID))
  174. mtu = n->links[bearer_id].mtu;
  175. tipc_node_put(n);
  176. return mtu;
  177. }
  178. /*
  179. * A trivial power-of-two bitmask technique is used for speed, since this
  180. * operation is done for every incoming TIPC packet. The number of hash table
  181. * entries has been chosen so that no hash chain exceeds 8 nodes and will
  182. * usually be much smaller (typically only a single node).
  183. */
  184. static unsigned int tipc_hashfn(u32 addr)
  185. {
  186. return addr & (NODE_HTABLE_SIZE - 1);
  187. }
  188. static void tipc_node_kref_release(struct kref *kref)
  189. {
  190. struct tipc_node *n = container_of(kref, struct tipc_node, kref);
  191. kfree(n->bc_entry.link);
  192. kfree_rcu(n, rcu);
  193. }
  194. static void tipc_node_put(struct tipc_node *node)
  195. {
  196. kref_put(&node->kref, tipc_node_kref_release);
  197. }
  198. static void tipc_node_get(struct tipc_node *node)
  199. {
  200. kref_get(&node->kref);
  201. }
  202. /*
  203. * tipc_node_find - locate specified node object, if it exists
  204. */
  205. static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
  206. {
  207. struct tipc_net *tn = tipc_net(net);
  208. struct tipc_node *node;
  209. unsigned int thash = tipc_hashfn(addr);
  210. if (unlikely(!in_own_cluster_exact(net, addr)))
  211. return NULL;
  212. rcu_read_lock();
  213. hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
  214. if (node->addr != addr)
  215. continue;
  216. if (!kref_get_unless_zero(&node->kref))
  217. node = NULL;
  218. break;
  219. }
  220. rcu_read_unlock();
  221. return node;
  222. }
  223. static void tipc_node_read_lock(struct tipc_node *n)
  224. {
  225. read_lock_bh(&n->lock);
  226. }
  227. static void tipc_node_read_unlock(struct tipc_node *n)
  228. {
  229. read_unlock_bh(&n->lock);
  230. }
  231. static void tipc_node_write_lock(struct tipc_node *n)
  232. {
  233. write_lock_bh(&n->lock);
  234. }
  235. static void tipc_node_write_unlock(struct tipc_node *n)
  236. {
  237. struct net *net = n->net;
  238. u32 addr = 0;
  239. u32 flags = n->action_flags;
  240. u32 link_id = 0;
  241. struct list_head *publ_list;
  242. if (likely(!flags)) {
  243. write_unlock_bh(&n->lock);
  244. return;
  245. }
  246. addr = n->addr;
  247. link_id = n->link_id;
  248. publ_list = &n->publ_list;
  249. n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
  250. TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
  251. write_unlock_bh(&n->lock);
  252. if (flags & TIPC_NOTIFY_NODE_DOWN)
  253. tipc_publ_notify(net, publ_list, addr);
  254. if (flags & TIPC_NOTIFY_NODE_UP)
  255. tipc_named_node_up(net, addr);
  256. if (flags & TIPC_NOTIFY_LINK_UP)
  257. tipc_nametbl_publish(net, TIPC_LINK_STATE, addr, addr,
  258. TIPC_NODE_SCOPE, link_id, addr);
  259. if (flags & TIPC_NOTIFY_LINK_DOWN)
  260. tipc_nametbl_withdraw(net, TIPC_LINK_STATE, addr,
  261. link_id, addr);
  262. }
  263. struct tipc_node *tipc_node_create(struct net *net, u32 addr, u16 capabilities)
  264. {
  265. struct tipc_net *tn = net_generic(net, tipc_net_id);
  266. struct tipc_node *n, *temp_node;
  267. int i;
  268. spin_lock_bh(&tn->node_list_lock);
  269. n = tipc_node_find(net, addr);
  270. if (n)
  271. goto exit;
  272. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  273. if (!n) {
  274. pr_warn("Node creation failed, no memory\n");
  275. goto exit;
  276. }
  277. n->addr = addr;
  278. n->net = net;
  279. n->capabilities = capabilities;
  280. kref_init(&n->kref);
  281. rwlock_init(&n->lock);
  282. INIT_HLIST_NODE(&n->hash);
  283. INIT_LIST_HEAD(&n->list);
  284. INIT_LIST_HEAD(&n->publ_list);
  285. INIT_LIST_HEAD(&n->conn_sks);
  286. skb_queue_head_init(&n->bc_entry.namedq);
  287. skb_queue_head_init(&n->bc_entry.inputq1);
  288. __skb_queue_head_init(&n->bc_entry.arrvq);
  289. skb_queue_head_init(&n->bc_entry.inputq2);
  290. for (i = 0; i < MAX_BEARERS; i++)
  291. spin_lock_init(&n->links[i].lock);
  292. n->state = SELF_DOWN_PEER_LEAVING;
  293. n->signature = INVALID_NODE_SIG;
  294. n->active_links[0] = INVALID_BEARER_ID;
  295. n->active_links[1] = INVALID_BEARER_ID;
  296. if (!tipc_link_bc_create(net, tipc_own_addr(net), n->addr,
  297. U16_MAX,
  298. tipc_link_window(tipc_bc_sndlink(net)),
  299. n->capabilities,
  300. &n->bc_entry.inputq1,
  301. &n->bc_entry.namedq,
  302. tipc_bc_sndlink(net),
  303. &n->bc_entry.link)) {
  304. pr_warn("Broadcast rcv link creation failed, no memory\n");
  305. kfree(n);
  306. n = NULL;
  307. goto exit;
  308. }
  309. tipc_node_get(n);
  310. setup_timer(&n->timer, tipc_node_timeout, (unsigned long)n);
  311. n->keepalive_intv = U32_MAX;
  312. hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
  313. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  314. if (n->addr < temp_node->addr)
  315. break;
  316. }
  317. list_add_tail_rcu(&n->list, &temp_node->list);
  318. exit:
  319. spin_unlock_bh(&tn->node_list_lock);
  320. return n;
  321. }
  322. static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
  323. {
  324. unsigned long tol = tipc_link_tolerance(l);
  325. unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
  326. unsigned long keepalive_intv = msecs_to_jiffies(intv);
  327. /* Link with lowest tolerance determines timer interval */
  328. if (keepalive_intv < n->keepalive_intv)
  329. n->keepalive_intv = keepalive_intv;
  330. /* Ensure link's abort limit corresponds to current interval */
  331. tipc_link_set_abort_limit(l, tol / jiffies_to_msecs(n->keepalive_intv));
  332. }
  333. static void tipc_node_delete(struct tipc_node *node)
  334. {
  335. list_del_rcu(&node->list);
  336. hlist_del_rcu(&node->hash);
  337. tipc_node_put(node);
  338. del_timer_sync(&node->timer);
  339. tipc_node_put(node);
  340. }
  341. void tipc_node_stop(struct net *net)
  342. {
  343. struct tipc_net *tn = tipc_net(net);
  344. struct tipc_node *node, *t_node;
  345. spin_lock_bh(&tn->node_list_lock);
  346. list_for_each_entry_safe(node, t_node, &tn->node_list, list)
  347. tipc_node_delete(node);
  348. spin_unlock_bh(&tn->node_list_lock);
  349. }
  350. void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
  351. {
  352. struct tipc_node *n;
  353. if (in_own_node(net, addr))
  354. return;
  355. n = tipc_node_find(net, addr);
  356. if (!n) {
  357. pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
  358. return;
  359. }
  360. tipc_node_write_lock(n);
  361. list_add_tail(subscr, &n->publ_list);
  362. tipc_node_write_unlock(n);
  363. tipc_node_put(n);
  364. }
  365. void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
  366. {
  367. struct tipc_node *n;
  368. if (in_own_node(net, addr))
  369. return;
  370. n = tipc_node_find(net, addr);
  371. if (!n) {
  372. pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
  373. return;
  374. }
  375. tipc_node_write_lock(n);
  376. list_del_init(subscr);
  377. tipc_node_write_unlock(n);
  378. tipc_node_put(n);
  379. }
  380. int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
  381. {
  382. struct tipc_node *node;
  383. struct tipc_sock_conn *conn;
  384. int err = 0;
  385. if (in_own_node(net, dnode))
  386. return 0;
  387. node = tipc_node_find(net, dnode);
  388. if (!node) {
  389. pr_warn("Connecting sock to node 0x%x failed\n", dnode);
  390. return -EHOSTUNREACH;
  391. }
  392. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  393. if (!conn) {
  394. err = -EHOSTUNREACH;
  395. goto exit;
  396. }
  397. conn->peer_node = dnode;
  398. conn->port = port;
  399. conn->peer_port = peer_port;
  400. tipc_node_write_lock(node);
  401. list_add_tail(&conn->list, &node->conn_sks);
  402. tipc_node_write_unlock(node);
  403. exit:
  404. tipc_node_put(node);
  405. return err;
  406. }
  407. void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
  408. {
  409. struct tipc_node *node;
  410. struct tipc_sock_conn *conn, *safe;
  411. if (in_own_node(net, dnode))
  412. return;
  413. node = tipc_node_find(net, dnode);
  414. if (!node)
  415. return;
  416. tipc_node_write_lock(node);
  417. list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
  418. if (port != conn->port)
  419. continue;
  420. list_del(&conn->list);
  421. kfree(conn);
  422. }
  423. tipc_node_write_unlock(node);
  424. tipc_node_put(node);
  425. }
  426. /* tipc_node_timeout - handle expiration of node timer
  427. */
  428. static void tipc_node_timeout(unsigned long data)
  429. {
  430. struct tipc_node *n = (struct tipc_node *)data;
  431. struct tipc_link_entry *le;
  432. struct sk_buff_head xmitq;
  433. int bearer_id;
  434. int rc = 0;
  435. __skb_queue_head_init(&xmitq);
  436. for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
  437. tipc_node_read_lock(n);
  438. le = &n->links[bearer_id];
  439. spin_lock_bh(&le->lock);
  440. if (le->link) {
  441. /* Link tolerance may change asynchronously: */
  442. tipc_node_calculate_timer(n, le->link);
  443. rc = tipc_link_timeout(le->link, &xmitq);
  444. }
  445. spin_unlock_bh(&le->lock);
  446. tipc_node_read_unlock(n);
  447. tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr);
  448. if (rc & TIPC_LINK_DOWN_EVT)
  449. tipc_node_link_down(n, bearer_id, false);
  450. }
  451. mod_timer(&n->timer, jiffies + n->keepalive_intv);
  452. }
  453. /**
  454. * __tipc_node_link_up - handle addition of link
  455. * Node lock must be held by caller
  456. * Link becomes active (alone or shared) or standby, depending on its priority.
  457. */
  458. static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
  459. struct sk_buff_head *xmitq)
  460. {
  461. int *slot0 = &n->active_links[0];
  462. int *slot1 = &n->active_links[1];
  463. struct tipc_link *ol = node_active_link(n, 0);
  464. struct tipc_link *nl = n->links[bearer_id].link;
  465. if (!nl)
  466. return;
  467. tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
  468. if (!tipc_link_is_up(nl))
  469. return;
  470. n->working_links++;
  471. n->action_flags |= TIPC_NOTIFY_LINK_UP;
  472. n->link_id = tipc_link_id(nl);
  473. /* Leave room for tunnel header when returning 'mtu' to users: */
  474. n->links[bearer_id].mtu = tipc_link_mtu(nl) - INT_H_SIZE;
  475. tipc_bearer_add_dest(n->net, bearer_id, n->addr);
  476. tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
  477. pr_debug("Established link <%s> on network plane %c\n",
  478. tipc_link_name(nl), tipc_link_plane(nl));
  479. /* Ensure that a STATE message goes first */
  480. tipc_link_build_state_msg(nl, xmitq);
  481. /* First link? => give it both slots */
  482. if (!ol) {
  483. *slot0 = bearer_id;
  484. *slot1 = bearer_id;
  485. tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
  486. n->action_flags |= TIPC_NOTIFY_NODE_UP;
  487. tipc_link_set_active(nl, true);
  488. tipc_bcast_add_peer(n->net, nl, xmitq);
  489. return;
  490. }
  491. /* Second link => redistribute slots */
  492. if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
  493. pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
  494. *slot0 = bearer_id;
  495. *slot1 = bearer_id;
  496. tipc_link_set_active(nl, true);
  497. tipc_link_set_active(ol, false);
  498. } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
  499. tipc_link_set_active(nl, true);
  500. *slot1 = bearer_id;
  501. } else {
  502. pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
  503. }
  504. /* Prepare synchronization with first link */
  505. tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
  506. }
  507. /**
  508. * tipc_node_link_up - handle addition of link
  509. *
  510. * Link becomes active (alone or shared) or standby, depending on its priority.
  511. */
  512. static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
  513. struct sk_buff_head *xmitq)
  514. {
  515. struct tipc_media_addr *maddr;
  516. tipc_node_write_lock(n);
  517. __tipc_node_link_up(n, bearer_id, xmitq);
  518. maddr = &n->links[bearer_id].maddr;
  519. tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr);
  520. tipc_node_write_unlock(n);
  521. }
  522. /**
  523. * __tipc_node_link_down - handle loss of link
  524. */
  525. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  526. struct sk_buff_head *xmitq,
  527. struct tipc_media_addr **maddr)
  528. {
  529. struct tipc_link_entry *le = &n->links[*bearer_id];
  530. int *slot0 = &n->active_links[0];
  531. int *slot1 = &n->active_links[1];
  532. int i, highest = 0, prio;
  533. struct tipc_link *l, *_l, *tnl;
  534. l = n->links[*bearer_id].link;
  535. if (!l || tipc_link_is_reset(l))
  536. return;
  537. n->working_links--;
  538. n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
  539. n->link_id = tipc_link_id(l);
  540. tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
  541. pr_debug("Lost link <%s> on network plane %c\n",
  542. tipc_link_name(l), tipc_link_plane(l));
  543. /* Select new active link if any available */
  544. *slot0 = INVALID_BEARER_ID;
  545. *slot1 = INVALID_BEARER_ID;
  546. for (i = 0; i < MAX_BEARERS; i++) {
  547. _l = n->links[i].link;
  548. if (!_l || !tipc_link_is_up(_l))
  549. continue;
  550. if (_l == l)
  551. continue;
  552. prio = tipc_link_prio(_l);
  553. if (prio < highest)
  554. continue;
  555. if (prio > highest) {
  556. highest = prio;
  557. *slot0 = i;
  558. *slot1 = i;
  559. continue;
  560. }
  561. *slot1 = i;
  562. }
  563. if (!tipc_node_is_up(n)) {
  564. if (tipc_link_peer_is_down(l))
  565. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  566. tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
  567. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  568. tipc_link_reset(l);
  569. tipc_link_build_reset_msg(l, xmitq);
  570. *maddr = &n->links[*bearer_id].maddr;
  571. node_lost_contact(n, &le->inputq);
  572. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  573. return;
  574. }
  575. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  576. /* There is still a working link => initiate failover */
  577. *bearer_id = n->active_links[0];
  578. tnl = n->links[*bearer_id].link;
  579. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  580. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  581. n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
  582. tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
  583. tipc_link_reset(l);
  584. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  585. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  586. tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
  587. *maddr = &n->links[*bearer_id].maddr;
  588. }
  589. static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
  590. {
  591. struct tipc_link_entry *le = &n->links[bearer_id];
  592. struct tipc_link *l = le->link;
  593. struct tipc_media_addr *maddr;
  594. struct sk_buff_head xmitq;
  595. if (!l)
  596. return;
  597. __skb_queue_head_init(&xmitq);
  598. tipc_node_write_lock(n);
  599. if (!tipc_link_is_establishing(l)) {
  600. __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
  601. if (delete) {
  602. kfree(l);
  603. le->link = NULL;
  604. n->link_cnt--;
  605. }
  606. } else {
  607. /* Defuse pending tipc_node_link_up() */
  608. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  609. }
  610. tipc_node_write_unlock(n);
  611. tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
  612. tipc_sk_rcv(n->net, &le->inputq);
  613. }
  614. static bool tipc_node_is_up(struct tipc_node *n)
  615. {
  616. return n->active_links[0] != INVALID_BEARER_ID;
  617. }
  618. void tipc_node_check_dest(struct net *net, u32 onode,
  619. struct tipc_bearer *b,
  620. u16 capabilities, u32 signature,
  621. struct tipc_media_addr *maddr,
  622. bool *respond, bool *dupl_addr)
  623. {
  624. struct tipc_node *n;
  625. struct tipc_link *l;
  626. struct tipc_link_entry *le;
  627. bool addr_match = false;
  628. bool sign_match = false;
  629. bool link_up = false;
  630. bool accept_addr = false;
  631. bool reset = true;
  632. char *if_name;
  633. *dupl_addr = false;
  634. *respond = false;
  635. n = tipc_node_create(net, onode, capabilities);
  636. if (!n)
  637. return;
  638. tipc_node_write_lock(n);
  639. le = &n->links[b->identity];
  640. /* Prepare to validate requesting node's signature and media address */
  641. l = le->link;
  642. link_up = l && tipc_link_is_up(l);
  643. addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
  644. sign_match = (signature == n->signature);
  645. /* These three flags give us eight permutations: */
  646. if (sign_match && addr_match && link_up) {
  647. /* All is fine. Do nothing. */
  648. reset = false;
  649. } else if (sign_match && addr_match && !link_up) {
  650. /* Respond. The link will come up in due time */
  651. *respond = true;
  652. } else if (sign_match && !addr_match && link_up) {
  653. /* Peer has changed i/f address without rebooting.
  654. * If so, the link will reset soon, and the next
  655. * discovery will be accepted. So we can ignore it.
  656. * It may also be an cloned or malicious peer having
  657. * chosen the same node address and signature as an
  658. * existing one.
  659. * Ignore requests until the link goes down, if ever.
  660. */
  661. *dupl_addr = true;
  662. } else if (sign_match && !addr_match && !link_up) {
  663. /* Peer link has changed i/f address without rebooting.
  664. * It may also be a cloned or malicious peer; we can't
  665. * distinguish between the two.
  666. * The signature is correct, so we must accept.
  667. */
  668. accept_addr = true;
  669. *respond = true;
  670. } else if (!sign_match && addr_match && link_up) {
  671. /* Peer node rebooted. Two possibilities:
  672. * - Delayed re-discovery; this link endpoint has already
  673. * reset and re-established contact with the peer, before
  674. * receiving a discovery message from that node.
  675. * (The peer happened to receive one from this node first).
  676. * - The peer came back so fast that our side has not
  677. * discovered it yet. Probing from this side will soon
  678. * reset the link, since there can be no working link
  679. * endpoint at the peer end, and the link will re-establish.
  680. * Accept the signature, since it comes from a known peer.
  681. */
  682. n->signature = signature;
  683. } else if (!sign_match && addr_match && !link_up) {
  684. /* The peer node has rebooted.
  685. * Accept signature, since it is a known peer.
  686. */
  687. n->signature = signature;
  688. *respond = true;
  689. } else if (!sign_match && !addr_match && link_up) {
  690. /* Peer rebooted with new address, or a new/duplicate peer.
  691. * Ignore until the link goes down, if ever.
  692. */
  693. *dupl_addr = true;
  694. } else if (!sign_match && !addr_match && !link_up) {
  695. /* Peer rebooted with new address, or it is a new peer.
  696. * Accept signature and address.
  697. */
  698. n->signature = signature;
  699. accept_addr = true;
  700. *respond = true;
  701. }
  702. if (!accept_addr)
  703. goto exit;
  704. /* Now create new link if not already existing */
  705. if (!l) {
  706. if (n->link_cnt == 2) {
  707. pr_warn("Cannot establish 3rd link to %x\n", n->addr);
  708. goto exit;
  709. }
  710. if_name = strchr(b->name, ':') + 1;
  711. if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
  712. b->net_plane, b->mtu, b->priority,
  713. b->window, mod(tipc_net(net)->random),
  714. tipc_own_addr(net), onode,
  715. n->capabilities,
  716. tipc_bc_sndlink(n->net), n->bc_entry.link,
  717. &le->inputq,
  718. &n->bc_entry.namedq, &l)) {
  719. *respond = false;
  720. goto exit;
  721. }
  722. tipc_link_reset(l);
  723. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  724. if (n->state == NODE_FAILINGOVER)
  725. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  726. le->link = l;
  727. n->link_cnt++;
  728. tipc_node_calculate_timer(n, l);
  729. if (n->link_cnt == 1)
  730. if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
  731. tipc_node_get(n);
  732. }
  733. memcpy(&le->maddr, maddr, sizeof(*maddr));
  734. exit:
  735. tipc_node_write_unlock(n);
  736. if (reset && l && !tipc_link_is_reset(l))
  737. tipc_node_link_down(n, b->identity, false);
  738. tipc_node_put(n);
  739. }
  740. void tipc_node_delete_links(struct net *net, int bearer_id)
  741. {
  742. struct tipc_net *tn = net_generic(net, tipc_net_id);
  743. struct tipc_node *n;
  744. rcu_read_lock();
  745. list_for_each_entry_rcu(n, &tn->node_list, list) {
  746. tipc_node_link_down(n, bearer_id, true);
  747. }
  748. rcu_read_unlock();
  749. }
  750. static void tipc_node_reset_links(struct tipc_node *n)
  751. {
  752. char addr_string[16];
  753. int i;
  754. pr_warn("Resetting all links to %s\n",
  755. tipc_addr_string_fill(addr_string, n->addr));
  756. for (i = 0; i < MAX_BEARERS; i++) {
  757. tipc_node_link_down(n, i, false);
  758. }
  759. }
  760. /* tipc_node_fsm_evt - node finite state machine
  761. * Determines when contact is allowed with peer node
  762. */
  763. static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
  764. {
  765. int state = n->state;
  766. switch (state) {
  767. case SELF_DOWN_PEER_DOWN:
  768. switch (evt) {
  769. case SELF_ESTABL_CONTACT_EVT:
  770. state = SELF_UP_PEER_COMING;
  771. break;
  772. case PEER_ESTABL_CONTACT_EVT:
  773. state = SELF_COMING_PEER_UP;
  774. break;
  775. case SELF_LOST_CONTACT_EVT:
  776. case PEER_LOST_CONTACT_EVT:
  777. break;
  778. case NODE_SYNCH_END_EVT:
  779. case NODE_SYNCH_BEGIN_EVT:
  780. case NODE_FAILOVER_BEGIN_EVT:
  781. case NODE_FAILOVER_END_EVT:
  782. default:
  783. goto illegal_evt;
  784. }
  785. break;
  786. case SELF_UP_PEER_UP:
  787. switch (evt) {
  788. case SELF_LOST_CONTACT_EVT:
  789. state = SELF_DOWN_PEER_LEAVING;
  790. break;
  791. case PEER_LOST_CONTACT_EVT:
  792. state = SELF_LEAVING_PEER_DOWN;
  793. break;
  794. case NODE_SYNCH_BEGIN_EVT:
  795. state = NODE_SYNCHING;
  796. break;
  797. case NODE_FAILOVER_BEGIN_EVT:
  798. state = NODE_FAILINGOVER;
  799. break;
  800. case SELF_ESTABL_CONTACT_EVT:
  801. case PEER_ESTABL_CONTACT_EVT:
  802. case NODE_SYNCH_END_EVT:
  803. case NODE_FAILOVER_END_EVT:
  804. break;
  805. default:
  806. goto illegal_evt;
  807. }
  808. break;
  809. case SELF_DOWN_PEER_LEAVING:
  810. switch (evt) {
  811. case PEER_LOST_CONTACT_EVT:
  812. state = SELF_DOWN_PEER_DOWN;
  813. break;
  814. case SELF_ESTABL_CONTACT_EVT:
  815. case PEER_ESTABL_CONTACT_EVT:
  816. case SELF_LOST_CONTACT_EVT:
  817. break;
  818. case NODE_SYNCH_END_EVT:
  819. case NODE_SYNCH_BEGIN_EVT:
  820. case NODE_FAILOVER_BEGIN_EVT:
  821. case NODE_FAILOVER_END_EVT:
  822. default:
  823. goto illegal_evt;
  824. }
  825. break;
  826. case SELF_UP_PEER_COMING:
  827. switch (evt) {
  828. case PEER_ESTABL_CONTACT_EVT:
  829. state = SELF_UP_PEER_UP;
  830. break;
  831. case SELF_LOST_CONTACT_EVT:
  832. state = SELF_DOWN_PEER_LEAVING;
  833. break;
  834. case SELF_ESTABL_CONTACT_EVT:
  835. case PEER_LOST_CONTACT_EVT:
  836. case NODE_SYNCH_END_EVT:
  837. case NODE_FAILOVER_BEGIN_EVT:
  838. break;
  839. case NODE_SYNCH_BEGIN_EVT:
  840. case NODE_FAILOVER_END_EVT:
  841. default:
  842. goto illegal_evt;
  843. }
  844. break;
  845. case SELF_COMING_PEER_UP:
  846. switch (evt) {
  847. case SELF_ESTABL_CONTACT_EVT:
  848. state = SELF_UP_PEER_UP;
  849. break;
  850. case PEER_LOST_CONTACT_EVT:
  851. state = SELF_LEAVING_PEER_DOWN;
  852. break;
  853. case SELF_LOST_CONTACT_EVT:
  854. case PEER_ESTABL_CONTACT_EVT:
  855. break;
  856. case NODE_SYNCH_END_EVT:
  857. case NODE_SYNCH_BEGIN_EVT:
  858. case NODE_FAILOVER_BEGIN_EVT:
  859. case NODE_FAILOVER_END_EVT:
  860. default:
  861. goto illegal_evt;
  862. }
  863. break;
  864. case SELF_LEAVING_PEER_DOWN:
  865. switch (evt) {
  866. case SELF_LOST_CONTACT_EVT:
  867. state = SELF_DOWN_PEER_DOWN;
  868. break;
  869. case SELF_ESTABL_CONTACT_EVT:
  870. case PEER_ESTABL_CONTACT_EVT:
  871. case PEER_LOST_CONTACT_EVT:
  872. break;
  873. case NODE_SYNCH_END_EVT:
  874. case NODE_SYNCH_BEGIN_EVT:
  875. case NODE_FAILOVER_BEGIN_EVT:
  876. case NODE_FAILOVER_END_EVT:
  877. default:
  878. goto illegal_evt;
  879. }
  880. break;
  881. case NODE_FAILINGOVER:
  882. switch (evt) {
  883. case SELF_LOST_CONTACT_EVT:
  884. state = SELF_DOWN_PEER_LEAVING;
  885. break;
  886. case PEER_LOST_CONTACT_EVT:
  887. state = SELF_LEAVING_PEER_DOWN;
  888. break;
  889. case NODE_FAILOVER_END_EVT:
  890. state = SELF_UP_PEER_UP;
  891. break;
  892. case NODE_FAILOVER_BEGIN_EVT:
  893. case SELF_ESTABL_CONTACT_EVT:
  894. case PEER_ESTABL_CONTACT_EVT:
  895. break;
  896. case NODE_SYNCH_BEGIN_EVT:
  897. case NODE_SYNCH_END_EVT:
  898. default:
  899. goto illegal_evt;
  900. }
  901. break;
  902. case NODE_SYNCHING:
  903. switch (evt) {
  904. case SELF_LOST_CONTACT_EVT:
  905. state = SELF_DOWN_PEER_LEAVING;
  906. break;
  907. case PEER_LOST_CONTACT_EVT:
  908. state = SELF_LEAVING_PEER_DOWN;
  909. break;
  910. case NODE_SYNCH_END_EVT:
  911. state = SELF_UP_PEER_UP;
  912. break;
  913. case NODE_FAILOVER_BEGIN_EVT:
  914. state = NODE_FAILINGOVER;
  915. break;
  916. case NODE_SYNCH_BEGIN_EVT:
  917. case SELF_ESTABL_CONTACT_EVT:
  918. case PEER_ESTABL_CONTACT_EVT:
  919. break;
  920. case NODE_FAILOVER_END_EVT:
  921. default:
  922. goto illegal_evt;
  923. }
  924. break;
  925. default:
  926. pr_err("Unknown node fsm state %x\n", state);
  927. break;
  928. }
  929. n->state = state;
  930. return;
  931. illegal_evt:
  932. pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
  933. }
  934. static void node_lost_contact(struct tipc_node *n,
  935. struct sk_buff_head *inputq)
  936. {
  937. char addr_string[16];
  938. struct tipc_sock_conn *conn, *safe;
  939. struct tipc_link *l;
  940. struct list_head *conns = &n->conn_sks;
  941. struct sk_buff *skb;
  942. uint i;
  943. pr_debug("Lost contact with %s\n",
  944. tipc_addr_string_fill(addr_string, n->addr));
  945. /* Clean up broadcast state */
  946. tipc_bcast_remove_peer(n->net, n->bc_entry.link);
  947. /* Abort any ongoing link failover */
  948. for (i = 0; i < MAX_BEARERS; i++) {
  949. l = n->links[i].link;
  950. if (l)
  951. tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
  952. }
  953. /* Notify publications from this node */
  954. n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
  955. /* Notify sockets connected to node */
  956. list_for_each_entry_safe(conn, safe, conns, list) {
  957. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
  958. SHORT_H_SIZE, 0, tipc_own_addr(n->net),
  959. conn->peer_node, conn->port,
  960. conn->peer_port, TIPC_ERR_NO_NODE);
  961. if (likely(skb))
  962. skb_queue_tail(inputq, skb);
  963. list_del(&conn->list);
  964. kfree(conn);
  965. }
  966. }
  967. /**
  968. * tipc_node_get_linkname - get the name of a link
  969. *
  970. * @bearer_id: id of the bearer
  971. * @node: peer node address
  972. * @linkname: link name output buffer
  973. *
  974. * Returns 0 on success
  975. */
  976. int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
  977. char *linkname, size_t len)
  978. {
  979. struct tipc_link *link;
  980. int err = -EINVAL;
  981. struct tipc_node *node = tipc_node_find(net, addr);
  982. if (!node)
  983. return err;
  984. if (bearer_id >= MAX_BEARERS)
  985. goto exit;
  986. tipc_node_read_lock(node);
  987. link = node->links[bearer_id].link;
  988. if (link) {
  989. strncpy(linkname, tipc_link_name(link), len);
  990. err = 0;
  991. }
  992. exit:
  993. tipc_node_read_unlock(node);
  994. tipc_node_put(node);
  995. return err;
  996. }
  997. /* Caller should hold node lock for the passed node */
  998. static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
  999. {
  1000. void *hdr;
  1001. struct nlattr *attrs;
  1002. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1003. NLM_F_MULTI, TIPC_NL_NODE_GET);
  1004. if (!hdr)
  1005. return -EMSGSIZE;
  1006. attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
  1007. if (!attrs)
  1008. goto msg_full;
  1009. if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
  1010. goto attr_msg_full;
  1011. if (tipc_node_is_up(node))
  1012. if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
  1013. goto attr_msg_full;
  1014. nla_nest_end(msg->skb, attrs);
  1015. genlmsg_end(msg->skb, hdr);
  1016. return 0;
  1017. attr_msg_full:
  1018. nla_nest_cancel(msg->skb, attrs);
  1019. msg_full:
  1020. genlmsg_cancel(msg->skb, hdr);
  1021. return -EMSGSIZE;
  1022. }
  1023. /**
  1024. * tipc_node_xmit() is the general link level function for message sending
  1025. * @net: the applicable net namespace
  1026. * @list: chain of buffers containing message
  1027. * @dnode: address of destination node
  1028. * @selector: a number used for deterministic link selection
  1029. * Consumes the buffer chain, except when returning -ELINKCONG
  1030. * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
  1031. */
  1032. int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
  1033. u32 dnode, int selector)
  1034. {
  1035. struct tipc_link_entry *le = NULL;
  1036. struct tipc_node *n;
  1037. struct sk_buff_head xmitq;
  1038. int bearer_id;
  1039. int rc;
  1040. if (in_own_node(net, dnode)) {
  1041. tipc_sk_rcv(net, list);
  1042. return 0;
  1043. }
  1044. n = tipc_node_find(net, dnode);
  1045. if (unlikely(!n)) {
  1046. skb_queue_purge(list);
  1047. return -EHOSTUNREACH;
  1048. }
  1049. tipc_node_read_lock(n);
  1050. bearer_id = n->active_links[selector & 1];
  1051. if (unlikely(bearer_id == INVALID_BEARER_ID)) {
  1052. tipc_node_read_unlock(n);
  1053. tipc_node_put(n);
  1054. skb_queue_purge(list);
  1055. return -EHOSTUNREACH;
  1056. }
  1057. __skb_queue_head_init(&xmitq);
  1058. le = &n->links[bearer_id];
  1059. spin_lock_bh(&le->lock);
  1060. rc = tipc_link_xmit(le->link, list, &xmitq);
  1061. spin_unlock_bh(&le->lock);
  1062. tipc_node_read_unlock(n);
  1063. if (likely(rc == 0))
  1064. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1065. else if (rc == -ENOBUFS)
  1066. tipc_node_link_down(n, bearer_id, false);
  1067. tipc_node_put(n);
  1068. return rc;
  1069. }
  1070. /* tipc_node_xmit_skb(): send single buffer to destination
  1071. * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
  1072. * messages, which will not be rejected
  1073. * The only exception is datagram messages rerouted after secondary
  1074. * lookup, which are rare and safe to dispose of anyway.
  1075. * TODO: Return real return value, and let callers use
  1076. * tipc_wait_for_sendpkt() where applicable
  1077. */
  1078. int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  1079. u32 selector)
  1080. {
  1081. struct sk_buff_head head;
  1082. int rc;
  1083. skb_queue_head_init(&head);
  1084. __skb_queue_tail(&head, skb);
  1085. rc = tipc_node_xmit(net, &head, dnode, selector);
  1086. if (rc == -ELINKCONG)
  1087. kfree_skb(skb);
  1088. return 0;
  1089. }
  1090. void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
  1091. {
  1092. struct sk_buff *txskb;
  1093. struct tipc_node *n;
  1094. u32 dst;
  1095. rcu_read_lock();
  1096. list_for_each_entry_rcu(n, tipc_nodes(net), list) {
  1097. dst = n->addr;
  1098. if (in_own_node(net, dst))
  1099. continue;
  1100. if (!tipc_node_is_up(n))
  1101. continue;
  1102. txskb = pskb_copy(skb, GFP_ATOMIC);
  1103. if (!txskb)
  1104. break;
  1105. msg_set_destnode(buf_msg(txskb), dst);
  1106. tipc_node_xmit_skb(net, txskb, dst, 0);
  1107. }
  1108. rcu_read_unlock();
  1109. kfree_skb(skb);
  1110. }
  1111. /**
  1112. * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
  1113. * @net: the applicable net namespace
  1114. * @skb: TIPC packet
  1115. * @bearer_id: id of bearer message arrived on
  1116. *
  1117. * Invoked with no locks held.
  1118. */
  1119. static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
  1120. {
  1121. int rc;
  1122. struct sk_buff_head xmitq;
  1123. struct tipc_bclink_entry *be;
  1124. struct tipc_link_entry *le;
  1125. struct tipc_msg *hdr = buf_msg(skb);
  1126. int usr = msg_user(hdr);
  1127. u32 dnode = msg_destnode(hdr);
  1128. struct tipc_node *n;
  1129. __skb_queue_head_init(&xmitq);
  1130. /* If NACK for other node, let rcv link for that node peek into it */
  1131. if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
  1132. n = tipc_node_find(net, dnode);
  1133. else
  1134. n = tipc_node_find(net, msg_prevnode(hdr));
  1135. if (!n) {
  1136. kfree_skb(skb);
  1137. return;
  1138. }
  1139. be = &n->bc_entry;
  1140. le = &n->links[bearer_id];
  1141. rc = tipc_bcast_rcv(net, be->link, skb);
  1142. /* Broadcast link reset may happen at reassembly failure */
  1143. if (rc & TIPC_LINK_DOWN_EVT)
  1144. tipc_node_reset_links(n);
  1145. /* Broadcast ACKs are sent on a unicast link */
  1146. if (rc & TIPC_LINK_SND_BC_ACK) {
  1147. tipc_node_read_lock(n);
  1148. tipc_link_build_state_msg(le->link, &xmitq);
  1149. tipc_node_read_unlock(n);
  1150. }
  1151. if (!skb_queue_empty(&xmitq))
  1152. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1153. /* Deliver. 'arrvq' is under inputq2's lock protection */
  1154. if (!skb_queue_empty(&be->inputq1)) {
  1155. spin_lock_bh(&be->inputq2.lock);
  1156. spin_lock_bh(&be->inputq1.lock);
  1157. skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
  1158. spin_unlock_bh(&be->inputq1.lock);
  1159. spin_unlock_bh(&be->inputq2.lock);
  1160. tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
  1161. }
  1162. tipc_node_put(n);
  1163. }
  1164. /**
  1165. * tipc_node_check_state - check and if necessary update node state
  1166. * @skb: TIPC packet
  1167. * @bearer_id: identity of bearer delivering the packet
  1168. * Returns true if state is ok, otherwise consumes buffer and returns false
  1169. */
  1170. static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
  1171. int bearer_id, struct sk_buff_head *xmitq)
  1172. {
  1173. struct tipc_msg *hdr = buf_msg(skb);
  1174. int usr = msg_user(hdr);
  1175. int mtyp = msg_type(hdr);
  1176. u16 oseqno = msg_seqno(hdr);
  1177. u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
  1178. u16 exp_pkts = msg_msgcnt(hdr);
  1179. u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
  1180. int state = n->state;
  1181. struct tipc_link *l, *tnl, *pl = NULL;
  1182. struct tipc_media_addr *maddr;
  1183. int pb_id;
  1184. l = n->links[bearer_id].link;
  1185. if (!l)
  1186. return false;
  1187. rcv_nxt = tipc_link_rcv_nxt(l);
  1188. if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
  1189. return true;
  1190. /* Find parallel link, if any */
  1191. for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
  1192. if ((pb_id != bearer_id) && n->links[pb_id].link) {
  1193. pl = n->links[pb_id].link;
  1194. break;
  1195. }
  1196. }
  1197. /* Check and update node accesibility if applicable */
  1198. if (state == SELF_UP_PEER_COMING) {
  1199. if (!tipc_link_is_up(l))
  1200. return true;
  1201. if (!msg_peer_link_is_up(hdr))
  1202. return true;
  1203. tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
  1204. }
  1205. if (state == SELF_DOWN_PEER_LEAVING) {
  1206. if (msg_peer_node_is_up(hdr))
  1207. return false;
  1208. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  1209. return true;
  1210. }
  1211. if (state == SELF_LEAVING_PEER_DOWN)
  1212. return false;
  1213. /* Ignore duplicate packets */
  1214. if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
  1215. return true;
  1216. /* Initiate or update failover mode if applicable */
  1217. if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
  1218. syncpt = oseqno + exp_pkts - 1;
  1219. if (pl && tipc_link_is_up(pl)) {
  1220. __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
  1221. tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
  1222. tipc_link_inputq(l));
  1223. }
  1224. /* If pkts arrive out of order, use lowest calculated syncpt */
  1225. if (less(syncpt, n->sync_point))
  1226. n->sync_point = syncpt;
  1227. }
  1228. /* Open parallel link when tunnel link reaches synch point */
  1229. if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
  1230. if (!more(rcv_nxt, n->sync_point))
  1231. return true;
  1232. tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
  1233. if (pl)
  1234. tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
  1235. return true;
  1236. }
  1237. /* No synching needed if only one link */
  1238. if (!pl || !tipc_link_is_up(pl))
  1239. return true;
  1240. /* Initiate synch mode if applicable */
  1241. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
  1242. syncpt = iseqno + exp_pkts - 1;
  1243. if (!tipc_link_is_up(l)) {
  1244. tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1245. __tipc_node_link_up(n, bearer_id, xmitq);
  1246. }
  1247. if (n->state == SELF_UP_PEER_UP) {
  1248. n->sync_point = syncpt;
  1249. tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
  1250. tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
  1251. }
  1252. }
  1253. /* Open tunnel link when parallel link reaches synch point */
  1254. if (n->state == NODE_SYNCHING) {
  1255. if (tipc_link_is_synching(l)) {
  1256. tnl = l;
  1257. } else {
  1258. tnl = pl;
  1259. pl = l;
  1260. }
  1261. inputq_len = skb_queue_len(tipc_link_inputq(pl));
  1262. dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
  1263. if (more(dlv_nxt, n->sync_point)) {
  1264. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  1265. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  1266. return true;
  1267. }
  1268. if (l == pl)
  1269. return true;
  1270. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
  1271. return true;
  1272. if (usr == LINK_PROTOCOL)
  1273. return true;
  1274. return false;
  1275. }
  1276. return true;
  1277. }
  1278. /**
  1279. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1280. * @net: the applicable net namespace
  1281. * @skb: TIPC packet
  1282. * @bearer: pointer to bearer message arrived on
  1283. *
  1284. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1285. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1286. */
  1287. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
  1288. {
  1289. struct sk_buff_head xmitq;
  1290. struct tipc_node *n;
  1291. struct tipc_msg *hdr = buf_msg(skb);
  1292. int usr = msg_user(hdr);
  1293. int bearer_id = b->identity;
  1294. struct tipc_link_entry *le;
  1295. u16 bc_ack = msg_bcast_ack(hdr);
  1296. int rc = 0;
  1297. __skb_queue_head_init(&xmitq);
  1298. /* Ensure message is well-formed */
  1299. if (unlikely(!tipc_msg_validate(skb)))
  1300. goto discard;
  1301. /* Handle arrival of discovery or broadcast packet */
  1302. if (unlikely(msg_non_seq(hdr))) {
  1303. if (unlikely(usr == LINK_CONFIG))
  1304. return tipc_disc_rcv(net, skb, b);
  1305. else
  1306. return tipc_node_bc_rcv(net, skb, bearer_id);
  1307. }
  1308. /* Locate neighboring node that sent packet */
  1309. n = tipc_node_find(net, msg_prevnode(hdr));
  1310. if (unlikely(!n))
  1311. goto discard;
  1312. le = &n->links[bearer_id];
  1313. /* Ensure broadcast reception is in synch with peer's send state */
  1314. if (unlikely(usr == LINK_PROTOCOL))
  1315. tipc_bcast_sync_rcv(net, n->bc_entry.link, hdr);
  1316. else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
  1317. tipc_bcast_ack_rcv(net, n->bc_entry.link, bc_ack);
  1318. /* Receive packet directly if conditions permit */
  1319. tipc_node_read_lock(n);
  1320. if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
  1321. spin_lock_bh(&le->lock);
  1322. if (le->link) {
  1323. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1324. skb = NULL;
  1325. }
  1326. spin_unlock_bh(&le->lock);
  1327. }
  1328. tipc_node_read_unlock(n);
  1329. /* Check/update node state before receiving */
  1330. if (unlikely(skb)) {
  1331. tipc_node_write_lock(n);
  1332. if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
  1333. if (le->link) {
  1334. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1335. skb = NULL;
  1336. }
  1337. }
  1338. tipc_node_write_unlock(n);
  1339. }
  1340. if (unlikely(rc & TIPC_LINK_UP_EVT))
  1341. tipc_node_link_up(n, bearer_id, &xmitq);
  1342. if (unlikely(rc & TIPC_LINK_DOWN_EVT))
  1343. tipc_node_link_down(n, bearer_id, false);
  1344. if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
  1345. tipc_named_rcv(net, &n->bc_entry.namedq);
  1346. if (!skb_queue_empty(&le->inputq))
  1347. tipc_sk_rcv(net, &le->inputq);
  1348. if (!skb_queue_empty(&xmitq))
  1349. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1350. tipc_node_put(n);
  1351. discard:
  1352. kfree_skb(skb);
  1353. }
  1354. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1355. {
  1356. int err;
  1357. struct net *net = sock_net(skb->sk);
  1358. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1359. int done = cb->args[0];
  1360. int last_addr = cb->args[1];
  1361. struct tipc_node *node;
  1362. struct tipc_nl_msg msg;
  1363. if (done)
  1364. return 0;
  1365. msg.skb = skb;
  1366. msg.portid = NETLINK_CB(cb->skb).portid;
  1367. msg.seq = cb->nlh->nlmsg_seq;
  1368. rcu_read_lock();
  1369. if (last_addr) {
  1370. node = tipc_node_find(net, last_addr);
  1371. if (!node) {
  1372. rcu_read_unlock();
  1373. /* We never set seq or call nl_dump_check_consistent()
  1374. * this means that setting prev_seq here will cause the
  1375. * consistence check to fail in the netlink callback
  1376. * handler. Resulting in the NLMSG_DONE message having
  1377. * the NLM_F_DUMP_INTR flag set if the node state
  1378. * changed while we released the lock.
  1379. */
  1380. cb->prev_seq = 1;
  1381. return -EPIPE;
  1382. }
  1383. tipc_node_put(node);
  1384. }
  1385. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1386. if (last_addr) {
  1387. if (node->addr == last_addr)
  1388. last_addr = 0;
  1389. else
  1390. continue;
  1391. }
  1392. tipc_node_read_lock(node);
  1393. err = __tipc_nl_add_node(&msg, node);
  1394. if (err) {
  1395. last_addr = node->addr;
  1396. tipc_node_read_unlock(node);
  1397. goto out;
  1398. }
  1399. tipc_node_read_unlock(node);
  1400. }
  1401. done = 1;
  1402. out:
  1403. cb->args[0] = done;
  1404. cb->args[1] = last_addr;
  1405. rcu_read_unlock();
  1406. return skb->len;
  1407. }
  1408. /* tipc_node_find_by_name - locate owner node of link by link's name
  1409. * @net: the applicable net namespace
  1410. * @name: pointer to link name string
  1411. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1412. *
  1413. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1414. */
  1415. static struct tipc_node *tipc_node_find_by_name(struct net *net,
  1416. const char *link_name,
  1417. unsigned int *bearer_id)
  1418. {
  1419. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1420. struct tipc_link *l;
  1421. struct tipc_node *n;
  1422. struct tipc_node *found_node = NULL;
  1423. int i;
  1424. *bearer_id = 0;
  1425. rcu_read_lock();
  1426. list_for_each_entry_rcu(n, &tn->node_list, list) {
  1427. tipc_node_read_lock(n);
  1428. for (i = 0; i < MAX_BEARERS; i++) {
  1429. l = n->links[i].link;
  1430. if (l && !strcmp(tipc_link_name(l), link_name)) {
  1431. *bearer_id = i;
  1432. found_node = n;
  1433. break;
  1434. }
  1435. }
  1436. tipc_node_read_unlock(n);
  1437. if (found_node)
  1438. break;
  1439. }
  1440. rcu_read_unlock();
  1441. return found_node;
  1442. }
  1443. int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
  1444. {
  1445. int err;
  1446. int res = 0;
  1447. int bearer_id;
  1448. char *name;
  1449. struct tipc_link *link;
  1450. struct tipc_node *node;
  1451. struct sk_buff_head xmitq;
  1452. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1453. struct net *net = sock_net(skb->sk);
  1454. __skb_queue_head_init(&xmitq);
  1455. if (!info->attrs[TIPC_NLA_LINK])
  1456. return -EINVAL;
  1457. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1458. info->attrs[TIPC_NLA_LINK],
  1459. tipc_nl_link_policy);
  1460. if (err)
  1461. return err;
  1462. if (!attrs[TIPC_NLA_LINK_NAME])
  1463. return -EINVAL;
  1464. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1465. if (strcmp(name, tipc_bclink_name) == 0)
  1466. return tipc_nl_bc_link_set(net, attrs);
  1467. node = tipc_node_find_by_name(net, name, &bearer_id);
  1468. if (!node)
  1469. return -EINVAL;
  1470. tipc_node_read_lock(node);
  1471. link = node->links[bearer_id].link;
  1472. if (!link) {
  1473. res = -EINVAL;
  1474. goto out;
  1475. }
  1476. if (attrs[TIPC_NLA_LINK_PROP]) {
  1477. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1478. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1479. props);
  1480. if (err) {
  1481. res = err;
  1482. goto out;
  1483. }
  1484. if (props[TIPC_NLA_PROP_TOL]) {
  1485. u32 tol;
  1486. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1487. tipc_link_set_tolerance(link, tol, &xmitq);
  1488. }
  1489. if (props[TIPC_NLA_PROP_PRIO]) {
  1490. u32 prio;
  1491. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1492. tipc_link_set_prio(link, prio, &xmitq);
  1493. }
  1494. if (props[TIPC_NLA_PROP_WIN]) {
  1495. u32 win;
  1496. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1497. tipc_link_set_queue_limits(link, win);
  1498. }
  1499. }
  1500. out:
  1501. tipc_node_read_unlock(node);
  1502. tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
  1503. return res;
  1504. }
  1505. int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
  1506. {
  1507. struct net *net = genl_info_net(info);
  1508. struct tipc_nl_msg msg;
  1509. char *name;
  1510. int err;
  1511. msg.portid = info->snd_portid;
  1512. msg.seq = info->snd_seq;
  1513. if (!info->attrs[TIPC_NLA_LINK_NAME])
  1514. return -EINVAL;
  1515. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  1516. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1517. if (!msg.skb)
  1518. return -ENOMEM;
  1519. if (strcmp(name, tipc_bclink_name) == 0) {
  1520. err = tipc_nl_add_bc_link(net, &msg);
  1521. if (err) {
  1522. nlmsg_free(msg.skb);
  1523. return err;
  1524. }
  1525. } else {
  1526. int bearer_id;
  1527. struct tipc_node *node;
  1528. struct tipc_link *link;
  1529. node = tipc_node_find_by_name(net, name, &bearer_id);
  1530. if (!node)
  1531. return -EINVAL;
  1532. tipc_node_read_lock(node);
  1533. link = node->links[bearer_id].link;
  1534. if (!link) {
  1535. tipc_node_read_unlock(node);
  1536. nlmsg_free(msg.skb);
  1537. return -EINVAL;
  1538. }
  1539. err = __tipc_nl_add_link(net, &msg, link, 0);
  1540. tipc_node_read_unlock(node);
  1541. if (err) {
  1542. nlmsg_free(msg.skb);
  1543. return err;
  1544. }
  1545. }
  1546. return genlmsg_reply(msg.skb, info);
  1547. }
  1548. int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
  1549. {
  1550. int err;
  1551. char *link_name;
  1552. unsigned int bearer_id;
  1553. struct tipc_link *link;
  1554. struct tipc_node *node;
  1555. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1556. struct net *net = sock_net(skb->sk);
  1557. struct tipc_link_entry *le;
  1558. if (!info->attrs[TIPC_NLA_LINK])
  1559. return -EINVAL;
  1560. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1561. info->attrs[TIPC_NLA_LINK],
  1562. tipc_nl_link_policy);
  1563. if (err)
  1564. return err;
  1565. if (!attrs[TIPC_NLA_LINK_NAME])
  1566. return -EINVAL;
  1567. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1568. if (strcmp(link_name, tipc_bclink_name) == 0) {
  1569. err = tipc_bclink_reset_stats(net);
  1570. if (err)
  1571. return err;
  1572. return 0;
  1573. }
  1574. node = tipc_node_find_by_name(net, link_name, &bearer_id);
  1575. if (!node)
  1576. return -EINVAL;
  1577. le = &node->links[bearer_id];
  1578. tipc_node_read_lock(node);
  1579. spin_lock_bh(&le->lock);
  1580. link = node->links[bearer_id].link;
  1581. if (!link) {
  1582. spin_unlock_bh(&le->lock);
  1583. tipc_node_read_unlock(node);
  1584. return -EINVAL;
  1585. }
  1586. tipc_link_reset_stats(link);
  1587. spin_unlock_bh(&le->lock);
  1588. tipc_node_read_unlock(node);
  1589. return 0;
  1590. }
  1591. /* Caller should hold node lock */
  1592. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1593. struct tipc_node *node, u32 *prev_link)
  1594. {
  1595. u32 i;
  1596. int err;
  1597. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1598. *prev_link = i;
  1599. if (!node->links[i].link)
  1600. continue;
  1601. err = __tipc_nl_add_link(net, msg,
  1602. node->links[i].link, NLM_F_MULTI);
  1603. if (err)
  1604. return err;
  1605. }
  1606. *prev_link = 0;
  1607. return 0;
  1608. }
  1609. int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
  1610. {
  1611. struct net *net = sock_net(skb->sk);
  1612. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1613. struct tipc_node *node;
  1614. struct tipc_nl_msg msg;
  1615. u32 prev_node = cb->args[0];
  1616. u32 prev_link = cb->args[1];
  1617. int done = cb->args[2];
  1618. int err;
  1619. if (done)
  1620. return 0;
  1621. msg.skb = skb;
  1622. msg.portid = NETLINK_CB(cb->skb).portid;
  1623. msg.seq = cb->nlh->nlmsg_seq;
  1624. rcu_read_lock();
  1625. if (prev_node) {
  1626. node = tipc_node_find(net, prev_node);
  1627. if (!node) {
  1628. /* We never set seq or call nl_dump_check_consistent()
  1629. * this means that setting prev_seq here will cause the
  1630. * consistence check to fail in the netlink callback
  1631. * handler. Resulting in the last NLMSG_DONE message
  1632. * having the NLM_F_DUMP_INTR flag set.
  1633. */
  1634. cb->prev_seq = 1;
  1635. goto out;
  1636. }
  1637. tipc_node_put(node);
  1638. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1639. list) {
  1640. tipc_node_read_lock(node);
  1641. err = __tipc_nl_add_node_links(net, &msg, node,
  1642. &prev_link);
  1643. tipc_node_read_unlock(node);
  1644. if (err)
  1645. goto out;
  1646. prev_node = node->addr;
  1647. }
  1648. } else {
  1649. err = tipc_nl_add_bc_link(net, &msg);
  1650. if (err)
  1651. goto out;
  1652. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1653. tipc_node_read_lock(node);
  1654. err = __tipc_nl_add_node_links(net, &msg, node,
  1655. &prev_link);
  1656. tipc_node_read_unlock(node);
  1657. if (err)
  1658. goto out;
  1659. prev_node = node->addr;
  1660. }
  1661. }
  1662. done = 1;
  1663. out:
  1664. rcu_read_unlock();
  1665. cb->args[0] = prev_node;
  1666. cb->args[1] = prev_link;
  1667. cb->args[2] = done;
  1668. return skb->len;
  1669. }