node.c 48 KB

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