node.c 53 KB

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