translation-table.c 128 KB

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