node.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165
  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. #include "trace.h"
  46. #include "crypto.h"
  47. #define INVALID_NODE_SIG 0x10000
  48. #define NODE_CLEANUP_AFTER 300000
  49. /* Flags used to take different actions according to flag type
  50. * TIPC_NOTIFY_NODE_DOWN: notify node is down
  51. * TIPC_NOTIFY_NODE_UP: notify node is up
  52. * TIPC_DISTRIBUTE_NAME: publish or withdraw link state name type
  53. */
  54. enum {
  55. TIPC_NOTIFY_NODE_DOWN = (1 << 3),
  56. TIPC_NOTIFY_NODE_UP = (1 << 4),
  57. TIPC_NOTIFY_LINK_UP = (1 << 6),
  58. TIPC_NOTIFY_LINK_DOWN = (1 << 7)
  59. };
  60. struct tipc_link_entry {
  61. struct tipc_link *link;
  62. spinlock_t lock; /* per link */
  63. u32 mtu;
  64. struct sk_buff_head inputq;
  65. struct tipc_media_addr maddr;
  66. };
  67. struct tipc_bclink_entry {
  68. struct tipc_link *link;
  69. struct sk_buff_head inputq1;
  70. struct sk_buff_head arrvq;
  71. struct sk_buff_head inputq2;
  72. struct sk_buff_head namedq;
  73. u16 named_rcv_nxt;
  74. bool named_open;
  75. };
  76. /**
  77. * struct tipc_node - TIPC node structure
  78. * @addr: network address of node
  79. * @kref: reference counter to node object
  80. * @lock: rwlock governing access to structure
  81. * @net: the applicable net namespace
  82. * @hash: links to adjacent nodes in unsorted hash chain
  83. * @active_links: bearer ids of active links, used as index into links[] array
  84. * @links: array containing references to all links to node
  85. * @bc_entry: broadcast link entry
  86. * @action_flags: bit mask of different types of node actions
  87. * @state: connectivity state vs peer node
  88. * @preliminary: a preliminary node or not
  89. * @failover_sent: failover sent or not
  90. * @sync_point: sequence number where synch/failover is finished
  91. * @list: links to adjacent nodes in sorted list of cluster's nodes
  92. * @working_links: number of working links to node (both active and standby)
  93. * @link_cnt: number of links to node
  94. * @capabilities: bitmap, indicating peer node's functional capabilities
  95. * @signature: node instance identifier
  96. * @link_id: local and remote bearer ids of changing link, if any
  97. * @peer_id: 128-bit ID of peer
  98. * @peer_id_string: ID string of peer
  99. * @publ_list: list of publications
  100. * @conn_sks: list of connections (FIXME)
  101. * @timer: node's keepalive timer
  102. * @keepalive_intv: keepalive interval in milliseconds
  103. * @rcu: rcu struct for tipc_node
  104. * @delete_at: indicates the time for deleting a down node
  105. * @peer_net: peer's net namespace
  106. * @peer_hash_mix: hash for this peer (FIXME)
  107. * @crypto_rx: RX crypto handler
  108. */
  109. struct tipc_node {
  110. u32 addr;
  111. struct kref kref;
  112. rwlock_t lock;
  113. struct net *net;
  114. struct hlist_node hash;
  115. int active_links[2];
  116. struct tipc_link_entry links[MAX_BEARERS];
  117. struct tipc_bclink_entry bc_entry;
  118. int action_flags;
  119. struct list_head list;
  120. int state;
  121. bool preliminary;
  122. bool failover_sent;
  123. u16 sync_point;
  124. int link_cnt;
  125. u16 working_links;
  126. u16 capabilities;
  127. u32 signature;
  128. u32 link_id;
  129. u8 peer_id[16];
  130. char peer_id_string[NODE_ID_STR_LEN];
  131. struct list_head publ_list;
  132. struct list_head conn_sks;
  133. unsigned long keepalive_intv;
  134. struct timer_list timer;
  135. struct rcu_head rcu;
  136. unsigned long delete_at;
  137. struct net *peer_net;
  138. u32 peer_hash_mix;
  139. #ifdef CONFIG_TIPC_CRYPTO
  140. struct tipc_crypto *crypto_rx;
  141. #endif
  142. };
  143. /* Node FSM states and events:
  144. */
  145. enum {
  146. SELF_DOWN_PEER_DOWN = 0xdd,
  147. SELF_UP_PEER_UP = 0xaa,
  148. SELF_DOWN_PEER_LEAVING = 0xd1,
  149. SELF_UP_PEER_COMING = 0xac,
  150. SELF_COMING_PEER_UP = 0xca,
  151. SELF_LEAVING_PEER_DOWN = 0x1d,
  152. NODE_FAILINGOVER = 0xf0,
  153. NODE_SYNCHING = 0xcc
  154. };
  155. enum {
  156. SELF_ESTABL_CONTACT_EVT = 0xece,
  157. SELF_LOST_CONTACT_EVT = 0x1ce,
  158. PEER_ESTABL_CONTACT_EVT = 0x9ece,
  159. PEER_LOST_CONTACT_EVT = 0x91ce,
  160. NODE_FAILOVER_BEGIN_EVT = 0xfbe,
  161. NODE_FAILOVER_END_EVT = 0xfee,
  162. NODE_SYNCH_BEGIN_EVT = 0xcbe,
  163. NODE_SYNCH_END_EVT = 0xcee
  164. };
  165. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  166. struct sk_buff_head *xmitq,
  167. struct tipc_media_addr **maddr);
  168. static void tipc_node_link_down(struct tipc_node *n, int bearer_id,
  169. bool delete);
  170. static void node_lost_contact(struct tipc_node *n, struct sk_buff_head *inputq);
  171. static void tipc_node_delete(struct tipc_node *node);
  172. static void tipc_node_timeout(struct timer_list *t);
  173. static void tipc_node_fsm_evt(struct tipc_node *n, int evt);
  174. static struct tipc_node *tipc_node_find(struct net *net, u32 addr);
  175. static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id);
  176. static bool node_is_up(struct tipc_node *n);
  177. static void tipc_node_delete_from_list(struct tipc_node *node);
  178. struct tipc_sock_conn {
  179. u32 port;
  180. u32 peer_port;
  181. u32 peer_node;
  182. struct list_head list;
  183. };
  184. static struct tipc_link *node_active_link(struct tipc_node *n, int sel)
  185. {
  186. int bearer_id = n->active_links[sel & 1];
  187. if (unlikely(bearer_id == INVALID_BEARER_ID))
  188. return NULL;
  189. return n->links[bearer_id].link;
  190. }
  191. int tipc_node_get_mtu(struct net *net, u32 addr, u32 sel, bool connected)
  192. {
  193. struct tipc_node *n;
  194. int bearer_id;
  195. unsigned int mtu = MAX_MSG_SIZE;
  196. n = tipc_node_find(net, addr);
  197. if (unlikely(!n))
  198. return mtu;
  199. /* Allow MAX_MSG_SIZE when building connection oriented message
  200. * if they are in the same core network
  201. */
  202. if (n->peer_net && connected) {
  203. tipc_node_put(n);
  204. return mtu;
  205. }
  206. bearer_id = n->active_links[sel & 1];
  207. if (likely(bearer_id != INVALID_BEARER_ID))
  208. mtu = n->links[bearer_id].mtu;
  209. tipc_node_put(n);
  210. return mtu;
  211. }
  212. bool tipc_node_get_id(struct net *net, u32 addr, u8 *id)
  213. {
  214. u8 *own_id = tipc_own_id(net);
  215. struct tipc_node *n;
  216. if (!own_id)
  217. return true;
  218. if (addr == tipc_own_addr(net)) {
  219. memcpy(id, own_id, TIPC_NODEID_LEN);
  220. return true;
  221. }
  222. n = tipc_node_find(net, addr);
  223. if (!n)
  224. return false;
  225. memcpy(id, &n->peer_id, TIPC_NODEID_LEN);
  226. tipc_node_put(n);
  227. return true;
  228. }
  229. u16 tipc_node_get_capabilities(struct net *net, u32 addr)
  230. {
  231. struct tipc_node *n;
  232. u16 caps;
  233. n = tipc_node_find(net, addr);
  234. if (unlikely(!n))
  235. return TIPC_NODE_CAPABILITIES;
  236. caps = n->capabilities;
  237. tipc_node_put(n);
  238. return caps;
  239. }
  240. u32 tipc_node_get_addr(struct tipc_node *node)
  241. {
  242. return (node) ? node->addr : 0;
  243. }
  244. char *tipc_node_get_id_str(struct tipc_node *node)
  245. {
  246. return node->peer_id_string;
  247. }
  248. #ifdef CONFIG_TIPC_CRYPTO
  249. /**
  250. * tipc_node_crypto_rx - Retrieve crypto RX handle from node
  251. * @__n: target tipc_node
  252. * Note: node ref counter must be held first!
  253. */
  254. struct tipc_crypto *tipc_node_crypto_rx(struct tipc_node *__n)
  255. {
  256. return (__n) ? __n->crypto_rx : NULL;
  257. }
  258. struct tipc_crypto *tipc_node_crypto_rx_by_list(struct list_head *pos)
  259. {
  260. return container_of(pos, struct tipc_node, list)->crypto_rx;
  261. }
  262. struct tipc_crypto *tipc_node_crypto_rx_by_addr(struct net *net, u32 addr)
  263. {
  264. struct tipc_node *n;
  265. n = tipc_node_find(net, addr);
  266. return (n) ? n->crypto_rx : NULL;
  267. }
  268. #endif
  269. static void tipc_node_free(struct rcu_head *rp)
  270. {
  271. struct tipc_node *n = container_of(rp, struct tipc_node, rcu);
  272. #ifdef CONFIG_TIPC_CRYPTO
  273. tipc_crypto_stop(&n->crypto_rx);
  274. #endif
  275. kfree(n);
  276. }
  277. static void tipc_node_kref_release(struct kref *kref)
  278. {
  279. struct tipc_node *n = container_of(kref, struct tipc_node, kref);
  280. kfree(n->bc_entry.link);
  281. call_rcu(&n->rcu, tipc_node_free);
  282. }
  283. void tipc_node_put(struct tipc_node *node)
  284. {
  285. kref_put(&node->kref, tipc_node_kref_release);
  286. }
  287. void tipc_node_get(struct tipc_node *node)
  288. {
  289. kref_get(&node->kref);
  290. }
  291. /*
  292. * tipc_node_find - locate specified node object, if it exists
  293. */
  294. static struct tipc_node *tipc_node_find(struct net *net, u32 addr)
  295. {
  296. struct tipc_net *tn = tipc_net(net);
  297. struct tipc_node *node;
  298. unsigned int thash = tipc_hashfn(addr);
  299. rcu_read_lock();
  300. hlist_for_each_entry_rcu(node, &tn->node_htable[thash], hash) {
  301. if (node->addr != addr || node->preliminary)
  302. continue;
  303. if (!kref_get_unless_zero(&node->kref))
  304. node = NULL;
  305. break;
  306. }
  307. rcu_read_unlock();
  308. return node;
  309. }
  310. /* tipc_node_find_by_id - locate specified node object by its 128-bit id
  311. * Note: this function is called only when a discovery request failed
  312. * to find the node by its 32-bit id, and is not time critical
  313. */
  314. static struct tipc_node *tipc_node_find_by_id(struct net *net, u8 *id)
  315. {
  316. struct tipc_net *tn = tipc_net(net);
  317. struct tipc_node *n;
  318. bool found = false;
  319. rcu_read_lock();
  320. list_for_each_entry_rcu(n, &tn->node_list, list) {
  321. read_lock_bh(&n->lock);
  322. if (!memcmp(id, n->peer_id, 16) &&
  323. kref_get_unless_zero(&n->kref))
  324. found = true;
  325. read_unlock_bh(&n->lock);
  326. if (found)
  327. break;
  328. }
  329. rcu_read_unlock();
  330. return found ? n : NULL;
  331. }
  332. static void tipc_node_read_lock(struct tipc_node *n)
  333. __acquires(n->lock)
  334. {
  335. read_lock_bh(&n->lock);
  336. }
  337. static void tipc_node_read_unlock(struct tipc_node *n)
  338. __releases(n->lock)
  339. {
  340. read_unlock_bh(&n->lock);
  341. }
  342. static void tipc_node_write_lock(struct tipc_node *n)
  343. __acquires(n->lock)
  344. {
  345. write_lock_bh(&n->lock);
  346. }
  347. static void tipc_node_write_unlock_fast(struct tipc_node *n)
  348. __releases(n->lock)
  349. {
  350. write_unlock_bh(&n->lock);
  351. }
  352. static void tipc_node_write_unlock(struct tipc_node *n)
  353. __releases(n->lock)
  354. {
  355. struct tipc_socket_addr sk;
  356. struct net *net = n->net;
  357. u32 flags = n->action_flags;
  358. struct list_head *publ_list;
  359. struct tipc_uaddr ua;
  360. u32 bearer_id, node;
  361. if (likely(!flags)) {
  362. write_unlock_bh(&n->lock);
  363. return;
  364. }
  365. tipc_uaddr(&ua, TIPC_SERVICE_RANGE, TIPC_NODE_SCOPE,
  366. TIPC_LINK_STATE, n->addr, n->addr);
  367. sk.ref = n->link_id;
  368. sk.node = tipc_own_addr(net);
  369. node = n->addr;
  370. bearer_id = n->link_id & 0xffff;
  371. publ_list = &n->publ_list;
  372. n->action_flags &= ~(TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
  373. TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP);
  374. write_unlock_bh(&n->lock);
  375. if (flags & TIPC_NOTIFY_NODE_DOWN)
  376. tipc_publ_notify(net, publ_list, node, n->capabilities);
  377. if (flags & TIPC_NOTIFY_NODE_UP)
  378. tipc_named_node_up(net, node, n->capabilities);
  379. if (flags & TIPC_NOTIFY_LINK_UP) {
  380. tipc_mon_peer_up(net, node, bearer_id);
  381. tipc_nametbl_publish(net, &ua, &sk, sk.ref);
  382. }
  383. if (flags & TIPC_NOTIFY_LINK_DOWN) {
  384. tipc_mon_peer_down(net, node, bearer_id);
  385. tipc_nametbl_withdraw(net, &ua, &sk, sk.ref);
  386. }
  387. }
  388. static void tipc_node_assign_peer_net(struct tipc_node *n, u32 hash_mixes)
  389. {
  390. int net_id = tipc_netid(n->net);
  391. struct tipc_net *tn_peer;
  392. struct net *tmp;
  393. u32 hash_chk;
  394. if (n->peer_net)
  395. return;
  396. for_each_net_rcu(tmp) {
  397. tn_peer = tipc_net(tmp);
  398. if (!tn_peer)
  399. continue;
  400. /* Integrity checking whether node exists in namespace or not */
  401. if (tn_peer->net_id != net_id)
  402. continue;
  403. if (memcmp(n->peer_id, tn_peer->node_id, NODE_ID_LEN))
  404. continue;
  405. hash_chk = tipc_net_hash_mixes(tmp, tn_peer->random);
  406. if (hash_mixes ^ hash_chk)
  407. continue;
  408. n->peer_net = tmp;
  409. n->peer_hash_mix = hash_mixes;
  410. break;
  411. }
  412. }
  413. struct tipc_node *tipc_node_create(struct net *net, u32 addr, u8 *peer_id,
  414. u16 capabilities, u32 hash_mixes,
  415. bool preliminary)
  416. {
  417. struct tipc_net *tn = net_generic(net, tipc_net_id);
  418. struct tipc_link *l, *snd_l = tipc_bc_sndlink(net);
  419. struct tipc_node *n, *temp_node;
  420. unsigned long intv;
  421. int bearer_id;
  422. int i;
  423. spin_lock_bh(&tn->node_list_lock);
  424. n = tipc_node_find(net, addr) ?:
  425. tipc_node_find_by_id(net, peer_id);
  426. if (n) {
  427. if (!n->preliminary)
  428. goto update;
  429. if (preliminary)
  430. goto exit;
  431. /* A preliminary node becomes "real" now, refresh its data */
  432. tipc_node_write_lock(n);
  433. if (!tipc_link_bc_create(net, tipc_own_addr(net), addr, peer_id, U16_MAX,
  434. tipc_link_min_win(snd_l), tipc_link_max_win(snd_l),
  435. n->capabilities, &n->bc_entry.inputq1,
  436. &n->bc_entry.namedq, snd_l, &n->bc_entry.link)) {
  437. pr_warn("Broadcast rcv link refresh failed, no memory\n");
  438. tipc_node_write_unlock_fast(n);
  439. tipc_node_put(n);
  440. n = NULL;
  441. goto exit;
  442. }
  443. n->preliminary = false;
  444. n->addr = addr;
  445. hlist_del_rcu(&n->hash);
  446. hlist_add_head_rcu(&n->hash,
  447. &tn->node_htable[tipc_hashfn(addr)]);
  448. list_del_rcu(&n->list);
  449. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  450. if (n->addr < temp_node->addr)
  451. break;
  452. }
  453. list_add_tail_rcu(&n->list, &temp_node->list);
  454. tipc_node_write_unlock_fast(n);
  455. update:
  456. if (n->peer_hash_mix ^ hash_mixes)
  457. tipc_node_assign_peer_net(n, hash_mixes);
  458. if (n->capabilities == capabilities)
  459. goto exit;
  460. /* Same node may come back with new capabilities */
  461. tipc_node_write_lock(n);
  462. n->capabilities = capabilities;
  463. for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
  464. l = n->links[bearer_id].link;
  465. if (l)
  466. tipc_link_update_caps(l, capabilities);
  467. }
  468. tipc_node_write_unlock_fast(n);
  469. /* Calculate cluster capabilities */
  470. tn->capabilities = TIPC_NODE_CAPABILITIES;
  471. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  472. tn->capabilities &= temp_node->capabilities;
  473. }
  474. tipc_bcast_toggle_rcast(net,
  475. (tn->capabilities & TIPC_BCAST_RCAST));
  476. goto exit;
  477. }
  478. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  479. if (!n) {
  480. pr_warn("Node creation failed, no memory\n");
  481. goto exit;
  482. }
  483. tipc_nodeid2string(n->peer_id_string, peer_id);
  484. #ifdef CONFIG_TIPC_CRYPTO
  485. if (unlikely(tipc_crypto_start(&n->crypto_rx, net, n))) {
  486. pr_warn("Failed to start crypto RX(%s)!\n", n->peer_id_string);
  487. kfree(n);
  488. n = NULL;
  489. goto exit;
  490. }
  491. #endif
  492. n->addr = addr;
  493. n->preliminary = preliminary;
  494. memcpy(&n->peer_id, peer_id, 16);
  495. n->net = net;
  496. n->peer_net = NULL;
  497. n->peer_hash_mix = 0;
  498. /* Assign kernel local namespace if exists */
  499. tipc_node_assign_peer_net(n, hash_mixes);
  500. n->capabilities = capabilities;
  501. kref_init(&n->kref);
  502. rwlock_init(&n->lock);
  503. INIT_HLIST_NODE(&n->hash);
  504. INIT_LIST_HEAD(&n->list);
  505. INIT_LIST_HEAD(&n->publ_list);
  506. INIT_LIST_HEAD(&n->conn_sks);
  507. skb_queue_head_init(&n->bc_entry.namedq);
  508. skb_queue_head_init(&n->bc_entry.inputq1);
  509. __skb_queue_head_init(&n->bc_entry.arrvq);
  510. skb_queue_head_init(&n->bc_entry.inputq2);
  511. for (i = 0; i < MAX_BEARERS; i++)
  512. spin_lock_init(&n->links[i].lock);
  513. n->state = SELF_DOWN_PEER_LEAVING;
  514. n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
  515. n->signature = INVALID_NODE_SIG;
  516. n->active_links[0] = INVALID_BEARER_ID;
  517. n->active_links[1] = INVALID_BEARER_ID;
  518. if (!preliminary &&
  519. !tipc_link_bc_create(net, tipc_own_addr(net), addr, peer_id, U16_MAX,
  520. tipc_link_min_win(snd_l), tipc_link_max_win(snd_l),
  521. n->capabilities, &n->bc_entry.inputq1,
  522. &n->bc_entry.namedq, snd_l, &n->bc_entry.link)) {
  523. pr_warn("Broadcast rcv link creation failed, no memory\n");
  524. tipc_node_put(n);
  525. n = NULL;
  526. goto exit;
  527. }
  528. tipc_node_get(n);
  529. timer_setup(&n->timer, tipc_node_timeout, 0);
  530. /* Start a slow timer anyway, crypto needs it */
  531. n->keepalive_intv = 10000;
  532. intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
  533. if (!mod_timer(&n->timer, intv))
  534. tipc_node_get(n);
  535. hlist_add_head_rcu(&n->hash, &tn->node_htable[tipc_hashfn(addr)]);
  536. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  537. if (n->addr < temp_node->addr)
  538. break;
  539. }
  540. list_add_tail_rcu(&n->list, &temp_node->list);
  541. /* Calculate cluster capabilities */
  542. tn->capabilities = TIPC_NODE_CAPABILITIES;
  543. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  544. tn->capabilities &= temp_node->capabilities;
  545. }
  546. tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
  547. trace_tipc_node_create(n, true, " ");
  548. exit:
  549. spin_unlock_bh(&tn->node_list_lock);
  550. return n;
  551. }
  552. static void tipc_node_calculate_timer(struct tipc_node *n, struct tipc_link *l)
  553. {
  554. unsigned long tol = tipc_link_tolerance(l);
  555. unsigned long intv = ((tol / 4) > 500) ? 500 : tol / 4;
  556. /* Link with lowest tolerance determines timer interval */
  557. if (intv < n->keepalive_intv)
  558. n->keepalive_intv = intv;
  559. /* Ensure link's abort limit corresponds to current tolerance */
  560. tipc_link_set_abort_limit(l, tol / n->keepalive_intv);
  561. }
  562. static void tipc_node_delete_from_list(struct tipc_node *node)
  563. {
  564. #ifdef CONFIG_TIPC_CRYPTO
  565. tipc_crypto_key_flush(node->crypto_rx);
  566. #endif
  567. list_del_rcu(&node->list);
  568. hlist_del_rcu(&node->hash);
  569. tipc_node_put(node);
  570. }
  571. static void tipc_node_delete(struct tipc_node *node)
  572. {
  573. trace_tipc_node_delete(node, true, " ");
  574. tipc_node_delete_from_list(node);
  575. del_timer_sync(&node->timer);
  576. tipc_node_put(node);
  577. }
  578. void tipc_node_stop(struct net *net)
  579. {
  580. struct tipc_net *tn = tipc_net(net);
  581. struct tipc_node *node, *t_node;
  582. spin_lock_bh(&tn->node_list_lock);
  583. list_for_each_entry_safe(node, t_node, &tn->node_list, list)
  584. tipc_node_delete(node);
  585. spin_unlock_bh(&tn->node_list_lock);
  586. }
  587. void tipc_node_subscribe(struct net *net, struct list_head *subscr, u32 addr)
  588. {
  589. struct tipc_node *n;
  590. if (in_own_node(net, addr))
  591. return;
  592. n = tipc_node_find(net, addr);
  593. if (!n) {
  594. pr_warn("Node subscribe rejected, unknown node 0x%x\n", addr);
  595. return;
  596. }
  597. tipc_node_write_lock(n);
  598. list_add_tail(subscr, &n->publ_list);
  599. tipc_node_write_unlock_fast(n);
  600. tipc_node_put(n);
  601. }
  602. void tipc_node_unsubscribe(struct net *net, struct list_head *subscr, u32 addr)
  603. {
  604. struct tipc_node *n;
  605. if (in_own_node(net, addr))
  606. return;
  607. n = tipc_node_find(net, addr);
  608. if (!n) {
  609. pr_warn("Node unsubscribe rejected, unknown node 0x%x\n", addr);
  610. return;
  611. }
  612. tipc_node_write_lock(n);
  613. list_del_init(subscr);
  614. tipc_node_write_unlock_fast(n);
  615. tipc_node_put(n);
  616. }
  617. int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
  618. {
  619. struct tipc_node *node;
  620. struct tipc_sock_conn *conn;
  621. int err = 0;
  622. if (in_own_node(net, dnode))
  623. return 0;
  624. node = tipc_node_find(net, dnode);
  625. if (!node) {
  626. pr_warn("Connecting sock to node 0x%x failed\n", dnode);
  627. return -EHOSTUNREACH;
  628. }
  629. conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
  630. if (!conn) {
  631. err = -EHOSTUNREACH;
  632. goto exit;
  633. }
  634. conn->peer_node = dnode;
  635. conn->port = port;
  636. conn->peer_port = peer_port;
  637. tipc_node_write_lock(node);
  638. list_add_tail(&conn->list, &node->conn_sks);
  639. tipc_node_write_unlock(node);
  640. exit:
  641. tipc_node_put(node);
  642. return err;
  643. }
  644. void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
  645. {
  646. struct tipc_node *node;
  647. struct tipc_sock_conn *conn, *safe;
  648. if (in_own_node(net, dnode))
  649. return;
  650. node = tipc_node_find(net, dnode);
  651. if (!node)
  652. return;
  653. tipc_node_write_lock(node);
  654. list_for_each_entry_safe(conn, safe, &node->conn_sks, list) {
  655. if (port != conn->port)
  656. continue;
  657. list_del(&conn->list);
  658. kfree(conn);
  659. }
  660. tipc_node_write_unlock(node);
  661. tipc_node_put(node);
  662. }
  663. static void tipc_node_clear_links(struct tipc_node *node)
  664. {
  665. int i;
  666. for (i = 0; i < MAX_BEARERS; i++) {
  667. struct tipc_link_entry *le = &node->links[i];
  668. if (le->link) {
  669. kfree(le->link);
  670. le->link = NULL;
  671. node->link_cnt--;
  672. }
  673. }
  674. }
  675. /* tipc_node_cleanup - delete nodes that does not
  676. * have active links for NODE_CLEANUP_AFTER time
  677. */
  678. static bool tipc_node_cleanup(struct tipc_node *peer)
  679. {
  680. struct tipc_node *temp_node;
  681. struct tipc_net *tn = tipc_net(peer->net);
  682. bool deleted = false;
  683. /* If lock held by tipc_node_stop() the node will be deleted anyway */
  684. if (!spin_trylock_bh(&tn->node_list_lock))
  685. return false;
  686. tipc_node_write_lock(peer);
  687. if (!node_is_up(peer) && time_after(jiffies, peer->delete_at)) {
  688. tipc_node_clear_links(peer);
  689. tipc_node_delete_from_list(peer);
  690. deleted = true;
  691. }
  692. tipc_node_write_unlock(peer);
  693. if (!deleted) {
  694. spin_unlock_bh(&tn->node_list_lock);
  695. return deleted;
  696. }
  697. /* Calculate cluster capabilities */
  698. tn->capabilities = TIPC_NODE_CAPABILITIES;
  699. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  700. tn->capabilities &= temp_node->capabilities;
  701. }
  702. tipc_bcast_toggle_rcast(peer->net,
  703. (tn->capabilities & TIPC_BCAST_RCAST));
  704. spin_unlock_bh(&tn->node_list_lock);
  705. return deleted;
  706. }
  707. /* tipc_node_timeout - handle expiration of node timer
  708. */
  709. static void tipc_node_timeout(struct timer_list *t)
  710. {
  711. struct tipc_node *n = from_timer(n, t, timer);
  712. struct tipc_link_entry *le;
  713. struct sk_buff_head xmitq;
  714. int remains = n->link_cnt;
  715. int bearer_id;
  716. int rc = 0;
  717. trace_tipc_node_timeout(n, false, " ");
  718. if (!node_is_up(n) && tipc_node_cleanup(n)) {
  719. /*Removing the reference of Timer*/
  720. tipc_node_put(n);
  721. return;
  722. }
  723. #ifdef CONFIG_TIPC_CRYPTO
  724. /* Take any crypto key related actions first */
  725. tipc_crypto_timeout(n->crypto_rx);
  726. #endif
  727. __skb_queue_head_init(&xmitq);
  728. /* Initial node interval to value larger (10 seconds), then it will be
  729. * recalculated with link lowest tolerance
  730. */
  731. tipc_node_read_lock(n);
  732. n->keepalive_intv = 10000;
  733. tipc_node_read_unlock(n);
  734. for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
  735. tipc_node_read_lock(n);
  736. le = &n->links[bearer_id];
  737. if (le->link) {
  738. spin_lock_bh(&le->lock);
  739. /* Link tolerance may change asynchronously: */
  740. tipc_node_calculate_timer(n, le->link);
  741. rc = tipc_link_timeout(le->link, &xmitq);
  742. spin_unlock_bh(&le->lock);
  743. remains--;
  744. }
  745. tipc_node_read_unlock(n);
  746. tipc_bearer_xmit(n->net, bearer_id, &xmitq, &le->maddr, n);
  747. if (rc & TIPC_LINK_DOWN_EVT)
  748. tipc_node_link_down(n, bearer_id, false);
  749. }
  750. mod_timer(&n->timer, jiffies + msecs_to_jiffies(n->keepalive_intv));
  751. }
  752. /**
  753. * __tipc_node_link_up - handle addition of link
  754. * @n: target tipc_node
  755. * @bearer_id: id of the bearer
  756. * @xmitq: queue for messages to be xmited on
  757. * Node lock must be held by caller
  758. * Link becomes active (alone or shared) or standby, depending on its priority.
  759. */
  760. static void __tipc_node_link_up(struct tipc_node *n, int bearer_id,
  761. struct sk_buff_head *xmitq)
  762. {
  763. int *slot0 = &n->active_links[0];
  764. int *slot1 = &n->active_links[1];
  765. struct tipc_link *ol = node_active_link(n, 0);
  766. struct tipc_link *nl = n->links[bearer_id].link;
  767. if (!nl || tipc_link_is_up(nl))
  768. return;
  769. tipc_link_fsm_evt(nl, LINK_ESTABLISH_EVT);
  770. if (!tipc_link_is_up(nl))
  771. return;
  772. n->working_links++;
  773. n->action_flags |= TIPC_NOTIFY_LINK_UP;
  774. n->link_id = tipc_link_id(nl);
  775. /* Leave room for tunnel header when returning 'mtu' to users: */
  776. n->links[bearer_id].mtu = tipc_link_mss(nl);
  777. tipc_bearer_add_dest(n->net, bearer_id, n->addr);
  778. tipc_bcast_inc_bearer_dst_cnt(n->net, bearer_id);
  779. pr_debug("Established link <%s> on network plane %c\n",
  780. tipc_link_name(nl), tipc_link_plane(nl));
  781. trace_tipc_node_link_up(n, true, " ");
  782. /* Ensure that a STATE message goes first */
  783. tipc_link_build_state_msg(nl, xmitq);
  784. /* First link? => give it both slots */
  785. if (!ol) {
  786. *slot0 = bearer_id;
  787. *slot1 = bearer_id;
  788. tipc_node_fsm_evt(n, SELF_ESTABL_CONTACT_EVT);
  789. n->action_flags |= TIPC_NOTIFY_NODE_UP;
  790. tipc_link_set_active(nl, true);
  791. tipc_bcast_add_peer(n->net, nl, xmitq);
  792. return;
  793. }
  794. /* Second link => redistribute slots */
  795. if (tipc_link_prio(nl) > tipc_link_prio(ol)) {
  796. pr_debug("Old link <%s> becomes standby\n", tipc_link_name(ol));
  797. *slot0 = bearer_id;
  798. *slot1 = bearer_id;
  799. tipc_link_set_active(nl, true);
  800. tipc_link_set_active(ol, false);
  801. } else if (tipc_link_prio(nl) == tipc_link_prio(ol)) {
  802. tipc_link_set_active(nl, true);
  803. *slot1 = bearer_id;
  804. } else {
  805. pr_debug("New link <%s> is standby\n", tipc_link_name(nl));
  806. }
  807. /* Prepare synchronization with first link */
  808. tipc_link_tnl_prepare(ol, nl, SYNCH_MSG, xmitq);
  809. }
  810. /**
  811. * tipc_node_link_up - handle addition of link
  812. * @n: target tipc_node
  813. * @bearer_id: id of the bearer
  814. * @xmitq: queue for messages to be xmited on
  815. *
  816. * Link becomes active (alone or shared) or standby, depending on its priority.
  817. */
  818. static void tipc_node_link_up(struct tipc_node *n, int bearer_id,
  819. struct sk_buff_head *xmitq)
  820. {
  821. struct tipc_media_addr *maddr;
  822. tipc_node_write_lock(n);
  823. __tipc_node_link_up(n, bearer_id, xmitq);
  824. maddr = &n->links[bearer_id].maddr;
  825. tipc_bearer_xmit(n->net, bearer_id, xmitq, maddr, n);
  826. tipc_node_write_unlock(n);
  827. }
  828. /**
  829. * tipc_node_link_failover() - start failover in case "half-failover"
  830. *
  831. * This function is only called in a very special situation where link
  832. * failover can be already started on peer node but not on this node.
  833. * This can happen when e.g.::
  834. *
  835. * 1. Both links <1A-2A>, <1B-2B> down
  836. * 2. Link endpoint 2A up, but 1A still down (e.g. due to network
  837. * disturbance, wrong session, etc.)
  838. * 3. Link <1B-2B> up
  839. * 4. Link endpoint 2A down (e.g. due to link tolerance timeout)
  840. * 5. Node 2 starts failover onto link <1B-2B>
  841. *
  842. * ==> Node 1 does never start link/node failover!
  843. *
  844. * @n: tipc node structure
  845. * @l: link peer endpoint failingover (- can be NULL)
  846. * @tnl: tunnel link
  847. * @xmitq: queue for messages to be xmited on tnl link later
  848. */
  849. static void tipc_node_link_failover(struct tipc_node *n, struct tipc_link *l,
  850. struct tipc_link *tnl,
  851. struct sk_buff_head *xmitq)
  852. {
  853. /* Avoid to be "self-failover" that can never end */
  854. if (!tipc_link_is_up(tnl))
  855. return;
  856. /* Don't rush, failure link may be in the process of resetting */
  857. if (l && !tipc_link_is_reset(l))
  858. return;
  859. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  860. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  861. n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
  862. tipc_link_failover_prepare(l, tnl, xmitq);
  863. if (l)
  864. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  865. tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
  866. }
  867. /**
  868. * __tipc_node_link_down - handle loss of link
  869. * @n: target tipc_node
  870. * @bearer_id: id of the bearer
  871. * @xmitq: queue for messages to be xmited on
  872. * @maddr: output media address of the bearer
  873. */
  874. static void __tipc_node_link_down(struct tipc_node *n, int *bearer_id,
  875. struct sk_buff_head *xmitq,
  876. struct tipc_media_addr **maddr)
  877. {
  878. struct tipc_link_entry *le = &n->links[*bearer_id];
  879. int *slot0 = &n->active_links[0];
  880. int *slot1 = &n->active_links[1];
  881. int i, highest = 0, prio;
  882. struct tipc_link *l, *_l, *tnl;
  883. l = n->links[*bearer_id].link;
  884. if (!l || tipc_link_is_reset(l))
  885. return;
  886. n->working_links--;
  887. n->action_flags |= TIPC_NOTIFY_LINK_DOWN;
  888. n->link_id = tipc_link_id(l);
  889. tipc_bearer_remove_dest(n->net, *bearer_id, n->addr);
  890. pr_debug("Lost link <%s> on network plane %c\n",
  891. tipc_link_name(l), tipc_link_plane(l));
  892. /* Select new active link if any available */
  893. *slot0 = INVALID_BEARER_ID;
  894. *slot1 = INVALID_BEARER_ID;
  895. for (i = 0; i < MAX_BEARERS; i++) {
  896. _l = n->links[i].link;
  897. if (!_l || !tipc_link_is_up(_l))
  898. continue;
  899. if (_l == l)
  900. continue;
  901. prio = tipc_link_prio(_l);
  902. if (prio < highest)
  903. continue;
  904. if (prio > highest) {
  905. highest = prio;
  906. *slot0 = i;
  907. *slot1 = i;
  908. continue;
  909. }
  910. *slot1 = i;
  911. }
  912. if (!node_is_up(n)) {
  913. if (tipc_link_peer_is_down(l))
  914. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  915. tipc_node_fsm_evt(n, SELF_LOST_CONTACT_EVT);
  916. trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down!");
  917. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  918. tipc_link_reset(l);
  919. tipc_link_build_reset_msg(l, xmitq);
  920. *maddr = &n->links[*bearer_id].maddr;
  921. node_lost_contact(n, &le->inputq);
  922. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  923. return;
  924. }
  925. tipc_bcast_dec_bearer_dst_cnt(n->net, *bearer_id);
  926. /* There is still a working link => initiate failover */
  927. *bearer_id = n->active_links[0];
  928. tnl = n->links[*bearer_id].link;
  929. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  930. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  931. n->sync_point = tipc_link_rcv_nxt(tnl) + (U16_MAX / 2 - 1);
  932. tipc_link_tnl_prepare(l, tnl, FAILOVER_MSG, xmitq);
  933. trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link down -> failover!");
  934. tipc_link_reset(l);
  935. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  936. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  937. tipc_node_fsm_evt(n, NODE_FAILOVER_BEGIN_EVT);
  938. *maddr = &n->links[*bearer_id].maddr;
  939. }
  940. static void tipc_node_link_down(struct tipc_node *n, int bearer_id, bool delete)
  941. {
  942. struct tipc_link_entry *le = &n->links[bearer_id];
  943. struct tipc_media_addr *maddr = NULL;
  944. struct tipc_link *l = le->link;
  945. int old_bearer_id = bearer_id;
  946. struct sk_buff_head xmitq;
  947. if (!l)
  948. return;
  949. __skb_queue_head_init(&xmitq);
  950. tipc_node_write_lock(n);
  951. if (!tipc_link_is_establishing(l)) {
  952. __tipc_node_link_down(n, &bearer_id, &xmitq, &maddr);
  953. } else {
  954. /* Defuse pending tipc_node_link_up() */
  955. tipc_link_reset(l);
  956. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  957. }
  958. if (delete) {
  959. kfree(l);
  960. le->link = NULL;
  961. n->link_cnt--;
  962. }
  963. trace_tipc_node_link_down(n, true, "node link down or deleted!");
  964. tipc_node_write_unlock(n);
  965. if (delete)
  966. tipc_mon_remove_peer(n->net, n->addr, old_bearer_id);
  967. if (!skb_queue_empty(&xmitq))
  968. tipc_bearer_xmit(n->net, bearer_id, &xmitq, maddr, n);
  969. tipc_sk_rcv(n->net, &le->inputq);
  970. }
  971. static bool node_is_up(struct tipc_node *n)
  972. {
  973. return n->active_links[0] != INVALID_BEARER_ID;
  974. }
  975. bool tipc_node_is_up(struct net *net, u32 addr)
  976. {
  977. struct tipc_node *n;
  978. bool retval = false;
  979. if (in_own_node(net, addr))
  980. return true;
  981. n = tipc_node_find(net, addr);
  982. if (!n)
  983. return false;
  984. retval = node_is_up(n);
  985. tipc_node_put(n);
  986. return retval;
  987. }
  988. static u32 tipc_node_suggest_addr(struct net *net, u32 addr)
  989. {
  990. struct tipc_node *n;
  991. addr ^= tipc_net(net)->random;
  992. while ((n = tipc_node_find(net, addr))) {
  993. tipc_node_put(n);
  994. addr++;
  995. }
  996. return addr;
  997. }
  998. /* tipc_node_try_addr(): Check if addr can be used by peer, suggest other if not
  999. * Returns suggested address if any, otherwise 0
  1000. */
  1001. u32 tipc_node_try_addr(struct net *net, u8 *id, u32 addr)
  1002. {
  1003. struct tipc_net *tn = tipc_net(net);
  1004. struct tipc_node *n;
  1005. bool preliminary;
  1006. u32 sugg_addr;
  1007. /* Suggest new address if some other peer is using this one */
  1008. n = tipc_node_find(net, addr);
  1009. if (n) {
  1010. if (!memcmp(n->peer_id, id, NODE_ID_LEN))
  1011. addr = 0;
  1012. tipc_node_put(n);
  1013. if (!addr)
  1014. return 0;
  1015. return tipc_node_suggest_addr(net, addr);
  1016. }
  1017. /* Suggest previously used address if peer is known */
  1018. n = tipc_node_find_by_id(net, id);
  1019. if (n) {
  1020. sugg_addr = n->addr;
  1021. preliminary = n->preliminary;
  1022. tipc_node_put(n);
  1023. if (!preliminary)
  1024. return sugg_addr;
  1025. }
  1026. /* Even this node may be in conflict */
  1027. if (tn->trial_addr == addr)
  1028. return tipc_node_suggest_addr(net, addr);
  1029. return 0;
  1030. }
  1031. void tipc_node_check_dest(struct net *net, u32 addr,
  1032. u8 *peer_id, struct tipc_bearer *b,
  1033. u16 capabilities, u32 signature, u32 hash_mixes,
  1034. struct tipc_media_addr *maddr,
  1035. bool *respond, bool *dupl_addr)
  1036. {
  1037. struct tipc_node *n;
  1038. struct tipc_link *l;
  1039. struct tipc_link_entry *le;
  1040. bool addr_match = false;
  1041. bool sign_match = false;
  1042. bool link_up = false;
  1043. bool link_is_reset = false;
  1044. bool accept_addr = false;
  1045. bool reset = false;
  1046. char *if_name;
  1047. unsigned long intv;
  1048. u16 session;
  1049. *dupl_addr = false;
  1050. *respond = false;
  1051. n = tipc_node_create(net, addr, peer_id, capabilities, hash_mixes,
  1052. false);
  1053. if (!n)
  1054. return;
  1055. tipc_node_write_lock(n);
  1056. le = &n->links[b->identity];
  1057. /* Prepare to validate requesting node's signature and media address */
  1058. l = le->link;
  1059. link_up = l && tipc_link_is_up(l);
  1060. link_is_reset = l && tipc_link_is_reset(l);
  1061. addr_match = l && !memcmp(&le->maddr, maddr, sizeof(*maddr));
  1062. sign_match = (signature == n->signature);
  1063. /* These three flags give us eight permutations: */
  1064. if (sign_match && addr_match && link_up) {
  1065. /* All is fine. Ignore requests. */
  1066. /* Peer node is not a container/local namespace */
  1067. if (!n->peer_hash_mix)
  1068. n->peer_hash_mix = hash_mixes;
  1069. } else if (sign_match && addr_match && !link_up) {
  1070. /* Respond. The link will come up in due time */
  1071. *respond = true;
  1072. } else if (sign_match && !addr_match && link_up) {
  1073. /* Peer has changed i/f address without rebooting.
  1074. * If so, the link will reset soon, and the next
  1075. * discovery will be accepted. So we can ignore it.
  1076. * It may also be a cloned or malicious peer having
  1077. * chosen the same node address and signature as an
  1078. * existing one.
  1079. * Ignore requests until the link goes down, if ever.
  1080. */
  1081. *dupl_addr = true;
  1082. } else if (sign_match && !addr_match && !link_up) {
  1083. /* Peer link has changed i/f address without rebooting.
  1084. * It may also be a cloned or malicious peer; we can't
  1085. * distinguish between the two.
  1086. * The signature is correct, so we must accept.
  1087. */
  1088. accept_addr = true;
  1089. *respond = true;
  1090. reset = true;
  1091. } else if (!sign_match && addr_match && link_up) {
  1092. /* Peer node rebooted. Two possibilities:
  1093. * - Delayed re-discovery; this link endpoint has already
  1094. * reset and re-established contact with the peer, before
  1095. * receiving a discovery message from that node.
  1096. * (The peer happened to receive one from this node first).
  1097. * - The peer came back so fast that our side has not
  1098. * discovered it yet. Probing from this side will soon
  1099. * reset the link, since there can be no working link
  1100. * endpoint at the peer end, and the link will re-establish.
  1101. * Accept the signature, since it comes from a known peer.
  1102. */
  1103. n->signature = signature;
  1104. } else if (!sign_match && addr_match && !link_up) {
  1105. /* The peer node has rebooted.
  1106. * Accept signature, since it is a known peer.
  1107. */
  1108. n->signature = signature;
  1109. *respond = true;
  1110. } else if (!sign_match && !addr_match && link_up) {
  1111. /* Peer rebooted with new address, or a new/duplicate peer.
  1112. * Ignore until the link goes down, if ever.
  1113. */
  1114. *dupl_addr = true;
  1115. } else if (!sign_match && !addr_match && !link_up) {
  1116. /* Peer rebooted with new address, or it is a new peer.
  1117. * Accept signature and address.
  1118. */
  1119. n->signature = signature;
  1120. accept_addr = true;
  1121. *respond = true;
  1122. reset = true;
  1123. }
  1124. if (!accept_addr)
  1125. goto exit;
  1126. /* Now create new link if not already existing */
  1127. if (!l) {
  1128. if (n->link_cnt == 2)
  1129. goto exit;
  1130. if_name = strchr(b->name, ':') + 1;
  1131. get_random_bytes(&session, sizeof(u16));
  1132. if (!tipc_link_create(net, if_name, b->identity, b->tolerance,
  1133. b->net_plane, b->mtu, b->priority,
  1134. b->min_win, b->max_win, session,
  1135. tipc_own_addr(net), addr, peer_id,
  1136. n->capabilities,
  1137. tipc_bc_sndlink(n->net), n->bc_entry.link,
  1138. &le->inputq,
  1139. &n->bc_entry.namedq, &l)) {
  1140. *respond = false;
  1141. goto exit;
  1142. }
  1143. trace_tipc_link_reset(l, TIPC_DUMP_ALL, "link created!");
  1144. tipc_link_reset(l);
  1145. tipc_link_fsm_evt(l, LINK_RESET_EVT);
  1146. if (n->state == NODE_FAILINGOVER)
  1147. tipc_link_fsm_evt(l, LINK_FAILOVER_BEGIN_EVT);
  1148. link_is_reset = tipc_link_is_reset(l);
  1149. le->link = l;
  1150. n->link_cnt++;
  1151. tipc_node_calculate_timer(n, l);
  1152. if (n->link_cnt == 1) {
  1153. intv = jiffies + msecs_to_jiffies(n->keepalive_intv);
  1154. if (!mod_timer(&n->timer, intv))
  1155. tipc_node_get(n);
  1156. }
  1157. }
  1158. memcpy(&le->maddr, maddr, sizeof(*maddr));
  1159. exit:
  1160. tipc_node_write_unlock(n);
  1161. if (reset && !link_is_reset)
  1162. tipc_node_link_down(n, b->identity, false);
  1163. tipc_node_put(n);
  1164. }
  1165. void tipc_node_delete_links(struct net *net, int bearer_id)
  1166. {
  1167. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1168. struct tipc_node *n;
  1169. rcu_read_lock();
  1170. list_for_each_entry_rcu(n, &tn->node_list, list) {
  1171. tipc_node_link_down(n, bearer_id, true);
  1172. }
  1173. rcu_read_unlock();
  1174. }
  1175. static void tipc_node_reset_links(struct tipc_node *n)
  1176. {
  1177. int i;
  1178. pr_warn("Resetting all links to %x\n", n->addr);
  1179. trace_tipc_node_reset_links(n, true, " ");
  1180. for (i = 0; i < MAX_BEARERS; i++) {
  1181. tipc_node_link_down(n, i, false);
  1182. }
  1183. }
  1184. /* tipc_node_fsm_evt - node finite state machine
  1185. * Determines when contact is allowed with peer node
  1186. */
  1187. static void tipc_node_fsm_evt(struct tipc_node *n, int evt)
  1188. {
  1189. int state = n->state;
  1190. switch (state) {
  1191. case SELF_DOWN_PEER_DOWN:
  1192. switch (evt) {
  1193. case SELF_ESTABL_CONTACT_EVT:
  1194. state = SELF_UP_PEER_COMING;
  1195. break;
  1196. case PEER_ESTABL_CONTACT_EVT:
  1197. state = SELF_COMING_PEER_UP;
  1198. break;
  1199. case SELF_LOST_CONTACT_EVT:
  1200. case PEER_LOST_CONTACT_EVT:
  1201. break;
  1202. case NODE_SYNCH_END_EVT:
  1203. case NODE_SYNCH_BEGIN_EVT:
  1204. case NODE_FAILOVER_BEGIN_EVT:
  1205. case NODE_FAILOVER_END_EVT:
  1206. default:
  1207. goto illegal_evt;
  1208. }
  1209. break;
  1210. case SELF_UP_PEER_UP:
  1211. switch (evt) {
  1212. case SELF_LOST_CONTACT_EVT:
  1213. state = SELF_DOWN_PEER_LEAVING;
  1214. break;
  1215. case PEER_LOST_CONTACT_EVT:
  1216. state = SELF_LEAVING_PEER_DOWN;
  1217. break;
  1218. case NODE_SYNCH_BEGIN_EVT:
  1219. state = NODE_SYNCHING;
  1220. break;
  1221. case NODE_FAILOVER_BEGIN_EVT:
  1222. state = NODE_FAILINGOVER;
  1223. break;
  1224. case SELF_ESTABL_CONTACT_EVT:
  1225. case PEER_ESTABL_CONTACT_EVT:
  1226. case NODE_SYNCH_END_EVT:
  1227. case NODE_FAILOVER_END_EVT:
  1228. break;
  1229. default:
  1230. goto illegal_evt;
  1231. }
  1232. break;
  1233. case SELF_DOWN_PEER_LEAVING:
  1234. switch (evt) {
  1235. case PEER_LOST_CONTACT_EVT:
  1236. state = SELF_DOWN_PEER_DOWN;
  1237. break;
  1238. case SELF_ESTABL_CONTACT_EVT:
  1239. case PEER_ESTABL_CONTACT_EVT:
  1240. case SELF_LOST_CONTACT_EVT:
  1241. break;
  1242. case NODE_SYNCH_END_EVT:
  1243. case NODE_SYNCH_BEGIN_EVT:
  1244. case NODE_FAILOVER_BEGIN_EVT:
  1245. case NODE_FAILOVER_END_EVT:
  1246. default:
  1247. goto illegal_evt;
  1248. }
  1249. break;
  1250. case SELF_UP_PEER_COMING:
  1251. switch (evt) {
  1252. case PEER_ESTABL_CONTACT_EVT:
  1253. state = SELF_UP_PEER_UP;
  1254. break;
  1255. case SELF_LOST_CONTACT_EVT:
  1256. state = SELF_DOWN_PEER_DOWN;
  1257. break;
  1258. case SELF_ESTABL_CONTACT_EVT:
  1259. case PEER_LOST_CONTACT_EVT:
  1260. case NODE_SYNCH_END_EVT:
  1261. case NODE_FAILOVER_BEGIN_EVT:
  1262. break;
  1263. case NODE_SYNCH_BEGIN_EVT:
  1264. case NODE_FAILOVER_END_EVT:
  1265. default:
  1266. goto illegal_evt;
  1267. }
  1268. break;
  1269. case SELF_COMING_PEER_UP:
  1270. switch (evt) {
  1271. case SELF_ESTABL_CONTACT_EVT:
  1272. state = SELF_UP_PEER_UP;
  1273. break;
  1274. case PEER_LOST_CONTACT_EVT:
  1275. state = SELF_DOWN_PEER_DOWN;
  1276. break;
  1277. case SELF_LOST_CONTACT_EVT:
  1278. case PEER_ESTABL_CONTACT_EVT:
  1279. break;
  1280. case NODE_SYNCH_END_EVT:
  1281. case NODE_SYNCH_BEGIN_EVT:
  1282. case NODE_FAILOVER_BEGIN_EVT:
  1283. case NODE_FAILOVER_END_EVT:
  1284. default:
  1285. goto illegal_evt;
  1286. }
  1287. break;
  1288. case SELF_LEAVING_PEER_DOWN:
  1289. switch (evt) {
  1290. case SELF_LOST_CONTACT_EVT:
  1291. state = SELF_DOWN_PEER_DOWN;
  1292. break;
  1293. case SELF_ESTABL_CONTACT_EVT:
  1294. case PEER_ESTABL_CONTACT_EVT:
  1295. case PEER_LOST_CONTACT_EVT:
  1296. break;
  1297. case NODE_SYNCH_END_EVT:
  1298. case NODE_SYNCH_BEGIN_EVT:
  1299. case NODE_FAILOVER_BEGIN_EVT:
  1300. case NODE_FAILOVER_END_EVT:
  1301. default:
  1302. goto illegal_evt;
  1303. }
  1304. break;
  1305. case NODE_FAILINGOVER:
  1306. switch (evt) {
  1307. case SELF_LOST_CONTACT_EVT:
  1308. state = SELF_DOWN_PEER_LEAVING;
  1309. break;
  1310. case PEER_LOST_CONTACT_EVT:
  1311. state = SELF_LEAVING_PEER_DOWN;
  1312. break;
  1313. case NODE_FAILOVER_END_EVT:
  1314. state = SELF_UP_PEER_UP;
  1315. break;
  1316. case NODE_FAILOVER_BEGIN_EVT:
  1317. case SELF_ESTABL_CONTACT_EVT:
  1318. case PEER_ESTABL_CONTACT_EVT:
  1319. break;
  1320. case NODE_SYNCH_BEGIN_EVT:
  1321. case NODE_SYNCH_END_EVT:
  1322. default:
  1323. goto illegal_evt;
  1324. }
  1325. break;
  1326. case NODE_SYNCHING:
  1327. switch (evt) {
  1328. case SELF_LOST_CONTACT_EVT:
  1329. state = SELF_DOWN_PEER_LEAVING;
  1330. break;
  1331. case PEER_LOST_CONTACT_EVT:
  1332. state = SELF_LEAVING_PEER_DOWN;
  1333. break;
  1334. case NODE_SYNCH_END_EVT:
  1335. state = SELF_UP_PEER_UP;
  1336. break;
  1337. case NODE_FAILOVER_BEGIN_EVT:
  1338. state = NODE_FAILINGOVER;
  1339. break;
  1340. case NODE_SYNCH_BEGIN_EVT:
  1341. case SELF_ESTABL_CONTACT_EVT:
  1342. case PEER_ESTABL_CONTACT_EVT:
  1343. break;
  1344. case NODE_FAILOVER_END_EVT:
  1345. default:
  1346. goto illegal_evt;
  1347. }
  1348. break;
  1349. default:
  1350. pr_err("Unknown node fsm state %x\n", state);
  1351. break;
  1352. }
  1353. trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
  1354. n->state = state;
  1355. return;
  1356. illegal_evt:
  1357. pr_err("Illegal node fsm evt %x in state %x\n", evt, state);
  1358. trace_tipc_node_fsm(n->peer_id, n->state, state, evt);
  1359. }
  1360. static void node_lost_contact(struct tipc_node *n,
  1361. struct sk_buff_head *inputq)
  1362. {
  1363. struct tipc_sock_conn *conn, *safe;
  1364. struct tipc_link *l;
  1365. struct list_head *conns = &n->conn_sks;
  1366. struct sk_buff *skb;
  1367. uint i;
  1368. pr_debug("Lost contact with %x\n", n->addr);
  1369. n->delete_at = jiffies + msecs_to_jiffies(NODE_CLEANUP_AFTER);
  1370. trace_tipc_node_lost_contact(n, true, " ");
  1371. /* Clean up broadcast state */
  1372. tipc_bcast_remove_peer(n->net, n->bc_entry.link);
  1373. skb_queue_purge(&n->bc_entry.namedq);
  1374. /* Abort any ongoing link failover */
  1375. for (i = 0; i < MAX_BEARERS; i++) {
  1376. l = n->links[i].link;
  1377. if (l)
  1378. tipc_link_fsm_evt(l, LINK_FAILOVER_END_EVT);
  1379. }
  1380. /* Notify publications from this node */
  1381. n->action_flags |= TIPC_NOTIFY_NODE_DOWN;
  1382. n->peer_net = NULL;
  1383. n->peer_hash_mix = 0;
  1384. /* Notify sockets connected to node */
  1385. list_for_each_entry_safe(conn, safe, conns, list) {
  1386. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
  1387. SHORT_H_SIZE, 0, tipc_own_addr(n->net),
  1388. conn->peer_node, conn->port,
  1389. conn->peer_port, TIPC_ERR_NO_NODE);
  1390. if (likely(skb))
  1391. skb_queue_tail(inputq, skb);
  1392. list_del(&conn->list);
  1393. kfree(conn);
  1394. }
  1395. }
  1396. /**
  1397. * tipc_node_get_linkname - get the name of a link
  1398. *
  1399. * @net: the applicable net namespace
  1400. * @bearer_id: id of the bearer
  1401. * @addr: peer node address
  1402. * @linkname: link name output buffer
  1403. * @len: size of @linkname output buffer
  1404. *
  1405. * Return: 0 on success
  1406. */
  1407. int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
  1408. char *linkname, size_t len)
  1409. {
  1410. struct tipc_link *link;
  1411. int err = -EINVAL;
  1412. struct tipc_node *node = tipc_node_find(net, addr);
  1413. if (!node)
  1414. return err;
  1415. if (bearer_id >= MAX_BEARERS)
  1416. goto exit;
  1417. tipc_node_read_lock(node);
  1418. link = node->links[bearer_id].link;
  1419. if (link) {
  1420. strncpy(linkname, tipc_link_name(link), len);
  1421. err = 0;
  1422. }
  1423. tipc_node_read_unlock(node);
  1424. exit:
  1425. tipc_node_put(node);
  1426. return err;
  1427. }
  1428. /* Caller should hold node lock for the passed node */
  1429. static int __tipc_nl_add_node(struct tipc_nl_msg *msg, struct tipc_node *node)
  1430. {
  1431. void *hdr;
  1432. struct nlattr *attrs;
  1433. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  1434. NLM_F_MULTI, TIPC_NL_NODE_GET);
  1435. if (!hdr)
  1436. return -EMSGSIZE;
  1437. attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_NODE);
  1438. if (!attrs)
  1439. goto msg_full;
  1440. if (nla_put_u32(msg->skb, TIPC_NLA_NODE_ADDR, node->addr))
  1441. goto attr_msg_full;
  1442. if (node_is_up(node))
  1443. if (nla_put_flag(msg->skb, TIPC_NLA_NODE_UP))
  1444. goto attr_msg_full;
  1445. nla_nest_end(msg->skb, attrs);
  1446. genlmsg_end(msg->skb, hdr);
  1447. return 0;
  1448. attr_msg_full:
  1449. nla_nest_cancel(msg->skb, attrs);
  1450. msg_full:
  1451. genlmsg_cancel(msg->skb, hdr);
  1452. return -EMSGSIZE;
  1453. }
  1454. static void tipc_lxc_xmit(struct net *peer_net, struct sk_buff_head *list)
  1455. {
  1456. struct tipc_msg *hdr = buf_msg(skb_peek(list));
  1457. struct sk_buff_head inputq;
  1458. switch (msg_user(hdr)) {
  1459. case TIPC_LOW_IMPORTANCE:
  1460. case TIPC_MEDIUM_IMPORTANCE:
  1461. case TIPC_HIGH_IMPORTANCE:
  1462. case TIPC_CRITICAL_IMPORTANCE:
  1463. if (msg_connected(hdr) || msg_named(hdr) ||
  1464. msg_direct(hdr)) {
  1465. tipc_loopback_trace(peer_net, list);
  1466. spin_lock_init(&list->lock);
  1467. tipc_sk_rcv(peer_net, list);
  1468. return;
  1469. }
  1470. if (msg_mcast(hdr)) {
  1471. tipc_loopback_trace(peer_net, list);
  1472. skb_queue_head_init(&inputq);
  1473. tipc_sk_mcast_rcv(peer_net, list, &inputq);
  1474. __skb_queue_purge(list);
  1475. skb_queue_purge(&inputq);
  1476. return;
  1477. }
  1478. return;
  1479. case MSG_FRAGMENTER:
  1480. if (tipc_msg_assemble(list)) {
  1481. tipc_loopback_trace(peer_net, list);
  1482. skb_queue_head_init(&inputq);
  1483. tipc_sk_mcast_rcv(peer_net, list, &inputq);
  1484. __skb_queue_purge(list);
  1485. skb_queue_purge(&inputq);
  1486. }
  1487. return;
  1488. case GROUP_PROTOCOL:
  1489. case CONN_MANAGER:
  1490. tipc_loopback_trace(peer_net, list);
  1491. spin_lock_init(&list->lock);
  1492. tipc_sk_rcv(peer_net, list);
  1493. return;
  1494. case LINK_PROTOCOL:
  1495. case NAME_DISTRIBUTOR:
  1496. case TUNNEL_PROTOCOL:
  1497. case BCAST_PROTOCOL:
  1498. return;
  1499. default:
  1500. return;
  1501. }
  1502. }
  1503. /**
  1504. * tipc_node_xmit() - general link level function for message sending
  1505. * @net: the applicable net namespace
  1506. * @list: chain of buffers containing message
  1507. * @dnode: address of destination node
  1508. * @selector: a number used for deterministic link selection
  1509. * Consumes the buffer chain.
  1510. * Return: 0 if success, otherwise: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE,-ENOBUF
  1511. */
  1512. int tipc_node_xmit(struct net *net, struct sk_buff_head *list,
  1513. u32 dnode, int selector)
  1514. {
  1515. struct tipc_link_entry *le = NULL;
  1516. struct tipc_node *n;
  1517. struct sk_buff_head xmitq;
  1518. bool node_up = false;
  1519. struct net *peer_net;
  1520. int bearer_id;
  1521. int rc;
  1522. if (in_own_node(net, dnode)) {
  1523. tipc_loopback_trace(net, list);
  1524. spin_lock_init(&list->lock);
  1525. tipc_sk_rcv(net, list);
  1526. return 0;
  1527. }
  1528. n = tipc_node_find(net, dnode);
  1529. if (unlikely(!n)) {
  1530. __skb_queue_purge(list);
  1531. return -EHOSTUNREACH;
  1532. }
  1533. rcu_read_lock();
  1534. tipc_node_read_lock(n);
  1535. node_up = node_is_up(n);
  1536. peer_net = n->peer_net;
  1537. tipc_node_read_unlock(n);
  1538. if (node_up && peer_net && check_net(peer_net)) {
  1539. /* xmit inner linux container */
  1540. tipc_lxc_xmit(peer_net, list);
  1541. if (likely(skb_queue_empty(list))) {
  1542. rcu_read_unlock();
  1543. tipc_node_put(n);
  1544. return 0;
  1545. }
  1546. }
  1547. rcu_read_unlock();
  1548. tipc_node_read_lock(n);
  1549. bearer_id = n->active_links[selector & 1];
  1550. if (unlikely(bearer_id == INVALID_BEARER_ID)) {
  1551. tipc_node_read_unlock(n);
  1552. tipc_node_put(n);
  1553. __skb_queue_purge(list);
  1554. return -EHOSTUNREACH;
  1555. }
  1556. __skb_queue_head_init(&xmitq);
  1557. le = &n->links[bearer_id];
  1558. spin_lock_bh(&le->lock);
  1559. rc = tipc_link_xmit(le->link, list, &xmitq);
  1560. spin_unlock_bh(&le->lock);
  1561. tipc_node_read_unlock(n);
  1562. if (unlikely(rc == -ENOBUFS))
  1563. tipc_node_link_down(n, bearer_id, false);
  1564. else
  1565. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
  1566. tipc_node_put(n);
  1567. return rc;
  1568. }
  1569. /* tipc_node_xmit_skb(): send single buffer to destination
  1570. * Buffers sent via this function are generally TIPC_SYSTEM_IMPORTANCE
  1571. * messages, which will not be rejected
  1572. * The only exception is datagram messages rerouted after secondary
  1573. * lookup, which are rare and safe to dispose of anyway.
  1574. */
  1575. int tipc_node_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
  1576. u32 selector)
  1577. {
  1578. struct sk_buff_head head;
  1579. __skb_queue_head_init(&head);
  1580. __skb_queue_tail(&head, skb);
  1581. tipc_node_xmit(net, &head, dnode, selector);
  1582. return 0;
  1583. }
  1584. /* tipc_node_distr_xmit(): send single buffer msgs to individual destinations
  1585. * Note: this is only for SYSTEM_IMPORTANCE messages, which cannot be rejected
  1586. */
  1587. int tipc_node_distr_xmit(struct net *net, struct sk_buff_head *xmitq)
  1588. {
  1589. struct sk_buff *skb;
  1590. u32 selector, dnode;
  1591. while ((skb = __skb_dequeue(xmitq))) {
  1592. selector = msg_origport(buf_msg(skb));
  1593. dnode = msg_destnode(buf_msg(skb));
  1594. tipc_node_xmit_skb(net, skb, dnode, selector);
  1595. }
  1596. return 0;
  1597. }
  1598. void tipc_node_broadcast(struct net *net, struct sk_buff *skb, int rc_dests)
  1599. {
  1600. struct sk_buff_head xmitq;
  1601. struct sk_buff *txskb;
  1602. struct tipc_node *n;
  1603. u16 dummy;
  1604. u32 dst;
  1605. /* Use broadcast if all nodes support it */
  1606. if (!rc_dests && tipc_bcast_get_mode(net) != BCLINK_MODE_RCAST) {
  1607. __skb_queue_head_init(&xmitq);
  1608. __skb_queue_tail(&xmitq, skb);
  1609. tipc_bcast_xmit(net, &xmitq, &dummy);
  1610. return;
  1611. }
  1612. /* Otherwise use legacy replicast method */
  1613. rcu_read_lock();
  1614. list_for_each_entry_rcu(n, tipc_nodes(net), list) {
  1615. dst = n->addr;
  1616. if (in_own_node(net, dst))
  1617. continue;
  1618. if (!node_is_up(n))
  1619. continue;
  1620. txskb = pskb_copy(skb, GFP_ATOMIC);
  1621. if (!txskb)
  1622. break;
  1623. msg_set_destnode(buf_msg(txskb), dst);
  1624. tipc_node_xmit_skb(net, txskb, dst, 0);
  1625. }
  1626. rcu_read_unlock();
  1627. kfree_skb(skb);
  1628. }
  1629. static void tipc_node_mcast_rcv(struct tipc_node *n)
  1630. {
  1631. struct tipc_bclink_entry *be = &n->bc_entry;
  1632. /* 'arrvq' is under inputq2's lock protection */
  1633. spin_lock_bh(&be->inputq2.lock);
  1634. spin_lock_bh(&be->inputq1.lock);
  1635. skb_queue_splice_tail_init(&be->inputq1, &be->arrvq);
  1636. spin_unlock_bh(&be->inputq1.lock);
  1637. spin_unlock_bh(&be->inputq2.lock);
  1638. tipc_sk_mcast_rcv(n->net, &be->arrvq, &be->inputq2);
  1639. }
  1640. static void tipc_node_bc_sync_rcv(struct tipc_node *n, struct tipc_msg *hdr,
  1641. int bearer_id, struct sk_buff_head *xmitq)
  1642. {
  1643. struct tipc_link *ucl;
  1644. int rc;
  1645. rc = tipc_bcast_sync_rcv(n->net, n->bc_entry.link, hdr, xmitq);
  1646. if (rc & TIPC_LINK_DOWN_EVT) {
  1647. tipc_node_reset_links(n);
  1648. return;
  1649. }
  1650. if (!(rc & TIPC_LINK_SND_STATE))
  1651. return;
  1652. /* If probe message, a STATE response will be sent anyway */
  1653. if (msg_probe(hdr))
  1654. return;
  1655. /* Produce a STATE message carrying broadcast NACK */
  1656. tipc_node_read_lock(n);
  1657. ucl = n->links[bearer_id].link;
  1658. if (ucl)
  1659. tipc_link_build_state_msg(ucl, xmitq);
  1660. tipc_node_read_unlock(n);
  1661. }
  1662. /**
  1663. * tipc_node_bc_rcv - process TIPC broadcast packet arriving from off-node
  1664. * @net: the applicable net namespace
  1665. * @skb: TIPC packet
  1666. * @bearer_id: id of bearer message arrived on
  1667. *
  1668. * Invoked with no locks held.
  1669. */
  1670. static void tipc_node_bc_rcv(struct net *net, struct sk_buff *skb, int bearer_id)
  1671. {
  1672. int rc;
  1673. struct sk_buff_head xmitq;
  1674. struct tipc_bclink_entry *be;
  1675. struct tipc_link_entry *le;
  1676. struct tipc_msg *hdr = buf_msg(skb);
  1677. int usr = msg_user(hdr);
  1678. u32 dnode = msg_destnode(hdr);
  1679. struct tipc_node *n;
  1680. __skb_queue_head_init(&xmitq);
  1681. /* If NACK for other node, let rcv link for that node peek into it */
  1682. if ((usr == BCAST_PROTOCOL) && (dnode != tipc_own_addr(net)))
  1683. n = tipc_node_find(net, dnode);
  1684. else
  1685. n = tipc_node_find(net, msg_prevnode(hdr));
  1686. if (!n) {
  1687. kfree_skb(skb);
  1688. return;
  1689. }
  1690. be = &n->bc_entry;
  1691. le = &n->links[bearer_id];
  1692. rc = tipc_bcast_rcv(net, be->link, skb);
  1693. /* Broadcast ACKs are sent on a unicast link */
  1694. if (rc & TIPC_LINK_SND_STATE) {
  1695. tipc_node_read_lock(n);
  1696. tipc_link_build_state_msg(le->link, &xmitq);
  1697. tipc_node_read_unlock(n);
  1698. }
  1699. if (!skb_queue_empty(&xmitq))
  1700. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
  1701. if (!skb_queue_empty(&be->inputq1))
  1702. tipc_node_mcast_rcv(n);
  1703. /* Handle NAME_DISTRIBUTOR messages sent from 1.7 nodes */
  1704. if (!skb_queue_empty(&n->bc_entry.namedq))
  1705. tipc_named_rcv(net, &n->bc_entry.namedq,
  1706. &n->bc_entry.named_rcv_nxt,
  1707. &n->bc_entry.named_open);
  1708. /* If reassembly or retransmission failure => reset all links to peer */
  1709. if (rc & TIPC_LINK_DOWN_EVT)
  1710. tipc_node_reset_links(n);
  1711. tipc_node_put(n);
  1712. }
  1713. /**
  1714. * tipc_node_check_state - check and if necessary update node state
  1715. * @n: target tipc_node
  1716. * @skb: TIPC packet
  1717. * @bearer_id: identity of bearer delivering the packet
  1718. * @xmitq: queue for messages to be xmited on
  1719. * Return: true if state and msg are ok, otherwise false
  1720. */
  1721. static bool tipc_node_check_state(struct tipc_node *n, struct sk_buff *skb,
  1722. int bearer_id, struct sk_buff_head *xmitq)
  1723. {
  1724. struct tipc_msg *hdr = buf_msg(skb);
  1725. int usr = msg_user(hdr);
  1726. int mtyp = msg_type(hdr);
  1727. u16 oseqno = msg_seqno(hdr);
  1728. u16 exp_pkts = msg_msgcnt(hdr);
  1729. u16 rcv_nxt, syncpt, dlv_nxt, inputq_len;
  1730. int state = n->state;
  1731. struct tipc_link *l, *tnl, *pl = NULL;
  1732. struct tipc_media_addr *maddr;
  1733. int pb_id;
  1734. if (trace_tipc_node_check_state_enabled()) {
  1735. trace_tipc_skb_dump(skb, false, "skb for node state check");
  1736. trace_tipc_node_check_state(n, true, " ");
  1737. }
  1738. l = n->links[bearer_id].link;
  1739. if (!l)
  1740. return false;
  1741. rcv_nxt = tipc_link_rcv_nxt(l);
  1742. if (likely((state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL)))
  1743. return true;
  1744. /* Find parallel link, if any */
  1745. for (pb_id = 0; pb_id < MAX_BEARERS; pb_id++) {
  1746. if ((pb_id != bearer_id) && n->links[pb_id].link) {
  1747. pl = n->links[pb_id].link;
  1748. break;
  1749. }
  1750. }
  1751. if (!tipc_link_validate_msg(l, hdr)) {
  1752. trace_tipc_skb_dump(skb, false, "PROTO invalid (2)!");
  1753. trace_tipc_link_dump(l, TIPC_DUMP_NONE, "PROTO invalid (2)!");
  1754. return false;
  1755. }
  1756. /* Check and update node accesibility if applicable */
  1757. if (state == SELF_UP_PEER_COMING) {
  1758. if (!tipc_link_is_up(l))
  1759. return true;
  1760. if (!msg_peer_link_is_up(hdr))
  1761. return true;
  1762. tipc_node_fsm_evt(n, PEER_ESTABL_CONTACT_EVT);
  1763. }
  1764. if (state == SELF_DOWN_PEER_LEAVING) {
  1765. if (msg_peer_node_is_up(hdr))
  1766. return false;
  1767. tipc_node_fsm_evt(n, PEER_LOST_CONTACT_EVT);
  1768. return true;
  1769. }
  1770. if (state == SELF_LEAVING_PEER_DOWN)
  1771. return false;
  1772. /* Ignore duplicate packets */
  1773. if ((usr != LINK_PROTOCOL) && less(oseqno, rcv_nxt))
  1774. return true;
  1775. /* Initiate or update failover mode if applicable */
  1776. if ((usr == TUNNEL_PROTOCOL) && (mtyp == FAILOVER_MSG)) {
  1777. syncpt = oseqno + exp_pkts - 1;
  1778. if (pl && !tipc_link_is_reset(pl)) {
  1779. __tipc_node_link_down(n, &pb_id, xmitq, &maddr);
  1780. trace_tipc_node_link_down(n, true,
  1781. "node link down <- failover!");
  1782. tipc_skb_queue_splice_tail_init(tipc_link_inputq(pl),
  1783. tipc_link_inputq(l));
  1784. }
  1785. /* If parallel link was already down, and this happened before
  1786. * the tunnel link came up, node failover was never started.
  1787. * Ensure that a FAILOVER_MSG is sent to get peer out of
  1788. * NODE_FAILINGOVER state, also this node must accept
  1789. * TUNNEL_MSGs from peer.
  1790. */
  1791. if (n->state != NODE_FAILINGOVER)
  1792. tipc_node_link_failover(n, pl, l, xmitq);
  1793. /* If pkts arrive out of order, use lowest calculated syncpt */
  1794. if (less(syncpt, n->sync_point))
  1795. n->sync_point = syncpt;
  1796. }
  1797. /* Open parallel link when tunnel link reaches synch point */
  1798. if ((n->state == NODE_FAILINGOVER) && tipc_link_is_up(l)) {
  1799. if (!more(rcv_nxt, n->sync_point))
  1800. return true;
  1801. tipc_node_fsm_evt(n, NODE_FAILOVER_END_EVT);
  1802. if (pl)
  1803. tipc_link_fsm_evt(pl, LINK_FAILOVER_END_EVT);
  1804. return true;
  1805. }
  1806. /* No syncing needed if only one link */
  1807. if (!pl || !tipc_link_is_up(pl))
  1808. return true;
  1809. /* Initiate synch mode if applicable */
  1810. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG) && (oseqno == 1)) {
  1811. if (n->capabilities & TIPC_TUNNEL_ENHANCED)
  1812. syncpt = msg_syncpt(hdr);
  1813. else
  1814. syncpt = msg_seqno(msg_inner_hdr(hdr)) + exp_pkts - 1;
  1815. if (!tipc_link_is_up(l))
  1816. __tipc_node_link_up(n, bearer_id, xmitq);
  1817. if (n->state == SELF_UP_PEER_UP) {
  1818. n->sync_point = syncpt;
  1819. tipc_link_fsm_evt(l, LINK_SYNCH_BEGIN_EVT);
  1820. tipc_node_fsm_evt(n, NODE_SYNCH_BEGIN_EVT);
  1821. }
  1822. }
  1823. /* Open tunnel link when parallel link reaches synch point */
  1824. if (n->state == NODE_SYNCHING) {
  1825. if (tipc_link_is_synching(l)) {
  1826. tnl = l;
  1827. } else {
  1828. tnl = pl;
  1829. pl = l;
  1830. }
  1831. inputq_len = skb_queue_len(tipc_link_inputq(pl));
  1832. dlv_nxt = tipc_link_rcv_nxt(pl) - inputq_len;
  1833. if (more(dlv_nxt, n->sync_point)) {
  1834. tipc_link_fsm_evt(tnl, LINK_SYNCH_END_EVT);
  1835. tipc_node_fsm_evt(n, NODE_SYNCH_END_EVT);
  1836. return true;
  1837. }
  1838. if (l == pl)
  1839. return true;
  1840. if ((usr == TUNNEL_PROTOCOL) && (mtyp == SYNCH_MSG))
  1841. return true;
  1842. if (usr == LINK_PROTOCOL)
  1843. return true;
  1844. return false;
  1845. }
  1846. return true;
  1847. }
  1848. /**
  1849. * tipc_rcv - process TIPC packets/messages arriving from off-node
  1850. * @net: the applicable net namespace
  1851. * @skb: TIPC packet
  1852. * @b: pointer to bearer message arrived on
  1853. *
  1854. * Invoked with no locks held. Bearer pointer must point to a valid bearer
  1855. * structure (i.e. cannot be NULL), but bearer can be inactive.
  1856. */
  1857. void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b)
  1858. {
  1859. struct sk_buff_head xmitq;
  1860. struct tipc_link_entry *le;
  1861. struct tipc_msg *hdr;
  1862. struct tipc_node *n;
  1863. int bearer_id = b->identity;
  1864. u32 self = tipc_own_addr(net);
  1865. int usr, rc = 0;
  1866. u16 bc_ack;
  1867. #ifdef CONFIG_TIPC_CRYPTO
  1868. struct tipc_ehdr *ehdr;
  1869. /* Check if message must be decrypted first */
  1870. if (TIPC_SKB_CB(skb)->decrypted || !tipc_ehdr_validate(skb))
  1871. goto rcv;
  1872. ehdr = (struct tipc_ehdr *)skb->data;
  1873. if (likely(ehdr->user != LINK_CONFIG)) {
  1874. n = tipc_node_find(net, ntohl(ehdr->addr));
  1875. if (unlikely(!n))
  1876. goto discard;
  1877. } else {
  1878. n = tipc_node_find_by_id(net, ehdr->id);
  1879. }
  1880. skb_dst_force(skb);
  1881. tipc_crypto_rcv(net, (n) ? n->crypto_rx : NULL, &skb, b);
  1882. if (!skb)
  1883. return;
  1884. rcv:
  1885. #endif
  1886. /* Ensure message is well-formed before touching the header */
  1887. if (unlikely(!tipc_msg_validate(&skb)))
  1888. goto discard;
  1889. __skb_queue_head_init(&xmitq);
  1890. hdr = buf_msg(skb);
  1891. usr = msg_user(hdr);
  1892. bc_ack = msg_bcast_ack(hdr);
  1893. /* Handle arrival of discovery or broadcast packet */
  1894. if (unlikely(msg_non_seq(hdr))) {
  1895. if (unlikely(usr == LINK_CONFIG))
  1896. return tipc_disc_rcv(net, skb, b);
  1897. else
  1898. return tipc_node_bc_rcv(net, skb, bearer_id);
  1899. }
  1900. /* Discard unicast link messages destined for another node */
  1901. if (unlikely(!msg_short(hdr) && (msg_destnode(hdr) != self)))
  1902. goto discard;
  1903. /* Locate neighboring node that sent packet */
  1904. n = tipc_node_find(net, msg_prevnode(hdr));
  1905. if (unlikely(!n))
  1906. goto discard;
  1907. le = &n->links[bearer_id];
  1908. /* Ensure broadcast reception is in synch with peer's send state */
  1909. if (unlikely(usr == LINK_PROTOCOL)) {
  1910. if (unlikely(skb_linearize(skb))) {
  1911. tipc_node_put(n);
  1912. goto discard;
  1913. }
  1914. hdr = buf_msg(skb);
  1915. tipc_node_bc_sync_rcv(n, hdr, bearer_id, &xmitq);
  1916. } else if (unlikely(tipc_link_acked(n->bc_entry.link) != bc_ack)) {
  1917. tipc_bcast_ack_rcv(net, n->bc_entry.link, hdr);
  1918. }
  1919. /* Receive packet directly if conditions permit */
  1920. tipc_node_read_lock(n);
  1921. if (likely((n->state == SELF_UP_PEER_UP) && (usr != TUNNEL_PROTOCOL))) {
  1922. spin_lock_bh(&le->lock);
  1923. if (le->link) {
  1924. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1925. skb = NULL;
  1926. }
  1927. spin_unlock_bh(&le->lock);
  1928. }
  1929. tipc_node_read_unlock(n);
  1930. /* Check/update node state before receiving */
  1931. if (unlikely(skb)) {
  1932. if (unlikely(skb_linearize(skb)))
  1933. goto out_node_put;
  1934. tipc_node_write_lock(n);
  1935. if (tipc_node_check_state(n, skb, bearer_id, &xmitq)) {
  1936. if (le->link) {
  1937. rc = tipc_link_rcv(le->link, skb, &xmitq);
  1938. skb = NULL;
  1939. }
  1940. }
  1941. tipc_node_write_unlock(n);
  1942. }
  1943. if (unlikely(rc & TIPC_LINK_UP_EVT))
  1944. tipc_node_link_up(n, bearer_id, &xmitq);
  1945. if (unlikely(rc & TIPC_LINK_DOWN_EVT))
  1946. tipc_node_link_down(n, bearer_id, false);
  1947. if (unlikely(!skb_queue_empty(&n->bc_entry.namedq)))
  1948. tipc_named_rcv(net, &n->bc_entry.namedq,
  1949. &n->bc_entry.named_rcv_nxt,
  1950. &n->bc_entry.named_open);
  1951. if (unlikely(!skb_queue_empty(&n->bc_entry.inputq1)))
  1952. tipc_node_mcast_rcv(n);
  1953. if (!skb_queue_empty(&le->inputq))
  1954. tipc_sk_rcv(net, &le->inputq);
  1955. if (!skb_queue_empty(&xmitq))
  1956. tipc_bearer_xmit(net, bearer_id, &xmitq, &le->maddr, n);
  1957. out_node_put:
  1958. tipc_node_put(n);
  1959. discard:
  1960. kfree_skb(skb);
  1961. }
  1962. void tipc_node_apply_property(struct net *net, struct tipc_bearer *b,
  1963. int prop)
  1964. {
  1965. struct tipc_net *tn = tipc_net(net);
  1966. int bearer_id = b->identity;
  1967. struct sk_buff_head xmitq;
  1968. struct tipc_link_entry *e;
  1969. struct tipc_node *n;
  1970. __skb_queue_head_init(&xmitq);
  1971. rcu_read_lock();
  1972. list_for_each_entry_rcu(n, &tn->node_list, list) {
  1973. tipc_node_write_lock(n);
  1974. e = &n->links[bearer_id];
  1975. if (e->link) {
  1976. if (prop == TIPC_NLA_PROP_TOL)
  1977. tipc_link_set_tolerance(e->link, b->tolerance,
  1978. &xmitq);
  1979. else if (prop == TIPC_NLA_PROP_MTU)
  1980. tipc_link_set_mtu(e->link, b->mtu);
  1981. /* Update MTU for node link entry */
  1982. e->mtu = tipc_link_mss(e->link);
  1983. }
  1984. tipc_node_write_unlock(n);
  1985. tipc_bearer_xmit(net, bearer_id, &xmitq, &e->maddr, NULL);
  1986. }
  1987. rcu_read_unlock();
  1988. }
  1989. int tipc_nl_peer_rm(struct sk_buff *skb, struct genl_info *info)
  1990. {
  1991. struct net *net = sock_net(skb->sk);
  1992. struct tipc_net *tn = net_generic(net, tipc_net_id);
  1993. struct nlattr *attrs[TIPC_NLA_NET_MAX + 1];
  1994. struct tipc_node *peer, *temp_node;
  1995. u8 node_id[NODE_ID_LEN];
  1996. u64 *w0 = (u64 *)&node_id[0];
  1997. u64 *w1 = (u64 *)&node_id[8];
  1998. u32 addr;
  1999. int err;
  2000. /* We identify the peer by its net */
  2001. if (!info->attrs[TIPC_NLA_NET])
  2002. return -EINVAL;
  2003. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_NET_MAX,
  2004. info->attrs[TIPC_NLA_NET],
  2005. tipc_nl_net_policy, info->extack);
  2006. if (err)
  2007. return err;
  2008. /* attrs[TIPC_NLA_NET_NODEID] and attrs[TIPC_NLA_NET_ADDR] are
  2009. * mutually exclusive cases
  2010. */
  2011. if (attrs[TIPC_NLA_NET_ADDR]) {
  2012. addr = nla_get_u32(attrs[TIPC_NLA_NET_ADDR]);
  2013. if (!addr)
  2014. return -EINVAL;
  2015. }
  2016. if (attrs[TIPC_NLA_NET_NODEID]) {
  2017. if (!attrs[TIPC_NLA_NET_NODEID_W1])
  2018. return -EINVAL;
  2019. *w0 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID]);
  2020. *w1 = nla_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]);
  2021. addr = hash128to32(node_id);
  2022. }
  2023. if (in_own_node(net, addr))
  2024. return -ENOTSUPP;
  2025. spin_lock_bh(&tn->node_list_lock);
  2026. peer = tipc_node_find(net, addr);
  2027. if (!peer) {
  2028. spin_unlock_bh(&tn->node_list_lock);
  2029. return -ENXIO;
  2030. }
  2031. tipc_node_write_lock(peer);
  2032. if (peer->state != SELF_DOWN_PEER_DOWN &&
  2033. peer->state != SELF_DOWN_PEER_LEAVING) {
  2034. tipc_node_write_unlock(peer);
  2035. err = -EBUSY;
  2036. goto err_out;
  2037. }
  2038. tipc_node_clear_links(peer);
  2039. tipc_node_write_unlock(peer);
  2040. tipc_node_delete(peer);
  2041. /* Calculate cluster capabilities */
  2042. tn->capabilities = TIPC_NODE_CAPABILITIES;
  2043. list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
  2044. tn->capabilities &= temp_node->capabilities;
  2045. }
  2046. tipc_bcast_toggle_rcast(net, (tn->capabilities & TIPC_BCAST_RCAST));
  2047. err = 0;
  2048. err_out:
  2049. tipc_node_put(peer);
  2050. spin_unlock_bh(&tn->node_list_lock);
  2051. return err;
  2052. }
  2053. int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2054. {
  2055. int err;
  2056. struct net *net = sock_net(skb->sk);
  2057. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2058. int done = cb->args[0];
  2059. int last_addr = cb->args[1];
  2060. struct tipc_node *node;
  2061. struct tipc_nl_msg msg;
  2062. if (done)
  2063. return 0;
  2064. msg.skb = skb;
  2065. msg.portid = NETLINK_CB(cb->skb).portid;
  2066. msg.seq = cb->nlh->nlmsg_seq;
  2067. rcu_read_lock();
  2068. if (last_addr) {
  2069. node = tipc_node_find(net, last_addr);
  2070. if (!node) {
  2071. rcu_read_unlock();
  2072. /* We never set seq or call nl_dump_check_consistent()
  2073. * this means that setting prev_seq here will cause the
  2074. * consistence check to fail in the netlink callback
  2075. * handler. Resulting in the NLMSG_DONE message having
  2076. * the NLM_F_DUMP_INTR flag set if the node state
  2077. * changed while we released the lock.
  2078. */
  2079. cb->prev_seq = 1;
  2080. return -EPIPE;
  2081. }
  2082. tipc_node_put(node);
  2083. }
  2084. list_for_each_entry_rcu(node, &tn->node_list, list) {
  2085. if (node->preliminary)
  2086. continue;
  2087. if (last_addr) {
  2088. if (node->addr == last_addr)
  2089. last_addr = 0;
  2090. else
  2091. continue;
  2092. }
  2093. tipc_node_read_lock(node);
  2094. err = __tipc_nl_add_node(&msg, node);
  2095. if (err) {
  2096. last_addr = node->addr;
  2097. tipc_node_read_unlock(node);
  2098. goto out;
  2099. }
  2100. tipc_node_read_unlock(node);
  2101. }
  2102. done = 1;
  2103. out:
  2104. cb->args[0] = done;
  2105. cb->args[1] = last_addr;
  2106. rcu_read_unlock();
  2107. return skb->len;
  2108. }
  2109. /* tipc_node_find_by_name - locate owner node of link by link's name
  2110. * @net: the applicable net namespace
  2111. * @name: pointer to link name string
  2112. * @bearer_id: pointer to index in 'node->links' array where the link was found.
  2113. *
  2114. * Returns pointer to node owning the link, or 0 if no matching link is found.
  2115. */
  2116. static struct tipc_node *tipc_node_find_by_name(struct net *net,
  2117. const char *link_name,
  2118. unsigned int *bearer_id)
  2119. {
  2120. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2121. struct tipc_link *l;
  2122. struct tipc_node *n;
  2123. struct tipc_node *found_node = NULL;
  2124. int i;
  2125. *bearer_id = 0;
  2126. rcu_read_lock();
  2127. list_for_each_entry_rcu(n, &tn->node_list, list) {
  2128. tipc_node_read_lock(n);
  2129. for (i = 0; i < MAX_BEARERS; i++) {
  2130. l = n->links[i].link;
  2131. if (l && !strcmp(tipc_link_name(l), link_name)) {
  2132. *bearer_id = i;
  2133. found_node = n;
  2134. break;
  2135. }
  2136. }
  2137. tipc_node_read_unlock(n);
  2138. if (found_node)
  2139. break;
  2140. }
  2141. rcu_read_unlock();
  2142. return found_node;
  2143. }
  2144. int tipc_nl_node_set_link(struct sk_buff *skb, struct genl_info *info)
  2145. {
  2146. int err;
  2147. int res = 0;
  2148. int bearer_id;
  2149. char *name;
  2150. struct tipc_link *link;
  2151. struct tipc_node *node;
  2152. struct sk_buff_head xmitq;
  2153. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  2154. struct net *net = sock_net(skb->sk);
  2155. __skb_queue_head_init(&xmitq);
  2156. if (!info->attrs[TIPC_NLA_LINK])
  2157. return -EINVAL;
  2158. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
  2159. info->attrs[TIPC_NLA_LINK],
  2160. tipc_nl_link_policy, info->extack);
  2161. if (err)
  2162. return err;
  2163. if (!attrs[TIPC_NLA_LINK_NAME])
  2164. return -EINVAL;
  2165. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  2166. if (strcmp(name, tipc_bclink_name) == 0)
  2167. return tipc_nl_bc_link_set(net, attrs);
  2168. node = tipc_node_find_by_name(net, name, &bearer_id);
  2169. if (!node)
  2170. return -EINVAL;
  2171. tipc_node_read_lock(node);
  2172. link = node->links[bearer_id].link;
  2173. if (!link) {
  2174. res = -EINVAL;
  2175. goto out;
  2176. }
  2177. if (attrs[TIPC_NLA_LINK_PROP]) {
  2178. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  2179. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
  2180. if (err) {
  2181. res = err;
  2182. goto out;
  2183. }
  2184. if (props[TIPC_NLA_PROP_TOL]) {
  2185. u32 tol;
  2186. tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
  2187. tipc_link_set_tolerance(link, tol, &xmitq);
  2188. }
  2189. if (props[TIPC_NLA_PROP_PRIO]) {
  2190. u32 prio;
  2191. prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
  2192. tipc_link_set_prio(link, prio, &xmitq);
  2193. }
  2194. if (props[TIPC_NLA_PROP_WIN]) {
  2195. u32 max_win;
  2196. max_win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  2197. tipc_link_set_queue_limits(link,
  2198. tipc_link_min_win(link),
  2199. max_win);
  2200. }
  2201. }
  2202. out:
  2203. tipc_node_read_unlock(node);
  2204. tipc_bearer_xmit(net, bearer_id, &xmitq, &node->links[bearer_id].maddr,
  2205. NULL);
  2206. return res;
  2207. }
  2208. int tipc_nl_node_get_link(struct sk_buff *skb, struct genl_info *info)
  2209. {
  2210. struct net *net = genl_info_net(info);
  2211. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  2212. struct tipc_nl_msg msg;
  2213. char *name;
  2214. int err;
  2215. msg.portid = info->snd_portid;
  2216. msg.seq = info->snd_seq;
  2217. if (!info->attrs[TIPC_NLA_LINK])
  2218. return -EINVAL;
  2219. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
  2220. info->attrs[TIPC_NLA_LINK],
  2221. tipc_nl_link_policy, info->extack);
  2222. if (err)
  2223. return err;
  2224. if (!attrs[TIPC_NLA_LINK_NAME])
  2225. return -EINVAL;
  2226. name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  2227. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  2228. if (!msg.skb)
  2229. return -ENOMEM;
  2230. if (strcmp(name, tipc_bclink_name) == 0) {
  2231. err = tipc_nl_add_bc_link(net, &msg, tipc_net(net)->bcl);
  2232. if (err)
  2233. goto err_free;
  2234. } else {
  2235. int bearer_id;
  2236. struct tipc_node *node;
  2237. struct tipc_link *link;
  2238. node = tipc_node_find_by_name(net, name, &bearer_id);
  2239. if (!node) {
  2240. err = -EINVAL;
  2241. goto err_free;
  2242. }
  2243. tipc_node_read_lock(node);
  2244. link = node->links[bearer_id].link;
  2245. if (!link) {
  2246. tipc_node_read_unlock(node);
  2247. err = -EINVAL;
  2248. goto err_free;
  2249. }
  2250. err = __tipc_nl_add_link(net, &msg, link, 0);
  2251. tipc_node_read_unlock(node);
  2252. if (err)
  2253. goto err_free;
  2254. }
  2255. return genlmsg_reply(msg.skb, info);
  2256. err_free:
  2257. nlmsg_free(msg.skb);
  2258. return err;
  2259. }
  2260. int tipc_nl_node_reset_link_stats(struct sk_buff *skb, struct genl_info *info)
  2261. {
  2262. int err;
  2263. char *link_name;
  2264. unsigned int bearer_id;
  2265. struct tipc_link *link;
  2266. struct tipc_node *node;
  2267. struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1];
  2268. struct net *net = sock_net(skb->sk);
  2269. struct tipc_net *tn = tipc_net(net);
  2270. struct tipc_link_entry *le;
  2271. if (!info->attrs[TIPC_NLA_LINK])
  2272. return -EINVAL;
  2273. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_LINK_MAX,
  2274. info->attrs[TIPC_NLA_LINK],
  2275. tipc_nl_link_policy, info->extack);
  2276. if (err)
  2277. return err;
  2278. if (!attrs[TIPC_NLA_LINK_NAME])
  2279. return -EINVAL;
  2280. link_name = nla_data(attrs[TIPC_NLA_LINK_NAME]);
  2281. err = -EINVAL;
  2282. if (!strcmp(link_name, tipc_bclink_name)) {
  2283. err = tipc_bclink_reset_stats(net, tipc_bc_sndlink(net));
  2284. if (err)
  2285. return err;
  2286. return 0;
  2287. } else if (strstr(link_name, tipc_bclink_name)) {
  2288. rcu_read_lock();
  2289. list_for_each_entry_rcu(node, &tn->node_list, list) {
  2290. tipc_node_read_lock(node);
  2291. link = node->bc_entry.link;
  2292. if (link && !strcmp(link_name, tipc_link_name(link))) {
  2293. err = tipc_bclink_reset_stats(net, link);
  2294. tipc_node_read_unlock(node);
  2295. break;
  2296. }
  2297. tipc_node_read_unlock(node);
  2298. }
  2299. rcu_read_unlock();
  2300. return err;
  2301. }
  2302. node = tipc_node_find_by_name(net, link_name, &bearer_id);
  2303. if (!node)
  2304. return -EINVAL;
  2305. le = &node->links[bearer_id];
  2306. tipc_node_read_lock(node);
  2307. spin_lock_bh(&le->lock);
  2308. link = node->links[bearer_id].link;
  2309. if (!link) {
  2310. spin_unlock_bh(&le->lock);
  2311. tipc_node_read_unlock(node);
  2312. return -EINVAL;
  2313. }
  2314. tipc_link_reset_stats(link);
  2315. spin_unlock_bh(&le->lock);
  2316. tipc_node_read_unlock(node);
  2317. return 0;
  2318. }
  2319. /* Caller should hold node lock */
  2320. static int __tipc_nl_add_node_links(struct net *net, struct tipc_nl_msg *msg,
  2321. struct tipc_node *node, u32 *prev_link,
  2322. bool bc_link)
  2323. {
  2324. u32 i;
  2325. int err;
  2326. for (i = *prev_link; i < MAX_BEARERS; i++) {
  2327. *prev_link = i;
  2328. if (!node->links[i].link)
  2329. continue;
  2330. err = __tipc_nl_add_link(net, msg,
  2331. node->links[i].link, NLM_F_MULTI);
  2332. if (err)
  2333. return err;
  2334. }
  2335. if (bc_link) {
  2336. *prev_link = i;
  2337. err = tipc_nl_add_bc_link(net, msg, node->bc_entry.link);
  2338. if (err)
  2339. return err;
  2340. }
  2341. *prev_link = 0;
  2342. return 0;
  2343. }
  2344. int tipc_nl_node_dump_link(struct sk_buff *skb, struct netlink_callback *cb)
  2345. {
  2346. struct net *net = sock_net(skb->sk);
  2347. struct nlattr **attrs = genl_dumpit_info(cb)->info.attrs;
  2348. struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
  2349. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2350. struct tipc_node *node;
  2351. struct tipc_nl_msg msg;
  2352. u32 prev_node = cb->args[0];
  2353. u32 prev_link = cb->args[1];
  2354. int done = cb->args[2];
  2355. bool bc_link = cb->args[3];
  2356. int err;
  2357. if (done)
  2358. return 0;
  2359. if (!prev_node) {
  2360. /* Check if broadcast-receiver links dumping is needed */
  2361. if (attrs && attrs[TIPC_NLA_LINK]) {
  2362. err = nla_parse_nested_deprecated(link,
  2363. TIPC_NLA_LINK_MAX,
  2364. attrs[TIPC_NLA_LINK],
  2365. tipc_nl_link_policy,
  2366. NULL);
  2367. if (unlikely(err))
  2368. return err;
  2369. if (unlikely(!link[TIPC_NLA_LINK_BROADCAST]))
  2370. return -EINVAL;
  2371. bc_link = true;
  2372. }
  2373. }
  2374. msg.skb = skb;
  2375. msg.portid = NETLINK_CB(cb->skb).portid;
  2376. msg.seq = cb->nlh->nlmsg_seq;
  2377. rcu_read_lock();
  2378. if (prev_node) {
  2379. node = tipc_node_find(net, prev_node);
  2380. if (!node) {
  2381. /* We never set seq or call nl_dump_check_consistent()
  2382. * this means that setting prev_seq here will cause the
  2383. * consistence check to fail in the netlink callback
  2384. * handler. Resulting in the last NLMSG_DONE message
  2385. * having the NLM_F_DUMP_INTR flag set.
  2386. */
  2387. cb->prev_seq = 1;
  2388. goto out;
  2389. }
  2390. tipc_node_put(node);
  2391. list_for_each_entry_continue_rcu(node, &tn->node_list,
  2392. list) {
  2393. tipc_node_read_lock(node);
  2394. err = __tipc_nl_add_node_links(net, &msg, node,
  2395. &prev_link, bc_link);
  2396. tipc_node_read_unlock(node);
  2397. if (err)
  2398. goto out;
  2399. prev_node = node->addr;
  2400. }
  2401. } else {
  2402. err = tipc_nl_add_bc_link(net, &msg, tn->bcl);
  2403. if (err)
  2404. goto out;
  2405. list_for_each_entry_rcu(node, &tn->node_list, list) {
  2406. tipc_node_read_lock(node);
  2407. err = __tipc_nl_add_node_links(net, &msg, node,
  2408. &prev_link, bc_link);
  2409. tipc_node_read_unlock(node);
  2410. if (err)
  2411. goto out;
  2412. prev_node = node->addr;
  2413. }
  2414. }
  2415. done = 1;
  2416. out:
  2417. rcu_read_unlock();
  2418. cb->args[0] = prev_node;
  2419. cb->args[1] = prev_link;
  2420. cb->args[2] = done;
  2421. cb->args[3] = bc_link;
  2422. return skb->len;
  2423. }
  2424. int tipc_nl_node_set_monitor(struct sk_buff *skb, struct genl_info *info)
  2425. {
  2426. struct nlattr *attrs[TIPC_NLA_MON_MAX + 1];
  2427. struct net *net = sock_net(skb->sk);
  2428. int err;
  2429. if (!info->attrs[TIPC_NLA_MON])
  2430. return -EINVAL;
  2431. err = nla_parse_nested_deprecated(attrs, TIPC_NLA_MON_MAX,
  2432. info->attrs[TIPC_NLA_MON],
  2433. tipc_nl_monitor_policy,
  2434. info->extack);
  2435. if (err)
  2436. return err;
  2437. if (attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]) {
  2438. u32 val;
  2439. val = nla_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]);
  2440. err = tipc_nl_monitor_set_threshold(net, val);
  2441. if (err)
  2442. return err;
  2443. }
  2444. return 0;
  2445. }
  2446. static int __tipc_nl_add_monitor_prop(struct net *net, struct tipc_nl_msg *msg)
  2447. {
  2448. struct nlattr *attrs;
  2449. void *hdr;
  2450. u32 val;
  2451. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  2452. 0, TIPC_NL_MON_GET);
  2453. if (!hdr)
  2454. return -EMSGSIZE;
  2455. attrs = nla_nest_start_noflag(msg->skb, TIPC_NLA_MON);
  2456. if (!attrs)
  2457. goto msg_full;
  2458. val = tipc_nl_monitor_get_threshold(net);
  2459. if (nla_put_u32(msg->skb, TIPC_NLA_MON_ACTIVATION_THRESHOLD, val))
  2460. goto attr_msg_full;
  2461. nla_nest_end(msg->skb, attrs);
  2462. genlmsg_end(msg->skb, hdr);
  2463. return 0;
  2464. attr_msg_full:
  2465. nla_nest_cancel(msg->skb, attrs);
  2466. msg_full:
  2467. genlmsg_cancel(msg->skb, hdr);
  2468. return -EMSGSIZE;
  2469. }
  2470. int tipc_nl_node_get_monitor(struct sk_buff *skb, struct genl_info *info)
  2471. {
  2472. struct net *net = sock_net(skb->sk);
  2473. struct tipc_nl_msg msg;
  2474. int err;
  2475. msg.skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  2476. if (!msg.skb)
  2477. return -ENOMEM;
  2478. msg.portid = info->snd_portid;
  2479. msg.seq = info->snd_seq;
  2480. err = __tipc_nl_add_monitor_prop(net, &msg);
  2481. if (err) {
  2482. nlmsg_free(msg.skb);
  2483. return err;
  2484. }
  2485. return genlmsg_reply(msg.skb, info);
  2486. }
  2487. int tipc_nl_node_dump_monitor(struct sk_buff *skb, struct netlink_callback *cb)
  2488. {
  2489. struct net *net = sock_net(skb->sk);
  2490. u32 prev_bearer = cb->args[0];
  2491. struct tipc_nl_msg msg;
  2492. int bearer_id;
  2493. int err;
  2494. if (prev_bearer == MAX_BEARERS)
  2495. return 0;
  2496. msg.skb = skb;
  2497. msg.portid = NETLINK_CB(cb->skb).portid;
  2498. msg.seq = cb->nlh->nlmsg_seq;
  2499. rtnl_lock();
  2500. for (bearer_id = prev_bearer; bearer_id < MAX_BEARERS; bearer_id++) {
  2501. err = __tipc_nl_add_monitor(net, &msg, bearer_id);
  2502. if (err)
  2503. break;
  2504. }
  2505. rtnl_unlock();
  2506. cb->args[0] = bearer_id;
  2507. return skb->len;
  2508. }
  2509. int tipc_nl_node_dump_monitor_peer(struct sk_buff *skb,
  2510. struct netlink_callback *cb)
  2511. {
  2512. struct net *net = sock_net(skb->sk);
  2513. u32 prev_node = cb->args[1];
  2514. u32 bearer_id = cb->args[2];
  2515. int done = cb->args[0];
  2516. struct tipc_nl_msg msg;
  2517. int err;
  2518. if (!prev_node) {
  2519. struct nlattr **attrs = genl_dumpit_info(cb)->info.attrs;
  2520. struct nlattr *mon[TIPC_NLA_MON_MAX + 1];
  2521. if (!attrs[TIPC_NLA_MON])
  2522. return -EINVAL;
  2523. err = nla_parse_nested_deprecated(mon, TIPC_NLA_MON_MAX,
  2524. attrs[TIPC_NLA_MON],
  2525. tipc_nl_monitor_policy,
  2526. NULL);
  2527. if (err)
  2528. return err;
  2529. if (!mon[TIPC_NLA_MON_REF])
  2530. return -EINVAL;
  2531. bearer_id = nla_get_u32(mon[TIPC_NLA_MON_REF]);
  2532. if (bearer_id >= MAX_BEARERS)
  2533. return -EINVAL;
  2534. }
  2535. if (done)
  2536. return 0;
  2537. msg.skb = skb;
  2538. msg.portid = NETLINK_CB(cb->skb).portid;
  2539. msg.seq = cb->nlh->nlmsg_seq;
  2540. rtnl_lock();
  2541. err = tipc_nl_add_monitor_peer(net, &msg, bearer_id, &prev_node);
  2542. if (!err)
  2543. done = 1;
  2544. rtnl_unlock();
  2545. cb->args[0] = done;
  2546. cb->args[1] = prev_node;
  2547. cb->args[2] = bearer_id;
  2548. return skb->len;
  2549. }
  2550. #ifdef CONFIG_TIPC_CRYPTO
  2551. static int tipc_nl_retrieve_key(struct nlattr **attrs,
  2552. struct tipc_aead_key **pkey)
  2553. {
  2554. struct nlattr *attr = attrs[TIPC_NLA_NODE_KEY];
  2555. struct tipc_aead_key *key;
  2556. if (!attr)
  2557. return -ENODATA;
  2558. if (nla_len(attr) < sizeof(*key))
  2559. return -EINVAL;
  2560. key = (struct tipc_aead_key *)nla_data(attr);
  2561. if (key->keylen > TIPC_AEAD_KEYLEN_MAX ||
  2562. nla_len(attr) < tipc_aead_key_size(key))
  2563. return -EINVAL;
  2564. *pkey = key;
  2565. return 0;
  2566. }
  2567. static int tipc_nl_retrieve_nodeid(struct nlattr **attrs, u8 **node_id)
  2568. {
  2569. struct nlattr *attr = attrs[TIPC_NLA_NODE_ID];
  2570. if (!attr)
  2571. return -ENODATA;
  2572. if (nla_len(attr) < TIPC_NODEID_LEN)
  2573. return -EINVAL;
  2574. *node_id = (u8 *)nla_data(attr);
  2575. return 0;
  2576. }
  2577. static int tipc_nl_retrieve_rekeying(struct nlattr **attrs, u32 *intv)
  2578. {
  2579. struct nlattr *attr = attrs[TIPC_NLA_NODE_REKEYING];
  2580. if (!attr)
  2581. return -ENODATA;
  2582. *intv = nla_get_u32(attr);
  2583. return 0;
  2584. }
  2585. static int __tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
  2586. {
  2587. struct nlattr *attrs[TIPC_NLA_NODE_MAX + 1];
  2588. struct net *net = sock_net(skb->sk);
  2589. struct tipc_crypto *tx = tipc_net(net)->crypto_tx, *c = tx;
  2590. struct tipc_node *n = NULL;
  2591. struct tipc_aead_key *ukey;
  2592. bool rekeying = true, master_key = false;
  2593. u8 *id, *own_id, mode;
  2594. u32 intv = 0;
  2595. int rc = 0;
  2596. if (!info->attrs[TIPC_NLA_NODE])
  2597. return -EINVAL;
  2598. rc = nla_parse_nested(attrs, TIPC_NLA_NODE_MAX,
  2599. info->attrs[TIPC_NLA_NODE],
  2600. tipc_nl_node_policy, info->extack);
  2601. if (rc)
  2602. return rc;
  2603. own_id = tipc_own_id(net);
  2604. if (!own_id) {
  2605. GENL_SET_ERR_MSG(info, "not found own node identity (set id?)");
  2606. return -EPERM;
  2607. }
  2608. rc = tipc_nl_retrieve_rekeying(attrs, &intv);
  2609. if (rc == -ENODATA)
  2610. rekeying = false;
  2611. rc = tipc_nl_retrieve_key(attrs, &ukey);
  2612. if (rc == -ENODATA && rekeying)
  2613. goto rekeying;
  2614. else if (rc)
  2615. return rc;
  2616. rc = tipc_aead_key_validate(ukey, info);
  2617. if (rc)
  2618. return rc;
  2619. rc = tipc_nl_retrieve_nodeid(attrs, &id);
  2620. switch (rc) {
  2621. case -ENODATA:
  2622. mode = CLUSTER_KEY;
  2623. master_key = !!(attrs[TIPC_NLA_NODE_KEY_MASTER]);
  2624. break;
  2625. case 0:
  2626. mode = PER_NODE_KEY;
  2627. if (memcmp(id, own_id, NODE_ID_LEN)) {
  2628. n = tipc_node_find_by_id(net, id) ?:
  2629. tipc_node_create(net, 0, id, 0xffffu, 0, true);
  2630. if (unlikely(!n))
  2631. return -ENOMEM;
  2632. c = n->crypto_rx;
  2633. }
  2634. break;
  2635. default:
  2636. return rc;
  2637. }
  2638. /* Initiate the TX/RX key */
  2639. rc = tipc_crypto_key_init(c, ukey, mode, master_key);
  2640. if (n)
  2641. tipc_node_put(n);
  2642. if (unlikely(rc < 0)) {
  2643. GENL_SET_ERR_MSG(info, "unable to initiate or attach new key");
  2644. return rc;
  2645. } else if (c == tx) {
  2646. /* Distribute TX key but not master one */
  2647. if (!master_key && tipc_crypto_key_distr(tx, rc, NULL))
  2648. GENL_SET_ERR_MSG(info, "failed to replicate new key");
  2649. rekeying:
  2650. /* Schedule TX rekeying if needed */
  2651. tipc_crypto_rekeying_sched(tx, rekeying, intv);
  2652. }
  2653. return 0;
  2654. }
  2655. int tipc_nl_node_set_key(struct sk_buff *skb, struct genl_info *info)
  2656. {
  2657. int err;
  2658. rtnl_lock();
  2659. err = __tipc_nl_node_set_key(skb, info);
  2660. rtnl_unlock();
  2661. return err;
  2662. }
  2663. static int __tipc_nl_node_flush_key(struct sk_buff *skb,
  2664. struct genl_info *info)
  2665. {
  2666. struct net *net = sock_net(skb->sk);
  2667. struct tipc_net *tn = tipc_net(net);
  2668. struct tipc_node *n;
  2669. tipc_crypto_key_flush(tn->crypto_tx);
  2670. rcu_read_lock();
  2671. list_for_each_entry_rcu(n, &tn->node_list, list)
  2672. tipc_crypto_key_flush(n->crypto_rx);
  2673. rcu_read_unlock();
  2674. return 0;
  2675. }
  2676. int tipc_nl_node_flush_key(struct sk_buff *skb, struct genl_info *info)
  2677. {
  2678. int err;
  2679. rtnl_lock();
  2680. err = __tipc_nl_node_flush_key(skb, info);
  2681. rtnl_unlock();
  2682. return err;
  2683. }
  2684. #endif
  2685. /**
  2686. * tipc_node_dump - dump TIPC node data
  2687. * @n: tipc node to be dumped
  2688. * @more: dump more?
  2689. * - false: dump only tipc node data
  2690. * - true: dump node link data as well
  2691. * @buf: returned buffer of dump data in format
  2692. */
  2693. int tipc_node_dump(struct tipc_node *n, bool more, char *buf)
  2694. {
  2695. int i = 0;
  2696. size_t sz = (more) ? NODE_LMAX : NODE_LMIN;
  2697. if (!n) {
  2698. i += scnprintf(buf, sz, "node data: (null)\n");
  2699. return i;
  2700. }
  2701. i += scnprintf(buf, sz, "node data: %x", n->addr);
  2702. i += scnprintf(buf + i, sz - i, " %x", n->state);
  2703. i += scnprintf(buf + i, sz - i, " %d", n->active_links[0]);
  2704. i += scnprintf(buf + i, sz - i, " %d", n->active_links[1]);
  2705. i += scnprintf(buf + i, sz - i, " %x", n->action_flags);
  2706. i += scnprintf(buf + i, sz - i, " %u", n->failover_sent);
  2707. i += scnprintf(buf + i, sz - i, " %u", n->sync_point);
  2708. i += scnprintf(buf + i, sz - i, " %d", n->link_cnt);
  2709. i += scnprintf(buf + i, sz - i, " %u", n->working_links);
  2710. i += scnprintf(buf + i, sz - i, " %x", n->capabilities);
  2711. i += scnprintf(buf + i, sz - i, " %lu\n", n->keepalive_intv);
  2712. if (!more)
  2713. return i;
  2714. i += scnprintf(buf + i, sz - i, "link_entry[0]:\n");
  2715. i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[0].mtu);
  2716. i += scnprintf(buf + i, sz - i, " media: ");
  2717. i += tipc_media_addr_printf(buf + i, sz - i, &n->links[0].maddr);
  2718. i += scnprintf(buf + i, sz - i, "\n");
  2719. i += tipc_link_dump(n->links[0].link, TIPC_DUMP_NONE, buf + i);
  2720. i += scnprintf(buf + i, sz - i, " inputq: ");
  2721. i += tipc_list_dump(&n->links[0].inputq, false, buf + i);
  2722. i += scnprintf(buf + i, sz - i, "link_entry[1]:\n");
  2723. i += scnprintf(buf + i, sz - i, " mtu: %u\n", n->links[1].mtu);
  2724. i += scnprintf(buf + i, sz - i, " media: ");
  2725. i += tipc_media_addr_printf(buf + i, sz - i, &n->links[1].maddr);
  2726. i += scnprintf(buf + i, sz - i, "\n");
  2727. i += tipc_link_dump(n->links[1].link, TIPC_DUMP_NONE, buf + i);
  2728. i += scnprintf(buf + i, sz - i, " inputq: ");
  2729. i += tipc_list_dump(&n->links[1].inputq, false, buf + i);
  2730. i += scnprintf(buf + i, sz - i, "bclink:\n ");
  2731. i += tipc_link_dump(n->bc_entry.link, TIPC_DUMP_NONE, buf + i);
  2732. return i;
  2733. }
  2734. void tipc_node_pre_cleanup_net(struct net *exit_net)
  2735. {
  2736. struct tipc_node *n;
  2737. struct tipc_net *tn;
  2738. struct net *tmp;
  2739. rcu_read_lock();
  2740. for_each_net_rcu(tmp) {
  2741. if (tmp == exit_net)
  2742. continue;
  2743. tn = tipc_net(tmp);
  2744. if (!tn)
  2745. continue;
  2746. spin_lock_bh(&tn->node_list_lock);
  2747. list_for_each_entry_rcu(n, &tn->node_list, list) {
  2748. if (!n->peer_net)
  2749. continue;
  2750. if (n->peer_net != exit_net)
  2751. continue;
  2752. tipc_node_write_lock(n);
  2753. n->peer_net = NULL;
  2754. n->peer_hash_mix = 0;
  2755. tipc_node_write_unlock_fast(n);
  2756. break;
  2757. }
  2758. spin_unlock_bh(&tn->node_list_lock);
  2759. }
  2760. rcu_read_unlock();
  2761. }