node.c 57 KB

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