node.c 55 KB

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