translation-table.c 126 KB

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