node.c 48 KB

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