translation-table.c 107 KB

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