translation-table.c 128 KB

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