translation-table.c 133 KB

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