node.c 59 KB

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