node.c 54 KB

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