translation-table.c 129 KB

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