node.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  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_link *l = le->link;
  607. struct tipc_media_addr *maddr;
  608. struct sk_buff_head xmitq;
  609. int old_bearer_id = bearer_id;
  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. tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr);
  629. tipc_sk_rcv(n->net, &le->inputq);
  630. }
  631. static bool tipc_node_is_up(struct tipc_node *n)
  632. {
  633. return n->active_links[0] != INVALID_BEARER_ID;
  634. }
  635. void tipc_node_check_dest(struct net *net, u32 onode,
  636. struct tipc_bearer *b,
  637. u16 capabilities, u32 signature,
  638. struct tipc_media_addr *maddr,
  639. bool *respond, bool *dupl_addr)
  640. {
  641. struct tipc_node *n;
  642. struct tipc_link *l;
  643. struct tipc_link_entry *le;
  644. bool addr_match = false;
  645. bool sign_match = false;
  646. bool link_up = false;
  647. bool accept_addr = false;
  648. bool reset = true;
  649. char *if_name;
  650. unsigned long intv;
  651. *dupl_addr = false;
  652. *respond = false;
  653. n = tipc_node_create(net, onode, capabilities);
  654. if (!n)
  655. return;
  656. tipc_node_write_lock(n);
  657. le = &n->links[b->identity];
  658. /* Prepare to validate requesting node's signature and media address */
  659. l = le->link;
  660. link_up = l && tipc_link_is_up(l);
  661. addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
  662. sign_match = (signature == n->signature);
  663. /* These three flags give us eight permutations: */
  664. if (sign_match && addr_match && link_up) {
  665. /* All is fine. Do nothing. */
  666. reset = false;
  667. } else if (sign_match && addr_match && !link_up) {
  668. /* Respond. The link will come up in due time */
  669. *respond = true;
  670. } else if (sign_match && !addr_match && link_up) {
  671. /* Peer has changed i/f address without rebooting.
  672. * If so, the link will reset soon, and the next
  673. * discovery will be accepted. So we can ignore it.
  674. * It may also be an cloned or malicious peer having
  675. * chosen the same node address and signature as an
  676. * existing one.
  677. * Ignore requests until the link goes down, if ever.
  678. */
  679. *dupl_addr = true;
  680. } else if (sign_match && !addr_match && !link_up) {
  681. /* Peer link has changed i/f address without rebooting.
  682. * It may also be a cloned or malicious peer; we can't
  683. * distinguish between the two.
  684. * The signature is correct, so we must accept.
  685. */
  686. accept_addr = true;
  687. *respond = true;
  688. } else if (!sign_match && addr_match && link_up) {
  689. /* Peer node rebooted. Two possibilities:
  690. * - Delayed re-discovery; this link endpoint has already
  691. * reset and re-established contact with the peer, before
  692. * receiving a discovery message from that node.
  693. * (The peer happened to receive one from this node first).
  694. * - The peer came back so fast that our side has not
  695. * discovered it yet. Probing from this side will soon
  696. * reset the link, since there can be no working link
  697. * endpoint at the peer end, and the link will re-establish.
  698. * Accept the signature, since it comes from a known peer.
  699. */
  700. n->signature = signature;
  701. } else if (!sign_match && addr_match && !link_up) {
  702. /* The peer node has rebooted.
  703. * Accept signature, since it is a known peer.
  704. */
  705. n->signature = signature;
  706. *respond = true;
  707. } else if (!sign_match && !addr_match && link_up) {
  708. /* Peer rebooted with new address, or a new/duplicate peer.
  709. * Ignore until the link goes down, if ever.
  710. */
  711. *dupl_addr = true;
  712. } else if (!sign_match && !addr_match && !link_up) {
  713. /* Peer rebooted with new address, or it is a new peer.
  714. * Accept signature and address.
  715. */
  716. n->signature = signature;
  717. accept_addr = true;
  718. *respond = true;
  719. }
  720. if (!accept_addr)
  721. goto exit;
  722. /* Now create new link if not already existing */
  723. if (!l) {
  724. if (n->link_cnt == 2) {
  725. pr_warn("Cannot establish 3rd link to %x\n", n->addr);
  726. goto exit;
  727. }
  728. if_name = strchr(b->name, ':') + 1;
  729. if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
  730. b->net_plane, b->mtu, b->priority,
  731. b->window, mod(tipc_net(net)->random),
  732. tipc_own_addr(net), onode,
  733. n->capabilities,
  734. tipc_bc_sndlink(n->net), n->bc_entry.link,
  735. &le->inputq,
  736. &n->bc_entry.namedq, &l)) {
  737. *respond = false;
  738. goto exit;
  739. }
  740. tipc_link_reset(l);
  741. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  742. if (n->state == NODE_FAILINGOVER)
  743. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  744. le->link = l;
  745. n->link_cnt++;
  746. tipc_node_calculate_timer(n, l);
  747. if (n->link_cnt == 1) {
  748. intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
  749. if (!mod_timer(&n->timer, intv))
  750. tipc_node_get(n);
  751. }
  752. }
  753. memcpy(&le->maddr, maddr, sizeof(*maddr));
  754. exit:
  755. tipc_node_write_unlock(n);
  756. if (reset && l && !tipc_link_is_reset(l))
  757. tipc_node_link_down(n, b->identity, false);
  758. tipc_node_put(n);
  759. }
  760. void tipc_node_delete_links(struct net *net, int bearer_id)
  761. {
  762. struct tipc_net *tn = net_generic(net, tipc_net_id);
  763. struct tipc_node *n;
  764. rcu_read_lock();
  765. list_for_each_entry_rcu(n, &tn->node_list, list) {
  766. tipc_node_link_down(n, bearer_id, true);
  767. }
  768. rcu_read_unlock();
  769. }
  770. static void tipc_node_reset_links(struct tipc_node *n)
  771. {
  772. char addr_string[16];
  773. int i;
  774. pr_warn("Resetting all links to %s\n",
  775. tipc_addr_string_fill(addr_string, n->addr));
  776. for (i = 0; i < MAX_BEARERS; i++) {
  777. tipc_node_link_down(n, i, false);
  778. }
  779. }
  780. /* tipc_node_fsm_evt - node finite state machine
  781. * Determines when contact is allowed with peer node
  782. */
  783. static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
  784. {
  785. int state = n->state;
  786. switch (state) {
  787. case SELF_DOWN_PEER_DOWN:
  788. switch (evt) {
  789. case SELF_ESTABL_CONTACT_EVT:
  790. state = SELF_UP_PEER_COMING;
  791. break;
  792. case PEER_ESTABL_CONTACT_EVT:
  793. state = SELF_COMING_PEER_UP;
  794. break;
  795. case SELF_LOST_CONTACT_EVT:
  796. case PEER_LOST_CONTACT_EVT:
  797. break;
  798. case NODE_SYNCH_END_EVT:
  799. case NODE_SYNCH_BEGIN_EVT:
  800. case NODE_FAILOVER_BEGIN_EVT:
  801. case NODE_FAILOVER_END_EVT:
  802. default:
  803. goto illegal_evt;
  804. }
  805. break;
  806. case SELF_UP_PEER_UP:
  807. switch (evt) {
  808. case SELF_LOST_CONTACT_EVT:
  809. state = SELF_DOWN_PEER_LEAVING;
  810. break;
  811. case PEER_LOST_CONTACT_EVT:
  812. state = SELF_LEAVING_PEER_DOWN;
  813. break;
  814. case NODE_SYNCH_BEGIN_EVT:
  815. state = NODE_SYNCHING;
  816. break;
  817. case NODE_FAILOVER_BEGIN_EVT:
  818. state = NODE_FAILINGOVER;
  819. break;
  820. case SELF_ESTABL_CONTACT_EVT:
  821. case PEER_ESTABL_CONTACT_EVT:
  822. case NODE_SYNCH_END_EVT:
  823. case NODE_FAILOVER_END_EVT:
  824. break;
  825. default:
  826. goto illegal_evt;
  827. }
  828. break;
  829. case SELF_DOWN_PEER_LEAVING:
  830. switch (evt) {
  831. case PEER_LOST_CONTACT_EVT:
  832. state = SELF_DOWN_PEER_DOWN;
  833. break;
  834. case SELF_ESTABL_CONTACT_EVT:
  835. case PEER_ESTABL_CONTACT_EVT:
  836. case SELF_LOST_CONTACT_EVT:
  837. break;
  838. case NODE_SYNCH_END_EVT:
  839. case NODE_SYNCH_BEGIN_EVT:
  840. case NODE_FAILOVER_BEGIN_EVT:
  841. case NODE_FAILOVER_END_EVT:
  842. default:
  843. goto illegal_evt;
  844. }
  845. break;
  846. case SELF_UP_PEER_COMING:
  847. switch (evt) {
  848. case PEER_ESTABL_CONTACT_EVT:
  849. state = SELF_UP_PEER_UP;
  850. break;
  851. case SELF_LOST_CONTACT_EVT:
  852. state = SELF_DOWN_PEER_DOWN;
  853. break;
  854. case SELF_ESTABL_CONTACT_EVT:
  855. case PEER_LOST_CONTACT_EVT:
  856. case NODE_SYNCH_END_EVT:
  857. case NODE_FAILOVER_BEGIN_EVT:
  858. break;
  859. case NODE_SYNCH_BEGIN_EVT:
  860. case NODE_FAILOVER_END_EVT:
  861. default:
  862. goto illegal_evt;
  863. }
  864. break;
  865. case SELF_COMING_PEER_UP:
  866. switch (evt) {
  867. case SELF_ESTABL_CONTACT_EVT:
  868. state = SELF_UP_PEER_UP;
  869. break;
  870. case PEER_LOST_CONTACT_EVT:
  871. state = SELF_DOWN_PEER_DOWN;
  872. break;
  873. case SELF_LOST_CONTACT_EVT:
  874. case PEER_ESTABL_CONTACT_EVT:
  875. break;
  876. case NODE_SYNCH_END_EVT:
  877. case NODE_SYNCH_BEGIN_EVT:
  878. case NODE_FAILOVER_BEGIN_EVT:
  879. case NODE_FAILOVER_END_EVT:
  880. default:
  881. goto illegal_evt;
  882. }
  883. break;
  884. case SELF_LEAVING_PEER_DOWN:
  885. switch (evt) {
  886. case SELF_LOST_CONTACT_EVT:
  887. state = SELF_DOWN_PEER_DOWN;
  888. break;
  889. case SELF_ESTABL_CONTACT_EVT:
  890. case PEER_ESTABL_CONTACT_EVT:
  891. case PEER_LOST_CONTACT_EVT:
  892. break;
  893. case NODE_SYNCH_END_EVT:
  894. case NODE_SYNCH_BEGIN_EVT:
  895. case NODE_FAILOVER_BEGIN_EVT:
  896. case NODE_FAILOVER_END_EVT:
  897. default:
  898. goto illegal_evt;
  899. }
  900. break;
  901. case NODE_FAILINGOVER:
  902. switch (evt) {
  903. case SELF_LOST_CONTACT_EVT:
  904. state = SELF_DOWN_PEER_LEAVING;
  905. break;
  906. case PEER_LOST_CONTACT_EVT:
  907. state = SELF_LEAVING_PEER_DOWN;
  908. break;
  909. case NODE_FAILOVER_END_EVT:
  910. state = SELF_UP_PEER_UP;
  911. break;
  912. case NODE_FAILOVER_BEGIN_EVT:
  913. case SELF_ESTABL_CONTACT_EVT:
  914. case PEER_ESTABL_CONTACT_EVT:
  915. break;
  916. case NODE_SYNCH_BEGIN_EVT:
  917. case NODE_SYNCH_END_EVT:
  918. default:
  919. goto illegal_evt;
  920. }
  921. break;
  922. case NODE_SYNCHING:
  923. switch (evt) {
  924. case SELF_LOST_CONTACT_EVT:
  925. state = SELF_DOWN_PEER_LEAVING;
  926. break;
  927. case PEER_LOST_CONTACT_EVT:
  928. state = SELF_LEAVING_PEER_DOWN;
  929. break;
  930. case NODE_SYNCH_END_EVT:
  931. state = SELF_UP_PEER_UP;
  932. break;
  933. case NODE_FAILOVER_BEGIN_EVT:
  934. state = NODE_FAILINGOVER;
  935. break;
  936. case NODE_SYNCH_BEGIN_EVT:
  937. case SELF_ESTABL_CONTACT_EVT:
  938. case PEER_ESTABL_CONTACT_EVT:
  939. break;
  940. case NODE_FAILOVER_END_EVT:
  941. default:
  942. goto illegal_evt;
  943. }
  944. break;
  945. default:
  946. pr_err("Unknown node fsm state %x\n", state);
  947. break;
  948. }
  949. n->state = state;
  950. return;
  951. illegal_evt:
  952. pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
  953. }
  954. static void node_lost_contact(struct tipc_node *n,
  955. struct sk_buff_head *inputq)
  956. {
  957. char addr_string[16];
  958. struct tipc_sock_conn *conn, *safe;
  959. struct tipc_link *l;
  960. struct list_head *conns = &n->conn_sks;
  961. struct sk_buff *skb;
  962. uint i;
  963. pr_debug("Lost contact with %s\n",
  964. tipc_addr_string_fill(addr_string, n->addr));
  965. /* Clean up broadcast state */
  966. tipc_bcast_remove_peer(n->net, n->bc_entry.link);
  967. /* Abort any ongoing link failover */
  968. for (i = 0; i < MAX_BEARERS; i++) {
  969. l = n->links[i].link;
  970. if (l)
  971. tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
  972. }
  973. /* Notify publications from this node */
  974. n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
  975. /* Notify sockets connected to node */
  976. list_for_each_entry_safe(conn, safe, conns, list) {
  977. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
  978. SHORT_H_SIZE, 0, tipc_own_addr(n->net),
  979. conn->peer_node, conn->port,
  980. conn->peer_port, TIPC_ERR_NO_NODE);
  981. if (likely(skb))
  982. skb_queue_tail(inputq, skb);
  983. list_del(&conn->list);
  984. kfree(conn);
  985. }
  986. }
  987. /**
  988. * tipc_node_get_linkname - get the name of a link
  989. *
  990. * @bearer_id: id of the bearer
  991. * @node: peer node address
  992. * @linkname: link name output buffer
  993. *
  994. * Returns 0 on success
  995. */
  996. int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
  997. char *linkname, size_t len)
  998. {
  999. struct tipc_link *link;
  1000. int err = -EINVAL;
  1001. struct tipc_node *node = tipc_node_find(net, addr);
  1002. if (!node)
  1003. return err;
  1004. if (bearer_id >= MAX_BEARERS)
  1005. goto exit;
  1006. tipc_node_read_lock(node);
  1007. link = node->links[bearer_id].link;
  1008. if (link) {
  1009. strncpy(linkname, tipc_link_name(link), len);
  1010. err = 0;
  1011. }
  1012. exit:
  1013. tipc_node_read_unlock(node);
  1014. tipc_node_put(node);
  1015. return err;
  1016. }
  1017. /* Caller should hold node lock for the passed node */
  1018. static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
  1019. {
  1020. void *hdr;
  1021. struct nlattr *attrs;
  1022. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1023. NLM_F_MULTI, TIPC_NL_NODE_GET);
  1024. if (!hdr)
  1025. return -EMSGSIZE;
  1026. attrs = nla_nest_start(msg->skb, TIPC_NLA_NODE);
  1027. if (!attrs)
  1028. goto msg_full;
  1029. if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
  1030. goto attr_msg_full;
  1031. if (tipc_node_is_up(node))
  1032. if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
  1033. goto attr_msg_full;
  1034. nla_nest_end(msg->skb, attrs);
  1035. genlmsg_end(msg->skb, hdr);
  1036. return 0;
  1037. attr_msg_full:
  1038. nla_nest_cancel(msg->skb, attrs);
  1039. msg_full:
  1040. genlmsg_cancel(msg->skb, hdr);
  1041. return -EMSGSIZE;
  1042. }
  1043. /**
  1044. * tipc_node_xmit() is the general link level function for message sending
  1045. * @net: the applicable net namespace
  1046. * @list: chain of buffers containing message
  1047. * @dnode: address of destination node
  1048. * @selector: a number used for deterministic link selection
  1049. * Consumes the buffer chain, except when returning -ELINKCONG
  1050. * Returns 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
  1051. */
  1052. int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
  1053. u32 dnode, int selector)
  1054. {
  1055. struct tipc_link_entry *le = NULL;
  1056. struct tipc_node *n;
  1057. struct sk_buff_head xmitq;
  1058. int bearer_id;
  1059. int rc;
  1060. if (in_own_node(net, dnode)) {
  1061. tipc_sk_rcv(net, list);
  1062. return 0;
  1063. }
  1064. n = tipc_node_find(net, dnode);
  1065. if (unlikely(!n)) {
  1066. skb_queue_purge(list);
  1067. return -EHOSTUNREACH;
  1068. }
  1069. tipc_node_read_lock(n);
  1070. bearer_id = n->active_links[selector & 1];
  1071. if (unlikely(bearer_id == INVALID_BEARER_ID)) {
  1072. tipc_node_read_unlock(n);
  1073. tipc_node_put(n);
  1074. skb_queue_purge(list);
  1075. return -EHOSTUNREACH;
  1076. }
  1077. __skb_queue_head_init(&xmitq);
  1078. le = &n->links[bearer_id];
  1079. spin_lock_bh(&le->lock);
  1080. rc = tipc_link_xmit(le->link, list, &xmitq);
  1081. spin_unlock_bh(&le->lock);
  1082. tipc_node_read_unlock(n);
  1083. if (likely(rc == 0))
  1084. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1085. else if (rc == -ENOBUFS)
  1086. tipc_node_link_down(n, bearer_id, false);
  1087. tipc_node_put(n);
  1088. return rc;
  1089. }
  1090. /* tipc_node_xmit_skb(): send single buffer to destination
  1091. * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
  1092. * messages, which will not be rejected
  1093. * The only exception is datagram messages rerouted after secondary
  1094. * lookup, which are rare and safe to dispose of anyway.
  1095. * TODO: Return real return value, and let callers use
  1096. * tipc_wait_for_sendpkt() where applicable
  1097. */
  1098. int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  1099. u32 selector)
  1100. {
  1101. struct sk_buff_head head;
  1102. int rc;
  1103. skb_queue_head_init(&head);
  1104. __skb_queue_tail(&head, skb);
  1105. rc = tipc_node_xmit(net, &head, dnode, selector);
  1106. if (rc == -ELINKCONG)
  1107. kfree_skb(skb);
  1108. return 0;
  1109. }
  1110. void tipc_node_broadcast(struct net *net, struct sk_buff *skb)
  1111. {
  1112. struct sk_buff *txskb;
  1113. struct tipc_node *n;
  1114. u32 dst;
  1115. rcu_read_lock();
  1116. list_for_each_entry_rcu(n, tipc_nodes(net), list) {
  1117. dst = n->addr;
  1118. if (in_own_node(net, dst))
  1119. continue;
  1120. if (!tipc_node_is_up(n))
  1121. continue;
  1122. txskb = pskb_copy(skb, GFP_ATOMIC);
  1123. if (!txskb)
  1124. break;
  1125. msg_set_destnode(buf_msg(txskb), dst);
  1126. tipc_node_xmit_skb(net, txskb, dst, 0);
  1127. }
  1128. rcu_read_unlock();
  1129. kfree_skb(skb);
  1130. }
  1131. static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
  1132. int bearer_id, struct sk_buff_head *xmitq)
  1133. {
  1134. struct tipc_link *ucl;
  1135. int rc;
  1136. rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr);
  1137. if (rc & TIPC_LINK_DOWN_EVT) {
  1138. tipc_bearer_reset_all(n->net);
  1139. return;
  1140. }
  1141. if (!(rc & TIPC_LINK_SND_STATE))
  1142. return;
  1143. /* If probe message, a STATE response will be sent anyway */
  1144. if (msg_probe(hdr))
  1145. return;
  1146. /* Produce a STATE message carrying broadcast NACK */
  1147. tipc_node_read_lock(n);
  1148. ucl = n->links[bearer_id].link;
  1149. if (ucl)
  1150. tipc_link_build_state_msg(ucl, xmitq);
  1151. tipc_node_read_unlock(n);
  1152. }
  1153. /**
  1154. * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
  1155. * @net: the applicable net namespace
  1156. * @skb: TIPC packet
  1157. * @bearer_id: id of bearer message arrived on
  1158. *
  1159. * Invoked with no locks held.
  1160. */
  1161. static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
  1162. {
  1163. int rc;
  1164. struct sk_buff_head xmitq;
  1165. struct tipc_bclink_entry *be;
  1166. struct tipc_link_entry *le;
  1167. struct tipc_msg *hdr = buf_msg(skb);
  1168. int usr = msg_user(hdr);
  1169. u32 dnode = msg_destnode(hdr);
  1170. struct tipc_node *n;
  1171. __skb_queue_head_init(&xmitq);
  1172. /* If NACK for other node, let rcv link for that node peek into it */
  1173. if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
  1174. n = tipc_node_find(net, dnode);
  1175. else
  1176. n = tipc_node_find(net, msg_prevnode(hdr));
  1177. if (!n) {
  1178. kfree_skb(skb);
  1179. return;
  1180. }
  1181. be = &n->bc_entry;
  1182. le = &n->links[bearer_id];
  1183. rc = tipc_bcast_rcv(net, be->link, skb);
  1184. /* Broadcast ACKs are sent on a unicast link */
  1185. if (rc & TIPC_LINK_SND_STATE) {
  1186. tipc_node_read_lock(n);
  1187. tipc_link_build_state_msg(le->link, &xmitq);
  1188. tipc_node_read_unlock(n);
  1189. }
  1190. if (!skb_queue_empty(&xmitq))
  1191. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1192. /* Deliver. 'arrvq' is under inputq2's lock protection */
  1193. if (!skb_queue_empty(&be->inputq1)) {
  1194. spin_lock_bh(&be->inputq2.lock);
  1195. spin_lock_bh(&be->inputq1.lock);
  1196. skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
  1197. spin_unlock_bh(&be->inputq1.lock);
  1198. spin_unlock_bh(&be->inputq2.lock);
  1199. tipc_sk_mcast_rcv(net, &be->arrvq, &be->inputq2);
  1200. }
  1201. if (rc & TIPC_LINK_DOWN_EVT) {
  1202. /* Reception reassembly failure => reset all links to peer */
  1203. if (!tipc_link_is_up(be->link))
  1204. tipc_node_reset_links(n);
  1205. /* Retransmission failure => reset all links to all peers */
  1206. if (!tipc_link_is_up(tipc_bc_sndlink(net)))
  1207. tipc_bearer_reset_all(net);
  1208. }
  1209. tipc_node_put(n);
  1210. }
  1211. /**
  1212. * tipc_node_check_state - check and if necessary update node state
  1213. * @skb: TIPC packet
  1214. * @bearer_id: identity of bearer delivering the packet
  1215. * Returns true if state is ok, otherwise consumes buffer and returns false
  1216. */
  1217. static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
  1218. int bearer_id, struct sk_buff_head *xmitq)
  1219. {
  1220. struct tipc_msg *hdr = buf_msg(skb);
  1221. int usr = msg_user(hdr);
  1222. int mtyp = msg_type(hdr);
  1223. u16 oseqno = msg_seqno(hdr);
  1224. u16 iseqno = msg_seqno(msg_get_wrapped(hdr));
  1225. u16 exp_pkts = msg_msgcnt(hdr);
  1226. u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
  1227. int state = n->state;
  1228. struct tipc_link *l, *tnl, *pl = NULL;
  1229. struct tipc_media_addr *maddr;
  1230. int pb_id;
  1231. l = n->links[bearer_id].link;
  1232. if (!l)
  1233. return false;
  1234. rcv_nxt = tipc_link_rcv_nxt(l);
  1235. if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
  1236. return true;
  1237. /* Find parallel link, if any */
  1238. for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
  1239. if ((pb_id != bearer_id) && n->links[pb_id].link) {
  1240. pl = n->links[pb_id].link;
  1241. break;
  1242. }
  1243. }
  1244. /* Check and update node accesibility if applicable */
  1245. if (state == SELF_UP_PEER_COMING) {
  1246. if (!tipc_link_is_up(l))
  1247. return true;
  1248. if (!msg_peer_link_is_up(hdr))
  1249. return true;
  1250. tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
  1251. }
  1252. if (state == SELF_DOWN_PEER_LEAVING) {
  1253. if (msg_peer_node_is_up(hdr))
  1254. return false;
  1255. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  1256. return true;
  1257. }
  1258. if (state == SELF_LEAVING_PEER_DOWN)
  1259. return false;
  1260. /* Ignore duplicate packets */
  1261. if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
  1262. return true;
  1263. /* Initiate or update failover mode if applicable */
  1264. if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
  1265. syncpt = oseqno + exp_pkts - 1;
  1266. if (pl && tipc_link_is_up(pl)) {
  1267. __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
  1268. tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
  1269. tipc_link_inputq(l));
  1270. }
  1271. /* If pkts arrive out of order, use lowest calculated syncpt */
  1272. if (less(syncpt, n->sync_point))
  1273. n->sync_point = syncpt;
  1274. }
  1275. /* Open parallel link when tunnel link reaches synch point */
  1276. if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
  1277. if (!more(rcv_nxt, n->sync_point))
  1278. return true;
  1279. tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
  1280. if (pl)
  1281. tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
  1282. return true;
  1283. }
  1284. /* No synching needed if only one link */
  1285. if (!pl || !tipc_link_is_up(pl))
  1286. return true;
  1287. /* Initiate synch mode if applicable */
  1288. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
  1289. syncpt = iseqno + exp_pkts - 1;
  1290. if (!tipc_link_is_up(l)) {
  1291. tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
  1292. __tipc_node_link_up(n, bearer_id, xmitq);
  1293. }
  1294. if (n->state == SELF_UP_PEER_UP) {
  1295. n->sync_point = syncpt;
  1296. tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
  1297. tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
  1298. }
  1299. }
  1300. /* Open tunnel link when parallel link reaches synch point */
  1301. if (n->state == NODE_SYNCHING) {
  1302. if (tipc_link_is_synching(l)) {
  1303. tnl = l;
  1304. } else {
  1305. tnl = pl;
  1306. pl = l;
  1307. }
  1308. inputq_len = skb_queue_len(tipc_link_inputq(pl));
  1309. dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
  1310. if (more(dlv_nxt, n->sync_point)) {
  1311. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  1312. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  1313. return true;
  1314. }
  1315. if (l == pl)
  1316. return true;
  1317. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
  1318. return true;
  1319. if (usr == LINK_PROTOCOL)
  1320. return true;
  1321. return false;
  1322. }
  1323. return true;
  1324. }
  1325. /**
  1326. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1327. * @net: the applicable net namespace
  1328. * @skb: TIPC packet
  1329. * @bearer: pointer to bearer message arrived on
  1330. *
  1331. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1332. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1333. */
  1334. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
  1335. {
  1336. struct sk_buff_head xmitq;
  1337. struct tipc_node *n;
  1338. struct tipc_msg *hdr = buf_msg(skb);
  1339. int usr = msg_user(hdr);
  1340. int bearer_id = b->identity;
  1341. struct tipc_link_entry *le;
  1342. u16 bc_ack = msg_bcast_ack(hdr);
  1343. u32 self = tipc_own_addr(net);
  1344. int rc = 0;
  1345. __skb_queue_head_init(&xmitq);
  1346. /* Ensure message is well-formed */
  1347. if (unlikely(!tipc_msg_validate(skb)))
  1348. goto discard;
  1349. /* Handle arrival of discovery or broadcast packet */
  1350. if (unlikely(msg_non_seq(hdr))) {
  1351. if (unlikely(usr == LINK_CONFIG))
  1352. return tipc_disc_rcv(net, skb, b);
  1353. else
  1354. return tipc_node_bc_rcv(net, skb, bearer_id);
  1355. }
  1356. /* Discard unicast link messages destined for another node */
  1357. if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
  1358. goto discard;
  1359. /* Locate neighboring node that sent packet */
  1360. n = tipc_node_find(net, msg_prevnode(hdr));
  1361. if (unlikely(!n))
  1362. goto discard;
  1363. le = &n->links[bearer_id];
  1364. /* Ensure broadcast reception is in synch with peer's send state */
  1365. if (unlikely(usr == LINK_PROTOCOL))
  1366. tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
  1367. else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack))
  1368. tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
  1369. /* Receive packet directly if conditions permit */
  1370. tipc_node_read_lock(n);
  1371. if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
  1372. spin_lock_bh(&le->lock);
  1373. if (le->link) {
  1374. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1375. skb = NULL;
  1376. }
  1377. spin_unlock_bh(&le->lock);
  1378. }
  1379. tipc_node_read_unlock(n);
  1380. /* Check/update node state before receiving */
  1381. if (unlikely(skb)) {
  1382. tipc_node_write_lock(n);
  1383. if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
  1384. if (le->link) {
  1385. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1386. skb = NULL;
  1387. }
  1388. }
  1389. tipc_node_write_unlock(n);
  1390. }
  1391. if (unlikely(rc & TIPC_LINK_UP_EVT))
  1392. tipc_node_link_up(n, bearer_id, &xmitq);
  1393. if (unlikely(rc & TIPC_LINK_DOWN_EVT))
  1394. tipc_node_link_down(n, bearer_id, false);
  1395. if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
  1396. tipc_named_rcv(net, &n->bc_entry.namedq);
  1397. if (!skb_queue_empty(&le->inputq))
  1398. tipc_sk_rcv(net, &le->inputq);
  1399. if (!skb_queue_empty(&xmitq))
  1400. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr);
  1401. tipc_node_put(n);
  1402. discard:
  1403. kfree_skb(skb);
  1404. }
  1405. int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
  1406. {
  1407. struct net *net = sock_net(skb->sk);
  1408. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1409. struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
  1410. struct tipc_node *peer;
  1411. u32 addr;
  1412. int err;
  1413. int i;
  1414. /* We identify the peer by its net */
  1415. if (!info->attrs[TIPC_NLA_NET])
  1416. return -EINVAL;
  1417. err = nla_parse_nested(attrs, TIPC_NLA_NET_MAX,
  1418. info->attrs[TIPC_NLA_NET],
  1419. tipc_nl_net_policy);
  1420. if (err)
  1421. return err;
  1422. if (!attrs[TIPC_NLA_NET_ADDR])
  1423. return -EINVAL;
  1424. addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
  1425. if (in_own_node(net, addr))
  1426. return -ENOTSUPP;
  1427. spin_lock_bh(&tn->node_list_lock);
  1428. peer = tipc_node_find(net, addr);
  1429. if (!peer) {
  1430. spin_unlock_bh(&tn->node_list_lock);
  1431. return -ENXIO;
  1432. }
  1433. tipc_node_write_lock(peer);
  1434. if (peer->state != SELF_DOWN_PEER_DOWN &&
  1435. peer->state != SELF_DOWN_PEER_LEAVING) {
  1436. tipc_node_write_unlock(peer);
  1437. err = -EBUSY;
  1438. goto err_out;
  1439. }
  1440. for (i = 0; i < MAX_BEARERS; i++) {
  1441. struct tipc_link_entry *le = &peer->links[i];
  1442. if (le->link) {
  1443. kfree(le->link);
  1444. le->link = NULL;
  1445. peer->link_cnt--;
  1446. }
  1447. }
  1448. tipc_node_write_unlock(peer);
  1449. tipc_node_delete(peer);
  1450. err = 0;
  1451. err_out:
  1452. tipc_node_put(peer);
  1453. spin_unlock_bh(&tn->node_list_lock);
  1454. return err;
  1455. }
  1456. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1457. {
  1458. int err;
  1459. struct net *net = sock_net(skb->sk);
  1460. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1461. int done = cb->args[0];
  1462. int last_addr = cb->args[1];
  1463. struct tipc_node *node;
  1464. struct tipc_nl_msg msg;
  1465. if (done)
  1466. return 0;
  1467. msg.skb = skb;
  1468. msg.portid = NETLINK_CB(cb->skb).portid;
  1469. msg.seq = cb->nlh->nlmsg_seq;
  1470. rcu_read_lock();
  1471. if (last_addr) {
  1472. node = tipc_node_find(net, last_addr);
  1473. if (!node) {
  1474. rcu_read_unlock();
  1475. /* We never set seq or call nl_dump_check_consistent()
  1476. * this means that setting prev_seq here will cause the
  1477. * consistence check to fail in the netlink callback
  1478. * handler. Resulting in the NLMSG_DONE message having
  1479. * the NLM_F_DUMP_INTR flag set if the node state
  1480. * changed while we released the lock.
  1481. */
  1482. cb->prev_seq = 1;
  1483. return -EPIPE;
  1484. }
  1485. tipc_node_put(node);
  1486. }
  1487. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1488. if (last_addr) {
  1489. if (node->addr == last_addr)
  1490. last_addr = 0;
  1491. else
  1492. continue;
  1493. }
  1494. tipc_node_read_lock(node);
  1495. err = __tipc_nl_add_node(&msg, node);
  1496. if (err) {
  1497. last_addr = node->addr;
  1498. tipc_node_read_unlock(node);
  1499. goto out;
  1500. }
  1501. tipc_node_read_unlock(node);
  1502. }
  1503. done = 1;
  1504. out:
  1505. cb->args[0] = done;
  1506. cb->args[1] = last_addr;
  1507. rcu_read_unlock();
  1508. return skb->len;
  1509. }
  1510. /* tipc_node_find_by_name - locate owner node of link by link's name
  1511. * @net: the applicable net namespace
  1512. * @name: pointer to link name string
  1513. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  1514. *
  1515. * Returns pointer to node owning the link, or 0 if no matching link is found.
  1516. */
  1517. static struct tipc_node *tipc_node_find_by_name(struct net *net,
  1518. const char *link_name,
  1519. unsigned int *bearer_id)
  1520. {
  1521. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1522. struct tipc_link *l;
  1523. struct tipc_node *n;
  1524. struct tipc_node *found_node = NULL;
  1525. int i;
  1526. *bearer_id = 0;
  1527. rcu_read_lock();
  1528. list_for_each_entry_rcu(n, &tn->node_list, list) {
  1529. tipc_node_read_lock(n);
  1530. for (i = 0; i < MAX_BEARERS; i++) {
  1531. l = n->links[i].link;
  1532. if (l && !strcmp(tipc_link_name(l), link_name)) {
  1533. *bearer_id = i;
  1534. found_node = n;
  1535. break;
  1536. }
  1537. }
  1538. tipc_node_read_unlock(n);
  1539. if (found_node)
  1540. break;
  1541. }
  1542. rcu_read_unlock();
  1543. return found_node;
  1544. }
  1545. int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
  1546. {
  1547. int err;
  1548. int res = 0;
  1549. int bearer_id;
  1550. char *name;
  1551. struct tipc_link *link;
  1552. struct tipc_node *node;
  1553. struct sk_buff_head xmitq;
  1554. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1555. struct net *net = sock_net(skb->sk);
  1556. __skb_queue_head_init(&xmitq);
  1557. if (!info->attrs[TIPC_NLA_LINK])
  1558. return -EINVAL;
  1559. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1560. info->attrs[TIPC_NLA_LINK],
  1561. tipc_nl_link_policy);
  1562. if (err)
  1563. return err;
  1564. if (!attrs[TIPC_NLA_LINK_NAME])
  1565. return -EINVAL;
  1566. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1567. if (strcmp(name, tipc_bclink_name) == 0)
  1568. return tipc_nl_bc_link_set(net, attrs);
  1569. node = tipc_node_find_by_name(net, name, &bearer_id);
  1570. if (!node)
  1571. return -EINVAL;
  1572. tipc_node_read_lock(node);
  1573. link = node->links[bearer_id].link;
  1574. if (!link) {
  1575. res = -EINVAL;
  1576. goto out;
  1577. }
  1578. if (attrs[TIPC_NLA_LINK_PROP]) {
  1579. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  1580. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP],
  1581. props);
  1582. if (err) {
  1583. res = err;
  1584. goto out;
  1585. }
  1586. if (props[TIPC_NLA_PROP_TOL]) {
  1587. u32 tol;
  1588. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  1589. tipc_link_set_tolerance(link, tol, &xmitq);
  1590. }
  1591. if (props[TIPC_NLA_PROP_PRIO]) {
  1592. u32 prio;
  1593. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  1594. tipc_link_set_prio(link, prio, &xmitq);
  1595. }
  1596. if (props[TIPC_NLA_PROP_WIN]) {
  1597. u32 win;
  1598. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  1599. tipc_link_set_queue_limits(link, win);
  1600. }
  1601. }
  1602. out:
  1603. tipc_node_read_unlock(node);
  1604. tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr);
  1605. return res;
  1606. }
  1607. int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
  1608. {
  1609. struct net *net = genl_info_net(info);
  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_NAME])
  1616. return -EINVAL;
  1617. name = nla_data(info->attrs[TIPC_NLA_LINK_NAME]);
  1618. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1619. if (!msg.skb)
  1620. return -ENOMEM;
  1621. if (strcmp(name, tipc_bclink_name) == 0) {
  1622. err = tipc_nl_add_bc_link(net, &msg);
  1623. if (err)
  1624. goto err_free;
  1625. } else {
  1626. int bearer_id;
  1627. struct tipc_node *node;
  1628. struct tipc_link *link;
  1629. node = tipc_node_find_by_name(net, name, &bearer_id);
  1630. if (!node) {
  1631. err = -EINVAL;
  1632. goto err_free;
  1633. }
  1634. tipc_node_read_lock(node);
  1635. link = node->links[bearer_id].link;
  1636. if (!link) {
  1637. tipc_node_read_unlock(node);
  1638. err = -EINVAL;
  1639. goto err_free;
  1640. }
  1641. err = __tipc_nl_add_link(net, &msg, link, 0);
  1642. tipc_node_read_unlock(node);
  1643. if (err)
  1644. goto err_free;
  1645. }
  1646. return genlmsg_reply(msg.skb, info);
  1647. err_free:
  1648. nlmsg_free(msg.skb);
  1649. return err;
  1650. }
  1651. int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
  1652. {
  1653. int err;
  1654. char *link_name;
  1655. unsigned int bearer_id;
  1656. struct tipc_link *link;
  1657. struct tipc_node *node;
  1658. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  1659. struct net *net = sock_net(skb->sk);
  1660. struct tipc_link_entry *le;
  1661. if (!info->attrs[TIPC_NLA_LINK])
  1662. return -EINVAL;
  1663. err = nla_parse_nested(attrs, TIPC_NLA_LINK_MAX,
  1664. info->attrs[TIPC_NLA_LINK],
  1665. tipc_nl_link_policy);
  1666. if (err)
  1667. return err;
  1668. if (!attrs[TIPC_NLA_LINK_NAME])
  1669. return -EINVAL;
  1670. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  1671. if (strcmp(link_name, tipc_bclink_name) == 0) {
  1672. err = tipc_bclink_reset_stats(net);
  1673. if (err)
  1674. return err;
  1675. return 0;
  1676. }
  1677. node = tipc_node_find_by_name(net, link_name, &bearer_id);
  1678. if (!node)
  1679. return -EINVAL;
  1680. le = &node->links[bearer_id];
  1681. tipc_node_read_lock(node);
  1682. spin_lock_bh(&le->lock);
  1683. link = node->links[bearer_id].link;
  1684. if (!link) {
  1685. spin_unlock_bh(&le->lock);
  1686. tipc_node_read_unlock(node);
  1687. return -EINVAL;
  1688. }
  1689. tipc_link_reset_stats(link);
  1690. spin_unlock_bh(&le->lock);
  1691. tipc_node_read_unlock(node);
  1692. return 0;
  1693. }
  1694. /* Caller should hold node lock */
  1695. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  1696. struct tipc_node *node, u32 *prev_link)
  1697. {
  1698. u32 i;
  1699. int err;
  1700. for (i = *prev_link; i < MAX_BEARERS; i++) {
  1701. *prev_link = i;
  1702. if (!node->links[i].link)
  1703. continue;
  1704. err = __tipc_nl_add_link(net, msg,
  1705. node->links[i].link, NLM_F_MULTI);
  1706. if (err)
  1707. return err;
  1708. }
  1709. *prev_link = 0;
  1710. return 0;
  1711. }
  1712. int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
  1713. {
  1714. struct net *net = sock_net(skb->sk);
  1715. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1716. struct tipc_node *node;
  1717. struct tipc_nl_msg msg;
  1718. u32 prev_node = cb->args[0];
  1719. u32 prev_link = cb->args[1];
  1720. int done = cb->args[2];
  1721. int err;
  1722. if (done)
  1723. return 0;
  1724. msg.skb = skb;
  1725. msg.portid = NETLINK_CB(cb->skb).portid;
  1726. msg.seq = cb->nlh->nlmsg_seq;
  1727. rcu_read_lock();
  1728. if (prev_node) {
  1729. node = tipc_node_find(net, prev_node);
  1730. if (!node) {
  1731. /* We never set seq or call nl_dump_check_consistent()
  1732. * this means that setting prev_seq here will cause the
  1733. * consistence check to fail in the netlink callback
  1734. * handler. Resulting in the last NLMSG_DONE message
  1735. * having the NLM_F_DUMP_INTR flag set.
  1736. */
  1737. cb->prev_seq = 1;
  1738. goto out;
  1739. }
  1740. tipc_node_put(node);
  1741. list_for_each_entry_continue_rcu(node, &tn->node_list,
  1742. list) {
  1743. tipc_node_read_lock(node);
  1744. err = __tipc_nl_add_node_links(net, &msg, node,
  1745. &prev_link);
  1746. tipc_node_read_unlock(node);
  1747. if (err)
  1748. goto out;
  1749. prev_node = node->addr;
  1750. }
  1751. } else {
  1752. err = tipc_nl_add_bc_link(net, &msg);
  1753. if (err)
  1754. goto out;
  1755. list_for_each_entry_rcu(node, &tn->node_list, list) {
  1756. tipc_node_read_lock(node);
  1757. err = __tipc_nl_add_node_links(net, &msg, node,
  1758. &prev_link);
  1759. tipc_node_read_unlock(node);
  1760. if (err)
  1761. goto out;
  1762. prev_node = node->addr;
  1763. }
  1764. }
  1765. done = 1;
  1766. out:
  1767. rcu_read_unlock();
  1768. cb->args[0] = prev_node;
  1769. cb->args[1] = prev_link;
  1770. cb->args[2] = done;
  1771. return skb->len;
  1772. }
  1773. int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
  1774. {
  1775. struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
  1776. struct net *net = sock_net(skb->sk);
  1777. int err;
  1778. if (!info->attrs[TIPC_NLA_MON])
  1779. return -EINVAL;
  1780. err = nla_parse_nested(attrs, TIPC_NLA_MON_MAX,
  1781. info->attrs[TIPC_NLA_MON],
  1782. tipc_nl_monitor_policy);
  1783. if (err)
  1784. return err;
  1785. if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
  1786. u32 val;
  1787. val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
  1788. err = tipc_nl_monitor_set_threshold(net, val);
  1789. if (err)
  1790. return err;
  1791. }
  1792. return 0;
  1793. }
  1794. static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
  1795. {
  1796. struct nlattr *attrs;
  1797. void *hdr;
  1798. u32 val;
  1799. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1800. 0, TIPC_NL_MON_GET);
  1801. if (!hdr)
  1802. return -EMSGSIZE;
  1803. attrs = nla_nest_start(msg->skb, TIPC_NLA_MON);
  1804. if (!attrs)
  1805. goto msg_full;
  1806. val = tipc_nl_monitor_get_threshold(net);
  1807. if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
  1808. goto attr_msg_full;
  1809. nla_nest_end(msg->skb, attrs);
  1810. genlmsg_end(msg->skb, hdr);
  1811. return 0;
  1812. attr_msg_full:
  1813. nla_nest_cancel(msg->skb, attrs);
  1814. msg_full:
  1815. genlmsg_cancel(msg->skb, hdr);
  1816. return -EMSGSIZE;
  1817. }
  1818. int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
  1819. {
  1820. struct net *net = sock_net(skb->sk);
  1821. struct tipc_nl_msg msg;
  1822. int err;
  1823. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  1824. if (!msg.skb)
  1825. return -ENOMEM;
  1826. msg.portid = info->snd_portid;
  1827. msg.seq = info->snd_seq;
  1828. err = __tipc_nl_add_monitor_prop(net, &msg);
  1829. if (err) {
  1830. nlmsg_free(msg.skb);
  1831. return err;
  1832. }
  1833. return genlmsg_reply(msg.skb, info);
  1834. }
  1835. int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
  1836. {
  1837. struct net *net = sock_net(skb->sk);
  1838. u32 prev_bearer = cb->args[0];
  1839. struct tipc_nl_msg msg;
  1840. int err;
  1841. int i;
  1842. if (prev_bearer == MAX_BEARERS)
  1843. return 0;
  1844. msg.skb = skb;
  1845. msg.portid = NETLINK_CB(cb->skb).portid;
  1846. msg.seq = cb->nlh->nlmsg_seq;
  1847. rtnl_lock();
  1848. for (i = prev_bearer; i < MAX_BEARERS; i++) {
  1849. prev_bearer = i;
  1850. err = __tipc_nl_add_monitor(net, &msg, prev_bearer);
  1851. if (err)
  1852. goto out;
  1853. }
  1854. out:
  1855. rtnl_unlock();
  1856. cb->args[0] = prev_bearer;
  1857. return skb->len;
  1858. }
  1859. int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
  1860. struct netlink_callback *cb)
  1861. {
  1862. struct net *net = sock_net(skb->sk);
  1863. u32 prev_node = cb->args[1];
  1864. u32 bearer_id = cb->args[2];
  1865. int done = cb->args[0];
  1866. struct tipc_nl_msg msg;
  1867. int err;
  1868. if (!prev_node) {
  1869. struct nlattr **attrs;
  1870. struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
  1871. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  1872. if (err)
  1873. return err;
  1874. if (!attrs[TIPC_NLA_MON])
  1875. return -EINVAL;
  1876. err = nla_parse_nested(mon, TIPC_NLA_MON_MAX,
  1877. attrs[TIPC_NLA_MON],
  1878. tipc_nl_monitor_policy);
  1879. if (err)
  1880. return err;
  1881. if (!mon[TIPC_NLA_MON_REF])
  1882. return -EINVAL;
  1883. bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
  1884. if (bearer_id >= MAX_BEARERS)
  1885. return -EINVAL;
  1886. }
  1887. if (done)
  1888. return 0;
  1889. msg.skb = skb;
  1890. msg.portid = NETLINK_CB(cb->skb).portid;
  1891. msg.seq = cb->nlh->nlmsg_seq;
  1892. rtnl_lock();
  1893. err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
  1894. if (!err)
  1895. done = 1;
  1896. rtnl_unlock();
  1897. cb->args[0] = done;
  1898. cb->args[1] = prev_node;
  1899. cb->args[2] = bearer_id;
  1900. return skb->len;
  1901. }