node.c 48 KB

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