node.c 58 KB

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