node.c 79 KB

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