translation-table.c 124 KB

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