translation-table.c 128 KB

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