node.c 57 KB

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