translation-table.c 112 KB

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