translation-table.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2007-2018 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich, Antonio Quartulli
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "translation-table.h"
  19. #include "main.h"
  20. #include <linux/atomic.h>
  21. #include <linux/bitops.h>
  22. #include <linux/build_bug.h>
  23. #include <linux/byteorder/generic.h>
  24. #include <linux/cache.h>
  25. #include <linux/compiler.h>
  26. #include <linux/crc32c.h>
  27. #include <linux/errno.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/gfp.h>
  30. #include <linux/if_ether.h>
  31. #include <linux/init.h>
  32. #include <linux/jhash.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/kernel.h>
  35. #include <linux/kref.h>
  36. #include <linux/list.h>
  37. #include <linux/lockdep.h>
  38. #include <linux/net.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/netlink.h>
  41. #include <linux/rculist.h>
  42. #include <linux/rcupdate.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/skbuff.h>
  45. #include <linux/slab.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/stddef.h>
  48. #include <linux/string.h>
  49. #include <linux/workqueue.h>
  50. #include <net/genetlink.h>
  51. #include <net/netlink.h>
  52. #include <net/sock.h>
  53. #include <uapi/linux/batadv_packet.h>
  54. #include <uapi/linux/batman_adv.h>
  55. #include "bridge_loop_avoidance.h"
  56. #include "hard-interface.h"
  57. #include "hash.h"
  58. #include "log.h"
  59. #include "netlink.h"
  60. #include "originator.h"
  61. #include "soft-interface.h"
  62. #include "tvlv.h"
  63. static struct kmem_cache *batadv_tl_cache __read_mostly;
  64. static struct kmem_cache *batadv_tg_cache __read_mostly;
  65. static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
  66. static struct kmem_cache *batadv_tt_change_cache __read_mostly;
  67. static struct kmem_cache *batadv_tt_req_cache __read_mostly;
  68. static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
  69. /* hash class keys */
  70. static struct lock_class_key batadv_tt_local_hash_lock_class_key;
  71. static struct lock_class_key batadv_tt_global_hash_lock_class_key;
  72. static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
  73. unsigned short vid,
  74. struct batadv_orig_node *orig_node);
  75. static void batadv_tt_purge(struct work_struct *work);
  76. static void
  77. batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
  78. static void batadv_tt_global_del(struct batadv_priv *bat_priv,
  79. struct batadv_orig_node *orig_node,
  80. const unsigned char *addr,
  81. unsigned short vid, const char *message,
  82. bool roaming);
  83. /**
  84. * batadv_compare_tt() - check if two TT entries are the same
  85. * @node: the list element pointer of the first TT entry
  86. * @data2: pointer to the tt_common_entry of the second TT entry
  87. *
  88. * Compare the MAC address and the VLAN ID of the two TT entries and check if
  89. * they are the same TT client.
  90. * Return: true if the two TT clients are the same, false otherwise
  91. */
  92. static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
  93. {
  94. const void *data1 = container_of(node, struct batadv_tt_common_entry,
  95. hash_entry);
  96. const struct batadv_tt_common_entry *tt1 = data1;
  97. const struct batadv_tt_common_entry *tt2 = data2;
  98. return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
  99. }
  100. /**
  101. * batadv_choose_tt() - return the index of the tt entry in the hash table
  102. * @data: pointer to the tt_common_entry object to map
  103. * @size: the size of the hash table
  104. *
  105. * Return: the hash index where the object represented by 'data' should be
  106. * stored at.
  107. */
  108. static inline u32 batadv_choose_tt(const void *data, u32 size)
  109. {
  110. struct batadv_tt_common_entry *tt;
  111. u32 hash = 0;
  112. tt = (struct batadv_tt_common_entry *)data;
  113. hash = jhash(&tt->addr, ETH_ALEN, hash);
  114. hash = jhash(&tt->vid, sizeof(tt->vid), hash);
  115. return hash % size;
  116. }
  117. /**
  118. * batadv_tt_hash_find() - look for a client in the given hash table
  119. * @hash: the hash table to search
  120. * @addr: the mac address of the client to look for
  121. * @vid: VLAN identifier
  122. *
  123. * Return: a pointer to the tt_common struct belonging to the searched client if
  124. * found, NULL otherwise.
  125. */
  126. static struct batadv_tt_common_entry *
  127. batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
  128. unsigned short vid)
  129. {
  130. struct hlist_head *head;
  131. struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
  132. u32 index;
  133. if (!hash)
  134. return NULL;
  135. ether_addr_copy(to_search.addr, addr);
  136. to_search.vid = vid;
  137. index = batadv_choose_tt(&to_search, hash->size);
  138. head = &hash->table[index];
  139. rcu_read_lock();
  140. hlist_for_each_entry_rcu(tt, head, hash_entry) {
  141. if (!batadv_compare_eth(tt, addr))
  142. continue;
  143. if (tt->vid != vid)
  144. continue;
  145. if (!kref_get_unless_zero(&tt->refcount))
  146. continue;
  147. tt_tmp = tt;
  148. break;
  149. }
  150. rcu_read_unlock();
  151. return tt_tmp;
  152. }
  153. /**
  154. * batadv_tt_local_hash_find() - search the local table for a given client
  155. * @bat_priv: the bat priv with all the soft interface information
  156. * @addr: the mac address of the client to look for
  157. * @vid: VLAN identifier
  158. *
  159. * Return: a pointer to the corresponding tt_local_entry struct if the client is
  160. * found, NULL otherwise.
  161. */
  162. static struct batadv_tt_local_entry *
  163. batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
  164. unsigned short vid)
  165. {
  166. struct batadv_tt_common_entry *tt_common_entry;
  167. struct batadv_tt_local_entry *tt_local_entry = NULL;
  168. tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
  169. vid);
  170. if (tt_common_entry)
  171. tt_local_entry = container_of(tt_common_entry,
  172. struct batadv_tt_local_entry,
  173. common);
  174. return tt_local_entry;
  175. }
  176. /**
  177. * batadv_tt_global_hash_find() - search the global table for a given client
  178. * @bat_priv: the bat priv with all the soft interface information
  179. * @addr: the mac address of the client to look for
  180. * @vid: VLAN identifier
  181. *
  182. * Return: a pointer to the corresponding tt_global_entry struct if the client
  183. * is found, NULL otherwise.
  184. */
  185. static struct batadv_tt_global_entry *
  186. batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
  187. unsigned short vid)
  188. {
  189. struct batadv_tt_common_entry *tt_common_entry;
  190. struct batadv_tt_global_entry *tt_global_entry = NULL;
  191. tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
  192. vid);
  193. if (tt_common_entry)
  194. tt_global_entry = container_of(tt_common_entry,
  195. struct batadv_tt_global_entry,
  196. common);
  197. return tt_global_entry;
  198. }
  199. /**
  200. * batadv_tt_local_entry_free_rcu() - free the tt_local_entry
  201. * @rcu: rcu pointer of the tt_local_entry
  202. */
  203. static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
  204. {
  205. struct batadv_tt_local_entry *tt_local_entry;
  206. tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
  207. common.rcu);
  208. kmem_cache_free(batadv_tl_cache, tt_local_entry);
  209. }
  210. /**
  211. * batadv_tt_local_entry_release() - release tt_local_entry from lists and queue
  212. * for free after rcu grace period
  213. * @ref: kref pointer of the nc_node
  214. */
  215. static void batadv_tt_local_entry_release(struct kref *ref)
  216. {
  217. struct batadv_tt_local_entry *tt_local_entry;
  218. tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
  219. common.refcount);
  220. batadv_softif_vlan_put(tt_local_entry->vlan);
  221. call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
  222. }
  223. /**
  224. * batadv_tt_local_entry_put() - decrement the tt_local_entry refcounter and
  225. * possibly release it
  226. * @tt_local_entry: tt_local_entry to be free'd
  227. */
  228. static void
  229. batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
  230. {
  231. kref_put(&tt_local_entry->common.refcount,
  232. batadv_tt_local_entry_release);
  233. }
  234. /**
  235. * batadv_tt_global_entry_free_rcu() - free the tt_global_entry
  236. * @rcu: rcu pointer of the tt_global_entry
  237. */
  238. static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
  239. {
  240. struct batadv_tt_global_entry *tt_global_entry;
  241. tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
  242. common.rcu);
  243. kmem_cache_free(batadv_tg_cache, tt_global_entry);
  244. }
  245. /**
  246. * batadv_tt_global_entry_release() - release tt_global_entry from lists and
  247. * queue for free after rcu grace period
  248. * @ref: kref pointer of the nc_node
  249. */
  250. static void batadv_tt_global_entry_release(struct kref *ref)
  251. {
  252. struct batadv_tt_global_entry *tt_global_entry;
  253. tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
  254. common.refcount);
  255. batadv_tt_global_del_orig_list(tt_global_entry);
  256. call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
  257. }
  258. /**
  259. * batadv_tt_global_entry_put() - decrement the tt_global_entry refcounter and
  260. * possibly release it
  261. * @tt_global_entry: tt_global_entry to be free'd
  262. */
  263. static void
  264. batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
  265. {
  266. kref_put(&tt_global_entry->common.refcount,
  267. batadv_tt_global_entry_release);
  268. }
  269. /**
  270. * batadv_tt_global_hash_count() - count the number of orig entries
  271. * @bat_priv: the bat priv with all the soft interface information
  272. * @addr: the mac address of the client to count entries for
  273. * @vid: VLAN identifier
  274. *
  275. * Return: the number of originators advertising the given address/data
  276. * (excluding ourself).
  277. */
  278. int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
  279. const u8 *addr, unsigned short vid)
  280. {
  281. struct batadv_tt_global_entry *tt_global_entry;
  282. int count;
  283. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  284. if (!tt_global_entry)
  285. return 0;
  286. count = atomic_read(&tt_global_entry->orig_list_count);
  287. batadv_tt_global_entry_put(tt_global_entry);
  288. return count;
  289. }
  290. /**
  291. * batadv_tt_local_size_mod() - change the size by v of the local table
  292. * identified by vid
  293. * @bat_priv: the bat priv with all the soft interface information
  294. * @vid: the VLAN identifier of the sub-table to change
  295. * @v: the amount to sum to the local table size
  296. */
  297. static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
  298. unsigned short vid, int v)
  299. {
  300. struct batadv_softif_vlan *vlan;
  301. vlan = batadv_softif_vlan_get(bat_priv, vid);
  302. if (!vlan)
  303. return;
  304. atomic_add(v, &vlan->tt.num_entries);
  305. batadv_softif_vlan_put(vlan);
  306. }
  307. /**
  308. * batadv_tt_local_size_inc() - increase by one the local table size for the
  309. * given vid
  310. * @bat_priv: the bat priv with all the soft interface information
  311. * @vid: the VLAN identifier
  312. */
  313. static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
  314. unsigned short vid)
  315. {
  316. batadv_tt_local_size_mod(bat_priv, vid, 1);
  317. }
  318. /**
  319. * batadv_tt_local_size_dec() - decrease by one the local table size for the
  320. * given vid
  321. * @bat_priv: the bat priv with all the soft interface information
  322. * @vid: the VLAN identifier
  323. */
  324. static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
  325. unsigned short vid)
  326. {
  327. batadv_tt_local_size_mod(bat_priv, vid, -1);
  328. }
  329. /**
  330. * batadv_tt_global_size_mod() - change the size by v of the global table
  331. * for orig_node identified by vid
  332. * @orig_node: the originator for which the table has to be modified
  333. * @vid: the VLAN identifier
  334. * @v: the amount to sum to the global table size
  335. */
  336. static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
  337. unsigned short vid, int v)
  338. {
  339. struct batadv_orig_node_vlan *vlan;
  340. vlan = batadv_orig_node_vlan_new(orig_node, vid);
  341. if (!vlan)
  342. return;
  343. if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
  344. spin_lock_bh(&orig_node->vlan_list_lock);
  345. if (!hlist_unhashed(&vlan->list)) {
  346. hlist_del_init_rcu(&vlan->list);
  347. batadv_orig_node_vlan_put(vlan);
  348. }
  349. spin_unlock_bh(&orig_node->vlan_list_lock);
  350. }
  351. batadv_orig_node_vlan_put(vlan);
  352. }
  353. /**
  354. * batadv_tt_global_size_inc() - increase by one the global table size for the
  355. * given vid
  356. * @orig_node: the originator which global table size has to be decreased
  357. * @vid: the vlan identifier
  358. */
  359. static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
  360. unsigned short vid)
  361. {
  362. batadv_tt_global_size_mod(orig_node, vid, 1);
  363. }
  364. /**
  365. * batadv_tt_global_size_dec() - decrease by one the global table size for the
  366. * given vid
  367. * @orig_node: the originator which global table size has to be decreased
  368. * @vid: the vlan identifier
  369. */
  370. static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
  371. unsigned short vid)
  372. {
  373. batadv_tt_global_size_mod(orig_node, vid, -1);
  374. }
  375. /**
  376. * batadv_tt_orig_list_entry_free_rcu() - free the orig_entry
  377. * @rcu: rcu pointer of the orig_entry
  378. */
  379. static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
  380. {
  381. struct batadv_tt_orig_list_entry *orig_entry;
  382. orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
  383. kmem_cache_free(batadv_tt_orig_cache, orig_entry);
  384. }
  385. /**
  386. * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
  387. * queue for free after rcu grace period
  388. * @ref: kref pointer of the tt orig entry
  389. */
  390. static void batadv_tt_orig_list_entry_release(struct kref *ref)
  391. {
  392. struct batadv_tt_orig_list_entry *orig_entry;
  393. orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
  394. refcount);
  395. batadv_orig_node_put(orig_entry->orig_node);
  396. call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
  397. }
  398. /**
  399. * batadv_tt_orig_list_entry_put() - decrement the tt orig entry refcounter and
  400. * possibly release it
  401. * @orig_entry: tt orig entry to be free'd
  402. */
  403. static void
  404. batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
  405. {
  406. kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
  407. }
  408. /**
  409. * batadv_tt_local_event() - store a local TT event (ADD/DEL)
  410. * @bat_priv: the bat priv with all the soft interface information
  411. * @tt_local_entry: the TT entry involved in the event
  412. * @event_flags: flags to store in the event structure
  413. */
  414. static void batadv_tt_local_event(struct batadv_priv *bat_priv,
  415. struct batadv_tt_local_entry *tt_local_entry,
  416. u8 event_flags)
  417. {
  418. struct batadv_tt_change_node *tt_change_node, *entry, *safe;
  419. struct batadv_tt_common_entry *common = &tt_local_entry->common;
  420. u8 flags = common->flags | event_flags;
  421. bool event_removed = false;
  422. bool del_op_requested, del_op_entry;
  423. tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
  424. if (!tt_change_node)
  425. return;
  426. tt_change_node->change.flags = flags;
  427. memset(tt_change_node->change.reserved, 0,
  428. sizeof(tt_change_node->change.reserved));
  429. ether_addr_copy(tt_change_node->change.addr, common->addr);
  430. tt_change_node->change.vid = htons(common->vid);
  431. del_op_requested = flags & BATADV_TT_CLIENT_DEL;
  432. /* check for ADD+DEL or DEL+ADD events */
  433. spin_lock_bh(&bat_priv->tt.changes_list_lock);
  434. list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
  435. list) {
  436. if (!batadv_compare_eth(entry->change.addr, common->addr))
  437. continue;
  438. /* DEL+ADD in the same orig interval have no effect and can be
  439. * removed to avoid silly behaviour on the receiver side. The
  440. * other way around (ADD+DEL) can happen in case of roaming of
  441. * a client still in the NEW state. Roaming of NEW clients is
  442. * now possible due to automatically recognition of "temporary"
  443. * clients
  444. */
  445. del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
  446. if (!del_op_requested && del_op_entry)
  447. goto del;
  448. if (del_op_requested && !del_op_entry)
  449. goto del;
  450. /* this is a second add in the same originator interval. It
  451. * means that flags have been changed: update them!
  452. */
  453. if (!del_op_requested && !del_op_entry)
  454. entry->change.flags = flags;
  455. continue;
  456. del:
  457. list_del(&entry->list);
  458. kmem_cache_free(batadv_tt_change_cache, entry);
  459. kmem_cache_free(batadv_tt_change_cache, tt_change_node);
  460. event_removed = true;
  461. goto unlock;
  462. }
  463. /* track the change in the OGMinterval list */
  464. list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
  465. unlock:
  466. spin_unlock_bh(&bat_priv->tt.changes_list_lock);
  467. if (event_removed)
  468. atomic_dec(&bat_priv->tt.local_changes);
  469. else
  470. atomic_inc(&bat_priv->tt.local_changes);
  471. }
  472. /**
  473. * batadv_tt_len() - compute length in bytes of given number of tt changes
  474. * @changes_num: number of tt changes
  475. *
  476. * Return: computed length in bytes.
  477. */
  478. static int batadv_tt_len(int changes_num)
  479. {
  480. return changes_num * sizeof(struct batadv_tvlv_tt_change);
  481. }
  482. /**
  483. * batadv_tt_entries() - compute the number of entries fitting in tt_len bytes
  484. * @tt_len: available space
  485. *
  486. * Return: the number of entries.
  487. */
  488. static u16 batadv_tt_entries(u16 tt_len)
  489. {
  490. return tt_len / batadv_tt_len(1);
  491. }
  492. /**
  493. * batadv_tt_local_table_transmit_size() - calculates the local translation
  494. * table size when transmitted over the air
  495. * @bat_priv: the bat priv with all the soft interface information
  496. *
  497. * Return: local translation table size in bytes.
  498. */
  499. static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
  500. {
  501. u16 num_vlan = 0;
  502. u16 tt_local_entries = 0;
  503. struct batadv_softif_vlan *vlan;
  504. int hdr_size;
  505. rcu_read_lock();
  506. hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
  507. num_vlan++;
  508. tt_local_entries += atomic_read(&vlan->tt.num_entries);
  509. }
  510. rcu_read_unlock();
  511. /* header size of tvlv encapsulated tt response payload */
  512. hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
  513. hdr_size += sizeof(struct batadv_tvlv_hdr);
  514. hdr_size += sizeof(struct batadv_tvlv_tt_data);
  515. hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
  516. return hdr_size + batadv_tt_len(tt_local_entries);
  517. }
  518. static int batadv_tt_local_init(struct batadv_priv *bat_priv)
  519. {
  520. if (bat_priv->tt.local_hash)
  521. return 0;
  522. bat_priv->tt.local_hash = batadv_hash_new(1024);
  523. if (!bat_priv->tt.local_hash)
  524. return -ENOMEM;
  525. batadv_hash_set_lock_class(bat_priv->tt.local_hash,
  526. &batadv_tt_local_hash_lock_class_key);
  527. return 0;
  528. }
  529. static void batadv_tt_global_free(struct batadv_priv *bat_priv,
  530. struct batadv_tt_global_entry *tt_global,
  531. const char *message)
  532. {
  533. struct batadv_tt_global_entry *tt_removed_entry;
  534. struct hlist_node *tt_removed_node;
  535. batadv_dbg(BATADV_DBG_TT, bat_priv,
  536. "Deleting global tt entry %pM (vid: %d): %s\n",
  537. tt_global->common.addr,
  538. batadv_print_vid(tt_global->common.vid), message);
  539. tt_removed_node = batadv_hash_remove(bat_priv->tt.global_hash,
  540. batadv_compare_tt,
  541. batadv_choose_tt,
  542. &tt_global->common);
  543. if (!tt_removed_node)
  544. return;
  545. /* drop reference of remove hash entry */
  546. tt_removed_entry = hlist_entry(tt_removed_node,
  547. struct batadv_tt_global_entry,
  548. common.hash_entry);
  549. batadv_tt_global_entry_put(tt_removed_entry);
  550. }
  551. /**
  552. * batadv_tt_local_add() - add a new client to the local table or update an
  553. * existing client
  554. * @soft_iface: netdev struct of the mesh interface
  555. * @addr: the mac address of the client to add
  556. * @vid: VLAN identifier
  557. * @ifindex: index of the interface where the client is connected to (useful to
  558. * identify wireless clients)
  559. * @mark: the value contained in the skb->mark field of the received packet (if
  560. * any)
  561. *
  562. * Return: true if the client was successfully added, false otherwise.
  563. */
  564. bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
  565. unsigned short vid, int ifindex, u32 mark)
  566. {
  567. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  568. struct batadv_tt_local_entry *tt_local;
  569. struct batadv_tt_global_entry *tt_global = NULL;
  570. struct net *net = dev_net(soft_iface);
  571. struct batadv_softif_vlan *vlan;
  572. struct net_device *in_dev = NULL;
  573. struct batadv_hard_iface *in_hardif = NULL;
  574. struct hlist_head *head;
  575. struct batadv_tt_orig_list_entry *orig_entry;
  576. int hash_added, table_size, packet_size_max;
  577. bool ret = false;
  578. bool roamed_back = false;
  579. u8 remote_flags;
  580. u32 match_mark;
  581. if (ifindex != BATADV_NULL_IFINDEX)
  582. in_dev = dev_get_by_index(net, ifindex);
  583. if (in_dev)
  584. in_hardif = batadv_hardif_get_by_netdev(in_dev);
  585. tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
  586. if (!is_multicast_ether_addr(addr))
  587. tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
  588. if (tt_local) {
  589. tt_local->last_seen = jiffies;
  590. if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
  591. batadv_dbg(BATADV_DBG_TT, bat_priv,
  592. "Re-adding pending client %pM (vid: %d)\n",
  593. addr, batadv_print_vid(vid));
  594. /* whatever the reason why the PENDING flag was set,
  595. * this is a client which was enqueued to be removed in
  596. * this orig_interval. Since it popped up again, the
  597. * flag can be reset like it was never enqueued
  598. */
  599. tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
  600. goto add_event;
  601. }
  602. if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
  603. batadv_dbg(BATADV_DBG_TT, bat_priv,
  604. "Roaming client %pM (vid: %d) came back to its original location\n",
  605. addr, batadv_print_vid(vid));
  606. /* the ROAM flag is set because this client roamed away
  607. * and the node got a roaming_advertisement message. Now
  608. * that the client popped up again at its original
  609. * location such flag can be unset
  610. */
  611. tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
  612. roamed_back = true;
  613. }
  614. goto check_roaming;
  615. }
  616. /* Ignore the client if we cannot send it in a full table response. */
  617. table_size = batadv_tt_local_table_transmit_size(bat_priv);
  618. table_size += batadv_tt_len(1);
  619. packet_size_max = atomic_read(&bat_priv->packet_size_max);
  620. if (table_size > packet_size_max) {
  621. net_ratelimited_function(batadv_info, soft_iface,
  622. "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
  623. table_size, packet_size_max, addr);
  624. goto out;
  625. }
  626. tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
  627. if (!tt_local)
  628. goto out;
  629. /* increase the refcounter of the related vlan */
  630. vlan = batadv_softif_vlan_get(bat_priv, vid);
  631. if (!vlan) {
  632. net_ratelimited_function(batadv_info, soft_iface,
  633. "adding TT local entry %pM to non-existent VLAN %d\n",
  634. addr, batadv_print_vid(vid));
  635. kmem_cache_free(batadv_tl_cache, tt_local);
  636. tt_local = NULL;
  637. goto out;
  638. }
  639. batadv_dbg(BATADV_DBG_TT, bat_priv,
  640. "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
  641. addr, batadv_print_vid(vid),
  642. (u8)atomic_read(&bat_priv->tt.vn));
  643. ether_addr_copy(tt_local->common.addr, addr);
  644. /* The local entry has to be marked as NEW to avoid to send it in
  645. * a full table response going out before the next ttvn increment
  646. * (consistency check)
  647. */
  648. tt_local->common.flags = BATADV_TT_CLIENT_NEW;
  649. tt_local->common.vid = vid;
  650. if (batadv_is_wifi_hardif(in_hardif))
  651. tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
  652. kref_init(&tt_local->common.refcount);
  653. tt_local->last_seen = jiffies;
  654. tt_local->common.added_at = tt_local->last_seen;
  655. tt_local->vlan = vlan;
  656. /* the batman interface mac and multicast addresses should never be
  657. * purged
  658. */
  659. if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
  660. is_multicast_ether_addr(addr))
  661. tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
  662. kref_get(&tt_local->common.refcount);
  663. hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
  664. batadv_choose_tt, &tt_local->common,
  665. &tt_local->common.hash_entry);
  666. if (unlikely(hash_added != 0)) {
  667. /* remove the reference for the hash */
  668. batadv_tt_local_entry_put(tt_local);
  669. goto out;
  670. }
  671. add_event:
  672. batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
  673. check_roaming:
  674. /* Check whether it is a roaming, but don't do anything if the roaming
  675. * process has already been handled
  676. */
  677. if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
  678. /* These node are probably going to update their tt table */
  679. head = &tt_global->orig_list;
  680. rcu_read_lock();
  681. hlist_for_each_entry_rcu(orig_entry, head, list) {
  682. batadv_send_roam_adv(bat_priv, tt_global->common.addr,
  683. tt_global->common.vid,
  684. orig_entry->orig_node);
  685. }
  686. rcu_read_unlock();
  687. if (roamed_back) {
  688. batadv_tt_global_free(bat_priv, tt_global,
  689. "Roaming canceled");
  690. tt_global = NULL;
  691. } else {
  692. /* The global entry has to be marked as ROAMING and
  693. * has to be kept for consistency purpose
  694. */
  695. tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
  696. tt_global->roam_at = jiffies;
  697. }
  698. }
  699. /* store the current remote flags before altering them. This helps
  700. * understanding is flags are changing or not
  701. */
  702. remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
  703. if (batadv_is_wifi_hardif(in_hardif))
  704. tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
  705. else
  706. tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
  707. /* check the mark in the skb: if it's equal to the configured
  708. * isolation_mark, it means the packet is coming from an isolated
  709. * non-mesh client
  710. */
  711. match_mark = (mark & bat_priv->isolation_mark_mask);
  712. if (bat_priv->isolation_mark_mask &&
  713. match_mark == bat_priv->isolation_mark)
  714. tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
  715. else
  716. tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
  717. /* if any "dynamic" flag has been modified, resend an ADD event for this
  718. * entry so that all the nodes can get the new flags
  719. */
  720. if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
  721. batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
  722. ret = true;
  723. out:
  724. if (in_hardif)
  725. batadv_hardif_put(in_hardif);
  726. if (in_dev)
  727. dev_put(in_dev);
  728. if (tt_local)
  729. batadv_tt_local_entry_put(tt_local);
  730. if (tt_global)
  731. batadv_tt_global_entry_put(tt_global);
  732. return ret;
  733. }
  734. /**
  735. * batadv_tt_prepare_tvlv_global_data() - prepare the TVLV TT header to send
  736. * within a TT Response directed to another node
  737. * @orig_node: originator for which the TT data has to be prepared
  738. * @tt_data: uninitialised pointer to the address of the TVLV buffer
  739. * @tt_change: uninitialised pointer to the address of the area where the TT
  740. * changed can be stored
  741. * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
  742. * function reserves the amount of space needed to send the entire global TT
  743. * table. In case of success the value is updated with the real amount of
  744. * reserved bytes
  745. * Allocate the needed amount of memory for the entire TT TVLV and write its
  746. * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
  747. * objects, one per active VLAN served by the originator node.
  748. *
  749. * Return: the size of the allocated buffer or 0 in case of failure.
  750. */
  751. static u16
  752. batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
  753. struct batadv_tvlv_tt_data **tt_data,
  754. struct batadv_tvlv_tt_change **tt_change,
  755. s32 *tt_len)
  756. {
  757. u16 num_vlan = 0;
  758. u16 num_entries = 0;
  759. u16 change_offset;
  760. u16 tvlv_len;
  761. struct batadv_tvlv_tt_vlan_data *tt_vlan;
  762. struct batadv_orig_node_vlan *vlan;
  763. u8 *tt_change_ptr;
  764. spin_lock_bh(&orig_node->vlan_list_lock);
  765. hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
  766. num_vlan++;
  767. num_entries += atomic_read(&vlan->tt.num_entries);
  768. }
  769. change_offset = sizeof(**tt_data);
  770. change_offset += num_vlan * sizeof(*tt_vlan);
  771. /* if tt_len is negative, allocate the space needed by the full table */
  772. if (*tt_len < 0)
  773. *tt_len = batadv_tt_len(num_entries);
  774. tvlv_len = *tt_len;
  775. tvlv_len += change_offset;
  776. *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
  777. if (!*tt_data) {
  778. *tt_len = 0;
  779. goto out;
  780. }
  781. (*tt_data)->flags = BATADV_NO_FLAGS;
  782. (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
  783. (*tt_data)->num_vlan = htons(num_vlan);
  784. tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
  785. hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
  786. tt_vlan->vid = htons(vlan->vid);
  787. tt_vlan->crc = htonl(vlan->tt.crc);
  788. tt_vlan++;
  789. }
  790. tt_change_ptr = (u8 *)*tt_data + change_offset;
  791. *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
  792. out:
  793. spin_unlock_bh(&orig_node->vlan_list_lock);
  794. return tvlv_len;
  795. }
  796. /**
  797. * batadv_tt_prepare_tvlv_local_data() - allocate and prepare the TT TVLV for
  798. * this node
  799. * @bat_priv: the bat priv with all the soft interface information
  800. * @tt_data: uninitialised pointer to the address of the TVLV buffer
  801. * @tt_change: uninitialised pointer to the address of the area where the TT
  802. * changes can be stored
  803. * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
  804. * function reserves the amount of space needed to send the entire local TT
  805. * table. In case of success the value is updated with the real amount of
  806. * reserved bytes
  807. *
  808. * Allocate the needed amount of memory for the entire TT TVLV and write its
  809. * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
  810. * objects, one per active VLAN.
  811. *
  812. * Return: the size of the allocated buffer or 0 in case of failure.
  813. */
  814. static u16
  815. batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
  816. struct batadv_tvlv_tt_data **tt_data,
  817. struct batadv_tvlv_tt_change **tt_change,
  818. s32 *tt_len)
  819. {
  820. struct batadv_tvlv_tt_vlan_data *tt_vlan;
  821. struct batadv_softif_vlan *vlan;
  822. u16 num_vlan = 0;
  823. u16 vlan_entries = 0;
  824. u16 total_entries = 0;
  825. u16 tvlv_len;
  826. u8 *tt_change_ptr;
  827. int change_offset;
  828. spin_lock_bh(&bat_priv->softif_vlan_list_lock);
  829. hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
  830. vlan_entries = atomic_read(&vlan->tt.num_entries);
  831. if (vlan_entries < 1)
  832. continue;
  833. num_vlan++;
  834. total_entries += vlan_entries;
  835. }
  836. change_offset = sizeof(**tt_data);
  837. change_offset += num_vlan * sizeof(*tt_vlan);
  838. /* if tt_len is negative, allocate the space needed by the full table */
  839. if (*tt_len < 0)
  840. *tt_len = batadv_tt_len(total_entries);
  841. tvlv_len = *tt_len;
  842. tvlv_len += change_offset;
  843. *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
  844. if (!*tt_data) {
  845. tvlv_len = 0;
  846. goto out;
  847. }
  848. (*tt_data)->flags = BATADV_NO_FLAGS;
  849. (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
  850. (*tt_data)->num_vlan = htons(num_vlan);
  851. tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
  852. hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
  853. vlan_entries = atomic_read(&vlan->tt.num_entries);
  854. if (vlan_entries < 1)
  855. continue;
  856. tt_vlan->vid = htons(vlan->vid);
  857. tt_vlan->crc = htonl(vlan->tt.crc);
  858. tt_vlan++;
  859. }
  860. tt_change_ptr = (u8 *)*tt_data + change_offset;
  861. *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
  862. out:
  863. spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
  864. return tvlv_len;
  865. }
  866. /**
  867. * batadv_tt_tvlv_container_update() - update the translation table tvlv
  868. * container after local tt changes have been committed
  869. * @bat_priv: the bat priv with all the soft interface information
  870. */
  871. static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
  872. {
  873. struct batadv_tt_change_node *entry, *safe;
  874. struct batadv_tvlv_tt_data *tt_data;
  875. struct batadv_tvlv_tt_change *tt_change;
  876. int tt_diff_len, tt_change_len = 0;
  877. int tt_diff_entries_num = 0;
  878. int tt_diff_entries_count = 0;
  879. u16 tvlv_len;
  880. tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
  881. tt_diff_len = batadv_tt_len(tt_diff_entries_num);
  882. /* if we have too many changes for one packet don't send any
  883. * and wait for the tt table request which will be fragmented
  884. */
  885. if (tt_diff_len > bat_priv->soft_iface->mtu)
  886. tt_diff_len = 0;
  887. tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
  888. &tt_change, &tt_diff_len);
  889. if (!tvlv_len)
  890. return;
  891. tt_data->flags = BATADV_TT_OGM_DIFF;
  892. if (tt_diff_len == 0)
  893. goto container_register;
  894. spin_lock_bh(&bat_priv->tt.changes_list_lock);
  895. atomic_set(&bat_priv->tt.local_changes, 0);
  896. list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
  897. list) {
  898. if (tt_diff_entries_count < tt_diff_entries_num) {
  899. memcpy(tt_change + tt_diff_entries_count,
  900. &entry->change,
  901. sizeof(struct batadv_tvlv_tt_change));
  902. tt_diff_entries_count++;
  903. }
  904. list_del(&entry->list);
  905. kmem_cache_free(batadv_tt_change_cache, entry);
  906. }
  907. spin_unlock_bh(&bat_priv->tt.changes_list_lock);
  908. /* Keep the buffer for possible tt_request */
  909. spin_lock_bh(&bat_priv->tt.last_changeset_lock);
  910. kfree(bat_priv->tt.last_changeset);
  911. bat_priv->tt.last_changeset_len = 0;
  912. bat_priv->tt.last_changeset = NULL;
  913. tt_change_len = batadv_tt_len(tt_diff_entries_count);
  914. /* check whether this new OGM has no changes due to size problems */
  915. if (tt_diff_entries_count > 0) {
  916. /* if kmalloc() fails we will reply with the full table
  917. * instead of providing the diff
  918. */
  919. bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
  920. if (bat_priv->tt.last_changeset) {
  921. memcpy(bat_priv->tt.last_changeset,
  922. tt_change, tt_change_len);
  923. bat_priv->tt.last_changeset_len = tt_diff_len;
  924. }
  925. }
  926. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  927. container_register:
  928. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
  929. tvlv_len);
  930. kfree(tt_data);
  931. }
  932. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  933. /**
  934. * batadv_tt_local_seq_print_text() - Print the local tt table in a seq file
  935. * @seq: seq file to print on
  936. * @offset: not used
  937. *
  938. * Return: always 0
  939. */
  940. int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
  941. {
  942. struct net_device *net_dev = (struct net_device *)seq->private;
  943. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  944. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  945. struct batadv_tt_common_entry *tt_common_entry;
  946. struct batadv_tt_local_entry *tt_local;
  947. struct batadv_hard_iface *primary_if;
  948. struct hlist_head *head;
  949. u32 i;
  950. int last_seen_secs;
  951. int last_seen_msecs;
  952. unsigned long last_seen_jiffies;
  953. bool no_purge;
  954. u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
  955. primary_if = batadv_seq_print_text_primary_if_get(seq);
  956. if (!primary_if)
  957. goto out;
  958. seq_printf(seq,
  959. "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
  960. net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
  961. seq_puts(seq,
  962. " Client VID Flags Last seen (CRC )\n");
  963. for (i = 0; i < hash->size; i++) {
  964. head = &hash->table[i];
  965. rcu_read_lock();
  966. hlist_for_each_entry_rcu(tt_common_entry,
  967. head, hash_entry) {
  968. tt_local = container_of(tt_common_entry,
  969. struct batadv_tt_local_entry,
  970. common);
  971. last_seen_jiffies = jiffies - tt_local->last_seen;
  972. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  973. last_seen_secs = last_seen_msecs / 1000;
  974. last_seen_msecs = last_seen_msecs % 1000;
  975. no_purge = tt_common_entry->flags & np_flag;
  976. seq_printf(seq,
  977. " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
  978. tt_common_entry->addr,
  979. batadv_print_vid(tt_common_entry->vid),
  980. ((tt_common_entry->flags &
  981. BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
  982. no_purge ? 'P' : '.',
  983. ((tt_common_entry->flags &
  984. BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
  985. ((tt_common_entry->flags &
  986. BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
  987. ((tt_common_entry->flags &
  988. BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
  989. ((tt_common_entry->flags &
  990. BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
  991. no_purge ? 0 : last_seen_secs,
  992. no_purge ? 0 : last_seen_msecs,
  993. tt_local->vlan->tt.crc);
  994. }
  995. rcu_read_unlock();
  996. }
  997. out:
  998. if (primary_if)
  999. batadv_hardif_put(primary_if);
  1000. return 0;
  1001. }
  1002. #endif
  1003. /**
  1004. * batadv_tt_local_dump_entry() - Dump one TT local entry into a message
  1005. * @msg :Netlink message to dump into
  1006. * @portid: Port making netlink request
  1007. * @seq: Sequence number of netlink message
  1008. * @bat_priv: The bat priv with all the soft interface information
  1009. * @common: tt local & tt global common data
  1010. *
  1011. * Return: Error code, or 0 on success
  1012. */
  1013. static int
  1014. batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  1015. struct batadv_priv *bat_priv,
  1016. struct batadv_tt_common_entry *common)
  1017. {
  1018. void *hdr;
  1019. struct batadv_softif_vlan *vlan;
  1020. struct batadv_tt_local_entry *local;
  1021. unsigned int last_seen_msecs;
  1022. u32 crc;
  1023. local = container_of(common, struct batadv_tt_local_entry, common);
  1024. last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
  1025. vlan = batadv_softif_vlan_get(bat_priv, common->vid);
  1026. if (!vlan)
  1027. return 0;
  1028. crc = vlan->tt.crc;
  1029. batadv_softif_vlan_put(vlan);
  1030. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1031. NLM_F_MULTI,
  1032. BATADV_CMD_GET_TRANSTABLE_LOCAL);
  1033. if (!hdr)
  1034. return -ENOBUFS;
  1035. if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
  1036. nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
  1037. nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
  1038. nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
  1039. goto nla_put_failure;
  1040. if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
  1041. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
  1042. goto nla_put_failure;
  1043. genlmsg_end(msg, hdr);
  1044. return 0;
  1045. nla_put_failure:
  1046. genlmsg_cancel(msg, hdr);
  1047. return -EMSGSIZE;
  1048. }
  1049. /**
  1050. * batadv_tt_local_dump_bucket() - Dump one TT local bucket into a message
  1051. * @msg: Netlink message to dump into
  1052. * @portid: Port making netlink request
  1053. * @seq: Sequence number of netlink message
  1054. * @bat_priv: The bat priv with all the soft interface information
  1055. * @head: Pointer to the list containing the local tt entries
  1056. * @idx_s: Number of entries to skip
  1057. *
  1058. * Return: Error code, or 0 on success
  1059. */
  1060. static int
  1061. batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  1062. struct batadv_priv *bat_priv,
  1063. struct hlist_head *head, int *idx_s)
  1064. {
  1065. struct batadv_tt_common_entry *common;
  1066. int idx = 0;
  1067. rcu_read_lock();
  1068. hlist_for_each_entry_rcu(common, head, hash_entry) {
  1069. if (idx++ < *idx_s)
  1070. continue;
  1071. if (batadv_tt_local_dump_entry(msg, portid, seq, bat_priv,
  1072. common)) {
  1073. rcu_read_unlock();
  1074. *idx_s = idx - 1;
  1075. return -EMSGSIZE;
  1076. }
  1077. }
  1078. rcu_read_unlock();
  1079. *idx_s = 0;
  1080. return 0;
  1081. }
  1082. /**
  1083. * batadv_tt_local_dump() - Dump TT local entries into a message
  1084. * @msg: Netlink message to dump into
  1085. * @cb: Parameters from query
  1086. *
  1087. * Return: Error code, or 0 on success
  1088. */
  1089. int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
  1090. {
  1091. struct net *net = sock_net(cb->skb->sk);
  1092. struct net_device *soft_iface;
  1093. struct batadv_priv *bat_priv;
  1094. struct batadv_hard_iface *primary_if = NULL;
  1095. struct batadv_hashtable *hash;
  1096. struct hlist_head *head;
  1097. int ret;
  1098. int ifindex;
  1099. int bucket = cb->args[0];
  1100. int idx = cb->args[1];
  1101. int portid = NETLINK_CB(cb->skb).portid;
  1102. ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
  1103. if (!ifindex)
  1104. return -EINVAL;
  1105. soft_iface = dev_get_by_index(net, ifindex);
  1106. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  1107. ret = -ENODEV;
  1108. goto out;
  1109. }
  1110. bat_priv = netdev_priv(soft_iface);
  1111. primary_if = batadv_primary_if_get_selected(bat_priv);
  1112. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  1113. ret = -ENOENT;
  1114. goto out;
  1115. }
  1116. hash = bat_priv->tt.local_hash;
  1117. while (bucket < hash->size) {
  1118. head = &hash->table[bucket];
  1119. if (batadv_tt_local_dump_bucket(msg, portid, cb->nlh->nlmsg_seq,
  1120. bat_priv, head, &idx))
  1121. break;
  1122. bucket++;
  1123. }
  1124. ret = msg->len;
  1125. out:
  1126. if (primary_if)
  1127. batadv_hardif_put(primary_if);
  1128. if (soft_iface)
  1129. dev_put(soft_iface);
  1130. cb->args[0] = bucket;
  1131. cb->args[1] = idx;
  1132. return ret;
  1133. }
  1134. static void
  1135. batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
  1136. struct batadv_tt_local_entry *tt_local_entry,
  1137. u16 flags, const char *message)
  1138. {
  1139. batadv_tt_local_event(bat_priv, tt_local_entry, flags);
  1140. /* The local client has to be marked as "pending to be removed" but has
  1141. * to be kept in the table in order to send it in a full table
  1142. * response issued before the net ttvn increment (consistency check)
  1143. */
  1144. tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
  1145. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1146. "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
  1147. tt_local_entry->common.addr,
  1148. batadv_print_vid(tt_local_entry->common.vid), message);
  1149. }
  1150. /**
  1151. * batadv_tt_local_remove() - logically remove an entry from the local table
  1152. * @bat_priv: the bat priv with all the soft interface information
  1153. * @addr: the MAC address of the client to remove
  1154. * @vid: VLAN identifier
  1155. * @message: message to append to the log on deletion
  1156. * @roaming: true if the deletion is due to a roaming event
  1157. *
  1158. * Return: the flags assigned to the local entry before being deleted
  1159. */
  1160. u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
  1161. unsigned short vid, const char *message,
  1162. bool roaming)
  1163. {
  1164. struct batadv_tt_local_entry *tt_removed_entry;
  1165. struct batadv_tt_local_entry *tt_local_entry;
  1166. u16 flags, curr_flags = BATADV_NO_FLAGS;
  1167. struct hlist_node *tt_removed_node;
  1168. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
  1169. if (!tt_local_entry)
  1170. goto out;
  1171. curr_flags = tt_local_entry->common.flags;
  1172. flags = BATADV_TT_CLIENT_DEL;
  1173. /* if this global entry addition is due to a roaming, the node has to
  1174. * mark the local entry as "roamed" in order to correctly reroute
  1175. * packets later
  1176. */
  1177. if (roaming) {
  1178. flags |= BATADV_TT_CLIENT_ROAM;
  1179. /* mark the local client as ROAMed */
  1180. tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
  1181. }
  1182. if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
  1183. batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
  1184. message);
  1185. goto out;
  1186. }
  1187. /* if this client has been added right now, it is possible to
  1188. * immediately purge it
  1189. */
  1190. batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
  1191. tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
  1192. batadv_compare_tt,
  1193. batadv_choose_tt,
  1194. &tt_local_entry->common);
  1195. if (!tt_removed_node)
  1196. goto out;
  1197. /* drop reference of remove hash entry */
  1198. tt_removed_entry = hlist_entry(tt_removed_node,
  1199. struct batadv_tt_local_entry,
  1200. common.hash_entry);
  1201. batadv_tt_local_entry_put(tt_removed_entry);
  1202. out:
  1203. if (tt_local_entry)
  1204. batadv_tt_local_entry_put(tt_local_entry);
  1205. return curr_flags;
  1206. }
  1207. /**
  1208. * batadv_tt_local_purge_list() - purge inactive tt local entries
  1209. * @bat_priv: the bat priv with all the soft interface information
  1210. * @head: pointer to the list containing the local tt entries
  1211. * @timeout: parameter deciding whether a given tt local entry is considered
  1212. * inactive or not
  1213. */
  1214. static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
  1215. struct hlist_head *head,
  1216. int timeout)
  1217. {
  1218. struct batadv_tt_local_entry *tt_local_entry;
  1219. struct batadv_tt_common_entry *tt_common_entry;
  1220. struct hlist_node *node_tmp;
  1221. hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
  1222. hash_entry) {
  1223. tt_local_entry = container_of(tt_common_entry,
  1224. struct batadv_tt_local_entry,
  1225. common);
  1226. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
  1227. continue;
  1228. /* entry already marked for deletion */
  1229. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
  1230. continue;
  1231. if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
  1232. continue;
  1233. batadv_tt_local_set_pending(bat_priv, tt_local_entry,
  1234. BATADV_TT_CLIENT_DEL, "timed out");
  1235. }
  1236. }
  1237. /**
  1238. * batadv_tt_local_purge() - purge inactive tt local entries
  1239. * @bat_priv: the bat priv with all the soft interface information
  1240. * @timeout: parameter deciding whether a given tt local entry is considered
  1241. * inactive or not
  1242. */
  1243. static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
  1244. int timeout)
  1245. {
  1246. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  1247. struct hlist_head *head;
  1248. spinlock_t *list_lock; /* protects write access to the hash lists */
  1249. u32 i;
  1250. for (i = 0; i < hash->size; i++) {
  1251. head = &hash->table[i];
  1252. list_lock = &hash->list_locks[i];
  1253. spin_lock_bh(list_lock);
  1254. batadv_tt_local_purge_list(bat_priv, head, timeout);
  1255. spin_unlock_bh(list_lock);
  1256. }
  1257. }
  1258. static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
  1259. {
  1260. struct batadv_hashtable *hash;
  1261. spinlock_t *list_lock; /* protects write access to the hash lists */
  1262. struct batadv_tt_common_entry *tt_common_entry;
  1263. struct batadv_tt_local_entry *tt_local;
  1264. struct hlist_node *node_tmp;
  1265. struct hlist_head *head;
  1266. u32 i;
  1267. if (!bat_priv->tt.local_hash)
  1268. return;
  1269. hash = bat_priv->tt.local_hash;
  1270. for (i = 0; i < hash->size; i++) {
  1271. head = &hash->table[i];
  1272. list_lock = &hash->list_locks[i];
  1273. spin_lock_bh(list_lock);
  1274. hlist_for_each_entry_safe(tt_common_entry, node_tmp,
  1275. head, hash_entry) {
  1276. hlist_del_rcu(&tt_common_entry->hash_entry);
  1277. tt_local = container_of(tt_common_entry,
  1278. struct batadv_tt_local_entry,
  1279. common);
  1280. batadv_tt_local_entry_put(tt_local);
  1281. }
  1282. spin_unlock_bh(list_lock);
  1283. }
  1284. batadv_hash_destroy(hash);
  1285. bat_priv->tt.local_hash = NULL;
  1286. }
  1287. static int batadv_tt_global_init(struct batadv_priv *bat_priv)
  1288. {
  1289. if (bat_priv->tt.global_hash)
  1290. return 0;
  1291. bat_priv->tt.global_hash = batadv_hash_new(1024);
  1292. if (!bat_priv->tt.global_hash)
  1293. return -ENOMEM;
  1294. batadv_hash_set_lock_class(bat_priv->tt.global_hash,
  1295. &batadv_tt_global_hash_lock_class_key);
  1296. return 0;
  1297. }
  1298. static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
  1299. {
  1300. struct batadv_tt_change_node *entry, *safe;
  1301. spin_lock_bh(&bat_priv->tt.changes_list_lock);
  1302. list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
  1303. list) {
  1304. list_del(&entry->list);
  1305. kmem_cache_free(batadv_tt_change_cache, entry);
  1306. }
  1307. atomic_set(&bat_priv->tt.local_changes, 0);
  1308. spin_unlock_bh(&bat_priv->tt.changes_list_lock);
  1309. }
  1310. /**
  1311. * batadv_tt_global_orig_entry_find() - find a TT orig_list_entry
  1312. * @entry: the TT global entry where the orig_list_entry has to be
  1313. * extracted from
  1314. * @orig_node: the originator for which the orig_list_entry has to be found
  1315. *
  1316. * retrieve the orig_tt_list_entry belonging to orig_node from the
  1317. * batadv_tt_global_entry list
  1318. *
  1319. * Return: it with an increased refcounter, NULL if not found
  1320. */
  1321. static struct batadv_tt_orig_list_entry *
  1322. batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
  1323. const struct batadv_orig_node *orig_node)
  1324. {
  1325. struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
  1326. const struct hlist_head *head;
  1327. rcu_read_lock();
  1328. head = &entry->orig_list;
  1329. hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
  1330. if (tmp_orig_entry->orig_node != orig_node)
  1331. continue;
  1332. if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
  1333. continue;
  1334. orig_entry = tmp_orig_entry;
  1335. break;
  1336. }
  1337. rcu_read_unlock();
  1338. return orig_entry;
  1339. }
  1340. /**
  1341. * batadv_tt_global_entry_has_orig() - check if a TT global entry is also
  1342. * handled by a given originator
  1343. * @entry: the TT global entry to check
  1344. * @orig_node: the originator to search in the list
  1345. * @flags: a pointer to store TT flags for the given @entry received
  1346. * from @orig_node
  1347. *
  1348. * find out if an orig_node is already in the list of a tt_global_entry.
  1349. *
  1350. * Return: true if found, false otherwise
  1351. */
  1352. static bool
  1353. batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
  1354. const struct batadv_orig_node *orig_node,
  1355. u8 *flags)
  1356. {
  1357. struct batadv_tt_orig_list_entry *orig_entry;
  1358. bool found = false;
  1359. orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
  1360. if (orig_entry) {
  1361. found = true;
  1362. if (flags)
  1363. *flags = orig_entry->flags;
  1364. batadv_tt_orig_list_entry_put(orig_entry);
  1365. }
  1366. return found;
  1367. }
  1368. /**
  1369. * batadv_tt_global_sync_flags() - update TT sync flags
  1370. * @tt_global: the TT global entry to update sync flags in
  1371. *
  1372. * Updates the sync flag bits in the tt_global flag attribute with a logical
  1373. * OR of all sync flags from any of its TT orig entries.
  1374. */
  1375. static void
  1376. batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
  1377. {
  1378. struct batadv_tt_orig_list_entry *orig_entry;
  1379. const struct hlist_head *head;
  1380. u16 flags = BATADV_NO_FLAGS;
  1381. rcu_read_lock();
  1382. head = &tt_global->orig_list;
  1383. hlist_for_each_entry_rcu(orig_entry, head, list)
  1384. flags |= orig_entry->flags;
  1385. rcu_read_unlock();
  1386. flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
  1387. tt_global->common.flags = flags;
  1388. }
  1389. /**
  1390. * batadv_tt_global_orig_entry_add() - add or update a TT orig entry
  1391. * @tt_global: the TT global entry to add an orig entry in
  1392. * @orig_node: the originator to add an orig entry for
  1393. * @ttvn: translation table version number of this changeset
  1394. * @flags: TT sync flags
  1395. */
  1396. static void
  1397. batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
  1398. struct batadv_orig_node *orig_node, int ttvn,
  1399. u8 flags)
  1400. {
  1401. struct batadv_tt_orig_list_entry *orig_entry;
  1402. spin_lock_bh(&tt_global->list_lock);
  1403. orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
  1404. if (orig_entry) {
  1405. /* refresh the ttvn: the current value could be a bogus one that
  1406. * was added during a "temporary client detection"
  1407. */
  1408. orig_entry->ttvn = ttvn;
  1409. orig_entry->flags = flags;
  1410. goto sync_flags;
  1411. }
  1412. orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
  1413. if (!orig_entry)
  1414. goto out;
  1415. INIT_HLIST_NODE(&orig_entry->list);
  1416. kref_get(&orig_node->refcount);
  1417. batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
  1418. orig_entry->orig_node = orig_node;
  1419. orig_entry->ttvn = ttvn;
  1420. orig_entry->flags = flags;
  1421. kref_init(&orig_entry->refcount);
  1422. kref_get(&orig_entry->refcount);
  1423. hlist_add_head_rcu(&orig_entry->list,
  1424. &tt_global->orig_list);
  1425. atomic_inc(&tt_global->orig_list_count);
  1426. sync_flags:
  1427. batadv_tt_global_sync_flags(tt_global);
  1428. out:
  1429. if (orig_entry)
  1430. batadv_tt_orig_list_entry_put(orig_entry);
  1431. spin_unlock_bh(&tt_global->list_lock);
  1432. }
  1433. /**
  1434. * batadv_tt_global_add() - add a new TT global entry or update an existing one
  1435. * @bat_priv: the bat priv with all the soft interface information
  1436. * @orig_node: the originator announcing the client
  1437. * @tt_addr: the mac address of the non-mesh client
  1438. * @vid: VLAN identifier
  1439. * @flags: TT flags that have to be set for this non-mesh client
  1440. * @ttvn: the tt version number ever announcing this non-mesh client
  1441. *
  1442. * Add a new TT global entry for the given originator. If the entry already
  1443. * exists add a new reference to the given originator (a global entry can have
  1444. * references to multiple originators) and adjust the flags attribute to reflect
  1445. * the function argument.
  1446. * If a TT local entry exists for this non-mesh client remove it.
  1447. *
  1448. * The caller must hold orig_node refcount.
  1449. *
  1450. * Return: true if the new entry has been added, false otherwise
  1451. */
  1452. static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
  1453. struct batadv_orig_node *orig_node,
  1454. const unsigned char *tt_addr,
  1455. unsigned short vid, u16 flags, u8 ttvn)
  1456. {
  1457. struct batadv_tt_global_entry *tt_global_entry;
  1458. struct batadv_tt_local_entry *tt_local_entry;
  1459. bool ret = false;
  1460. int hash_added;
  1461. struct batadv_tt_common_entry *common;
  1462. u16 local_flags;
  1463. /* ignore global entries from backbone nodes */
  1464. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
  1465. return true;
  1466. tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
  1467. tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
  1468. /* if the node already has a local client for this entry, it has to wait
  1469. * for a roaming advertisement instead of manually messing up the global
  1470. * table
  1471. */
  1472. if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
  1473. !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
  1474. goto out;
  1475. if (!tt_global_entry) {
  1476. tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
  1477. GFP_ATOMIC);
  1478. if (!tt_global_entry)
  1479. goto out;
  1480. common = &tt_global_entry->common;
  1481. ether_addr_copy(common->addr, tt_addr);
  1482. common->vid = vid;
  1483. if (!is_multicast_ether_addr(common->addr))
  1484. common->flags = flags & (~BATADV_TT_SYNC_MASK);
  1485. tt_global_entry->roam_at = 0;
  1486. /* node must store current time in case of roaming. This is
  1487. * needed to purge this entry out on timeout (if nobody claims
  1488. * it)
  1489. */
  1490. if (flags & BATADV_TT_CLIENT_ROAM)
  1491. tt_global_entry->roam_at = jiffies;
  1492. kref_init(&common->refcount);
  1493. common->added_at = jiffies;
  1494. INIT_HLIST_HEAD(&tt_global_entry->orig_list);
  1495. atomic_set(&tt_global_entry->orig_list_count, 0);
  1496. spin_lock_init(&tt_global_entry->list_lock);
  1497. kref_get(&common->refcount);
  1498. hash_added = batadv_hash_add(bat_priv->tt.global_hash,
  1499. batadv_compare_tt,
  1500. batadv_choose_tt, common,
  1501. &common->hash_entry);
  1502. if (unlikely(hash_added != 0)) {
  1503. /* remove the reference for the hash */
  1504. batadv_tt_global_entry_put(tt_global_entry);
  1505. goto out_remove;
  1506. }
  1507. } else {
  1508. common = &tt_global_entry->common;
  1509. /* If there is already a global entry, we can use this one for
  1510. * our processing.
  1511. * But if we are trying to add a temporary client then here are
  1512. * two options at this point:
  1513. * 1) the global client is not a temporary client: the global
  1514. * client has to be left as it is, temporary information
  1515. * should never override any already known client state
  1516. * 2) the global client is a temporary client: purge the
  1517. * originator list and add the new one orig_entry
  1518. */
  1519. if (flags & BATADV_TT_CLIENT_TEMP) {
  1520. if (!(common->flags & BATADV_TT_CLIENT_TEMP))
  1521. goto out;
  1522. if (batadv_tt_global_entry_has_orig(tt_global_entry,
  1523. orig_node, NULL))
  1524. goto out_remove;
  1525. batadv_tt_global_del_orig_list(tt_global_entry);
  1526. goto add_orig_entry;
  1527. }
  1528. /* if the client was temporary added before receiving the first
  1529. * OGM announcing it, we have to clear the TEMP flag. Also,
  1530. * remove the previous temporary orig node and re-add it
  1531. * if required. If the orig entry changed, the new one which
  1532. * is a non-temporary entry is preferred.
  1533. */
  1534. if (common->flags & BATADV_TT_CLIENT_TEMP) {
  1535. batadv_tt_global_del_orig_list(tt_global_entry);
  1536. common->flags &= ~BATADV_TT_CLIENT_TEMP;
  1537. }
  1538. /* the change can carry possible "attribute" flags like the
  1539. * TT_CLIENT_TEMP, therefore they have to be copied in the
  1540. * client entry
  1541. */
  1542. if (!is_multicast_ether_addr(common->addr))
  1543. common->flags |= flags & (~BATADV_TT_SYNC_MASK);
  1544. /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
  1545. * one originator left in the list and we previously received a
  1546. * delete + roaming change for this originator.
  1547. *
  1548. * We should first delete the old originator before adding the
  1549. * new one.
  1550. */
  1551. if (common->flags & BATADV_TT_CLIENT_ROAM) {
  1552. batadv_tt_global_del_orig_list(tt_global_entry);
  1553. common->flags &= ~BATADV_TT_CLIENT_ROAM;
  1554. tt_global_entry->roam_at = 0;
  1555. }
  1556. }
  1557. add_orig_entry:
  1558. /* add the new orig_entry (if needed) or update it */
  1559. batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
  1560. flags & BATADV_TT_SYNC_MASK);
  1561. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1562. "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
  1563. common->addr, batadv_print_vid(common->vid),
  1564. orig_node->orig);
  1565. ret = true;
  1566. out_remove:
  1567. /* Do not remove multicast addresses from the local hash on
  1568. * global additions
  1569. */
  1570. if (is_multicast_ether_addr(tt_addr))
  1571. goto out;
  1572. /* remove address from local hash if present */
  1573. local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
  1574. "global tt received",
  1575. flags & BATADV_TT_CLIENT_ROAM);
  1576. tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
  1577. if (!(flags & BATADV_TT_CLIENT_ROAM))
  1578. /* this is a normal global add. Therefore the client is not in a
  1579. * roaming state anymore.
  1580. */
  1581. tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
  1582. out:
  1583. if (tt_global_entry)
  1584. batadv_tt_global_entry_put(tt_global_entry);
  1585. if (tt_local_entry)
  1586. batadv_tt_local_entry_put(tt_local_entry);
  1587. return ret;
  1588. }
  1589. /**
  1590. * batadv_transtable_best_orig() - Get best originator list entry from tt entry
  1591. * @bat_priv: the bat priv with all the soft interface information
  1592. * @tt_global_entry: global translation table entry to be analyzed
  1593. *
  1594. * This functon assumes the caller holds rcu_read_lock().
  1595. * Return: best originator list entry or NULL on errors.
  1596. */
  1597. static struct batadv_tt_orig_list_entry *
  1598. batadv_transtable_best_orig(struct batadv_priv *bat_priv,
  1599. struct batadv_tt_global_entry *tt_global_entry)
  1600. {
  1601. struct batadv_neigh_node *router, *best_router = NULL;
  1602. struct batadv_algo_ops *bao = bat_priv->algo_ops;
  1603. struct hlist_head *head;
  1604. struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
  1605. head = &tt_global_entry->orig_list;
  1606. hlist_for_each_entry_rcu(orig_entry, head, list) {
  1607. router = batadv_orig_router_get(orig_entry->orig_node,
  1608. BATADV_IF_DEFAULT);
  1609. if (!router)
  1610. continue;
  1611. if (best_router &&
  1612. bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
  1613. BATADV_IF_DEFAULT) <= 0) {
  1614. batadv_neigh_node_put(router);
  1615. continue;
  1616. }
  1617. /* release the refcount for the "old" best */
  1618. if (best_router)
  1619. batadv_neigh_node_put(best_router);
  1620. best_entry = orig_entry;
  1621. best_router = router;
  1622. }
  1623. if (best_router)
  1624. batadv_neigh_node_put(best_router);
  1625. return best_entry;
  1626. }
  1627. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1628. /**
  1629. * batadv_tt_global_print_entry() - print all orig nodes who announce the
  1630. * address for this global entry
  1631. * @bat_priv: the bat priv with all the soft interface information
  1632. * @tt_global_entry: global translation table entry to be printed
  1633. * @seq: debugfs table seq_file struct
  1634. *
  1635. * This functon assumes the caller holds rcu_read_lock().
  1636. */
  1637. static void
  1638. batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
  1639. struct batadv_tt_global_entry *tt_global_entry,
  1640. struct seq_file *seq)
  1641. {
  1642. struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
  1643. struct batadv_tt_common_entry *tt_common_entry;
  1644. struct batadv_orig_node_vlan *vlan;
  1645. struct hlist_head *head;
  1646. u8 last_ttvn;
  1647. u16 flags;
  1648. tt_common_entry = &tt_global_entry->common;
  1649. flags = tt_common_entry->flags;
  1650. best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
  1651. if (best_entry) {
  1652. vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
  1653. tt_common_entry->vid);
  1654. if (!vlan) {
  1655. seq_printf(seq,
  1656. " * Cannot retrieve VLAN %d for originator %pM\n",
  1657. batadv_print_vid(tt_common_entry->vid),
  1658. best_entry->orig_node->orig);
  1659. goto print_list;
  1660. }
  1661. last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
  1662. seq_printf(seq,
  1663. " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
  1664. '*', tt_global_entry->common.addr,
  1665. batadv_print_vid(tt_global_entry->common.vid),
  1666. best_entry->ttvn, best_entry->orig_node->orig,
  1667. last_ttvn, vlan->tt.crc,
  1668. ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
  1669. ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
  1670. ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
  1671. ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
  1672. batadv_orig_node_vlan_put(vlan);
  1673. }
  1674. print_list:
  1675. head = &tt_global_entry->orig_list;
  1676. hlist_for_each_entry_rcu(orig_entry, head, list) {
  1677. if (best_entry == orig_entry)
  1678. continue;
  1679. vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
  1680. tt_common_entry->vid);
  1681. if (!vlan) {
  1682. seq_printf(seq,
  1683. " + Cannot retrieve VLAN %d for originator %pM\n",
  1684. batadv_print_vid(tt_common_entry->vid),
  1685. orig_entry->orig_node->orig);
  1686. continue;
  1687. }
  1688. last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
  1689. seq_printf(seq,
  1690. " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
  1691. '+', tt_global_entry->common.addr,
  1692. batadv_print_vid(tt_global_entry->common.vid),
  1693. orig_entry->ttvn, orig_entry->orig_node->orig,
  1694. last_ttvn, vlan->tt.crc,
  1695. ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
  1696. ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
  1697. ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
  1698. ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
  1699. batadv_orig_node_vlan_put(vlan);
  1700. }
  1701. }
  1702. /**
  1703. * batadv_tt_global_seq_print_text() - Print the global tt table in a seq file
  1704. * @seq: seq file to print on
  1705. * @offset: not used
  1706. *
  1707. * Return: always 0
  1708. */
  1709. int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
  1710. {
  1711. struct net_device *net_dev = (struct net_device *)seq->private;
  1712. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1713. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1714. struct batadv_tt_common_entry *tt_common_entry;
  1715. struct batadv_tt_global_entry *tt_global;
  1716. struct batadv_hard_iface *primary_if;
  1717. struct hlist_head *head;
  1718. u32 i;
  1719. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1720. if (!primary_if)
  1721. goto out;
  1722. seq_printf(seq,
  1723. "Globally announced TT entries received via the mesh %s\n",
  1724. net_dev->name);
  1725. seq_puts(seq,
  1726. " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
  1727. for (i = 0; i < hash->size; i++) {
  1728. head = &hash->table[i];
  1729. rcu_read_lock();
  1730. hlist_for_each_entry_rcu(tt_common_entry,
  1731. head, hash_entry) {
  1732. tt_global = container_of(tt_common_entry,
  1733. struct batadv_tt_global_entry,
  1734. common);
  1735. batadv_tt_global_print_entry(bat_priv, tt_global, seq);
  1736. }
  1737. rcu_read_unlock();
  1738. }
  1739. out:
  1740. if (primary_if)
  1741. batadv_hardif_put(primary_if);
  1742. return 0;
  1743. }
  1744. #endif
  1745. /**
  1746. * batadv_tt_global_dump_subentry() - Dump all TT local entries into a message
  1747. * @msg: Netlink message to dump into
  1748. * @portid: Port making netlink request
  1749. * @seq: Sequence number of netlink message
  1750. * @common: tt local & tt global common data
  1751. * @orig: Originator node announcing a non-mesh client
  1752. * @best: Is the best originator for the TT entry
  1753. *
  1754. * Return: Error code, or 0 on success
  1755. */
  1756. static int
  1757. batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
  1758. struct batadv_tt_common_entry *common,
  1759. struct batadv_tt_orig_list_entry *orig,
  1760. bool best)
  1761. {
  1762. u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
  1763. void *hdr;
  1764. struct batadv_orig_node_vlan *vlan;
  1765. u8 last_ttvn;
  1766. u32 crc;
  1767. vlan = batadv_orig_node_vlan_get(orig->orig_node,
  1768. common->vid);
  1769. if (!vlan)
  1770. return 0;
  1771. crc = vlan->tt.crc;
  1772. batadv_orig_node_vlan_put(vlan);
  1773. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1774. NLM_F_MULTI,
  1775. BATADV_CMD_GET_TRANSTABLE_GLOBAL);
  1776. if (!hdr)
  1777. return -ENOBUFS;
  1778. last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
  1779. if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
  1780. nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  1781. orig->orig_node->orig) ||
  1782. nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
  1783. nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
  1784. nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
  1785. nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
  1786. nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
  1787. goto nla_put_failure;
  1788. if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
  1789. goto nla_put_failure;
  1790. genlmsg_end(msg, hdr);
  1791. return 0;
  1792. nla_put_failure:
  1793. genlmsg_cancel(msg, hdr);
  1794. return -EMSGSIZE;
  1795. }
  1796. /**
  1797. * batadv_tt_global_dump_entry() - Dump one TT global entry into a message
  1798. * @msg: Netlink message to dump into
  1799. * @portid: Port making netlink request
  1800. * @seq: Sequence number of netlink message
  1801. * @bat_priv: The bat priv with all the soft interface information
  1802. * @common: tt local & tt global common data
  1803. * @sub_s: Number of entries to skip
  1804. *
  1805. * This function assumes the caller holds rcu_read_lock().
  1806. *
  1807. * Return: Error code, or 0 on success
  1808. */
  1809. static int
  1810. batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  1811. struct batadv_priv *bat_priv,
  1812. struct batadv_tt_common_entry *common, int *sub_s)
  1813. {
  1814. struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
  1815. struct batadv_tt_global_entry *global;
  1816. struct hlist_head *head;
  1817. int sub = 0;
  1818. bool best;
  1819. global = container_of(common, struct batadv_tt_global_entry, common);
  1820. best_entry = batadv_transtable_best_orig(bat_priv, global);
  1821. head = &global->orig_list;
  1822. hlist_for_each_entry_rcu(orig_entry, head, list) {
  1823. if (sub++ < *sub_s)
  1824. continue;
  1825. best = (orig_entry == best_entry);
  1826. if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
  1827. orig_entry, best)) {
  1828. *sub_s = sub - 1;
  1829. return -EMSGSIZE;
  1830. }
  1831. }
  1832. *sub_s = 0;
  1833. return 0;
  1834. }
  1835. /**
  1836. * batadv_tt_global_dump_bucket() - Dump one TT local bucket into a message
  1837. * @msg: Netlink message to dump into
  1838. * @portid: Port making netlink request
  1839. * @seq: Sequence number of netlink message
  1840. * @bat_priv: The bat priv with all the soft interface information
  1841. * @head: Pointer to the list containing the global tt entries
  1842. * @idx_s: Number of entries to skip
  1843. * @sub: Number of entries to skip
  1844. *
  1845. * Return: Error code, or 0 on success
  1846. */
  1847. static int
  1848. batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  1849. struct batadv_priv *bat_priv,
  1850. struct hlist_head *head, int *idx_s, int *sub)
  1851. {
  1852. struct batadv_tt_common_entry *common;
  1853. int idx = 0;
  1854. rcu_read_lock();
  1855. hlist_for_each_entry_rcu(common, head, hash_entry) {
  1856. if (idx++ < *idx_s)
  1857. continue;
  1858. if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
  1859. common, sub)) {
  1860. rcu_read_unlock();
  1861. *idx_s = idx - 1;
  1862. return -EMSGSIZE;
  1863. }
  1864. }
  1865. rcu_read_unlock();
  1866. *idx_s = 0;
  1867. *sub = 0;
  1868. return 0;
  1869. }
  1870. /**
  1871. * batadv_tt_global_dump() - Dump TT global entries into a message
  1872. * @msg: Netlink message to dump into
  1873. * @cb: Parameters from query
  1874. *
  1875. * Return: Error code, or length of message on success
  1876. */
  1877. int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
  1878. {
  1879. struct net *net = sock_net(cb->skb->sk);
  1880. struct net_device *soft_iface;
  1881. struct batadv_priv *bat_priv;
  1882. struct batadv_hard_iface *primary_if = NULL;
  1883. struct batadv_hashtable *hash;
  1884. struct hlist_head *head;
  1885. int ret;
  1886. int ifindex;
  1887. int bucket = cb->args[0];
  1888. int idx = cb->args[1];
  1889. int sub = cb->args[2];
  1890. int portid = NETLINK_CB(cb->skb).portid;
  1891. ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
  1892. if (!ifindex)
  1893. return -EINVAL;
  1894. soft_iface = dev_get_by_index(net, ifindex);
  1895. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  1896. ret = -ENODEV;
  1897. goto out;
  1898. }
  1899. bat_priv = netdev_priv(soft_iface);
  1900. primary_if = batadv_primary_if_get_selected(bat_priv);
  1901. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  1902. ret = -ENOENT;
  1903. goto out;
  1904. }
  1905. hash = bat_priv->tt.global_hash;
  1906. while (bucket < hash->size) {
  1907. head = &hash->table[bucket];
  1908. if (batadv_tt_global_dump_bucket(msg, portid,
  1909. cb->nlh->nlmsg_seq, bat_priv,
  1910. head, &idx, &sub))
  1911. break;
  1912. bucket++;
  1913. }
  1914. ret = msg->len;
  1915. out:
  1916. if (primary_if)
  1917. batadv_hardif_put(primary_if);
  1918. if (soft_iface)
  1919. dev_put(soft_iface);
  1920. cb->args[0] = bucket;
  1921. cb->args[1] = idx;
  1922. cb->args[2] = sub;
  1923. return ret;
  1924. }
  1925. /**
  1926. * _batadv_tt_global_del_orig_entry() - remove and free an orig_entry
  1927. * @tt_global_entry: the global entry to remove the orig_entry from
  1928. * @orig_entry: the orig entry to remove and free
  1929. *
  1930. * Remove an orig_entry from its list in the given tt_global_entry and
  1931. * free this orig_entry afterwards.
  1932. *
  1933. * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
  1934. * part of a list.
  1935. */
  1936. static void
  1937. _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
  1938. struct batadv_tt_orig_list_entry *orig_entry)
  1939. {
  1940. lockdep_assert_held(&tt_global_entry->list_lock);
  1941. batadv_tt_global_size_dec(orig_entry->orig_node,
  1942. tt_global_entry->common.vid);
  1943. atomic_dec(&tt_global_entry->orig_list_count);
  1944. /* requires holding tt_global_entry->list_lock and orig_entry->list
  1945. * being part of a list
  1946. */
  1947. hlist_del_rcu(&orig_entry->list);
  1948. batadv_tt_orig_list_entry_put(orig_entry);
  1949. }
  1950. /* deletes the orig list of a tt_global_entry */
  1951. static void
  1952. batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
  1953. {
  1954. struct hlist_head *head;
  1955. struct hlist_node *safe;
  1956. struct batadv_tt_orig_list_entry *orig_entry;
  1957. spin_lock_bh(&tt_global_entry->list_lock);
  1958. head = &tt_global_entry->orig_list;
  1959. hlist_for_each_entry_safe(orig_entry, safe, head, list)
  1960. _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
  1961. spin_unlock_bh(&tt_global_entry->list_lock);
  1962. }
  1963. /**
  1964. * batadv_tt_global_del_orig_node() - remove orig_node from a global tt entry
  1965. * @bat_priv: the bat priv with all the soft interface information
  1966. * @tt_global_entry: the global entry to remove the orig_node from
  1967. * @orig_node: the originator announcing the client
  1968. * @message: message to append to the log on deletion
  1969. *
  1970. * Remove the given orig_node and its according orig_entry from the given
  1971. * global tt entry.
  1972. */
  1973. static void
  1974. batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
  1975. struct batadv_tt_global_entry *tt_global_entry,
  1976. struct batadv_orig_node *orig_node,
  1977. const char *message)
  1978. {
  1979. struct hlist_head *head;
  1980. struct hlist_node *safe;
  1981. struct batadv_tt_orig_list_entry *orig_entry;
  1982. unsigned short vid;
  1983. spin_lock_bh(&tt_global_entry->list_lock);
  1984. head = &tt_global_entry->orig_list;
  1985. hlist_for_each_entry_safe(orig_entry, safe, head, list) {
  1986. if (orig_entry->orig_node == orig_node) {
  1987. vid = tt_global_entry->common.vid;
  1988. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1989. "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
  1990. orig_node->orig,
  1991. tt_global_entry->common.addr,
  1992. batadv_print_vid(vid), message);
  1993. _batadv_tt_global_del_orig_entry(tt_global_entry,
  1994. orig_entry);
  1995. }
  1996. }
  1997. spin_unlock_bh(&tt_global_entry->list_lock);
  1998. }
  1999. /* If the client is to be deleted, we check if it is the last origantor entry
  2000. * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
  2001. * timer, otherwise we simply remove the originator scheduled for deletion.
  2002. */
  2003. static void
  2004. batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
  2005. struct batadv_tt_global_entry *tt_global_entry,
  2006. struct batadv_orig_node *orig_node,
  2007. const char *message)
  2008. {
  2009. bool last_entry = true;
  2010. struct hlist_head *head;
  2011. struct batadv_tt_orig_list_entry *orig_entry;
  2012. /* no local entry exists, case 1:
  2013. * Check if this is the last one or if other entries exist.
  2014. */
  2015. rcu_read_lock();
  2016. head = &tt_global_entry->orig_list;
  2017. hlist_for_each_entry_rcu(orig_entry, head, list) {
  2018. if (orig_entry->orig_node != orig_node) {
  2019. last_entry = false;
  2020. break;
  2021. }
  2022. }
  2023. rcu_read_unlock();
  2024. if (last_entry) {
  2025. /* its the last one, mark for roaming. */
  2026. tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
  2027. tt_global_entry->roam_at = jiffies;
  2028. } else {
  2029. /* there is another entry, we can simply delete this
  2030. * one and can still use the other one.
  2031. */
  2032. batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
  2033. orig_node, message);
  2034. }
  2035. }
  2036. /**
  2037. * batadv_tt_global_del() - remove a client from the global table
  2038. * @bat_priv: the bat priv with all the soft interface information
  2039. * @orig_node: an originator serving this client
  2040. * @addr: the mac address of the client
  2041. * @vid: VLAN identifier
  2042. * @message: a message explaining the reason for deleting the client to print
  2043. * for debugging purpose
  2044. * @roaming: true if the deletion has been triggered by a roaming event
  2045. */
  2046. static void batadv_tt_global_del(struct batadv_priv *bat_priv,
  2047. struct batadv_orig_node *orig_node,
  2048. const unsigned char *addr, unsigned short vid,
  2049. const char *message, bool roaming)
  2050. {
  2051. struct batadv_tt_global_entry *tt_global_entry;
  2052. struct batadv_tt_local_entry *local_entry = NULL;
  2053. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  2054. if (!tt_global_entry)
  2055. goto out;
  2056. if (!roaming) {
  2057. batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
  2058. orig_node, message);
  2059. if (hlist_empty(&tt_global_entry->orig_list))
  2060. batadv_tt_global_free(bat_priv, tt_global_entry,
  2061. message);
  2062. goto out;
  2063. }
  2064. /* if we are deleting a global entry due to a roam
  2065. * event, there are two possibilities:
  2066. * 1) the client roamed from node A to node B => if there
  2067. * is only one originator left for this client, we mark
  2068. * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
  2069. * wait for node B to claim it. In case of timeout
  2070. * the entry is purged.
  2071. *
  2072. * If there are other originators left, we directly delete
  2073. * the originator.
  2074. * 2) the client roamed to us => we can directly delete
  2075. * the global entry, since it is useless now.
  2076. */
  2077. local_entry = batadv_tt_local_hash_find(bat_priv,
  2078. tt_global_entry->common.addr,
  2079. vid);
  2080. if (local_entry) {
  2081. /* local entry exists, case 2: client roamed to us. */
  2082. batadv_tt_global_del_orig_list(tt_global_entry);
  2083. batadv_tt_global_free(bat_priv, tt_global_entry, message);
  2084. } else {
  2085. /* no local entry exists, case 1: check for roaming */
  2086. batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
  2087. orig_node, message);
  2088. }
  2089. out:
  2090. if (tt_global_entry)
  2091. batadv_tt_global_entry_put(tt_global_entry);
  2092. if (local_entry)
  2093. batadv_tt_local_entry_put(local_entry);
  2094. }
  2095. /**
  2096. * batadv_tt_global_del_orig() - remove all the TT global entries belonging to
  2097. * the given originator matching the provided vid
  2098. * @bat_priv: the bat priv with all the soft interface information
  2099. * @orig_node: the originator owning the entries to remove
  2100. * @match_vid: the VLAN identifier to match. If negative all the entries will be
  2101. * removed
  2102. * @message: debug message to print as "reason"
  2103. */
  2104. void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
  2105. struct batadv_orig_node *orig_node,
  2106. s32 match_vid,
  2107. const char *message)
  2108. {
  2109. struct batadv_tt_global_entry *tt_global;
  2110. struct batadv_tt_common_entry *tt_common_entry;
  2111. u32 i;
  2112. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  2113. struct hlist_node *safe;
  2114. struct hlist_head *head;
  2115. spinlock_t *list_lock; /* protects write access to the hash lists */
  2116. unsigned short vid;
  2117. if (!hash)
  2118. return;
  2119. for (i = 0; i < hash->size; i++) {
  2120. head = &hash->table[i];
  2121. list_lock = &hash->list_locks[i];
  2122. spin_lock_bh(list_lock);
  2123. hlist_for_each_entry_safe(tt_common_entry, safe,
  2124. head, hash_entry) {
  2125. /* remove only matching entries */
  2126. if (match_vid >= 0 && tt_common_entry->vid != match_vid)
  2127. continue;
  2128. tt_global = container_of(tt_common_entry,
  2129. struct batadv_tt_global_entry,
  2130. common);
  2131. batadv_tt_global_del_orig_node(bat_priv, tt_global,
  2132. orig_node, message);
  2133. if (hlist_empty(&tt_global->orig_list)) {
  2134. vid = tt_global->common.vid;
  2135. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2136. "Deleting global tt entry %pM (vid: %d): %s\n",
  2137. tt_global->common.addr,
  2138. batadv_print_vid(vid), message);
  2139. hlist_del_rcu(&tt_common_entry->hash_entry);
  2140. batadv_tt_global_entry_put(tt_global);
  2141. }
  2142. }
  2143. spin_unlock_bh(list_lock);
  2144. }
  2145. clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
  2146. }
  2147. static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
  2148. char **msg)
  2149. {
  2150. bool purge = false;
  2151. unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
  2152. unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
  2153. if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
  2154. batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
  2155. purge = true;
  2156. *msg = "Roaming timeout\n";
  2157. }
  2158. if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
  2159. batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
  2160. purge = true;
  2161. *msg = "Temporary client timeout\n";
  2162. }
  2163. return purge;
  2164. }
  2165. static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
  2166. {
  2167. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  2168. struct hlist_head *head;
  2169. struct hlist_node *node_tmp;
  2170. spinlock_t *list_lock; /* protects write access to the hash lists */
  2171. u32 i;
  2172. char *msg = NULL;
  2173. struct batadv_tt_common_entry *tt_common;
  2174. struct batadv_tt_global_entry *tt_global;
  2175. for (i = 0; i < hash->size; i++) {
  2176. head = &hash->table[i];
  2177. list_lock = &hash->list_locks[i];
  2178. spin_lock_bh(list_lock);
  2179. hlist_for_each_entry_safe(tt_common, node_tmp, head,
  2180. hash_entry) {
  2181. tt_global = container_of(tt_common,
  2182. struct batadv_tt_global_entry,
  2183. common);
  2184. if (!batadv_tt_global_to_purge(tt_global, &msg))
  2185. continue;
  2186. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2187. "Deleting global tt entry %pM (vid: %d): %s\n",
  2188. tt_global->common.addr,
  2189. batadv_print_vid(tt_global->common.vid),
  2190. msg);
  2191. hlist_del_rcu(&tt_common->hash_entry);
  2192. batadv_tt_global_entry_put(tt_global);
  2193. }
  2194. spin_unlock_bh(list_lock);
  2195. }
  2196. }
  2197. static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
  2198. {
  2199. struct batadv_hashtable *hash;
  2200. spinlock_t *list_lock; /* protects write access to the hash lists */
  2201. struct batadv_tt_common_entry *tt_common_entry;
  2202. struct batadv_tt_global_entry *tt_global;
  2203. struct hlist_node *node_tmp;
  2204. struct hlist_head *head;
  2205. u32 i;
  2206. if (!bat_priv->tt.global_hash)
  2207. return;
  2208. hash = bat_priv->tt.global_hash;
  2209. for (i = 0; i < hash->size; i++) {
  2210. head = &hash->table[i];
  2211. list_lock = &hash->list_locks[i];
  2212. spin_lock_bh(list_lock);
  2213. hlist_for_each_entry_safe(tt_common_entry, node_tmp,
  2214. head, hash_entry) {
  2215. hlist_del_rcu(&tt_common_entry->hash_entry);
  2216. tt_global = container_of(tt_common_entry,
  2217. struct batadv_tt_global_entry,
  2218. common);
  2219. batadv_tt_global_entry_put(tt_global);
  2220. }
  2221. spin_unlock_bh(list_lock);
  2222. }
  2223. batadv_hash_destroy(hash);
  2224. bat_priv->tt.global_hash = NULL;
  2225. }
  2226. static bool
  2227. _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
  2228. struct batadv_tt_global_entry *tt_global_entry)
  2229. {
  2230. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
  2231. tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
  2232. return true;
  2233. /* check if the two clients are marked as isolated */
  2234. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
  2235. tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
  2236. return true;
  2237. return false;
  2238. }
  2239. /**
  2240. * batadv_transtable_search() - get the mesh destination for a given client
  2241. * @bat_priv: the bat priv with all the soft interface information
  2242. * @src: mac address of the source client
  2243. * @addr: mac address of the destination client
  2244. * @vid: VLAN identifier
  2245. *
  2246. * Return: a pointer to the originator that was selected as destination in the
  2247. * mesh for contacting the client 'addr', NULL otherwise.
  2248. * In case of multiple originators serving the same client, the function returns
  2249. * the best one (best in terms of metric towards the destination node).
  2250. *
  2251. * If the two clients are AP isolated the function returns NULL.
  2252. */
  2253. struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
  2254. const u8 *src,
  2255. const u8 *addr,
  2256. unsigned short vid)
  2257. {
  2258. struct batadv_tt_local_entry *tt_local_entry = NULL;
  2259. struct batadv_tt_global_entry *tt_global_entry = NULL;
  2260. struct batadv_orig_node *orig_node = NULL;
  2261. struct batadv_tt_orig_list_entry *best_entry;
  2262. if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
  2263. tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
  2264. if (!tt_local_entry ||
  2265. (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
  2266. goto out;
  2267. }
  2268. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  2269. if (!tt_global_entry)
  2270. goto out;
  2271. /* check whether the clients should not communicate due to AP
  2272. * isolation
  2273. */
  2274. if (tt_local_entry &&
  2275. _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
  2276. goto out;
  2277. rcu_read_lock();
  2278. best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
  2279. /* found anything? */
  2280. if (best_entry)
  2281. orig_node = best_entry->orig_node;
  2282. if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
  2283. orig_node = NULL;
  2284. rcu_read_unlock();
  2285. out:
  2286. if (tt_global_entry)
  2287. batadv_tt_global_entry_put(tt_global_entry);
  2288. if (tt_local_entry)
  2289. batadv_tt_local_entry_put(tt_local_entry);
  2290. return orig_node;
  2291. }
  2292. /**
  2293. * batadv_tt_global_crc() - calculates the checksum of the local table belonging
  2294. * to the given orig_node
  2295. * @bat_priv: the bat priv with all the soft interface information
  2296. * @orig_node: originator for which the CRC should be computed
  2297. * @vid: VLAN identifier for which the CRC32 has to be computed
  2298. *
  2299. * This function computes the checksum for the global table corresponding to a
  2300. * specific originator. In particular, the checksum is computed as follows: For
  2301. * each client connected to the originator the CRC32C of the MAC address and the
  2302. * VID is computed and then all the CRC32Cs of the various clients are xor'ed
  2303. * together.
  2304. *
  2305. * The idea behind is that CRC32C should be used as much as possible in order to
  2306. * produce a unique hash of the table, but since the order which is used to feed
  2307. * the CRC32C function affects the result and since every node in the network
  2308. * probably sorts the clients differently, the hash function cannot be directly
  2309. * computed over the entire table. Hence the CRC32C is used only on
  2310. * the single client entry, while all the results are then xor'ed together
  2311. * because the XOR operation can combine them all while trying to reduce the
  2312. * noise as much as possible.
  2313. *
  2314. * Return: the checksum of the global table of a given originator.
  2315. */
  2316. static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
  2317. struct batadv_orig_node *orig_node,
  2318. unsigned short vid)
  2319. {
  2320. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  2321. struct batadv_tt_orig_list_entry *tt_orig;
  2322. struct batadv_tt_common_entry *tt_common;
  2323. struct batadv_tt_global_entry *tt_global;
  2324. struct hlist_head *head;
  2325. u32 i, crc_tmp, crc = 0;
  2326. u8 flags;
  2327. __be16 tmp_vid;
  2328. for (i = 0; i < hash->size; i++) {
  2329. head = &hash->table[i];
  2330. rcu_read_lock();
  2331. hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
  2332. tt_global = container_of(tt_common,
  2333. struct batadv_tt_global_entry,
  2334. common);
  2335. /* compute the CRC only for entries belonging to the
  2336. * VLAN identified by the vid passed as parameter
  2337. */
  2338. if (tt_common->vid != vid)
  2339. continue;
  2340. /* Roaming clients are in the global table for
  2341. * consistency only. They don't have to be
  2342. * taken into account while computing the
  2343. * global crc
  2344. */
  2345. if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
  2346. continue;
  2347. /* Temporary clients have not been announced yet, so
  2348. * they have to be skipped while computing the global
  2349. * crc
  2350. */
  2351. if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
  2352. continue;
  2353. /* find out if this global entry is announced by this
  2354. * originator
  2355. */
  2356. tt_orig = batadv_tt_global_orig_entry_find(tt_global,
  2357. orig_node);
  2358. if (!tt_orig)
  2359. continue;
  2360. /* use network order to read the VID: this ensures that
  2361. * every node reads the bytes in the same order.
  2362. */
  2363. tmp_vid = htons(tt_common->vid);
  2364. crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
  2365. /* compute the CRC on flags that have to be kept in sync
  2366. * among nodes
  2367. */
  2368. flags = tt_orig->flags;
  2369. crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
  2370. crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
  2371. batadv_tt_orig_list_entry_put(tt_orig);
  2372. }
  2373. rcu_read_unlock();
  2374. }
  2375. return crc;
  2376. }
  2377. /**
  2378. * batadv_tt_local_crc() - calculates the checksum of the local table
  2379. * @bat_priv: the bat priv with all the soft interface information
  2380. * @vid: VLAN identifier for which the CRC32 has to be computed
  2381. *
  2382. * For details about the computation, please refer to the documentation for
  2383. * batadv_tt_global_crc().
  2384. *
  2385. * Return: the checksum of the local table
  2386. */
  2387. static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
  2388. unsigned short vid)
  2389. {
  2390. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  2391. struct batadv_tt_common_entry *tt_common;
  2392. struct hlist_head *head;
  2393. u32 i, crc_tmp, crc = 0;
  2394. u8 flags;
  2395. __be16 tmp_vid;
  2396. for (i = 0; i < hash->size; i++) {
  2397. head = &hash->table[i];
  2398. rcu_read_lock();
  2399. hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
  2400. /* compute the CRC only for entries belonging to the
  2401. * VLAN identified by vid
  2402. */
  2403. if (tt_common->vid != vid)
  2404. continue;
  2405. /* not yet committed clients have not to be taken into
  2406. * account while computing the CRC
  2407. */
  2408. if (tt_common->flags & BATADV_TT_CLIENT_NEW)
  2409. continue;
  2410. /* use network order to read the VID: this ensures that
  2411. * every node reads the bytes in the same order.
  2412. */
  2413. tmp_vid = htons(tt_common->vid);
  2414. crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
  2415. /* compute the CRC on flags that have to be kept in sync
  2416. * among nodes
  2417. */
  2418. flags = tt_common->flags & BATADV_TT_SYNC_MASK;
  2419. crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
  2420. crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
  2421. }
  2422. rcu_read_unlock();
  2423. }
  2424. return crc;
  2425. }
  2426. /**
  2427. * batadv_tt_req_node_release() - free tt_req node entry
  2428. * @ref: kref pointer of the tt req_node entry
  2429. */
  2430. static void batadv_tt_req_node_release(struct kref *ref)
  2431. {
  2432. struct batadv_tt_req_node *tt_req_node;
  2433. tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
  2434. kmem_cache_free(batadv_tt_req_cache, tt_req_node);
  2435. }
  2436. /**
  2437. * batadv_tt_req_node_put() - decrement the tt_req_node refcounter and
  2438. * possibly release it
  2439. * @tt_req_node: tt_req_node to be free'd
  2440. */
  2441. static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
  2442. {
  2443. kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
  2444. }
  2445. static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
  2446. {
  2447. struct batadv_tt_req_node *node;
  2448. struct hlist_node *safe;
  2449. spin_lock_bh(&bat_priv->tt.req_list_lock);
  2450. hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  2451. hlist_del_init(&node->list);
  2452. batadv_tt_req_node_put(node);
  2453. }
  2454. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  2455. }
  2456. static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
  2457. struct batadv_orig_node *orig_node,
  2458. const void *tt_buff,
  2459. u16 tt_buff_len)
  2460. {
  2461. /* Replace the old buffer only if I received something in the
  2462. * last OGM (the OGM could carry no changes)
  2463. */
  2464. spin_lock_bh(&orig_node->tt_buff_lock);
  2465. if (tt_buff_len > 0) {
  2466. kfree(orig_node->tt_buff);
  2467. orig_node->tt_buff_len = 0;
  2468. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  2469. if (orig_node->tt_buff) {
  2470. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  2471. orig_node->tt_buff_len = tt_buff_len;
  2472. }
  2473. }
  2474. spin_unlock_bh(&orig_node->tt_buff_lock);
  2475. }
  2476. static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
  2477. {
  2478. struct batadv_tt_req_node *node;
  2479. struct hlist_node *safe;
  2480. spin_lock_bh(&bat_priv->tt.req_list_lock);
  2481. hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  2482. if (batadv_has_timed_out(node->issued_at,
  2483. BATADV_TT_REQUEST_TIMEOUT)) {
  2484. hlist_del_init(&node->list);
  2485. batadv_tt_req_node_put(node);
  2486. }
  2487. }
  2488. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  2489. }
  2490. /**
  2491. * batadv_tt_req_node_new() - search and possibly create a tt_req_node object
  2492. * @bat_priv: the bat priv with all the soft interface information
  2493. * @orig_node: orig node this request is being issued for
  2494. *
  2495. * Return: the pointer to the new tt_req_node struct if no request
  2496. * has already been issued for this orig_node, NULL otherwise.
  2497. */
  2498. static struct batadv_tt_req_node *
  2499. batadv_tt_req_node_new(struct batadv_priv *bat_priv,
  2500. struct batadv_orig_node *orig_node)
  2501. {
  2502. struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
  2503. spin_lock_bh(&bat_priv->tt.req_list_lock);
  2504. hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
  2505. if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
  2506. !batadv_has_timed_out(tt_req_node_tmp->issued_at,
  2507. BATADV_TT_REQUEST_TIMEOUT))
  2508. goto unlock;
  2509. }
  2510. tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
  2511. if (!tt_req_node)
  2512. goto unlock;
  2513. kref_init(&tt_req_node->refcount);
  2514. ether_addr_copy(tt_req_node->addr, orig_node->orig);
  2515. tt_req_node->issued_at = jiffies;
  2516. kref_get(&tt_req_node->refcount);
  2517. hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
  2518. unlock:
  2519. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  2520. return tt_req_node;
  2521. }
  2522. /**
  2523. * batadv_tt_local_valid() - verify local tt entry and get flags
  2524. * @entry_ptr: to be checked local tt entry
  2525. * @data_ptr: not used but definition required to satisfy the callback prototype
  2526. * @flags: a pointer to store TT flags for this client to
  2527. *
  2528. * Checks the validity of the given local TT entry. If it is, then the provided
  2529. * flags pointer is updated.
  2530. *
  2531. * Return: true if the entry is a valid, false otherwise.
  2532. */
  2533. static bool batadv_tt_local_valid(const void *entry_ptr,
  2534. const void *data_ptr,
  2535. u8 *flags)
  2536. {
  2537. const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
  2538. if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
  2539. return false;
  2540. if (flags)
  2541. *flags = tt_common_entry->flags;
  2542. return true;
  2543. }
  2544. /**
  2545. * batadv_tt_global_valid() - verify global tt entry and get flags
  2546. * @entry_ptr: to be checked global tt entry
  2547. * @data_ptr: an orig_node object (may be NULL)
  2548. * @flags: a pointer to store TT flags for this client to
  2549. *
  2550. * Checks the validity of the given global TT entry. If it is, then the provided
  2551. * flags pointer is updated either with the common (summed) TT flags if data_ptr
  2552. * is NULL or the specific, per originator TT flags otherwise.
  2553. *
  2554. * Return: true if the entry is a valid, false otherwise.
  2555. */
  2556. static bool batadv_tt_global_valid(const void *entry_ptr,
  2557. const void *data_ptr,
  2558. u8 *flags)
  2559. {
  2560. const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
  2561. const struct batadv_tt_global_entry *tt_global_entry;
  2562. const struct batadv_orig_node *orig_node = data_ptr;
  2563. if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
  2564. tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
  2565. return false;
  2566. tt_global_entry = container_of(tt_common_entry,
  2567. struct batadv_tt_global_entry,
  2568. common);
  2569. return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
  2570. flags);
  2571. }
  2572. /**
  2573. * batadv_tt_tvlv_generate() - fill the tvlv buff with the tt entries from the
  2574. * specified tt hash
  2575. * @bat_priv: the bat priv with all the soft interface information
  2576. * @hash: hash table containing the tt entries
  2577. * @tt_len: expected tvlv tt data buffer length in number of bytes
  2578. * @tvlv_buff: pointer to the buffer to fill with the TT data
  2579. * @valid_cb: function to filter tt change entries and to return TT flags
  2580. * @cb_data: data passed to the filter function as argument
  2581. *
  2582. * Fills the tvlv buff with the tt entries from the specified hash. If valid_cb
  2583. * is not provided then this becomes a no-op.
  2584. */
  2585. static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
  2586. struct batadv_hashtable *hash,
  2587. void *tvlv_buff, u16 tt_len,
  2588. bool (*valid_cb)(const void *,
  2589. const void *,
  2590. u8 *flags),
  2591. void *cb_data)
  2592. {
  2593. struct batadv_tt_common_entry *tt_common_entry;
  2594. struct batadv_tvlv_tt_change *tt_change;
  2595. struct hlist_head *head;
  2596. u16 tt_tot, tt_num_entries = 0;
  2597. u8 flags;
  2598. bool ret;
  2599. u32 i;
  2600. tt_tot = batadv_tt_entries(tt_len);
  2601. tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
  2602. if (!valid_cb)
  2603. return;
  2604. rcu_read_lock();
  2605. for (i = 0; i < hash->size; i++) {
  2606. head = &hash->table[i];
  2607. hlist_for_each_entry_rcu(tt_common_entry,
  2608. head, hash_entry) {
  2609. if (tt_tot == tt_num_entries)
  2610. break;
  2611. ret = valid_cb(tt_common_entry, cb_data, &flags);
  2612. if (!ret)
  2613. continue;
  2614. ether_addr_copy(tt_change->addr, tt_common_entry->addr);
  2615. tt_change->flags = flags;
  2616. tt_change->vid = htons(tt_common_entry->vid);
  2617. memset(tt_change->reserved, 0,
  2618. sizeof(tt_change->reserved));
  2619. tt_num_entries++;
  2620. tt_change++;
  2621. }
  2622. }
  2623. rcu_read_unlock();
  2624. }
  2625. /**
  2626. * batadv_tt_global_check_crc() - check if all the CRCs are correct
  2627. * @orig_node: originator for which the CRCs have to be checked
  2628. * @tt_vlan: pointer to the first tvlv VLAN entry
  2629. * @num_vlan: number of tvlv VLAN entries
  2630. *
  2631. * Return: true if all the received CRCs match the locally stored ones, false
  2632. * otherwise
  2633. */
  2634. static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
  2635. struct batadv_tvlv_tt_vlan_data *tt_vlan,
  2636. u16 num_vlan)
  2637. {
  2638. struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
  2639. struct batadv_orig_node_vlan *vlan;
  2640. int i, orig_num_vlan;
  2641. u32 crc;
  2642. /* check if each received CRC matches the locally stored one */
  2643. for (i = 0; i < num_vlan; i++) {
  2644. tt_vlan_tmp = tt_vlan + i;
  2645. /* if orig_node is a backbone node for this VLAN, don't check
  2646. * the CRC as we ignore all the global entries over it
  2647. */
  2648. if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
  2649. orig_node->orig,
  2650. ntohs(tt_vlan_tmp->vid)))
  2651. continue;
  2652. vlan = batadv_orig_node_vlan_get(orig_node,
  2653. ntohs(tt_vlan_tmp->vid));
  2654. if (!vlan)
  2655. return false;
  2656. crc = vlan->tt.crc;
  2657. batadv_orig_node_vlan_put(vlan);
  2658. if (crc != ntohl(tt_vlan_tmp->crc))
  2659. return false;
  2660. }
  2661. /* check if any excess VLANs exist locally for the originator
  2662. * which are not mentioned in the TVLV from the originator.
  2663. */
  2664. rcu_read_lock();
  2665. orig_num_vlan = 0;
  2666. hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
  2667. orig_num_vlan++;
  2668. rcu_read_unlock();
  2669. if (orig_num_vlan > num_vlan)
  2670. return false;
  2671. return true;
  2672. }
  2673. /**
  2674. * batadv_tt_local_update_crc() - update all the local CRCs
  2675. * @bat_priv: the bat priv with all the soft interface information
  2676. */
  2677. static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
  2678. {
  2679. struct batadv_softif_vlan *vlan;
  2680. /* recompute the global CRC for each VLAN */
  2681. rcu_read_lock();
  2682. hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
  2683. vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
  2684. }
  2685. rcu_read_unlock();
  2686. }
  2687. /**
  2688. * batadv_tt_global_update_crc() - update all the global CRCs for this orig_node
  2689. * @bat_priv: the bat priv with all the soft interface information
  2690. * @orig_node: the orig_node for which the CRCs have to be updated
  2691. */
  2692. static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
  2693. struct batadv_orig_node *orig_node)
  2694. {
  2695. struct batadv_orig_node_vlan *vlan;
  2696. u32 crc;
  2697. /* recompute the global CRC for each VLAN */
  2698. rcu_read_lock();
  2699. hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
  2700. /* if orig_node is a backbone node for this VLAN, don't compute
  2701. * the CRC as we ignore all the global entries over it
  2702. */
  2703. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
  2704. vlan->vid))
  2705. continue;
  2706. crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
  2707. vlan->tt.crc = crc;
  2708. }
  2709. rcu_read_unlock();
  2710. }
  2711. /**
  2712. * batadv_send_tt_request() - send a TT Request message to a given node
  2713. * @bat_priv: the bat priv with all the soft interface information
  2714. * @dst_orig_node: the destination of the message
  2715. * @ttvn: the version number that the source of the message is looking for
  2716. * @tt_vlan: pointer to the first tvlv VLAN object to request
  2717. * @num_vlan: number of tvlv VLAN entries
  2718. * @full_table: ask for the entire translation table if true, while only for the
  2719. * last TT diff otherwise
  2720. *
  2721. * Return: true if the TT Request was sent, false otherwise
  2722. */
  2723. static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
  2724. struct batadv_orig_node *dst_orig_node,
  2725. u8 ttvn,
  2726. struct batadv_tvlv_tt_vlan_data *tt_vlan,
  2727. u16 num_vlan, bool full_table)
  2728. {
  2729. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  2730. struct batadv_tt_req_node *tt_req_node = NULL;
  2731. struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
  2732. struct batadv_hard_iface *primary_if;
  2733. bool ret = false;
  2734. int i, size;
  2735. primary_if = batadv_primary_if_get_selected(bat_priv);
  2736. if (!primary_if)
  2737. goto out;
  2738. /* The new tt_req will be issued only if I'm not waiting for a
  2739. * reply from the same orig_node yet
  2740. */
  2741. tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
  2742. if (!tt_req_node)
  2743. goto out;
  2744. size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
  2745. tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
  2746. if (!tvlv_tt_data)
  2747. goto out;
  2748. tvlv_tt_data->flags = BATADV_TT_REQUEST;
  2749. tvlv_tt_data->ttvn = ttvn;
  2750. tvlv_tt_data->num_vlan = htons(num_vlan);
  2751. /* send all the CRCs within the request. This is needed by intermediate
  2752. * nodes to ensure they have the correct table before replying
  2753. */
  2754. tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
  2755. for (i = 0; i < num_vlan; i++) {
  2756. tt_vlan_req->vid = tt_vlan->vid;
  2757. tt_vlan_req->crc = tt_vlan->crc;
  2758. tt_vlan_req++;
  2759. tt_vlan++;
  2760. }
  2761. if (full_table)
  2762. tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
  2763. batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
  2764. dst_orig_node->orig, full_table ? 'F' : '.');
  2765. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
  2766. batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
  2767. dst_orig_node->orig, BATADV_TVLV_TT, 1,
  2768. tvlv_tt_data, size);
  2769. ret = true;
  2770. out:
  2771. if (primary_if)
  2772. batadv_hardif_put(primary_if);
  2773. if (ret && tt_req_node) {
  2774. spin_lock_bh(&bat_priv->tt.req_list_lock);
  2775. if (!hlist_unhashed(&tt_req_node->list)) {
  2776. hlist_del_init(&tt_req_node->list);
  2777. batadv_tt_req_node_put(tt_req_node);
  2778. }
  2779. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  2780. }
  2781. if (tt_req_node)
  2782. batadv_tt_req_node_put(tt_req_node);
  2783. kfree(tvlv_tt_data);
  2784. return ret;
  2785. }
  2786. /**
  2787. * batadv_send_other_tt_response() - send reply to tt request concerning another
  2788. * node's translation table
  2789. * @bat_priv: the bat priv with all the soft interface information
  2790. * @tt_data: tt data containing the tt request information
  2791. * @req_src: mac address of tt request sender
  2792. * @req_dst: mac address of tt request recipient
  2793. *
  2794. * Return: true if tt request reply was sent, false otherwise.
  2795. */
  2796. static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
  2797. struct batadv_tvlv_tt_data *tt_data,
  2798. u8 *req_src, u8 *req_dst)
  2799. {
  2800. struct batadv_orig_node *req_dst_orig_node;
  2801. struct batadv_orig_node *res_dst_orig_node = NULL;
  2802. struct batadv_tvlv_tt_change *tt_change;
  2803. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  2804. struct batadv_tvlv_tt_vlan_data *tt_vlan;
  2805. bool ret = false, full_table;
  2806. u8 orig_ttvn, req_ttvn;
  2807. u16 tvlv_len;
  2808. s32 tt_len;
  2809. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2810. "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
  2811. req_src, tt_data->ttvn, req_dst,
  2812. ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
  2813. /* Let's get the orig node of the REAL destination */
  2814. req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
  2815. if (!req_dst_orig_node)
  2816. goto out;
  2817. res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
  2818. if (!res_dst_orig_node)
  2819. goto out;
  2820. orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
  2821. req_ttvn = tt_data->ttvn;
  2822. tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
  2823. /* this node doesn't have the requested data */
  2824. if (orig_ttvn != req_ttvn ||
  2825. !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
  2826. ntohs(tt_data->num_vlan)))
  2827. goto out;
  2828. /* If the full table has been explicitly requested */
  2829. if (tt_data->flags & BATADV_TT_FULL_TABLE ||
  2830. !req_dst_orig_node->tt_buff)
  2831. full_table = true;
  2832. else
  2833. full_table = false;
  2834. /* TT fragmentation hasn't been implemented yet, so send as many
  2835. * TT entries fit a single packet as possible only
  2836. */
  2837. if (!full_table) {
  2838. spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
  2839. tt_len = req_dst_orig_node->tt_buff_len;
  2840. tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
  2841. &tvlv_tt_data,
  2842. &tt_change,
  2843. &tt_len);
  2844. if (!tt_len)
  2845. goto unlock;
  2846. /* Copy the last orig_node's OGM buffer */
  2847. memcpy(tt_change, req_dst_orig_node->tt_buff,
  2848. req_dst_orig_node->tt_buff_len);
  2849. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  2850. } else {
  2851. /* allocate the tvlv, put the tt_data and all the tt_vlan_data
  2852. * in the initial part
  2853. */
  2854. tt_len = -1;
  2855. tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
  2856. &tvlv_tt_data,
  2857. &tt_change,
  2858. &tt_len);
  2859. if (!tt_len)
  2860. goto out;
  2861. /* fill the rest of the tvlv with the real TT entries */
  2862. batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
  2863. tt_change, tt_len,
  2864. batadv_tt_global_valid,
  2865. req_dst_orig_node);
  2866. }
  2867. /* Don't send the response, if larger than fragmented packet. */
  2868. tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
  2869. if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
  2870. net_ratelimited_function(batadv_info, bat_priv->soft_iface,
  2871. "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
  2872. res_dst_orig_node->orig);
  2873. goto out;
  2874. }
  2875. tvlv_tt_data->flags = BATADV_TT_RESPONSE;
  2876. tvlv_tt_data->ttvn = req_ttvn;
  2877. if (full_table)
  2878. tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
  2879. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2880. "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
  2881. res_dst_orig_node->orig, req_dst_orig_node->orig,
  2882. full_table ? 'F' : '.', req_ttvn);
  2883. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
  2884. batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
  2885. req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
  2886. tvlv_len);
  2887. ret = true;
  2888. goto out;
  2889. unlock:
  2890. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  2891. out:
  2892. if (res_dst_orig_node)
  2893. batadv_orig_node_put(res_dst_orig_node);
  2894. if (req_dst_orig_node)
  2895. batadv_orig_node_put(req_dst_orig_node);
  2896. kfree(tvlv_tt_data);
  2897. return ret;
  2898. }
  2899. /**
  2900. * batadv_send_my_tt_response() - send reply to tt request concerning this
  2901. * node's translation table
  2902. * @bat_priv: the bat priv with all the soft interface information
  2903. * @tt_data: tt data containing the tt request information
  2904. * @req_src: mac address of tt request sender
  2905. *
  2906. * Return: true if tt request reply was sent, false otherwise.
  2907. */
  2908. static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
  2909. struct batadv_tvlv_tt_data *tt_data,
  2910. u8 *req_src)
  2911. {
  2912. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  2913. struct batadv_hard_iface *primary_if = NULL;
  2914. struct batadv_tvlv_tt_change *tt_change;
  2915. struct batadv_orig_node *orig_node;
  2916. u8 my_ttvn, req_ttvn;
  2917. u16 tvlv_len;
  2918. bool full_table;
  2919. s32 tt_len;
  2920. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2921. "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
  2922. req_src, tt_data->ttvn,
  2923. ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
  2924. spin_lock_bh(&bat_priv->tt.commit_lock);
  2925. my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
  2926. req_ttvn = tt_data->ttvn;
  2927. orig_node = batadv_orig_hash_find(bat_priv, req_src);
  2928. if (!orig_node)
  2929. goto out;
  2930. primary_if = batadv_primary_if_get_selected(bat_priv);
  2931. if (!primary_if)
  2932. goto out;
  2933. /* If the full table has been explicitly requested or the gap
  2934. * is too big send the whole local translation table
  2935. */
  2936. if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
  2937. !bat_priv->tt.last_changeset)
  2938. full_table = true;
  2939. else
  2940. full_table = false;
  2941. /* TT fragmentation hasn't been implemented yet, so send as many
  2942. * TT entries fit a single packet as possible only
  2943. */
  2944. if (!full_table) {
  2945. spin_lock_bh(&bat_priv->tt.last_changeset_lock);
  2946. tt_len = bat_priv->tt.last_changeset_len;
  2947. tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
  2948. &tvlv_tt_data,
  2949. &tt_change,
  2950. &tt_len);
  2951. if (!tt_len || !tvlv_len)
  2952. goto unlock;
  2953. /* Copy the last orig_node's OGM buffer */
  2954. memcpy(tt_change, bat_priv->tt.last_changeset,
  2955. bat_priv->tt.last_changeset_len);
  2956. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  2957. } else {
  2958. req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
  2959. /* allocate the tvlv, put the tt_data and all the tt_vlan_data
  2960. * in the initial part
  2961. */
  2962. tt_len = -1;
  2963. tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
  2964. &tvlv_tt_data,
  2965. &tt_change,
  2966. &tt_len);
  2967. if (!tt_len || !tvlv_len)
  2968. goto out;
  2969. /* fill the rest of the tvlv with the real TT entries */
  2970. batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
  2971. tt_change, tt_len,
  2972. batadv_tt_local_valid, NULL);
  2973. }
  2974. tvlv_tt_data->flags = BATADV_TT_RESPONSE;
  2975. tvlv_tt_data->ttvn = req_ttvn;
  2976. if (full_table)
  2977. tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
  2978. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2979. "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
  2980. orig_node->orig, full_table ? 'F' : '.', req_ttvn);
  2981. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
  2982. batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
  2983. req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
  2984. tvlv_len);
  2985. goto out;
  2986. unlock:
  2987. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  2988. out:
  2989. spin_unlock_bh(&bat_priv->tt.commit_lock);
  2990. if (orig_node)
  2991. batadv_orig_node_put(orig_node);
  2992. if (primary_if)
  2993. batadv_hardif_put(primary_if);
  2994. kfree(tvlv_tt_data);
  2995. /* The packet was for this host, so it doesn't need to be re-routed */
  2996. return true;
  2997. }
  2998. /**
  2999. * batadv_send_tt_response() - send reply to tt request
  3000. * @bat_priv: the bat priv with all the soft interface information
  3001. * @tt_data: tt data containing the tt request information
  3002. * @req_src: mac address of tt request sender
  3003. * @req_dst: mac address of tt request recipient
  3004. *
  3005. * Return: true if tt request reply was sent, false otherwise.
  3006. */
  3007. static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
  3008. struct batadv_tvlv_tt_data *tt_data,
  3009. u8 *req_src, u8 *req_dst)
  3010. {
  3011. if (batadv_is_my_mac(bat_priv, req_dst))
  3012. return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
  3013. return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
  3014. req_dst);
  3015. }
  3016. static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
  3017. struct batadv_orig_node *orig_node,
  3018. struct batadv_tvlv_tt_change *tt_change,
  3019. u16 tt_num_changes, u8 ttvn)
  3020. {
  3021. int i;
  3022. int roams;
  3023. for (i = 0; i < tt_num_changes; i++) {
  3024. if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
  3025. roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
  3026. batadv_tt_global_del(bat_priv, orig_node,
  3027. (tt_change + i)->addr,
  3028. ntohs((tt_change + i)->vid),
  3029. "tt removed by changes",
  3030. roams);
  3031. } else {
  3032. if (!batadv_tt_global_add(bat_priv, orig_node,
  3033. (tt_change + i)->addr,
  3034. ntohs((tt_change + i)->vid),
  3035. (tt_change + i)->flags, ttvn))
  3036. /* In case of problem while storing a
  3037. * global_entry, we stop the updating
  3038. * procedure without committing the
  3039. * ttvn change. This will avoid to send
  3040. * corrupted data on tt_request
  3041. */
  3042. return;
  3043. }
  3044. }
  3045. set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
  3046. }
  3047. static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
  3048. struct batadv_tvlv_tt_change *tt_change,
  3049. u8 ttvn, u8 *resp_src,
  3050. u16 num_entries)
  3051. {
  3052. struct batadv_orig_node *orig_node;
  3053. orig_node = batadv_orig_hash_find(bat_priv, resp_src);
  3054. if (!orig_node)
  3055. goto out;
  3056. /* Purge the old table first.. */
  3057. batadv_tt_global_del_orig(bat_priv, orig_node, -1,
  3058. "Received full table");
  3059. _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
  3060. ttvn);
  3061. spin_lock_bh(&orig_node->tt_buff_lock);
  3062. kfree(orig_node->tt_buff);
  3063. orig_node->tt_buff_len = 0;
  3064. orig_node->tt_buff = NULL;
  3065. spin_unlock_bh(&orig_node->tt_buff_lock);
  3066. atomic_set(&orig_node->last_ttvn, ttvn);
  3067. out:
  3068. if (orig_node)
  3069. batadv_orig_node_put(orig_node);
  3070. }
  3071. static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
  3072. struct batadv_orig_node *orig_node,
  3073. u16 tt_num_changes, u8 ttvn,
  3074. struct batadv_tvlv_tt_change *tt_change)
  3075. {
  3076. _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
  3077. tt_num_changes, ttvn);
  3078. batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
  3079. batadv_tt_len(tt_num_changes));
  3080. atomic_set(&orig_node->last_ttvn, ttvn);
  3081. }
  3082. /**
  3083. * batadv_is_my_client() - check if a client is served by the local node
  3084. * @bat_priv: the bat priv with all the soft interface information
  3085. * @addr: the mac address of the client to check
  3086. * @vid: VLAN identifier
  3087. *
  3088. * Return: true if the client is served by this node, false otherwise.
  3089. */
  3090. bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
  3091. unsigned short vid)
  3092. {
  3093. struct batadv_tt_local_entry *tt_local_entry;
  3094. bool ret = false;
  3095. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
  3096. if (!tt_local_entry)
  3097. goto out;
  3098. /* Check if the client has been logically deleted (but is kept for
  3099. * consistency purpose)
  3100. */
  3101. if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
  3102. (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
  3103. goto out;
  3104. ret = true;
  3105. out:
  3106. if (tt_local_entry)
  3107. batadv_tt_local_entry_put(tt_local_entry);
  3108. return ret;
  3109. }
  3110. /**
  3111. * batadv_handle_tt_response() - process incoming tt reply
  3112. * @bat_priv: the bat priv with all the soft interface information
  3113. * @tt_data: tt data containing the tt request information
  3114. * @resp_src: mac address of tt reply sender
  3115. * @num_entries: number of tt change entries appended to the tt data
  3116. */
  3117. static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
  3118. struct batadv_tvlv_tt_data *tt_data,
  3119. u8 *resp_src, u16 num_entries)
  3120. {
  3121. struct batadv_tt_req_node *node;
  3122. struct hlist_node *safe;
  3123. struct batadv_orig_node *orig_node = NULL;
  3124. struct batadv_tvlv_tt_change *tt_change;
  3125. u8 *tvlv_ptr = (u8 *)tt_data;
  3126. u16 change_offset;
  3127. batadv_dbg(BATADV_DBG_TT, bat_priv,
  3128. "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
  3129. resp_src, tt_data->ttvn, num_entries,
  3130. ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
  3131. orig_node = batadv_orig_hash_find(bat_priv, resp_src);
  3132. if (!orig_node)
  3133. goto out;
  3134. spin_lock_bh(&orig_node->tt_lock);
  3135. change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
  3136. change_offset *= ntohs(tt_data->num_vlan);
  3137. change_offset += sizeof(*tt_data);
  3138. tvlv_ptr += change_offset;
  3139. tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
  3140. if (tt_data->flags & BATADV_TT_FULL_TABLE) {
  3141. batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
  3142. resp_src, num_entries);
  3143. } else {
  3144. batadv_tt_update_changes(bat_priv, orig_node, num_entries,
  3145. tt_data->ttvn, tt_change);
  3146. }
  3147. /* Recalculate the CRC for this orig_node and store it */
  3148. batadv_tt_global_update_crc(bat_priv, orig_node);
  3149. spin_unlock_bh(&orig_node->tt_lock);
  3150. /* Delete the tt_req_node from pending tt_requests list */
  3151. spin_lock_bh(&bat_priv->tt.req_list_lock);
  3152. hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  3153. if (!batadv_compare_eth(node->addr, resp_src))
  3154. continue;
  3155. hlist_del_init(&node->list);
  3156. batadv_tt_req_node_put(node);
  3157. }
  3158. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  3159. out:
  3160. if (orig_node)
  3161. batadv_orig_node_put(orig_node);
  3162. }
  3163. static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
  3164. {
  3165. struct batadv_tt_roam_node *node, *safe;
  3166. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  3167. list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
  3168. list_del(&node->list);
  3169. kmem_cache_free(batadv_tt_roam_cache, node);
  3170. }
  3171. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  3172. }
  3173. static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
  3174. {
  3175. struct batadv_tt_roam_node *node, *safe;
  3176. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  3177. list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
  3178. if (!batadv_has_timed_out(node->first_time,
  3179. BATADV_ROAMING_MAX_TIME))
  3180. continue;
  3181. list_del(&node->list);
  3182. kmem_cache_free(batadv_tt_roam_cache, node);
  3183. }
  3184. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  3185. }
  3186. /**
  3187. * batadv_tt_check_roam_count() - check if a client has roamed too frequently
  3188. * @bat_priv: the bat priv with all the soft interface information
  3189. * @client: mac address of the roaming client
  3190. *
  3191. * This function checks whether the client already reached the
  3192. * maximum number of possible roaming phases. In this case the ROAMING_ADV
  3193. * will not be sent.
  3194. *
  3195. * Return: true if the ROAMING_ADV can be sent, false otherwise
  3196. */
  3197. static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
  3198. {
  3199. struct batadv_tt_roam_node *tt_roam_node;
  3200. bool ret = false;
  3201. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  3202. /* The new tt_req will be issued only if I'm not waiting for a
  3203. * reply from the same orig_node yet
  3204. */
  3205. list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
  3206. if (!batadv_compare_eth(tt_roam_node->addr, client))
  3207. continue;
  3208. if (batadv_has_timed_out(tt_roam_node->first_time,
  3209. BATADV_ROAMING_MAX_TIME))
  3210. continue;
  3211. if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
  3212. /* Sorry, you roamed too many times! */
  3213. goto unlock;
  3214. ret = true;
  3215. break;
  3216. }
  3217. if (!ret) {
  3218. tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
  3219. GFP_ATOMIC);
  3220. if (!tt_roam_node)
  3221. goto unlock;
  3222. tt_roam_node->first_time = jiffies;
  3223. atomic_set(&tt_roam_node->counter,
  3224. BATADV_ROAMING_MAX_COUNT - 1);
  3225. ether_addr_copy(tt_roam_node->addr, client);
  3226. list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
  3227. ret = true;
  3228. }
  3229. unlock:
  3230. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  3231. return ret;
  3232. }
  3233. /**
  3234. * batadv_send_roam_adv() - send a roaming advertisement message
  3235. * @bat_priv: the bat priv with all the soft interface information
  3236. * @client: mac address of the roaming client
  3237. * @vid: VLAN identifier
  3238. * @orig_node: message destination
  3239. *
  3240. * Send a ROAMING_ADV message to the node which was previously serving this
  3241. * client. This is done to inform the node that from now on all traffic destined
  3242. * for this particular roamed client has to be forwarded to the sender of the
  3243. * roaming message.
  3244. */
  3245. static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
  3246. unsigned short vid,
  3247. struct batadv_orig_node *orig_node)
  3248. {
  3249. struct batadv_hard_iface *primary_if;
  3250. struct batadv_tvlv_roam_adv tvlv_roam;
  3251. primary_if = batadv_primary_if_get_selected(bat_priv);
  3252. if (!primary_if)
  3253. goto out;
  3254. /* before going on we have to check whether the client has
  3255. * already roamed to us too many times
  3256. */
  3257. if (!batadv_tt_check_roam_count(bat_priv, client))
  3258. goto out;
  3259. batadv_dbg(BATADV_DBG_TT, bat_priv,
  3260. "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
  3261. orig_node->orig, client, batadv_print_vid(vid));
  3262. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
  3263. memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
  3264. tvlv_roam.vid = htons(vid);
  3265. batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
  3266. orig_node->orig, BATADV_TVLV_ROAM, 1,
  3267. &tvlv_roam, sizeof(tvlv_roam));
  3268. out:
  3269. if (primary_if)
  3270. batadv_hardif_put(primary_if);
  3271. }
  3272. static void batadv_tt_purge(struct work_struct *work)
  3273. {
  3274. struct delayed_work *delayed_work;
  3275. struct batadv_priv_tt *priv_tt;
  3276. struct batadv_priv *bat_priv;
  3277. delayed_work = to_delayed_work(work);
  3278. priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
  3279. bat_priv = container_of(priv_tt, struct batadv_priv, tt);
  3280. batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
  3281. batadv_tt_global_purge(bat_priv);
  3282. batadv_tt_req_purge(bat_priv);
  3283. batadv_tt_roam_purge(bat_priv);
  3284. queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
  3285. msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
  3286. }
  3287. /**
  3288. * batadv_tt_free() - Free translation table of soft interface
  3289. * @bat_priv: the bat priv with all the soft interface information
  3290. */
  3291. void batadv_tt_free(struct batadv_priv *bat_priv)
  3292. {
  3293. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_ROAM, 1);
  3294. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
  3295. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
  3296. cancel_delayed_work_sync(&bat_priv->tt.work);
  3297. batadv_tt_local_table_free(bat_priv);
  3298. batadv_tt_global_table_free(bat_priv);
  3299. batadv_tt_req_list_free(bat_priv);
  3300. batadv_tt_changes_list_free(bat_priv);
  3301. batadv_tt_roam_list_free(bat_priv);
  3302. kfree(bat_priv->tt.last_changeset);
  3303. }
  3304. /**
  3305. * batadv_tt_local_set_flags() - set or unset the specified flags on the local
  3306. * table and possibly count them in the TT size
  3307. * @bat_priv: the bat priv with all the soft interface information
  3308. * @flags: the flag to switch
  3309. * @enable: whether to set or unset the flag
  3310. * @count: whether to increase the TT size by the number of changed entries
  3311. */
  3312. static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
  3313. bool enable, bool count)
  3314. {
  3315. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  3316. struct batadv_tt_common_entry *tt_common_entry;
  3317. struct hlist_head *head;
  3318. u32 i;
  3319. if (!hash)
  3320. return;
  3321. for (i = 0; i < hash->size; i++) {
  3322. head = &hash->table[i];
  3323. rcu_read_lock();
  3324. hlist_for_each_entry_rcu(tt_common_entry,
  3325. head, hash_entry) {
  3326. if (enable) {
  3327. if ((tt_common_entry->flags & flags) == flags)
  3328. continue;
  3329. tt_common_entry->flags |= flags;
  3330. } else {
  3331. if (!(tt_common_entry->flags & flags))
  3332. continue;
  3333. tt_common_entry->flags &= ~flags;
  3334. }
  3335. if (!count)
  3336. continue;
  3337. batadv_tt_local_size_inc(bat_priv,
  3338. tt_common_entry->vid);
  3339. }
  3340. rcu_read_unlock();
  3341. }
  3342. }
  3343. /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
  3344. static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
  3345. {
  3346. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  3347. struct batadv_tt_common_entry *tt_common;
  3348. struct batadv_tt_local_entry *tt_local;
  3349. struct hlist_node *node_tmp;
  3350. struct hlist_head *head;
  3351. spinlock_t *list_lock; /* protects write access to the hash lists */
  3352. u32 i;
  3353. if (!hash)
  3354. return;
  3355. for (i = 0; i < hash->size; i++) {
  3356. head = &hash->table[i];
  3357. list_lock = &hash->list_locks[i];
  3358. spin_lock_bh(list_lock);
  3359. hlist_for_each_entry_safe(tt_common, node_tmp, head,
  3360. hash_entry) {
  3361. if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
  3362. continue;
  3363. batadv_dbg(BATADV_DBG_TT, bat_priv,
  3364. "Deleting local tt entry (%pM, vid: %d): pending\n",
  3365. tt_common->addr,
  3366. batadv_print_vid(tt_common->vid));
  3367. batadv_tt_local_size_dec(bat_priv, tt_common->vid);
  3368. hlist_del_rcu(&tt_common->hash_entry);
  3369. tt_local = container_of(tt_common,
  3370. struct batadv_tt_local_entry,
  3371. common);
  3372. batadv_tt_local_entry_put(tt_local);
  3373. }
  3374. spin_unlock_bh(list_lock);
  3375. }
  3376. }
  3377. /**
  3378. * batadv_tt_local_commit_changes_nolock() - commit all pending local tt changes
  3379. * which have been queued in the time since the last commit
  3380. * @bat_priv: the bat priv with all the soft interface information
  3381. *
  3382. * Caller must hold tt->commit_lock.
  3383. */
  3384. static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
  3385. {
  3386. lockdep_assert_held(&bat_priv->tt.commit_lock);
  3387. if (atomic_read(&bat_priv->tt.local_changes) < 1) {
  3388. if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
  3389. batadv_tt_tvlv_container_update(bat_priv);
  3390. return;
  3391. }
  3392. batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
  3393. batadv_tt_local_purge_pending_clients(bat_priv);
  3394. batadv_tt_local_update_crc(bat_priv);
  3395. /* Increment the TTVN only once per OGM interval */
  3396. atomic_inc(&bat_priv->tt.vn);
  3397. batadv_dbg(BATADV_DBG_TT, bat_priv,
  3398. "Local changes committed, updating to ttvn %u\n",
  3399. (u8)atomic_read(&bat_priv->tt.vn));
  3400. /* reset the sending counter */
  3401. atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
  3402. batadv_tt_tvlv_container_update(bat_priv);
  3403. }
  3404. /**
  3405. * batadv_tt_local_commit_changes() - commit all pending local tt changes which
  3406. * have been queued in the time since the last commit
  3407. * @bat_priv: the bat priv with all the soft interface information
  3408. */
  3409. void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
  3410. {
  3411. spin_lock_bh(&bat_priv->tt.commit_lock);
  3412. batadv_tt_local_commit_changes_nolock(bat_priv);
  3413. spin_unlock_bh(&bat_priv->tt.commit_lock);
  3414. }
  3415. /**
  3416. * batadv_is_ap_isolated() - Check if packet from upper layer should be dropped
  3417. * @bat_priv: the bat priv with all the soft interface information
  3418. * @src: source mac address of packet
  3419. * @dst: destination mac address of packet
  3420. * @vid: vlan id of packet
  3421. *
  3422. * Return: true when src+dst(+vid) pair should be isolated, false otherwise
  3423. */
  3424. bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
  3425. unsigned short vid)
  3426. {
  3427. struct batadv_tt_local_entry *tt_local_entry;
  3428. struct batadv_tt_global_entry *tt_global_entry;
  3429. struct batadv_softif_vlan *vlan;
  3430. bool ret = false;
  3431. vlan = batadv_softif_vlan_get(bat_priv, vid);
  3432. if (!vlan)
  3433. return false;
  3434. if (!atomic_read(&vlan->ap_isolation))
  3435. goto vlan_put;
  3436. tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
  3437. if (!tt_local_entry)
  3438. goto vlan_put;
  3439. tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
  3440. if (!tt_global_entry)
  3441. goto local_entry_put;
  3442. if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
  3443. ret = true;
  3444. batadv_tt_global_entry_put(tt_global_entry);
  3445. local_entry_put:
  3446. batadv_tt_local_entry_put(tt_local_entry);
  3447. vlan_put:
  3448. batadv_softif_vlan_put(vlan);
  3449. return ret;
  3450. }
  3451. /**
  3452. * batadv_tt_update_orig() - update global translation table with new tt
  3453. * information received via ogms
  3454. * @bat_priv: the bat priv with all the soft interface information
  3455. * @orig_node: the orig_node of the ogm
  3456. * @tt_buff: pointer to the first tvlv VLAN entry
  3457. * @tt_num_vlan: number of tvlv VLAN entries
  3458. * @tt_change: pointer to the first entry in the TT buffer
  3459. * @tt_num_changes: number of tt changes inside the tt buffer
  3460. * @ttvn: translation table version number of this changeset
  3461. */
  3462. static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
  3463. struct batadv_orig_node *orig_node,
  3464. const void *tt_buff, u16 tt_num_vlan,
  3465. struct batadv_tvlv_tt_change *tt_change,
  3466. u16 tt_num_changes, u8 ttvn)
  3467. {
  3468. u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
  3469. struct batadv_tvlv_tt_vlan_data *tt_vlan;
  3470. bool full_table = true;
  3471. bool has_tt_init;
  3472. tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
  3473. has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
  3474. &orig_node->capa_initialized);
  3475. /* orig table not initialised AND first diff is in the OGM OR the ttvn
  3476. * increased by one -> we can apply the attached changes
  3477. */
  3478. if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
  3479. /* the OGM could not contain the changes due to their size or
  3480. * because they have already been sent BATADV_TT_OGM_APPEND_MAX
  3481. * times.
  3482. * In this case send a tt request
  3483. */
  3484. if (!tt_num_changes) {
  3485. full_table = false;
  3486. goto request_table;
  3487. }
  3488. spin_lock_bh(&orig_node->tt_lock);
  3489. batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
  3490. ttvn, tt_change);
  3491. /* Even if we received the precomputed crc with the OGM, we
  3492. * prefer to recompute it to spot any possible inconsistency
  3493. * in the global table
  3494. */
  3495. batadv_tt_global_update_crc(bat_priv, orig_node);
  3496. spin_unlock_bh(&orig_node->tt_lock);
  3497. /* The ttvn alone is not enough to guarantee consistency
  3498. * because a single value could represent different states
  3499. * (due to the wrap around). Thus a node has to check whether
  3500. * the resulting table (after applying the changes) is still
  3501. * consistent or not. E.g. a node could disconnect while its
  3502. * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
  3503. * checking the CRC value is mandatory to detect the
  3504. * inconsistency
  3505. */
  3506. if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
  3507. tt_num_vlan))
  3508. goto request_table;
  3509. } else {
  3510. /* if we missed more than one change or our tables are not
  3511. * in sync anymore -> request fresh tt data
  3512. */
  3513. if (!has_tt_init || ttvn != orig_ttvn ||
  3514. !batadv_tt_global_check_crc(orig_node, tt_vlan,
  3515. tt_num_vlan)) {
  3516. request_table:
  3517. batadv_dbg(BATADV_DBG_TT, bat_priv,
  3518. "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
  3519. orig_node->orig, ttvn, orig_ttvn,
  3520. tt_num_changes);
  3521. batadv_send_tt_request(bat_priv, orig_node, ttvn,
  3522. tt_vlan, tt_num_vlan,
  3523. full_table);
  3524. return;
  3525. }
  3526. }
  3527. }
  3528. /**
  3529. * batadv_tt_global_client_is_roaming() - check if a client is marked as roaming
  3530. * @bat_priv: the bat priv with all the soft interface information
  3531. * @addr: the mac address of the client to check
  3532. * @vid: VLAN identifier
  3533. *
  3534. * Return: true if we know that the client has moved from its old originator
  3535. * to another one. This entry is still kept for consistency purposes and will be
  3536. * deleted later by a DEL or because of timeout
  3537. */
  3538. bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
  3539. u8 *addr, unsigned short vid)
  3540. {
  3541. struct batadv_tt_global_entry *tt_global_entry;
  3542. bool ret = false;
  3543. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  3544. if (!tt_global_entry)
  3545. goto out;
  3546. ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
  3547. batadv_tt_global_entry_put(tt_global_entry);
  3548. out:
  3549. return ret;
  3550. }
  3551. /**
  3552. * batadv_tt_local_client_is_roaming() - tells whether the client is roaming
  3553. * @bat_priv: the bat priv with all the soft interface information
  3554. * @addr: the mac address of the local client to query
  3555. * @vid: VLAN identifier
  3556. *
  3557. * Return: true if the local client is known to be roaming (it is not served by
  3558. * this node anymore) or not. If yes, the client is still present in the table
  3559. * to keep the latter consistent with the node TTVN
  3560. */
  3561. bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
  3562. u8 *addr, unsigned short vid)
  3563. {
  3564. struct batadv_tt_local_entry *tt_local_entry;
  3565. bool ret = false;
  3566. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
  3567. if (!tt_local_entry)
  3568. goto out;
  3569. ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
  3570. batadv_tt_local_entry_put(tt_local_entry);
  3571. out:
  3572. return ret;
  3573. }
  3574. /**
  3575. * batadv_tt_add_temporary_global_entry() - Add temporary entry to global TT
  3576. * @bat_priv: the bat priv with all the soft interface information
  3577. * @orig_node: orig node which the temporary entry should be associated with
  3578. * @addr: mac address of the client
  3579. * @vid: VLAN id of the new temporary global translation table
  3580. *
  3581. * Return: true when temporary tt entry could be added, false otherwise
  3582. */
  3583. bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
  3584. struct batadv_orig_node *orig_node,
  3585. const unsigned char *addr,
  3586. unsigned short vid)
  3587. {
  3588. /* ignore loop detect macs, they are not supposed to be in the tt local
  3589. * data as well.
  3590. */
  3591. if (batadv_bla_is_loopdetect_mac(addr))
  3592. return false;
  3593. if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
  3594. BATADV_TT_CLIENT_TEMP,
  3595. atomic_read(&orig_node->last_ttvn)))
  3596. return false;
  3597. batadv_dbg(BATADV_DBG_TT, bat_priv,
  3598. "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
  3599. addr, batadv_print_vid(vid), orig_node->orig);
  3600. return true;
  3601. }
  3602. /**
  3603. * batadv_tt_local_resize_to_mtu() - resize the local translation table fit the
  3604. * maximum packet size that can be transported through the mesh
  3605. * @soft_iface: netdev struct of the mesh interface
  3606. *
  3607. * Remove entries older than 'timeout' and half timeout if more entries need
  3608. * to be removed.
  3609. */
  3610. void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
  3611. {
  3612. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  3613. int packet_size_max = atomic_read(&bat_priv->packet_size_max);
  3614. int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
  3615. bool reduced = false;
  3616. spin_lock_bh(&bat_priv->tt.commit_lock);
  3617. while (true) {
  3618. table_size = batadv_tt_local_table_transmit_size(bat_priv);
  3619. if (packet_size_max >= table_size)
  3620. break;
  3621. batadv_tt_local_purge(bat_priv, timeout);
  3622. batadv_tt_local_purge_pending_clients(bat_priv);
  3623. timeout /= 2;
  3624. reduced = true;
  3625. net_ratelimited_function(batadv_info, soft_iface,
  3626. "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
  3627. packet_size_max);
  3628. }
  3629. /* commit these changes immediately, to avoid synchronization problem
  3630. * with the TTVN
  3631. */
  3632. if (reduced)
  3633. batadv_tt_local_commit_changes_nolock(bat_priv);
  3634. spin_unlock_bh(&bat_priv->tt.commit_lock);
  3635. }
  3636. /**
  3637. * batadv_tt_tvlv_ogm_handler_v1() - process incoming tt tvlv container
  3638. * @bat_priv: the bat priv with all the soft interface information
  3639. * @orig: the orig_node of the ogm
  3640. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  3641. * @tvlv_value: tvlv buffer containing the gateway data
  3642. * @tvlv_value_len: tvlv buffer length
  3643. */
  3644. static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  3645. struct batadv_orig_node *orig,
  3646. u8 flags, void *tvlv_value,
  3647. u16 tvlv_value_len)
  3648. {
  3649. struct batadv_tvlv_tt_vlan_data *tt_vlan;
  3650. struct batadv_tvlv_tt_change *tt_change;
  3651. struct batadv_tvlv_tt_data *tt_data;
  3652. u16 num_entries, num_vlan;
  3653. if (tvlv_value_len < sizeof(*tt_data))
  3654. return;
  3655. tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
  3656. tvlv_value_len -= sizeof(*tt_data);
  3657. num_vlan = ntohs(tt_data->num_vlan);
  3658. if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
  3659. return;
  3660. tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
  3661. tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
  3662. tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
  3663. num_entries = batadv_tt_entries(tvlv_value_len);
  3664. batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
  3665. num_entries, tt_data->ttvn);
  3666. }
  3667. /**
  3668. * batadv_tt_tvlv_unicast_handler_v1() - process incoming (unicast) tt tvlv
  3669. * container
  3670. * @bat_priv: the bat priv with all the soft interface information
  3671. * @src: mac address of tt tvlv sender
  3672. * @dst: mac address of tt tvlv recipient
  3673. * @tvlv_value: tvlv buffer containing the tt data
  3674. * @tvlv_value_len: tvlv buffer length
  3675. *
  3676. * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
  3677. * otherwise.
  3678. */
  3679. static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
  3680. u8 *src, u8 *dst,
  3681. void *tvlv_value,
  3682. u16 tvlv_value_len)
  3683. {
  3684. struct batadv_tvlv_tt_data *tt_data;
  3685. u16 tt_vlan_len, tt_num_entries;
  3686. char tt_flag;
  3687. bool ret;
  3688. if (tvlv_value_len < sizeof(*tt_data))
  3689. return NET_RX_SUCCESS;
  3690. tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
  3691. tvlv_value_len -= sizeof(*tt_data);
  3692. tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
  3693. tt_vlan_len *= ntohs(tt_data->num_vlan);
  3694. if (tvlv_value_len < tt_vlan_len)
  3695. return NET_RX_SUCCESS;
  3696. tvlv_value_len -= tt_vlan_len;
  3697. tt_num_entries = batadv_tt_entries(tvlv_value_len);
  3698. switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
  3699. case BATADV_TT_REQUEST:
  3700. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
  3701. /* If this node cannot provide a TT response the tt_request is
  3702. * forwarded
  3703. */
  3704. ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
  3705. if (!ret) {
  3706. if (tt_data->flags & BATADV_TT_FULL_TABLE)
  3707. tt_flag = 'F';
  3708. else
  3709. tt_flag = '.';
  3710. batadv_dbg(BATADV_DBG_TT, bat_priv,
  3711. "Routing TT_REQUEST to %pM [%c]\n",
  3712. dst, tt_flag);
  3713. /* tvlv API will re-route the packet */
  3714. return NET_RX_DROP;
  3715. }
  3716. break;
  3717. case BATADV_TT_RESPONSE:
  3718. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
  3719. if (batadv_is_my_mac(bat_priv, dst)) {
  3720. batadv_handle_tt_response(bat_priv, tt_data,
  3721. src, tt_num_entries);
  3722. return NET_RX_SUCCESS;
  3723. }
  3724. if (tt_data->flags & BATADV_TT_FULL_TABLE)
  3725. tt_flag = 'F';
  3726. else
  3727. tt_flag = '.';
  3728. batadv_dbg(BATADV_DBG_TT, bat_priv,
  3729. "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
  3730. /* tvlv API will re-route the packet */
  3731. return NET_RX_DROP;
  3732. }
  3733. return NET_RX_SUCCESS;
  3734. }
  3735. /**
  3736. * batadv_roam_tvlv_unicast_handler_v1() - process incoming tt roam tvlv
  3737. * container
  3738. * @bat_priv: the bat priv with all the soft interface information
  3739. * @src: mac address of tt tvlv sender
  3740. * @dst: mac address of tt tvlv recipient
  3741. * @tvlv_value: tvlv buffer containing the tt data
  3742. * @tvlv_value_len: tvlv buffer length
  3743. *
  3744. * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
  3745. * otherwise.
  3746. */
  3747. static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
  3748. u8 *src, u8 *dst,
  3749. void *tvlv_value,
  3750. u16 tvlv_value_len)
  3751. {
  3752. struct batadv_tvlv_roam_adv *roaming_adv;
  3753. struct batadv_orig_node *orig_node = NULL;
  3754. /* If this node is not the intended recipient of the
  3755. * roaming advertisement the packet is forwarded
  3756. * (the tvlv API will re-route the packet).
  3757. */
  3758. if (!batadv_is_my_mac(bat_priv, dst))
  3759. return NET_RX_DROP;
  3760. if (tvlv_value_len < sizeof(*roaming_adv))
  3761. goto out;
  3762. orig_node = batadv_orig_hash_find(bat_priv, src);
  3763. if (!orig_node)
  3764. goto out;
  3765. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
  3766. roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
  3767. batadv_dbg(BATADV_DBG_TT, bat_priv,
  3768. "Received ROAMING_ADV from %pM (client %pM)\n",
  3769. src, roaming_adv->client);
  3770. batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
  3771. ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
  3772. atomic_read(&orig_node->last_ttvn) + 1);
  3773. out:
  3774. if (orig_node)
  3775. batadv_orig_node_put(orig_node);
  3776. return NET_RX_SUCCESS;
  3777. }
  3778. /**
  3779. * batadv_tt_init() - initialise the translation table internals
  3780. * @bat_priv: the bat priv with all the soft interface information
  3781. *
  3782. * Return: 0 on success or negative error number in case of failure.
  3783. */
  3784. int batadv_tt_init(struct batadv_priv *bat_priv)
  3785. {
  3786. int ret;
  3787. /* synchronized flags must be remote */
  3788. BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
  3789. ret = batadv_tt_local_init(bat_priv);
  3790. if (ret < 0)
  3791. return ret;
  3792. ret = batadv_tt_global_init(bat_priv);
  3793. if (ret < 0)
  3794. return ret;
  3795. batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
  3796. batadv_tt_tvlv_unicast_handler_v1,
  3797. BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
  3798. batadv_tvlv_handler_register(bat_priv, NULL,
  3799. batadv_roam_tvlv_unicast_handler_v1,
  3800. BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
  3801. INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
  3802. queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
  3803. msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
  3804. return 1;
  3805. }
  3806. /**
  3807. * batadv_tt_global_is_isolated() - check if a client is marked as isolated
  3808. * @bat_priv: the bat priv with all the soft interface information
  3809. * @addr: the mac address of the client
  3810. * @vid: the identifier of the VLAN where this client is connected
  3811. *
  3812. * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
  3813. * otherwise
  3814. */
  3815. bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
  3816. const u8 *addr, unsigned short vid)
  3817. {
  3818. struct batadv_tt_global_entry *tt;
  3819. bool ret;
  3820. tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
  3821. if (!tt)
  3822. return false;
  3823. ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
  3824. batadv_tt_global_entry_put(tt);
  3825. return ret;
  3826. }
  3827. /**
  3828. * batadv_tt_cache_init() - Initialize tt memory object cache
  3829. *
  3830. * Return: 0 on success or negative error number in case of failure.
  3831. */
  3832. int __init batadv_tt_cache_init(void)
  3833. {
  3834. size_t tl_size = sizeof(struct batadv_tt_local_entry);
  3835. size_t tg_size = sizeof(struct batadv_tt_global_entry);
  3836. size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
  3837. size_t tt_change_size = sizeof(struct batadv_tt_change_node);
  3838. size_t tt_req_size = sizeof(struct batadv_tt_req_node);
  3839. size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
  3840. batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
  3841. SLAB_HWCACHE_ALIGN, NULL);
  3842. if (!batadv_tl_cache)
  3843. return -ENOMEM;
  3844. batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
  3845. SLAB_HWCACHE_ALIGN, NULL);
  3846. if (!batadv_tg_cache)
  3847. goto err_tt_tl_destroy;
  3848. batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
  3849. tt_orig_size, 0,
  3850. SLAB_HWCACHE_ALIGN, NULL);
  3851. if (!batadv_tt_orig_cache)
  3852. goto err_tt_tg_destroy;
  3853. batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
  3854. tt_change_size, 0,
  3855. SLAB_HWCACHE_ALIGN, NULL);
  3856. if (!batadv_tt_change_cache)
  3857. goto err_tt_orig_destroy;
  3858. batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
  3859. tt_req_size, 0,
  3860. SLAB_HWCACHE_ALIGN, NULL);
  3861. if (!batadv_tt_req_cache)
  3862. goto err_tt_change_destroy;
  3863. batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
  3864. tt_roam_size, 0,
  3865. SLAB_HWCACHE_ALIGN, NULL);
  3866. if (!batadv_tt_roam_cache)
  3867. goto err_tt_req_destroy;
  3868. return 0;
  3869. err_tt_req_destroy:
  3870. kmem_cache_destroy(batadv_tt_req_cache);
  3871. batadv_tt_req_cache = NULL;
  3872. err_tt_change_destroy:
  3873. kmem_cache_destroy(batadv_tt_change_cache);
  3874. batadv_tt_change_cache = NULL;
  3875. err_tt_orig_destroy:
  3876. kmem_cache_destroy(batadv_tt_orig_cache);
  3877. batadv_tt_orig_cache = NULL;
  3878. err_tt_tg_destroy:
  3879. kmem_cache_destroy(batadv_tg_cache);
  3880. batadv_tg_cache = NULL;
  3881. err_tt_tl_destroy:
  3882. kmem_cache_destroy(batadv_tl_cache);
  3883. batadv_tl_cache = NULL;
  3884. return -ENOMEM;
  3885. }
  3886. /**
  3887. * batadv_tt_cache_destroy() - Destroy tt memory object cache
  3888. */
  3889. void batadv_tt_cache_destroy(void)
  3890. {
  3891. kmem_cache_destroy(batadv_tl_cache);
  3892. kmem_cache_destroy(batadv_tg_cache);
  3893. kmem_cache_destroy(batadv_tt_orig_cache);
  3894. kmem_cache_destroy(batadv_tt_change_cache);
  3895. kmem_cache_destroy(batadv_tt_req_cache);
  3896. kmem_cache_destroy(batadv_tt_roam_cache);
  3897. }