translation-table.c 133 KB

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