node.c 59 KB

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