node.c 54 KB

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