translation-table.c 113 KB

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