node.c 57 KB

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