translation-table.c 130 KB

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