translation-table.c 133 KB

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