translation-table.c 101 KB

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