node.c 59 KB

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