node.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  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. /* First link? => give it both slots */
  480. if (!ol) {
  481. *slot0 = bearer_id;
  482. *slot1 = bearer_id;
  483. tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
  484. n->action_flags |= TIPC_NOTIFY_NODE_UP;
  485. tipc_bcast_add_peer(n->net, nl, xmitq);
  486. return;
  487. }
  488. /* Second link => redistribute slots */
  489. if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
  490. pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
  491. *slot0 = bearer_id;
  492. *slot1 = bearer_id;
  493. tipc_link_set_active(nl, true);
  494. tipc_link_set_active(ol, false);
  495. } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
  496. tipc_link_set_active(nl, true);
  497. *slot1 = bearer_id;
  498. } else {
  499. pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
  500. }
  501. /* Prepare synchronization with first link */
  502. tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
  503. }
  504. /**
  505. * tipc_node_link_up - handle addition of link
  506. *
  507. * Link becomes active (alone or shared) or standby, depending on its priority.
  508. */
  509. static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
  510. struct sk_buff_head *xmitq)
  511. {
  512. tipc_node_write_lock(n);
  513. __tipc_node_link_up(n, bearer_id, xmitq);
  514. tipc_node_write_unlock(n);
  515. }
  516. /**
  517. * __tipc_node_link_down - handle loss of link
  518. */
  519. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  520. struct sk_buff_head *xmitq,
  521. struct tipc_media_addr **maddr)
  522. {
  523. struct tipc_link_entry *le = &n->links[*bearer_id];
  524. int *slot0 = &n->active_links[0];
  525. int *slot1 = &n->active_links[1];
  526. int i, highest = 0, prio;
  527. struct tipc_link *l, *_l, *tnl;
  528. l = n->links[*bearer_id].link;
  529. if (!l || tipc_link_is_reset(l))
  530. return;
  531. n->working_links--;
  532. n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
  533. n->link_id = tipc_link_id(l);
  534. tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
  535. pr_debug("Lost link <%s> on network plane %c\n",
  536. tipc_link_name(l), tipc_link_plane(l));
  537. /* Select new active link if any available */
  538. *slot0 = INVALID_BEARER_ID;
  539. *slot1 = INVALID_BEARER_ID;
  540. for (i = 0; i < MAX_BEARERS; i++) {
  541. _l = n->links[i].link;
  542. if (!_l || !tipc_link_is_up(_l))
  543. continue;
  544. if (_l == l)
  545. continue;
  546. prio = tipc_link_prio(_l);
  547. if (prio < highest)
  548. continue;
  549. if (prio > highest) {
  550. highest = prio;
  551. *slot0 = i;
  552. *slot1 = i;
  553. continue;
  554. }
  555. *slot1 = i;
  556. }
  557. if (!tipc_node_is_up(n)) {
  558. if (tipc_link_peer_is_down(l))
  559. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  560. tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
  561. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  562. tipc_link_reset(l);
  563. tipc_link_build_reset_msg(l, xmitq);
  564. *maddr = &n->links[*bearer_id].maddr;
  565. node_lost_contact(n, &le->inputq);
  566. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  567. return;
  568. }
  569. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  570. /* There is still a working link => initiate failover */
  571. *bearer_id = n->active_links[0];
  572. tnl = n->links[*bearer_id].link;
  573. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  574. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  575. n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
  576. tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
  577. tipc_link_reset(l);
  578. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  579. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  580. tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
  581. *maddr = &n->links[*bearer_id].maddr;
  582. }
  583. static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
  584. {
  585. struct tipc_link_entry *le = &n->links[bearer_id];
  586. struct tipc_link *l = le->link;
  587. struct tipc_media_addr *maddr;
  588. struct sk_buff_head xmitq;
  589. if (!l)
  590. return;
  591. __skb_queue_head_init(&xmitq);
  592. tipc_node_write_lock(n);
  593. if (!tipc_link_is_establishing(l)) {
  594. __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
  595. if (delete) {
  596. kfree(l);
  597. le->link = NULL;
  598. n->link_cnt--;
  599. }
  600. } else {
  601. /* Defuse pending tipc_node_link_up() */
  602. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  603. }
  604. tipc_node_write_unlock(n);
  605. tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
  606. tipc_sk_rcv(n->net, &le->inputq);
  607. }
  608. static bool tipc_node_is_up(struct tipc_node *n)
  609. {
  610. return n->active_links[0] != INVALID_BEARER_ID;
  611. }
  612. void tipc_node_check_dest(struct net *net, u32 onode,
  613. struct tipc_bearer *b,
  614. u16 capabilities, u32 signature,
  615. struct tipc_media_addr *maddr,
  616. bool *respond, bool *dupl_addr)
  617. {
  618. struct tipc_node *n;
  619. struct tipc_link *l;
  620. struct tipc_link_entry *le;
  621. bool addr_match = false;
  622. bool sign_match = false;
  623. bool link_up = false;
  624. bool accept_addr = false;
  625. bool reset = true;
  626. char *if_name;
  627. *dupl_addr = false;
  628. *respond = false;
  629. n = tipc_node_create(net, onode, capabilities);
  630. if (!n)
  631. return;
  632. tipc_node_write_lock(n);
  633. le = &n->links[b->identity];
  634. /* Prepare to validate requesting node's signature and media address */
  635. l = le->link;
  636. link_up = l && tipc_link_is_up(l);
  637. addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
  638. sign_match = (signature == n->signature);
  639. /* These three flags give us eight permutations: */
  640. if (sign_match && addr_match && link_up) {
  641. /* All is fine. Do nothing. */
  642. reset = false;
  643. } else if (sign_match && addr_match && !link_up) {
  644. /* Respond. The link will come up in due time */
  645. *respond = true;
  646. } else if (sign_match && !addr_match && link_up) {
  647. /* Peer has changed i/f address without rebooting.
  648. * If so, the link will reset soon, and the next
  649. * discovery will be accepted. So we can ignore it.
  650. * It may also be an cloned or malicious peer having
  651. * chosen the same node address and signature as an
  652. * existing one.
  653. * Ignore requests until the link goes down, if ever.
  654. */
  655. *dupl_addr = true;
  656. } else if (sign_match && !addr_match && !link_up) {
  657. /* Peer link has changed i/f address without rebooting.
  658. * It may also be a cloned or malicious peer; we can't
  659. * distinguish between the two.
  660. * The signature is correct, so we must accept.
  661. */
  662. accept_addr = true;
  663. *respond = true;
  664. } else if (!sign_match && addr_match && link_up) {
  665. /* Peer node rebooted. Two possibilities:
  666. * - Delayed re-discovery; this link endpoint has already
  667. * reset and re-established contact with the peer, before
  668. * receiving a discovery message from that node.
  669. * (The peer happened to receive one from this node first).
  670. * - The peer came back so fast that our side has not
  671. * discovered it yet. Probing from this side will soon
  672. * reset the link, since there can be no working link
  673. * endpoint at the peer end, and the link will re-establish.
  674. * Accept the signature, since it comes from a known peer.
  675. */
  676. n->signature = signature;
  677. } else if (!sign_match && addr_match && !link_up) {
  678. /* The peer node has rebooted.
  679. * Accept signature, since it is a known peer.
  680. */
  681. n->signature = signature;
  682. *respond = true;
  683. } else if (!sign_match && !addr_match && link_up) {
  684. /* Peer rebooted with new address, or a new/duplicate peer.
  685. * Ignore until the link goes down, if ever.
  686. */
  687. *dupl_addr = true;
  688. } else if (!sign_match && !addr_match && !link_up) {
  689. /* Peer rebooted with new address, or it is a new peer.
  690. * Accept signature and address.
  691. */
  692. n->signature = signature;
  693. accept_addr = true;
  694. *respond = true;
  695. }
  696. if (!accept_addr)
  697. goto exit;
  698. /* Now create new link if not already existing */
  699. if (!l) {
  700. if (n->link_cnt == 2) {
  701. pr_warn("Cannot establish 3rd link to %x\n", n->addr);
  702. goto exit;
  703. }
  704. if_name = strchr(b->name, ':') + 1;
  705. if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
  706. b->net_plane, b->mtu, b->priority,
  707. b->window, mod(tipc_net(net)->random),
  708. tipc_own_addr(net), onode,
  709. n->capabilities,
  710. tipc_bc_sndlink(n->net), n->bc_entry.link,
  711. &le->inputq,
  712. &n->bc_entry.namedq, &l)) {
  713. *respond = false;
  714. goto exit;
  715. }
  716. tipc_link_reset(l);
  717. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  718. if (n->state == NODE_FAILINGOVER)
  719. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  720. le->link = l;
  721. n->link_cnt++;
  722. tipc_node_calculate_timer(n, l);
  723. if (n->link_cnt == 1)
  724. if (!mod_timer(&n->timer, jiffies + n->keepalive_intv))
  725. tipc_node_get(n);
  726. }
  727. memcpy(&le->maddr, maddr, sizeof(*maddr));
  728. exit:
  729. tipc_node_write_unlock(n);
  730. if (reset && l && !tipc_link_is_reset(l))
  731. tipc_node_link_down(n, b->identity, false);
  732. tipc_node_put(n);
  733. }
  734. void tipc_node_delete_links(struct net *net, int bearer_id)
  735. {
  736. struct tipc_net *tn = net_generic(net, tipc_net_id);
  737. struct tipc_node *n;
  738. rcu_read_lock();
  739. list_for_each_entry_rcu(n, &tn->node_list, list) {
  740. tipc_node_link_down(n, bearer_id, true);
  741. }
  742. rcu_read_unlock();
  743. }
  744. static void tipc_node_reset_links(struct tipc_node *n)
  745. {
  746. char addr_string[16];
  747. int i;
  748. pr_warn("Resetting all links to %s\n",
  749. tipc_addr_string_fill(addr_string, n->addr));
  750. for (i = 0; i < MAX_BEARERS; i++) {
  751. tipc_node_link_down(n, i, false);
  752. }
  753. }
  754. /* tipc_node_fsm_evt - node finite state machine
  755. * Determines when contact is allowed with peer node
  756. */
  757. static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
  758. {
  759. int state = n->state;
  760. switch (state) {
  761. case SELF_DOWN_PEER_DOWN:
  762. switch (evt) {
  763. case SELF_ESTABL_CONTACT_EVT:
  764. state = SELF_UP_PEER_COMING;
  765. break;
  766. case PEER_ESTABL_CONTACT_EVT:
  767. state = SELF_COMING_PEER_UP;
  768. break;
  769. case SELF_LOST_CONTACT_EVT:
  770. case PEER_LOST_CONTACT_EVT:
  771. break;
  772. case NODE_SYNCH_END_EVT:
  773. case NODE_SYNCH_BEGIN_EVT:
  774. case NODE_FAILOVER_BEGIN_EVT:
  775. case NODE_FAILOVER_END_EVT:
  776. default:
  777. goto illegal_evt;
  778. }
  779. break;
  780. case SELF_UP_PEER_UP:
  781. switch (evt) {
  782. case SELF_LOST_CONTACT_EVT:
  783. state = SELF_DOWN_PEER_LEAVING;
  784. break;
  785. case PEER_LOST_CONTACT_EVT:
  786. state = SELF_LEAVING_PEER_DOWN;
  787. break;
  788. case NODE_SYNCH_BEGIN_EVT:
  789. state = NODE_SYNCHING;
  790. break;
  791. case NODE_FAILOVER_BEGIN_EVT:
  792. state = NODE_FAILINGOVER;
  793. break;
  794. case SELF_ESTABL_CONTACT_EVT:
  795. case PEER_ESTABL_CONTACT_EVT:
  796. case NODE_SYNCH_END_EVT:
  797. case NODE_FAILOVER_END_EVT:
  798. break;
  799. default:
  800. goto illegal_evt;
  801. }
  802. break;
  803. case SELF_DOWN_PEER_LEAVING:
  804. switch (evt) {
  805. case PEER_LOST_CONTACT_EVT:
  806. state = SELF_DOWN_PEER_DOWN;
  807. break;
  808. case SELF_ESTABL_CONTACT_EVT:
  809. case PEER_ESTABL_CONTACT_EVT:
  810. case SELF_LOST_CONTACT_EVT:
  811. break;
  812. case NODE_SYNCH_END_EVT:
  813. case NODE_SYNCH_BEGIN_EVT:
  814. case NODE_FAILOVER_BEGIN_EVT:
  815. case NODE_FAILOVER_END_EVT:
  816. default:
  817. goto illegal_evt;
  818. }
  819. break;
  820. case SELF_UP_PEER_COMING:
  821. switch (evt) {
  822. case PEER_ESTABL_CONTACT_EVT:
  823. state = SELF_UP_PEER_UP;
  824. break;
  825. case SELF_LOST_CONTACT_EVT:
  826. state = SELF_DOWN_PEER_LEAVING;
  827. break;
  828. case SELF_ESTABL_CONTACT_EVT:
  829. case PEER_LOST_CONTACT_EVT:
  830. case NODE_SYNCH_END_EVT:
  831. case NODE_FAILOVER_BEGIN_EVT:
  832. break;
  833. case NODE_SYNCH_BEGIN_EVT:
  834. case NODE_FAILOVER_END_EVT:
  835. default:
  836. goto illegal_evt;
  837. }
  838. break;
  839. case SELF_COMING_PEER_UP:
  840. switch (evt) {
  841. case SELF_ESTABL_CONTACT_EVT:
  842. state = SELF_UP_PEER_UP;
  843. break;
  844. case PEER_LOST_CONTACT_EVT:
  845. state = SELF_LEAVING_PEER_DOWN;
  846. break;
  847. case SELF_LOST_CONTACT_EVT:
  848. case PEER_ESTABL_CONTACT_EVT:
  849. break;
  850. case NODE_SYNCH_END_EVT:
  851. case NODE_SYNCH_BEGIN_EVT:
  852. case NODE_FAILOVER_BEGIN_EVT:
  853. case NODE_FAILOVER_END_EVT:
  854. default:
  855. goto illegal_evt;
  856. }
  857. break;
  858. case SELF_LEAVING_PEER_DOWN:
  859. switch (evt) {
  860. case SELF_LOST_CONTACT_EVT:
  861. state = SELF_DOWN_PEER_DOWN;
  862. break;
  863. case SELF_ESTABL_CONTACT_EVT:
  864. case PEER_ESTABL_CONTACT_EVT:
  865. case PEER_LOST_CONTACT_EVT:
  866. break;
  867. case NODE_SYNCH_END_EVT:
  868. case NODE_SYNCH_BEGIN_EVT:
  869. case NODE_FAILOVER_BEGIN_EVT:
  870. case NODE_FAILOVER_END_EVT:
  871. default:
  872. goto illegal_evt;
  873. }
  874. break;
  875. case NODE_FAILINGOVER:
  876. switch (evt) {
  877. case SELF_LOST_CONTACT_EVT:
  878. state = SELF_DOWN_PEER_LEAVING;
  879. break;
  880. case PEER_LOST_CONTACT_EVT:
  881. state = SELF_LEAVING_PEER_DOWN;
  882. break;
  883. case NODE_FAILOVER_END_EVT:
  884. state = SELF_UP_PEER_UP;
  885. break;
  886. case NODE_FAILOVER_BEGIN_EVT:
  887. case SELF_ESTABL_CONTACT_EVT:
  888. case PEER_ESTABL_CONTACT_EVT:
  889. break;
  890. case NODE_SYNCH_BEGIN_EVT:
  891. case NODE_SYNCH_END_EVT:
  892. default:
  893. goto illegal_evt;
  894. }
  895. break;
  896. case NODE_SYNCHING:
  897. switch (evt) {
  898. case SELF_LOST_CONTACT_EVT:
  899. state = SELF_DOWN_PEER_LEAVING;
  900. break;
  901. case PEER_LOST_CONTACT_EVT:
  902. state = SELF_LEAVING_PEER_DOWN;
  903. break;
  904. case NODE_SYNCH_END_EVT:
  905. state = SELF_UP_PEER_UP;
  906. break;
  907. case NODE_FAILOVER_BEGIN_EVT:
  908. state = NODE_FAILINGOVER;
  909. break;
  910. case NODE_SYNCH_BEGIN_EVT:
  911. case SELF_ESTABL_CONTACT_EVT:
  912. case PEER_ESTABL_CONTACT_EVT:
  913. break;
  914. case NODE_FAILOVER_END_EVT:
  915. default:
  916. goto illegal_evt;
  917. }
  918. break;
  919. default:
  920. pr_err("Unknown node fsm state %x\n", state);
  921. break;
  922. }
  923. n->state = state;
  924. return;
  925. illegal_evt:
  926. pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
  927. }
  928. static void node_lost_contact(struct tipc_node *n,
  929. struct sk_buff_head *inputq)
  930. {
  931. char addr_string[16];
  932. struct tipc_sock_conn *conn, *safe;
  933. struct tipc_link *l;
  934. struct list_head *conns = &n->conn_sks;
  935. struct sk_buff *skb;
  936. uint i;
  937. pr_debug("Lost contact with %s\n",
  938. tipc_addr_string_fill(addr_string, n->addr));
  939. /* Clean up broadcast state */
  940. tipc_bcast_remove_peer(n->net, n->bc_entry.link);
  941. /* Abort any ongoing link failover */
  942. for (i = 0; i < MAX_BEARERS; i++) {
  943. l = n->links[i].link;
  944. if (l)
  945. tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
  946. }
  947. /* Notify publications from this node */
  948. n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
  949. /* Notify sockets connected to node */
  950. list_for_each_entry_safe(conn, safe, conns, list) {
  951. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
  952. SHORT_H_SIZE, 0, tipc_own_addr(n->net),
  953. conn->peer_node, conn->port,
  954. conn->peer_port, TIPC_ERR_NO_NODE);
  955. if (likely(skb))
  956. skb_queue_tail(inputq, skb);
  957. list_del(&conn->list);
  958. kfree(conn);
  959. }
  960. }
  961. /**
  962. * tipc_node_get_linkname - get the name of a link
  963. *
  964. * @bearer_id: id of the bearer
  965. * @node: peer node address
  966. * @linkname: link name output buffer
  967. *
  968. * Returns 0 on success
  969. */
  970. int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
  971. char *linkname, size_t len)
  972. {
  973. struct tipc_link *link;
  974. int err = -EINVAL;
  975. struct tipc_node *node = tipc_node_find(net, addr);
  976. if (!node)
  977. return err;
  978. if (bearer_id >= MAX_BEARERS)
  979. goto exit;
  980. tipc_node_read_lock(node);
  981. link = node->links[bearer_id].link;
  982. if (link) {
  983. strncpy(linkname, tipc_link_name(link), len);
  984. err = 0;
  985. }
  986. exit:
  987. tipc_node_read_unlock(node);
  988. tipc_node_put(node);
  989. return err;
  990. }
  991. /* Caller should hold node lock for the passed node */
  992. static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
  993. {
  994. void *hdr;
  995. struct nlattr *attrs;
  996. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  997. NLM_F_MULTI, TIPC_NL_NODE_GET);
  998. if (!hdr)
  999. return -EMSGSIZE;
  1000. attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
  1001. if (!attrs)
  1002. goto msg_full;
  1003. if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
  1004. goto attr_msg_full;
  1005. if (tipc_node_is_up(node))
  1006. if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
  1007. goto attr_msg_full;
  1008. nla_nest_end(msg->skb, attrs);
  1009. genlmsg_end(msg->skb, hdr);
  1010. return 0;
  1011. attr_msg_full:
  1012. nla_nest_cancel(msg->skb, attrs);
  1013. msg_full:
  1014. genlmsg_cancel(msg->skb, hdr);
  1015. return -EMSGSIZE;
  1016. }
  1017. /**
  1018. * tipc_node_xmit() is the general link level function for message sending
  1019. * @net: the applicable net namespace
  1020. * @list: chain of buffers containing message
  1021. * @dnode: address of destination node
  1022. * @selector: a number used for deterministic link selection
  1023. * Consumes the buffer chain, except when returning -ELINKCONG
  1024. * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
  1025. */
  1026. int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
  1027. u32 dnode, int selector)
  1028. {
  1029. struct tipc_link_entry *le = NULL;
  1030. struct tipc_node *n;
  1031. struct sk_buff_head xmitq;
  1032. int bearer_id;
  1033. int rc;
  1034. if (in_own_node(net, dnode)) {
  1035. tipc_sk_rcv(net, list);
  1036. return 0;
  1037. }
  1038. n = tipc_node_find(net, dnode);
  1039. if (unlikely(!n)) {
  1040. skb_queue_purge(list);
  1041. return -EHOSTUNREACH;
  1042. }
  1043. tipc_node_read_lock(n);
  1044. bearer_id = n->active_links[selector & 1];
  1045. if (unlikely(bearer_id == INVALID_BEARER_ID)) {
  1046. tipc_node_read_unlock(n);
  1047. tipc_node_put(n);
  1048. skb_queue_purge(list);
  1049. return -EHOSTUNREACH;
  1050. }
  1051. __skb_queue_head_init(&xmitq);
  1052. le = &n->links[bearer_id];
  1053. spin_lock_bh(&le->lock);
  1054. rc = tipc_link_xmit(le->link, list, &xmitq);
  1055. spin_unlock_bh(&le->lock);
  1056. tipc_node_read_unlock(n);
  1057. if (likely(rc == 0))
  1058. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1059. else if (rc == -ENOBUFS)
  1060. tipc_node_link_down(n, bearer_id, false);
  1061. tipc_node_put(n);
  1062. return rc;
  1063. }
  1064. /* tipc_node_xmit_skb(): send single buffer to destination
  1065. * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
  1066. * messages, which will not be rejected
  1067. * The only exception is datagram messages rerouted after secondary
  1068. * lookup, which are rare and safe to dispose of anyway.
  1069. * TODO: Return real return value, and let callers use
  1070. * tipc_wait_for_sendpkt() where applicable
  1071. */
  1072. int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  1073. u32 selector)
  1074. {
  1075. struct sk_buff_head head;
  1076. int rc;
  1077. skb_queue_head_init(&head);
  1078. __skb_queue_tail(&head, skb);
  1079. rc = tipc_node_xmit(net, &head, dnode, selector);
  1080. if (rc == -ELINKCONG)
  1081. kfree_skb(skb);
  1082. return 0;
  1083. }
  1084. void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
  1085. {
  1086. struct sk_buff *txskb;
  1087. struct tipc_node *n;
  1088. u32 dst;
  1089. rcu_read_lock();
  1090. list_for_each_entry_rcu(n, tipc_nodes(net), list) {
  1091. dst = n->addr;
  1092. if (in_own_node(net, dst))
  1093. continue;
  1094. if (!tipc_node_is_up(n))
  1095. continue;
  1096. txskb = pskb_copy(skb, GFP_ATOMIC);
  1097. if (!txskb)
  1098. break;
  1099. msg_set_destnode(buf_msg(txskb), dst);
  1100. tipc_node_xmit_skb(net, txskb, dst, 0);
  1101. }
  1102. rcu_read_unlock();
  1103. kfree_skb(skb);
  1104. }
  1105. /**
  1106. * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
  1107. * @net: the applicable net namespace
  1108. * @skb: TIPC packet
  1109. * @bearer_id: id of bearer message arrived on
  1110. *
  1111. * Invoked with no locks held.
  1112. */
  1113. static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
  1114. {
  1115. int rc;
  1116. struct sk_buff_head xmitq;
  1117. struct tipc_bclink_entry *be;
  1118. struct tipc_link_entry *le;
  1119. struct tipc_msg *hdr = buf_msg(skb);
  1120. int usr = msg_user(hdr);
  1121. u32 dnode = msg_destnode(hdr);
  1122. struct tipc_node *n;
  1123. __skb_queue_head_init(&xmitq);
  1124. /* If NACK for other node, let rcv link for that node peek into it */
  1125. if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
  1126. n = tipc_node_find(net, dnode);
  1127. else
  1128. n = tipc_node_find(net, msg_prevnode(hdr));
  1129. if (!n) {
  1130. kfree_skb(skb);
  1131. return;
  1132. }
  1133. be = &n->bc_entry;
  1134. le = &n->links[bearer_id];
  1135. rc = tipc_bcast_rcv(net, be->link, skb);
  1136. /* Broadcast link reset may happen at reassembly failure */
  1137. if (rc & TIPC_LINK_DOWN_EVT)
  1138. tipc_node_reset_links(n);
  1139. /* Broadcast ACKs are sent on a unicast link */
  1140. if (rc & TIPC_LINK_SND_BC_ACK) {
  1141. tipc_node_read_lock(n);
  1142. tipc_link_build_ack_msg(le->link, &xmitq);
  1143. tipc_node_read_unlock(n);
  1144. }
  1145. if (!skb_queue_empty(&xmitq))
  1146. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1147. /* Deliver. 'arrvq' is under inputq2's lock protection */
  1148. if (!skb_queue_empty(&be->inputq1)) {
  1149. spin_lock_bh(&be->inputq2.lock);
  1150. spin_lock_bh(&be->inputq1.lock);
  1151. skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
  1152. spin_unlock_bh(&be->inputq1.lock);
  1153. spin_unlock_bh(&be->inputq2.lock);
  1154. tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
  1155. }
  1156. tipc_node_put(n);
  1157. }
  1158. /**
  1159. * tipc_node_check_state - check and if necessary update node state
  1160. * @skb: TIPC packet
  1161. * @bearer_id: identity of bearer delivering the packet
  1162. * Returns true if state is ok, otherwise consumes buffer and returns false
  1163. */
  1164. static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
  1165. int bearer_id, struct sk_buff_head *xmitq)
  1166. {
  1167. struct tipc_msg *hdr = buf_msg(skb);
  1168. int usr = msg_user(hdr);
  1169. int mtyp = msg_type(hdr);
  1170. u16 oseqno = msg_seqno(hdr);
  1171. u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
  1172. u16 exp_pkts = msg_msgcnt(hdr);
  1173. u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
  1174. int state = n->state;
  1175. struct tipc_link *l, *tnl, *pl = NULL;
  1176. struct tipc_media_addr *maddr;
  1177. int pb_id;
  1178. l = n->links[bearer_id].link;
  1179. if (!l)
  1180. return false;
  1181. rcv_nxt = tipc_link_rcv_nxt(l);
  1182. if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
  1183. return true;
  1184. /* Find parallel link, if any */
  1185. for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
  1186. if ((pb_id != bearer_id) && n->links[pb_id].link) {
  1187. pl = n->links[pb_id].link;
  1188. break;
  1189. }
  1190. }
  1191. /* Check and update node accesibility if applicable */
  1192. if (state == SELF_UP_PEER_COMING) {
  1193. if (!tipc_link_is_up(l))
  1194. return true;
  1195. if (!msg_peer_link_is_up(hdr))
  1196. return true;
  1197. tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
  1198. }
  1199. if (state == SELF_DOWN_PEER_LEAVING) {
  1200. if (msg_peer_node_is_up(hdr))
  1201. return false;
  1202. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  1203. return true;
  1204. }
  1205. if (state == SELF_LEAVING_PEER_DOWN)
  1206. return false;
  1207. /* Ignore duplicate packets */
  1208. if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
  1209. return true;
  1210. /* Initiate or update failover mode if applicable */
  1211. if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
  1212. syncpt = oseqno + exp_pkts - 1;
  1213. if (pl && tipc_link_is_up(pl)) {
  1214. __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
  1215. tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
  1216. tipc_link_inputq(l));
  1217. }
  1218. /* If pkts arrive out of order, use lowest calculated syncpt */
  1219. if (less(syncpt, n->sync_point))
  1220. n->sync_point = syncpt;
  1221. }
  1222. /* Open parallel link when tunnel link reaches synch point */
  1223. if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
  1224. if (!more(rcv_nxt, n->sync_point))
  1225. return true;
  1226. tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
  1227. if (pl)
  1228. tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
  1229. return true;
  1230. }
  1231. /* No synching needed if only one link */
  1232. if (!pl || !tipc_link_is_up(pl))
  1233. return true;
  1234. /* Initiate synch mode if applicable */
  1235. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
  1236. syncpt = iseqno + exp_pkts - 1;
  1237. if (!tipc_link_is_up(l)) {
  1238. tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1239. __tipc_node_link_up(n, bearer_id, xmitq);
  1240. }
  1241. if (n->state == SELF_UP_PEER_UP) {
  1242. n->sync_point = syncpt;
  1243. tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
  1244. tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
  1245. }
  1246. }
  1247. /* Open tunnel link when parallel link reaches synch point */
  1248. if (n->state == NODE_SYNCHING) {
  1249. if (tipc_link_is_synching(l)) {
  1250. tnl = l;
  1251. } else {
  1252. tnl = pl;
  1253. pl = l;
  1254. }
  1255. inputq_len = skb_queue_len(tipc_link_inputq(pl));
  1256. dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
  1257. if (more(dlv_nxt, n->sync_point)) {
  1258. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  1259. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  1260. return true;
  1261. }
  1262. if (l == pl)
  1263. return true;
  1264. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
  1265. return true;
  1266. if (usr == LINK_PROTOCOL)
  1267. return true;
  1268. return false;
  1269. }
  1270. return true;
  1271. }
  1272. /**
  1273. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1274. * @net: the applicable net namespace
  1275. * @skb: TIPC packet
  1276. * @bearer: pointer to bearer message arrived on
  1277. *
  1278. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1279. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1280. */
  1281. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
  1282. {
  1283. struct sk_buff_head xmitq;
  1284. struct tipc_node *n;
  1285. struct tipc_msg *hdr = buf_msg(skb);
  1286. int usr = msg_user(hdr);
  1287. int bearer_id = b->identity;
  1288. struct tipc_link_entry *le;
  1289. u16 bc_ack = msg_bcast_ack(hdr);
  1290. u32 self = tipc_own_addr(net);
  1291. int rc = 0;
  1292. __skb_queue_head_init(&xmitq);
  1293. /* Ensure message is well-formed */
  1294. if (unlikely(!tipc_msg_validate(skb)))
  1295. goto discard;
  1296. /* Handle arrival of discovery or broadcast packet */
  1297. if (unlikely(msg_non_seq(hdr))) {
  1298. if (unlikely(usr == LINK_CONFIG))
  1299. return tipc_disc_rcv(net, skb, b);
  1300. else
  1301. return tipc_node_bc_rcv(net, skb, bearer_id);
  1302. }
  1303. /* Discard unicast link messages destined for another node */
  1304. if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
  1305. goto discard;
  1306. /* Locate neighboring node that sent packet */
  1307. n = tipc_node_find(net, msg_prevnode(hdr));
  1308. if (unlikely(!n))
  1309. goto discard;
  1310. le = &n->links[bearer_id];
  1311. /* Ensure broadcast reception is in synch with peer's send state */
  1312. if (unlikely(usr == LINK_PROTOCOL))
  1313. tipc_bcast_sync_rcv(net, n->bc_entry.link, hdr);
  1314. else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
  1315. tipc_bcast_ack_rcv(net, n->bc_entry.link, bc_ack);
  1316. /* Receive packet directly if conditions permit */
  1317. tipc_node_read_lock(n);
  1318. if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
  1319. spin_lock_bh(&le->lock);
  1320. if (le->link) {
  1321. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1322. skb = NULL;
  1323. }
  1324. spin_unlock_bh(&le->lock);
  1325. }
  1326. tipc_node_read_unlock(n);
  1327. /* Check/update node state before receiving */
  1328. if (unlikely(skb)) {
  1329. tipc_node_write_lock(n);
  1330. if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
  1331. if (le->link) {
  1332. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1333. skb = NULL;
  1334. }
  1335. }
  1336. tipc_node_write_unlock(n);
  1337. }
  1338. if (unlikely(rc & TIPC_LINK_UP_EVT))
  1339. tipc_node_link_up(n, bearer_id, &xmitq);
  1340. if (unlikely(rc & TIPC_LINK_DOWN_EVT))
  1341. tipc_node_link_down(n, bearer_id, false);
  1342. if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
  1343. tipc_named_rcv(net, &n->bc_entry.namedq);
  1344. if (!skb_queue_empty(&le->inputq))
  1345. tipc_sk_rcv(net, &le->inputq);
  1346. if (!skb_queue_empty(&xmitq))
  1347. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1348. tipc_node_put(n);
  1349. discard:
  1350. kfree_skb(skb);
  1351. }
  1352. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1353. {
  1354. int err;
  1355. struct net *net = sock_net(skb->sk);
  1356. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1357. int done = cb->args[0];
  1358. int last_addr = cb->args[1];
  1359. struct tipc_node *node;
  1360. struct tipc_nl_msg msg;
  1361. if (done)
  1362. return 0;
  1363. msg.skb = skb;
  1364. msg.portid = NETLINK_CB(cb->skb).portid;
  1365. msg.seq = cb->nlh->nlmsg_seq;
  1366. rcu_read_lock();
  1367. if (last_addr) {
  1368. node = tipc_node_find(net, last_addr);
  1369. if (!node) {
  1370. rcu_read_unlock();
  1371. /* We never set seq or call nl_dump_check_consistent()
  1372. * this means that setting prev_seq here will cause the
  1373. * consistence check to fail in the netlink callback
  1374. * handler. Resulting in the NLMSG_DONE message having
  1375. * the NLM_F_DUMP_INTR flag set if the node state
  1376. * changed while we released the lock.
  1377. */
  1378. cb->prev_seq = 1;
  1379. return -EPIPE;
  1380. }
  1381. tipc_node_put(node);
  1382. }
  1383. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1384. if (last_addr) {
  1385. if (node->addr == last_addr)
  1386. last_addr = 0;
  1387. else
  1388. continue;
  1389. }
  1390. tipc_node_read_lock(node);
  1391. err = __tipc_nl_add_node(&msg, node);
  1392. if (err) {
  1393. last_addr = node->addr;
  1394. tipc_node_read_unlock(node);
  1395. goto out;
  1396. }
  1397. tipc_node_read_unlock(node);
  1398. }
  1399. done = 1;
  1400. out:
  1401. cb->args[0] = done;
  1402. cb->args[1] = last_addr;
  1403. rcu_read_unlock();
  1404. return skb->len;
  1405. }
  1406. /* tipc_node_find_by_name - locate owner node of link by link's name
  1407. * @net: the applicable net namespace
  1408. * @name: pointer to link name string
  1409. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1410. *
  1411. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1412. */
  1413. static struct tipc_node *tipc_node_find_by_name(struct net *net,
  1414. const char *link_name,
  1415. unsigned int *bearer_id)
  1416. {
  1417. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1418. struct tipc_link *l;
  1419. struct tipc_node *n;
  1420. struct tipc_node *found_node = NULL;
  1421. int i;
  1422. *bearer_id = 0;
  1423. rcu_read_lock();
  1424. list_for_each_entry_rcu(n, &tn->node_list, list) {
  1425. tipc_node_read_lock(n);
  1426. for (i = 0; i < MAX_BEARERS; i++) {
  1427. l = n->links[i].link;
  1428. if (l && !strcmp(tipc_link_name(l), link_name)) {
  1429. *bearer_id = i;
  1430. found_node = n;
  1431. break;
  1432. }
  1433. }
  1434. tipc_node_read_unlock(n);
  1435. if (found_node)
  1436. break;
  1437. }
  1438. rcu_read_unlock();
  1439. return found_node;
  1440. }
  1441. int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
  1442. {
  1443. int err;
  1444. int res = 0;
  1445. int bearer_id;
  1446. char *name;
  1447. struct tipc_link *link;
  1448. struct tipc_node *node;
  1449. struct sk_buff_head xmitq;
  1450. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1451. struct net *net = sock_net(skb->sk);
  1452. __skb_queue_head_init(&xmitq);
  1453. if (!info->attrs[TIPC_NLA_LINK])
  1454. return -EINVAL;
  1455. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1456. info->attrs[TIPC_NLA_LINK],
  1457. tipc_nl_link_policy);
  1458. if (err)
  1459. return err;
  1460. if (!attrs[TIPC_NLA_LINK_NAME])
  1461. return -EINVAL;
  1462. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1463. if (strcmp(name, tipc_bclink_name) == 0)
  1464. return tipc_nl_bc_link_set(net, attrs);
  1465. node = tipc_node_find_by_name(net, name, &bearer_id);
  1466. if (!node)
  1467. return -EINVAL;
  1468. tipc_node_read_lock(node);
  1469. link = node->links[bearer_id].link;
  1470. if (!link) {
  1471. res = -EINVAL;
  1472. goto out;
  1473. }
  1474. if (attrs[TIPC_NLA_LINK_PROP]) {
  1475. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1476. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1477. props);
  1478. if (err) {
  1479. res = err;
  1480. goto out;
  1481. }
  1482. if (props[TIPC_NLA_PROP_TOL]) {
  1483. u32 tol;
  1484. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1485. tipc_link_set_tolerance(link, tol, &xmitq);
  1486. }
  1487. if (props[TIPC_NLA_PROP_PRIO]) {
  1488. u32 prio;
  1489. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1490. tipc_link_set_prio(link, prio, &xmitq);
  1491. }
  1492. if (props[TIPC_NLA_PROP_WIN]) {
  1493. u32 win;
  1494. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1495. tipc_link_set_queue_limits(link, win);
  1496. }
  1497. }
  1498. out:
  1499. tipc_node_read_unlock(node);
  1500. tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
  1501. return res;
  1502. }
  1503. int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
  1504. {
  1505. struct net *net = genl_info_net(info);
  1506. struct tipc_nl_msg msg;
  1507. char *name;
  1508. int err;
  1509. msg.portid = info->snd_portid;
  1510. msg.seq = info->snd_seq;
  1511. if (!info->attrs[TIPC_NLA_LINK_NAME])
  1512. return -EINVAL;
  1513. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  1514. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1515. if (!msg.skb)
  1516. return -ENOMEM;
  1517. if (strcmp(name, tipc_bclink_name) == 0) {
  1518. err = tipc_nl_add_bc_link(net, &msg);
  1519. if (err) {
  1520. nlmsg_free(msg.skb);
  1521. return err;
  1522. }
  1523. } else {
  1524. int bearer_id;
  1525. struct tipc_node *node;
  1526. struct tipc_link *link;
  1527. node = tipc_node_find_by_name(net, name, &bearer_id);
  1528. if (!node)
  1529. return -EINVAL;
  1530. tipc_node_read_lock(node);
  1531. link = node->links[bearer_id].link;
  1532. if (!link) {
  1533. tipc_node_read_unlock(node);
  1534. nlmsg_free(msg.skb);
  1535. return -EINVAL;
  1536. }
  1537. err = __tipc_nl_add_link(net, &msg, link, 0);
  1538. tipc_node_read_unlock(node);
  1539. if (err) {
  1540. nlmsg_free(msg.skb);
  1541. return err;
  1542. }
  1543. }
  1544. return genlmsg_reply(msg.skb, info);
  1545. }
  1546. int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
  1547. {
  1548. int err;
  1549. char *link_name;
  1550. unsigned int bearer_id;
  1551. struct tipc_link *link;
  1552. struct tipc_node *node;
  1553. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1554. struct net *net = sock_net(skb->sk);
  1555. struct tipc_link_entry *le;
  1556. if (!info->attrs[TIPC_NLA_LINK])
  1557. return -EINVAL;
  1558. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1559. info->attrs[TIPC_NLA_LINK],
  1560. tipc_nl_link_policy);
  1561. if (err)
  1562. return err;
  1563. if (!attrs[TIPC_NLA_LINK_NAME])
  1564. return -EINVAL;
  1565. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1566. if (strcmp(link_name, tipc_bclink_name) == 0) {
  1567. err = tipc_bclink_reset_stats(net);
  1568. if (err)
  1569. return err;
  1570. return 0;
  1571. }
  1572. node = tipc_node_find_by_name(net, link_name, &bearer_id);
  1573. if (!node)
  1574. return -EINVAL;
  1575. le = &node->links[bearer_id];
  1576. tipc_node_read_lock(node);
  1577. spin_lock_bh(&le->lock);
  1578. link = node->links[bearer_id].link;
  1579. if (!link) {
  1580. spin_unlock_bh(&le->lock);
  1581. tipc_node_read_unlock(node);
  1582. return -EINVAL;
  1583. }
  1584. tipc_link_reset_stats(link);
  1585. spin_unlock_bh(&le->lock);
  1586. tipc_node_read_unlock(node);
  1587. return 0;
  1588. }
  1589. /* Caller should hold node lock */
  1590. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1591. struct tipc_node *node, u32 *prev_link)
  1592. {
  1593. u32 i;
  1594. int err;
  1595. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1596. *prev_link = i;
  1597. if (!node->links[i].link)
  1598. continue;
  1599. err = __tipc_nl_add_link(net, msg,
  1600. node->links[i].link, NLM_F_MULTI);
  1601. if (err)
  1602. return err;
  1603. }
  1604. *prev_link = 0;
  1605. return 0;
  1606. }
  1607. int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
  1608. {
  1609. struct net *net = sock_net(skb->sk);
  1610. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1611. struct tipc_node *node;
  1612. struct tipc_nl_msg msg;
  1613. u32 prev_node = cb->args[0];
  1614. u32 prev_link = cb->args[1];
  1615. int done = cb->args[2];
  1616. int err;
  1617. if (done)
  1618. return 0;
  1619. msg.skb = skb;
  1620. msg.portid = NETLINK_CB(cb->skb).portid;
  1621. msg.seq = cb->nlh->nlmsg_seq;
  1622. rcu_read_lock();
  1623. if (prev_node) {
  1624. node = tipc_node_find(net, prev_node);
  1625. if (!node) {
  1626. /* We never set seq or call nl_dump_check_consistent()
  1627. * this means that setting prev_seq here will cause the
  1628. * consistence check to fail in the netlink callback
  1629. * handler. Resulting in the last NLMSG_DONE message
  1630. * having the NLM_F_DUMP_INTR flag set.
  1631. */
  1632. cb->prev_seq = 1;
  1633. goto out;
  1634. }
  1635. tipc_node_put(node);
  1636. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1637. list) {
  1638. tipc_node_read_lock(node);
  1639. err = __tipc_nl_add_node_links(net, &msg, node,
  1640. &prev_link);
  1641. tipc_node_read_unlock(node);
  1642. if (err)
  1643. goto out;
  1644. prev_node = node->addr;
  1645. }
  1646. } else {
  1647. err = tipc_nl_add_bc_link(net, &msg);
  1648. if (err)
  1649. goto out;
  1650. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1651. tipc_node_read_lock(node);
  1652. err = __tipc_nl_add_node_links(net, &msg, node,
  1653. &prev_link);
  1654. tipc_node_read_unlock(node);
  1655. if (err)
  1656. goto out;
  1657. prev_node = node->addr;
  1658. }
  1659. }
  1660. done = 1;
  1661. out:
  1662. rcu_read_unlock();
  1663. cb->args[0] = prev_node;
  1664. cb->args[1] = prev_link;
  1665. cb->args[2] = done;
  1666. return skb->len;
  1667. }