translation-table.c 128 KB

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