translation-table.c 111 KB

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