translation-table.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932
  1. /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich, Antonio Quartulli
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "translation-table.h"
  21. #include "soft-interface.h"
  22. #include "hard-interface.h"
  23. #include "send.h"
  24. #include "hash.h"
  25. #include "originator.h"
  26. #include "routing.h"
  27. #include "bridge_loop_avoidance.h"
  28. #include <linux/crc32c.h>
  29. /* hash class keys */
  30. static struct lock_class_key batadv_tt_local_hash_lock_class_key;
  31. static struct lock_class_key batadv_tt_global_hash_lock_class_key;
  32. static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
  33. unsigned short vid,
  34. struct batadv_orig_node *orig_node);
  35. static void batadv_tt_purge(struct work_struct *work);
  36. static void
  37. batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
  38. static void batadv_tt_global_del(struct batadv_priv *bat_priv,
  39. struct batadv_orig_node *orig_node,
  40. const unsigned char *addr,
  41. unsigned short vid, const char *message,
  42. bool roaming);
  43. /* returns 1 if they are the same mac addr */
  44. static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
  45. {
  46. const void *data1 = container_of(node, struct batadv_tt_common_entry,
  47. hash_entry);
  48. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  49. }
  50. /**
  51. * batadv_choose_tt - return the index of the tt entry in the hash table
  52. * @data: pointer to the tt_common_entry object to map
  53. * @size: the size of the hash table
  54. *
  55. * Returns the hash index where the object represented by 'data' should be
  56. * stored at.
  57. */
  58. static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
  59. {
  60. struct batadv_tt_common_entry *tt;
  61. uint32_t hash = 0;
  62. tt = (struct batadv_tt_common_entry *)data;
  63. hash = batadv_hash_bytes(hash, &tt->addr, ETH_ALEN);
  64. hash = batadv_hash_bytes(hash, &tt->vid, sizeof(tt->vid));
  65. hash += (hash << 3);
  66. hash ^= (hash >> 11);
  67. hash += (hash << 15);
  68. return hash % size;
  69. }
  70. /**
  71. * batadv_tt_hash_find - look for a client in the given hash table
  72. * @hash: the hash table to search
  73. * @addr: the mac address of the client to look for
  74. * @vid: VLAN identifier
  75. *
  76. * Returns a pointer to the tt_common struct belonging to the searched client if
  77. * found, NULL otherwise.
  78. */
  79. static struct batadv_tt_common_entry *
  80. batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
  81. unsigned short vid)
  82. {
  83. struct hlist_head *head;
  84. struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
  85. uint32_t index;
  86. if (!hash)
  87. return NULL;
  88. memcpy(to_search.addr, addr, ETH_ALEN);
  89. to_search.vid = vid;
  90. index = batadv_choose_tt(&to_search, hash->size);
  91. head = &hash->table[index];
  92. rcu_read_lock();
  93. hlist_for_each_entry_rcu(tt, head, hash_entry) {
  94. if (!batadv_compare_eth(tt, addr))
  95. continue;
  96. if (tt->vid != vid)
  97. continue;
  98. if (!atomic_inc_not_zero(&tt->refcount))
  99. continue;
  100. tt_tmp = tt;
  101. break;
  102. }
  103. rcu_read_unlock();
  104. return tt_tmp;
  105. }
  106. /**
  107. * batadv_tt_local_hash_find - search the local table for a given client
  108. * @bat_priv: the bat priv with all the soft interface information
  109. * @addr: the mac address of the client to look for
  110. * @vid: VLAN identifier
  111. *
  112. * Returns a pointer to the corresponding tt_local_entry struct if the client is
  113. * found, NULL otherwise.
  114. */
  115. static struct batadv_tt_local_entry *
  116. batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
  117. unsigned short vid)
  118. {
  119. struct batadv_tt_common_entry *tt_common_entry;
  120. struct batadv_tt_local_entry *tt_local_entry = NULL;
  121. tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
  122. vid);
  123. if (tt_common_entry)
  124. tt_local_entry = container_of(tt_common_entry,
  125. struct batadv_tt_local_entry,
  126. common);
  127. return tt_local_entry;
  128. }
  129. /**
  130. * batadv_tt_global_hash_find - search the global table for a given client
  131. * @bat_priv: the bat priv with all the soft interface information
  132. * @addr: the mac address of the client to look for
  133. * @vid: VLAN identifier
  134. *
  135. * Returns a pointer to the corresponding tt_global_entry struct if the client
  136. * is found, NULL otherwise.
  137. */
  138. static struct batadv_tt_global_entry *
  139. batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
  140. unsigned short vid)
  141. {
  142. struct batadv_tt_common_entry *tt_common_entry;
  143. struct batadv_tt_global_entry *tt_global_entry = NULL;
  144. tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
  145. vid);
  146. if (tt_common_entry)
  147. tt_global_entry = container_of(tt_common_entry,
  148. struct batadv_tt_global_entry,
  149. common);
  150. return tt_global_entry;
  151. }
  152. static void
  153. batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
  154. {
  155. if (atomic_dec_and_test(&tt_local_entry->common.refcount))
  156. kfree_rcu(tt_local_entry, common.rcu);
  157. }
  158. /**
  159. * batadv_tt_global_entry_free_ref - decrement the refcounter for a
  160. * tt_global_entry and possibly free it
  161. * @tt_global_entry: the object to free
  162. */
  163. static void
  164. batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
  165. {
  166. if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
  167. batadv_tt_global_del_orig_list(tt_global_entry);
  168. kfree_rcu(tt_global_entry, common.rcu);
  169. }
  170. }
  171. static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
  172. {
  173. struct batadv_tt_orig_list_entry *orig_entry;
  174. orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
  175. /* We are in an rcu callback here, therefore we cannot use
  176. * batadv_orig_node_free_ref() and its call_rcu():
  177. * An rcu_barrier() wouldn't wait for that to finish
  178. */
  179. batadv_orig_node_free_ref_now(orig_entry->orig_node);
  180. kfree(orig_entry);
  181. }
  182. static void
  183. batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
  184. {
  185. if (!atomic_dec_and_test(&orig_entry->refcount))
  186. return;
  187. /* to avoid race conditions, immediately decrease the tt counter */
  188. atomic_dec(&orig_entry->orig_node->tt_size);
  189. call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
  190. }
  191. /**
  192. * batadv_tt_local_event - store a local TT event (ADD/DEL)
  193. * @bat_priv: the bat priv with all the soft interface information
  194. * @tt_local_entry: the TT entry involved in the event
  195. * @event_flags: flags to store in the event structure
  196. */
  197. static void batadv_tt_local_event(struct batadv_priv *bat_priv,
  198. struct batadv_tt_local_entry *tt_local_entry,
  199. uint8_t event_flags)
  200. {
  201. struct batadv_tt_change_node *tt_change_node, *entry, *safe;
  202. struct batadv_tt_common_entry *common = &tt_local_entry->common;
  203. uint8_t flags = common->flags | event_flags;
  204. bool event_removed = false;
  205. bool del_op_requested, del_op_entry;
  206. tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
  207. if (!tt_change_node)
  208. return;
  209. tt_change_node->change.flags = flags;
  210. tt_change_node->change.reserved = 0;
  211. memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
  212. tt_change_node->change.vid = htons(common->vid);
  213. del_op_requested = flags & BATADV_TT_CLIENT_DEL;
  214. /* check for ADD+DEL or DEL+ADD events */
  215. spin_lock_bh(&bat_priv->tt.changes_list_lock);
  216. list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
  217. list) {
  218. if (!batadv_compare_eth(entry->change.addr, common->addr))
  219. continue;
  220. /* DEL+ADD in the same orig interval have no effect and can be
  221. * removed to avoid silly behaviour on the receiver side. The
  222. * other way around (ADD+DEL) can happen in case of roaming of
  223. * a client still in the NEW state. Roaming of NEW clients is
  224. * now possible due to automatically recognition of "temporary"
  225. * clients
  226. */
  227. del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
  228. if (!del_op_requested && del_op_entry)
  229. goto del;
  230. if (del_op_requested && !del_op_entry)
  231. goto del;
  232. continue;
  233. del:
  234. list_del(&entry->list);
  235. kfree(entry);
  236. kfree(tt_change_node);
  237. event_removed = true;
  238. goto unlock;
  239. }
  240. /* track the change in the OGMinterval list */
  241. list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
  242. unlock:
  243. spin_unlock_bh(&bat_priv->tt.changes_list_lock);
  244. if (event_removed)
  245. atomic_dec(&bat_priv->tt.local_changes);
  246. else
  247. atomic_inc(&bat_priv->tt.local_changes);
  248. }
  249. /**
  250. * batadv_tt_len - compute length in bytes of given number of tt changes
  251. * @changes_num: number of tt changes
  252. *
  253. * Returns computed length in bytes.
  254. */
  255. static int batadv_tt_len(int changes_num)
  256. {
  257. return changes_num * sizeof(struct batadv_tvlv_tt_change);
  258. }
  259. /**
  260. * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
  261. * @tt_len: available space
  262. *
  263. * Returns the number of entries.
  264. */
  265. static uint16_t batadv_tt_entries(uint16_t tt_len)
  266. {
  267. return tt_len / batadv_tt_len(1);
  268. }
  269. static int batadv_tt_local_init(struct batadv_priv *bat_priv)
  270. {
  271. if (bat_priv->tt.local_hash)
  272. return 0;
  273. bat_priv->tt.local_hash = batadv_hash_new(1024);
  274. if (!bat_priv->tt.local_hash)
  275. return -ENOMEM;
  276. batadv_hash_set_lock_class(bat_priv->tt.local_hash,
  277. &batadv_tt_local_hash_lock_class_key);
  278. return 0;
  279. }
  280. static void batadv_tt_global_free(struct batadv_priv *bat_priv,
  281. struct batadv_tt_global_entry *tt_global,
  282. const char *message)
  283. {
  284. batadv_dbg(BATADV_DBG_TT, bat_priv,
  285. "Deleting global tt entry %pM (vid: %d): %s\n",
  286. tt_global->common.addr,
  287. BATADV_PRINT_VID(tt_global->common.vid), message);
  288. batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
  289. batadv_choose_tt, &tt_global->common);
  290. batadv_tt_global_entry_free_ref(tt_global);
  291. }
  292. /**
  293. * batadv_tt_local_add - add a new client to the local table or update an
  294. * existing client
  295. * @soft_iface: netdev struct of the mesh interface
  296. * @addr: the mac address of the client to add
  297. * @vid: VLAN identifier
  298. * @ifindex: index of the interface where the client is connected to (useful to
  299. * identify wireless clients)
  300. */
  301. void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
  302. unsigned short vid, int ifindex)
  303. {
  304. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  305. struct batadv_tt_local_entry *tt_local;
  306. struct batadv_tt_global_entry *tt_global;
  307. struct hlist_head *head;
  308. struct batadv_tt_orig_list_entry *orig_entry;
  309. int hash_added;
  310. bool roamed_back = false;
  311. tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
  312. tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
  313. if (tt_local) {
  314. tt_local->last_seen = jiffies;
  315. if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
  316. batadv_dbg(BATADV_DBG_TT, bat_priv,
  317. "Re-adding pending client %pM (vid: %d)\n",
  318. addr, BATADV_PRINT_VID(vid));
  319. /* whatever the reason why the PENDING flag was set,
  320. * this is a client which was enqueued to be removed in
  321. * this orig_interval. Since it popped up again, the
  322. * flag can be reset like it was never enqueued
  323. */
  324. tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
  325. goto add_event;
  326. }
  327. if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
  328. batadv_dbg(BATADV_DBG_TT, bat_priv,
  329. "Roaming client %pM (vid: %d) came back to its original location\n",
  330. addr, BATADV_PRINT_VID(vid));
  331. /* the ROAM flag is set because this client roamed away
  332. * and the node got a roaming_advertisement message. Now
  333. * that the client popped up again at its original
  334. * location such flag can be unset
  335. */
  336. tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
  337. roamed_back = true;
  338. }
  339. goto check_roaming;
  340. }
  341. tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
  342. if (!tt_local)
  343. goto out;
  344. batadv_dbg(BATADV_DBG_TT, bat_priv,
  345. "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
  346. addr, BATADV_PRINT_VID(vid),
  347. (uint8_t)atomic_read(&bat_priv->tt.vn));
  348. memcpy(tt_local->common.addr, addr, ETH_ALEN);
  349. /* The local entry has to be marked as NEW to avoid to send it in
  350. * a full table response going out before the next ttvn increment
  351. * (consistency check)
  352. */
  353. tt_local->common.flags = BATADV_TT_CLIENT_NEW;
  354. tt_local->common.vid = vid;
  355. if (batadv_is_wifi_iface(ifindex))
  356. tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
  357. atomic_set(&tt_local->common.refcount, 2);
  358. tt_local->last_seen = jiffies;
  359. tt_local->common.added_at = tt_local->last_seen;
  360. /* the batman interface mac address should never be purged */
  361. if (batadv_compare_eth(addr, soft_iface->dev_addr))
  362. tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
  363. hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
  364. batadv_choose_tt, &tt_local->common,
  365. &tt_local->common.hash_entry);
  366. if (unlikely(hash_added != 0)) {
  367. /* remove the reference for the hash */
  368. batadv_tt_local_entry_free_ref(tt_local);
  369. goto out;
  370. }
  371. add_event:
  372. batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
  373. check_roaming:
  374. /* Check whether it is a roaming, but don't do anything if the roaming
  375. * process has already been handled
  376. */
  377. if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
  378. /* These node are probably going to update their tt table */
  379. head = &tt_global->orig_list;
  380. rcu_read_lock();
  381. hlist_for_each_entry_rcu(orig_entry, head, list) {
  382. batadv_send_roam_adv(bat_priv, tt_global->common.addr,
  383. tt_global->common.vid,
  384. orig_entry->orig_node);
  385. }
  386. rcu_read_unlock();
  387. if (roamed_back) {
  388. batadv_tt_global_free(bat_priv, tt_global,
  389. "Roaming canceled");
  390. tt_global = NULL;
  391. } else {
  392. /* The global entry has to be marked as ROAMING and
  393. * has to be kept for consistency purpose
  394. */
  395. tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
  396. tt_global->roam_at = jiffies;
  397. }
  398. }
  399. out:
  400. if (tt_local)
  401. batadv_tt_local_entry_free_ref(tt_local);
  402. if (tt_global)
  403. batadv_tt_global_entry_free_ref(tt_global);
  404. }
  405. /**
  406. * batadv_tt_tvlv_container_update - update the translation table tvlv container
  407. * after local tt changes have been committed
  408. * @bat_priv: the bat priv with all the soft interface information
  409. */
  410. static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
  411. {
  412. struct batadv_tt_change_node *entry, *safe;
  413. struct batadv_tvlv_tt_data *tt_data;
  414. struct batadv_tvlv_tt_change *tt_change;
  415. int tt_diff_len = 0, tt_change_len = 0;
  416. int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
  417. tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
  418. /* if we have too many changes for one packet don't send any
  419. * and wait for the tt table request which will be fragmented
  420. */
  421. if (tt_diff_len > bat_priv->soft_iface->mtu)
  422. tt_diff_len = 0;
  423. tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
  424. if (!tt_data)
  425. return;
  426. tt_data->flags = BATADV_TT_OGM_DIFF;
  427. tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
  428. tt_data->crc = htonl(bat_priv->tt.local_crc);
  429. if (tt_diff_len == 0)
  430. goto container_register;
  431. tt_diff_entries_num = batadv_tt_entries(tt_diff_len);
  432. spin_lock_bh(&bat_priv->tt.changes_list_lock);
  433. atomic_set(&bat_priv->tt.local_changes, 0);
  434. tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
  435. list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
  436. list) {
  437. if (tt_diff_entries_count < tt_diff_entries_num) {
  438. memcpy(tt_change + tt_diff_entries_count,
  439. &entry->change,
  440. sizeof(struct batadv_tvlv_tt_change));
  441. tt_diff_entries_count++;
  442. }
  443. list_del(&entry->list);
  444. kfree(entry);
  445. }
  446. spin_unlock_bh(&bat_priv->tt.changes_list_lock);
  447. /* Keep the buffer for possible tt_request */
  448. spin_lock_bh(&bat_priv->tt.last_changeset_lock);
  449. kfree(bat_priv->tt.last_changeset);
  450. bat_priv->tt.last_changeset_len = 0;
  451. bat_priv->tt.last_changeset = NULL;
  452. tt_change_len = batadv_tt_len(tt_diff_entries_count);
  453. /* check whether this new OGM has no changes due to size problems */
  454. if (tt_diff_entries_count > 0) {
  455. /* if kmalloc() fails we will reply with the full table
  456. * instead of providing the diff
  457. */
  458. bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
  459. if (bat_priv->tt.last_changeset) {
  460. memcpy(bat_priv->tt.last_changeset,
  461. tt_change, tt_change_len);
  462. bat_priv->tt.last_changeset_len = tt_diff_len;
  463. }
  464. }
  465. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  466. container_register:
  467. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
  468. sizeof(*tt_data) + tt_change_len);
  469. kfree(tt_data);
  470. }
  471. int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
  472. {
  473. struct net_device *net_dev = (struct net_device *)seq->private;
  474. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  475. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  476. struct batadv_tt_common_entry *tt_common_entry;
  477. struct batadv_tt_local_entry *tt_local;
  478. struct batadv_hard_iface *primary_if;
  479. struct hlist_head *head;
  480. uint32_t i;
  481. int last_seen_secs;
  482. int last_seen_msecs;
  483. unsigned long last_seen_jiffies;
  484. bool no_purge;
  485. uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
  486. primary_if = batadv_seq_print_text_primary_if_get(seq);
  487. if (!primary_if)
  488. goto out;
  489. seq_printf(seq,
  490. "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.8x):\n",
  491. net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
  492. bat_priv->tt.local_crc);
  493. seq_printf(seq, " %-13s %s %-7s %-10s\n", "Client", "VID",
  494. "Flags", "Last seen");
  495. for (i = 0; i < hash->size; i++) {
  496. head = &hash->table[i];
  497. rcu_read_lock();
  498. hlist_for_each_entry_rcu(tt_common_entry,
  499. head, hash_entry) {
  500. tt_local = container_of(tt_common_entry,
  501. struct batadv_tt_local_entry,
  502. common);
  503. last_seen_jiffies = jiffies - tt_local->last_seen;
  504. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  505. last_seen_secs = last_seen_msecs / 1000;
  506. last_seen_msecs = last_seen_msecs % 1000;
  507. no_purge = tt_common_entry->flags & np_flag;
  508. seq_printf(seq, " * %pM %4i [%c%c%c%c%c] %3u.%03u\n",
  509. tt_common_entry->addr,
  510. BATADV_PRINT_VID(tt_common_entry->vid),
  511. (tt_common_entry->flags &
  512. BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
  513. no_purge ? 'P' : '.',
  514. (tt_common_entry->flags &
  515. BATADV_TT_CLIENT_NEW ? 'N' : '.'),
  516. (tt_common_entry->flags &
  517. BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
  518. (tt_common_entry->flags &
  519. BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
  520. no_purge ? 0 : last_seen_secs,
  521. no_purge ? 0 : last_seen_msecs);
  522. }
  523. rcu_read_unlock();
  524. }
  525. out:
  526. if (primary_if)
  527. batadv_hardif_free_ref(primary_if);
  528. return 0;
  529. }
  530. static void
  531. batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
  532. struct batadv_tt_local_entry *tt_local_entry,
  533. uint16_t flags, const char *message)
  534. {
  535. batadv_tt_local_event(bat_priv, tt_local_entry, flags);
  536. /* The local client has to be marked as "pending to be removed" but has
  537. * to be kept in the table in order to send it in a full table
  538. * response issued before the net ttvn increment (consistency check)
  539. */
  540. tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
  541. batadv_dbg(BATADV_DBG_TT, bat_priv,
  542. "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
  543. tt_local_entry->common.addr,
  544. BATADV_PRINT_VID(tt_local_entry->common.vid), message);
  545. }
  546. /**
  547. * batadv_tt_local_remove - logically remove an entry from the local table
  548. * @bat_priv: the bat priv with all the soft interface information
  549. * @addr: the MAC address of the client to remove
  550. * @vid: VLAN identifier
  551. * @message: message to append to the log on deletion
  552. * @roaming: true if the deletion is due to a roaming event
  553. *
  554. * Returns the flags assigned to the local entry before being deleted
  555. */
  556. uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
  557. const uint8_t *addr, unsigned short vid,
  558. const char *message, bool roaming)
  559. {
  560. struct batadv_tt_local_entry *tt_local_entry;
  561. uint16_t flags, curr_flags = BATADV_NO_FLAGS;
  562. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
  563. if (!tt_local_entry)
  564. goto out;
  565. curr_flags = tt_local_entry->common.flags;
  566. flags = BATADV_TT_CLIENT_DEL;
  567. /* if this global entry addition is due to a roaming, the node has to
  568. * mark the local entry as "roamed" in order to correctly reroute
  569. * packets later
  570. */
  571. if (roaming) {
  572. flags |= BATADV_TT_CLIENT_ROAM;
  573. /* mark the local client as ROAMed */
  574. tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
  575. }
  576. if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
  577. batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
  578. message);
  579. goto out;
  580. }
  581. /* if this client has been added right now, it is possible to
  582. * immediately purge it
  583. */
  584. batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
  585. hlist_del_rcu(&tt_local_entry->common.hash_entry);
  586. batadv_tt_local_entry_free_ref(tt_local_entry);
  587. out:
  588. if (tt_local_entry)
  589. batadv_tt_local_entry_free_ref(tt_local_entry);
  590. return curr_flags;
  591. }
  592. static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
  593. struct hlist_head *head)
  594. {
  595. struct batadv_tt_local_entry *tt_local_entry;
  596. struct batadv_tt_common_entry *tt_common_entry;
  597. struct hlist_node *node_tmp;
  598. hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
  599. hash_entry) {
  600. tt_local_entry = container_of(tt_common_entry,
  601. struct batadv_tt_local_entry,
  602. common);
  603. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
  604. continue;
  605. /* entry already marked for deletion */
  606. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
  607. continue;
  608. if (!batadv_has_timed_out(tt_local_entry->last_seen,
  609. BATADV_TT_LOCAL_TIMEOUT))
  610. continue;
  611. batadv_tt_local_set_pending(bat_priv, tt_local_entry,
  612. BATADV_TT_CLIENT_DEL, "timed out");
  613. }
  614. }
  615. static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
  616. {
  617. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  618. struct hlist_head *head;
  619. spinlock_t *list_lock; /* protects write access to the hash lists */
  620. uint32_t i;
  621. for (i = 0; i < hash->size; i++) {
  622. head = &hash->table[i];
  623. list_lock = &hash->list_locks[i];
  624. spin_lock_bh(list_lock);
  625. batadv_tt_local_purge_list(bat_priv, head);
  626. spin_unlock_bh(list_lock);
  627. }
  628. }
  629. static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
  630. {
  631. struct batadv_hashtable *hash;
  632. spinlock_t *list_lock; /* protects write access to the hash lists */
  633. struct batadv_tt_common_entry *tt_common_entry;
  634. struct batadv_tt_local_entry *tt_local;
  635. struct hlist_node *node_tmp;
  636. struct hlist_head *head;
  637. uint32_t i;
  638. if (!bat_priv->tt.local_hash)
  639. return;
  640. hash = bat_priv->tt.local_hash;
  641. for (i = 0; i < hash->size; i++) {
  642. head = &hash->table[i];
  643. list_lock = &hash->list_locks[i];
  644. spin_lock_bh(list_lock);
  645. hlist_for_each_entry_safe(tt_common_entry, node_tmp,
  646. head, hash_entry) {
  647. hlist_del_rcu(&tt_common_entry->hash_entry);
  648. tt_local = container_of(tt_common_entry,
  649. struct batadv_tt_local_entry,
  650. common);
  651. batadv_tt_local_entry_free_ref(tt_local);
  652. }
  653. spin_unlock_bh(list_lock);
  654. }
  655. batadv_hash_destroy(hash);
  656. bat_priv->tt.local_hash = NULL;
  657. }
  658. static int batadv_tt_global_init(struct batadv_priv *bat_priv)
  659. {
  660. if (bat_priv->tt.global_hash)
  661. return 0;
  662. bat_priv->tt.global_hash = batadv_hash_new(1024);
  663. if (!bat_priv->tt.global_hash)
  664. return -ENOMEM;
  665. batadv_hash_set_lock_class(bat_priv->tt.global_hash,
  666. &batadv_tt_global_hash_lock_class_key);
  667. return 0;
  668. }
  669. static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
  670. {
  671. struct batadv_tt_change_node *entry, *safe;
  672. spin_lock_bh(&bat_priv->tt.changes_list_lock);
  673. list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
  674. list) {
  675. list_del(&entry->list);
  676. kfree(entry);
  677. }
  678. atomic_set(&bat_priv->tt.local_changes, 0);
  679. spin_unlock_bh(&bat_priv->tt.changes_list_lock);
  680. }
  681. /* retrieves the orig_tt_list_entry belonging to orig_node from the
  682. * batadv_tt_global_entry list
  683. *
  684. * returns it with an increased refcounter, NULL if not found
  685. */
  686. static struct batadv_tt_orig_list_entry *
  687. batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
  688. const struct batadv_orig_node *orig_node)
  689. {
  690. struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
  691. const struct hlist_head *head;
  692. rcu_read_lock();
  693. head = &entry->orig_list;
  694. hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
  695. if (tmp_orig_entry->orig_node != orig_node)
  696. continue;
  697. if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
  698. continue;
  699. orig_entry = tmp_orig_entry;
  700. break;
  701. }
  702. rcu_read_unlock();
  703. return orig_entry;
  704. }
  705. /* find out if an orig_node is already in the list of a tt_global_entry.
  706. * returns true if found, false otherwise
  707. */
  708. static bool
  709. batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
  710. const struct batadv_orig_node *orig_node)
  711. {
  712. struct batadv_tt_orig_list_entry *orig_entry;
  713. bool found = false;
  714. orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
  715. if (orig_entry) {
  716. found = true;
  717. batadv_tt_orig_list_entry_free_ref(orig_entry);
  718. }
  719. return found;
  720. }
  721. static void
  722. batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
  723. struct batadv_orig_node *orig_node, int ttvn)
  724. {
  725. struct batadv_tt_orig_list_entry *orig_entry;
  726. orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
  727. if (orig_entry) {
  728. /* refresh the ttvn: the current value could be a bogus one that
  729. * was added during a "temporary client detection"
  730. */
  731. orig_entry->ttvn = ttvn;
  732. goto out;
  733. }
  734. orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
  735. if (!orig_entry)
  736. goto out;
  737. INIT_HLIST_NODE(&orig_entry->list);
  738. atomic_inc(&orig_node->refcount);
  739. atomic_inc(&orig_node->tt_size);
  740. orig_entry->orig_node = orig_node;
  741. orig_entry->ttvn = ttvn;
  742. atomic_set(&orig_entry->refcount, 2);
  743. spin_lock_bh(&tt_global->list_lock);
  744. hlist_add_head_rcu(&orig_entry->list,
  745. &tt_global->orig_list);
  746. spin_unlock_bh(&tt_global->list_lock);
  747. out:
  748. if (orig_entry)
  749. batadv_tt_orig_list_entry_free_ref(orig_entry);
  750. }
  751. /**
  752. * batadv_tt_global_add - add a new TT global entry or update an existing one
  753. * @bat_priv: the bat priv with all the soft interface information
  754. * @orig_node: the originator announcing the client
  755. * @tt_addr: the mac address of the non-mesh client
  756. * @vid: VLAN identifier
  757. * @flags: TT flags that have to be set for this non-mesh client
  758. * @ttvn: the tt version number ever announcing this non-mesh client
  759. *
  760. * Add a new TT global entry for the given originator. If the entry already
  761. * exists add a new reference to the given originator (a global entry can have
  762. * references to multiple originators) and adjust the flags attribute to reflect
  763. * the function argument.
  764. * If a TT local entry exists for this non-mesh client remove it.
  765. *
  766. * The caller must hold orig_node refcount.
  767. *
  768. * Return true if the new entry has been added, false otherwise
  769. */
  770. static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
  771. struct batadv_orig_node *orig_node,
  772. const unsigned char *tt_addr,
  773. unsigned short vid, uint16_t flags,
  774. uint8_t ttvn)
  775. {
  776. struct batadv_tt_global_entry *tt_global_entry;
  777. struct batadv_tt_local_entry *tt_local_entry;
  778. bool ret = false;
  779. int hash_added;
  780. struct batadv_tt_common_entry *common;
  781. uint16_t local_flags;
  782. tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
  783. tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
  784. /* if the node already has a local client for this entry, it has to wait
  785. * for a roaming advertisement instead of manually messing up the global
  786. * table
  787. */
  788. if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
  789. !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
  790. goto out;
  791. if (!tt_global_entry) {
  792. tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
  793. if (!tt_global_entry)
  794. goto out;
  795. common = &tt_global_entry->common;
  796. memcpy(common->addr, tt_addr, ETH_ALEN);
  797. common->vid = vid;
  798. common->flags = flags;
  799. tt_global_entry->roam_at = 0;
  800. /* node must store current time in case of roaming. This is
  801. * needed to purge this entry out on timeout (if nobody claims
  802. * it)
  803. */
  804. if (flags & BATADV_TT_CLIENT_ROAM)
  805. tt_global_entry->roam_at = jiffies;
  806. atomic_set(&common->refcount, 2);
  807. common->added_at = jiffies;
  808. INIT_HLIST_HEAD(&tt_global_entry->orig_list);
  809. spin_lock_init(&tt_global_entry->list_lock);
  810. hash_added = batadv_hash_add(bat_priv->tt.global_hash,
  811. batadv_compare_tt,
  812. batadv_choose_tt, common,
  813. &common->hash_entry);
  814. if (unlikely(hash_added != 0)) {
  815. /* remove the reference for the hash */
  816. batadv_tt_global_entry_free_ref(tt_global_entry);
  817. goto out_remove;
  818. }
  819. } else {
  820. common = &tt_global_entry->common;
  821. /* If there is already a global entry, we can use this one for
  822. * our processing.
  823. * But if we are trying to add a temporary client then here are
  824. * two options at this point:
  825. * 1) the global client is not a temporary client: the global
  826. * client has to be left as it is, temporary information
  827. * should never override any already known client state
  828. * 2) the global client is a temporary client: purge the
  829. * originator list and add the new one orig_entry
  830. */
  831. if (flags & BATADV_TT_CLIENT_TEMP) {
  832. if (!(common->flags & BATADV_TT_CLIENT_TEMP))
  833. goto out;
  834. if (batadv_tt_global_entry_has_orig(tt_global_entry,
  835. orig_node))
  836. goto out_remove;
  837. batadv_tt_global_del_orig_list(tt_global_entry);
  838. goto add_orig_entry;
  839. }
  840. /* if the client was temporary added before receiving the first
  841. * OGM announcing it, we have to clear the TEMP flag
  842. */
  843. common->flags &= ~BATADV_TT_CLIENT_TEMP;
  844. /* the change can carry possible "attribute" flags like the
  845. * TT_CLIENT_WIFI, therefore they have to be copied in the
  846. * client entry
  847. */
  848. tt_global_entry->common.flags |= flags;
  849. /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
  850. * one originator left in the list and we previously received a
  851. * delete + roaming change for this originator.
  852. *
  853. * We should first delete the old originator before adding the
  854. * new one.
  855. */
  856. if (common->flags & BATADV_TT_CLIENT_ROAM) {
  857. batadv_tt_global_del_orig_list(tt_global_entry);
  858. common->flags &= ~BATADV_TT_CLIENT_ROAM;
  859. tt_global_entry->roam_at = 0;
  860. }
  861. }
  862. add_orig_entry:
  863. /* add the new orig_entry (if needed) or update it */
  864. batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
  865. batadv_dbg(BATADV_DBG_TT, bat_priv,
  866. "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
  867. common->addr, BATADV_PRINT_VID(common->vid),
  868. orig_node->orig);
  869. ret = true;
  870. out_remove:
  871. /* remove address from local hash if present */
  872. local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
  873. "global tt received",
  874. flags & BATADV_TT_CLIENT_ROAM);
  875. tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
  876. if (!(flags & BATADV_TT_CLIENT_ROAM))
  877. /* this is a normal global add. Therefore the client is not in a
  878. * roaming state anymore.
  879. */
  880. tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
  881. out:
  882. if (tt_global_entry)
  883. batadv_tt_global_entry_free_ref(tt_global_entry);
  884. if (tt_local_entry)
  885. batadv_tt_local_entry_free_ref(tt_local_entry);
  886. return ret;
  887. }
  888. /* batadv_transtable_best_orig - Get best originator list entry from tt entry
  889. * @tt_global_entry: global translation table entry to be analyzed
  890. *
  891. * This functon assumes the caller holds rcu_read_lock().
  892. * Returns best originator list entry or NULL on errors.
  893. */
  894. static struct batadv_tt_orig_list_entry *
  895. batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
  896. {
  897. struct batadv_neigh_node *router = NULL;
  898. struct hlist_head *head;
  899. struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
  900. int best_tq = 0;
  901. head = &tt_global_entry->orig_list;
  902. hlist_for_each_entry_rcu(orig_entry, head, list) {
  903. router = batadv_orig_node_get_router(orig_entry->orig_node);
  904. if (!router)
  905. continue;
  906. if (router->tq_avg > best_tq) {
  907. best_entry = orig_entry;
  908. best_tq = router->tq_avg;
  909. }
  910. batadv_neigh_node_free_ref(router);
  911. }
  912. return best_entry;
  913. }
  914. /* batadv_tt_global_print_entry - print all orig nodes who announce the address
  915. * for this global entry
  916. * @tt_global_entry: global translation table entry to be printed
  917. * @seq: debugfs table seq_file struct
  918. *
  919. * This functon assumes the caller holds rcu_read_lock().
  920. */
  921. static void
  922. batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
  923. struct seq_file *seq)
  924. {
  925. struct hlist_head *head;
  926. struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
  927. struct batadv_tt_common_entry *tt_common_entry;
  928. uint16_t flags;
  929. uint8_t last_ttvn;
  930. tt_common_entry = &tt_global_entry->common;
  931. flags = tt_common_entry->flags;
  932. best_entry = batadv_transtable_best_orig(tt_global_entry);
  933. if (best_entry) {
  934. last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
  935. seq_printf(seq,
  936. " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
  937. '*', tt_global_entry->common.addr,
  938. BATADV_PRINT_VID(tt_global_entry->common.vid),
  939. best_entry->ttvn, best_entry->orig_node->orig,
  940. last_ttvn, best_entry->orig_node->tt_crc,
  941. (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
  942. (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
  943. (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
  944. }
  945. head = &tt_global_entry->orig_list;
  946. hlist_for_each_entry_rcu(orig_entry, head, list) {
  947. if (best_entry == orig_entry)
  948. continue;
  949. last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
  950. seq_printf(seq,
  951. " %c %pM %4d (%3u) via %pM (%3u) [%c%c%c]\n",
  952. '+', tt_global_entry->common.addr,
  953. BATADV_PRINT_VID(tt_global_entry->common.vid),
  954. orig_entry->ttvn, orig_entry->orig_node->orig,
  955. last_ttvn,
  956. (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
  957. (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
  958. (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
  959. }
  960. }
  961. int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
  962. {
  963. struct net_device *net_dev = (struct net_device *)seq->private;
  964. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  965. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  966. struct batadv_tt_common_entry *tt_common_entry;
  967. struct batadv_tt_global_entry *tt_global;
  968. struct batadv_hard_iface *primary_if;
  969. struct hlist_head *head;
  970. uint32_t i;
  971. primary_if = batadv_seq_print_text_primary_if_get(seq);
  972. if (!primary_if)
  973. goto out;
  974. seq_printf(seq,
  975. "Globally announced TT entries received via the mesh %s\n",
  976. net_dev->name);
  977. seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
  978. "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
  979. "CRC", "Flags");
  980. for (i = 0; i < hash->size; i++) {
  981. head = &hash->table[i];
  982. rcu_read_lock();
  983. hlist_for_each_entry_rcu(tt_common_entry,
  984. head, hash_entry) {
  985. tt_global = container_of(tt_common_entry,
  986. struct batadv_tt_global_entry,
  987. common);
  988. batadv_tt_global_print_entry(tt_global, seq);
  989. }
  990. rcu_read_unlock();
  991. }
  992. out:
  993. if (primary_if)
  994. batadv_hardif_free_ref(primary_if);
  995. return 0;
  996. }
  997. /* deletes the orig list of a tt_global_entry */
  998. static void
  999. batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
  1000. {
  1001. struct hlist_head *head;
  1002. struct hlist_node *safe;
  1003. struct batadv_tt_orig_list_entry *orig_entry;
  1004. spin_lock_bh(&tt_global_entry->list_lock);
  1005. head = &tt_global_entry->orig_list;
  1006. hlist_for_each_entry_safe(orig_entry, safe, head, list) {
  1007. hlist_del_rcu(&orig_entry->list);
  1008. batadv_tt_orig_list_entry_free_ref(orig_entry);
  1009. }
  1010. spin_unlock_bh(&tt_global_entry->list_lock);
  1011. }
  1012. static void
  1013. batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
  1014. struct batadv_tt_global_entry *tt_global_entry,
  1015. struct batadv_orig_node *orig_node,
  1016. const char *message)
  1017. {
  1018. struct hlist_head *head;
  1019. struct hlist_node *safe;
  1020. struct batadv_tt_orig_list_entry *orig_entry;
  1021. unsigned short vid;
  1022. spin_lock_bh(&tt_global_entry->list_lock);
  1023. head = &tt_global_entry->orig_list;
  1024. hlist_for_each_entry_safe(orig_entry, safe, head, list) {
  1025. if (orig_entry->orig_node == orig_node) {
  1026. vid = tt_global_entry->common.vid;
  1027. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1028. "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
  1029. orig_node->orig,
  1030. tt_global_entry->common.addr,
  1031. BATADV_PRINT_VID(vid), message);
  1032. hlist_del_rcu(&orig_entry->list);
  1033. batadv_tt_orig_list_entry_free_ref(orig_entry);
  1034. }
  1035. }
  1036. spin_unlock_bh(&tt_global_entry->list_lock);
  1037. }
  1038. /* If the client is to be deleted, we check if it is the last origantor entry
  1039. * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
  1040. * timer, otherwise we simply remove the originator scheduled for deletion.
  1041. */
  1042. static void
  1043. batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
  1044. struct batadv_tt_global_entry *tt_global_entry,
  1045. struct batadv_orig_node *orig_node,
  1046. const char *message)
  1047. {
  1048. bool last_entry = true;
  1049. struct hlist_head *head;
  1050. struct batadv_tt_orig_list_entry *orig_entry;
  1051. /* no local entry exists, case 1:
  1052. * Check if this is the last one or if other entries exist.
  1053. */
  1054. rcu_read_lock();
  1055. head = &tt_global_entry->orig_list;
  1056. hlist_for_each_entry_rcu(orig_entry, head, list) {
  1057. if (orig_entry->orig_node != orig_node) {
  1058. last_entry = false;
  1059. break;
  1060. }
  1061. }
  1062. rcu_read_unlock();
  1063. if (last_entry) {
  1064. /* its the last one, mark for roaming. */
  1065. tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
  1066. tt_global_entry->roam_at = jiffies;
  1067. } else
  1068. /* there is another entry, we can simply delete this
  1069. * one and can still use the other one.
  1070. */
  1071. batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
  1072. orig_node, message);
  1073. }
  1074. /**
  1075. * batadv_tt_global_del - remove a client from the global table
  1076. * @bat_priv: the bat priv with all the soft interface information
  1077. * @orig_node: an originator serving this client
  1078. * @addr: the mac address of the client
  1079. * @vid: VLAN identifier
  1080. * @message: a message explaining the reason for deleting the client to print
  1081. * for debugging purpose
  1082. * @roaming: true if the deletion has been triggered by a roaming event
  1083. */
  1084. static void batadv_tt_global_del(struct batadv_priv *bat_priv,
  1085. struct batadv_orig_node *orig_node,
  1086. const unsigned char *addr, unsigned short vid,
  1087. const char *message, bool roaming)
  1088. {
  1089. struct batadv_tt_global_entry *tt_global_entry;
  1090. struct batadv_tt_local_entry *local_entry = NULL;
  1091. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  1092. if (!tt_global_entry)
  1093. goto out;
  1094. if (!roaming) {
  1095. batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
  1096. orig_node, message);
  1097. if (hlist_empty(&tt_global_entry->orig_list))
  1098. batadv_tt_global_free(bat_priv, tt_global_entry,
  1099. message);
  1100. goto out;
  1101. }
  1102. /* if we are deleting a global entry due to a roam
  1103. * event, there are two possibilities:
  1104. * 1) the client roamed from node A to node B => if there
  1105. * is only one originator left for this client, we mark
  1106. * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
  1107. * wait for node B to claim it. In case of timeout
  1108. * the entry is purged.
  1109. *
  1110. * If there are other originators left, we directly delete
  1111. * the originator.
  1112. * 2) the client roamed to us => we can directly delete
  1113. * the global entry, since it is useless now.
  1114. */
  1115. local_entry = batadv_tt_local_hash_find(bat_priv,
  1116. tt_global_entry->common.addr,
  1117. vid);
  1118. if (local_entry) {
  1119. /* local entry exists, case 2: client roamed to us. */
  1120. batadv_tt_global_del_orig_list(tt_global_entry);
  1121. batadv_tt_global_free(bat_priv, tt_global_entry, message);
  1122. } else
  1123. /* no local entry exists, case 1: check for roaming */
  1124. batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
  1125. orig_node, message);
  1126. out:
  1127. if (tt_global_entry)
  1128. batadv_tt_global_entry_free_ref(tt_global_entry);
  1129. if (local_entry)
  1130. batadv_tt_local_entry_free_ref(local_entry);
  1131. }
  1132. void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
  1133. struct batadv_orig_node *orig_node,
  1134. const char *message)
  1135. {
  1136. struct batadv_tt_global_entry *tt_global;
  1137. struct batadv_tt_common_entry *tt_common_entry;
  1138. uint32_t i;
  1139. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1140. struct hlist_node *safe;
  1141. struct hlist_head *head;
  1142. spinlock_t *list_lock; /* protects write access to the hash lists */
  1143. unsigned short vid;
  1144. if (!hash)
  1145. return;
  1146. for (i = 0; i < hash->size; i++) {
  1147. head = &hash->table[i];
  1148. list_lock = &hash->list_locks[i];
  1149. spin_lock_bh(list_lock);
  1150. hlist_for_each_entry_safe(tt_common_entry, safe,
  1151. head, hash_entry) {
  1152. tt_global = container_of(tt_common_entry,
  1153. struct batadv_tt_global_entry,
  1154. common);
  1155. batadv_tt_global_del_orig_entry(bat_priv, tt_global,
  1156. orig_node, message);
  1157. if (hlist_empty(&tt_global->orig_list)) {
  1158. vid = tt_global->common.vid;
  1159. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1160. "Deleting global tt entry %pM (vid: %d): %s\n",
  1161. tt_global->common.addr,
  1162. BATADV_PRINT_VID(vid), message);
  1163. hlist_del_rcu(&tt_common_entry->hash_entry);
  1164. batadv_tt_global_entry_free_ref(tt_global);
  1165. }
  1166. }
  1167. spin_unlock_bh(list_lock);
  1168. }
  1169. orig_node->tt_initialised = false;
  1170. }
  1171. static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
  1172. char **msg)
  1173. {
  1174. bool purge = false;
  1175. unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
  1176. unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
  1177. if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
  1178. batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
  1179. purge = true;
  1180. *msg = "Roaming timeout\n";
  1181. }
  1182. if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
  1183. batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
  1184. purge = true;
  1185. *msg = "Temporary client timeout\n";
  1186. }
  1187. return purge;
  1188. }
  1189. static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
  1190. {
  1191. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1192. struct hlist_head *head;
  1193. struct hlist_node *node_tmp;
  1194. spinlock_t *list_lock; /* protects write access to the hash lists */
  1195. uint32_t i;
  1196. char *msg = NULL;
  1197. struct batadv_tt_common_entry *tt_common;
  1198. struct batadv_tt_global_entry *tt_global;
  1199. for (i = 0; i < hash->size; i++) {
  1200. head = &hash->table[i];
  1201. list_lock = &hash->list_locks[i];
  1202. spin_lock_bh(list_lock);
  1203. hlist_for_each_entry_safe(tt_common, node_tmp, head,
  1204. hash_entry) {
  1205. tt_global = container_of(tt_common,
  1206. struct batadv_tt_global_entry,
  1207. common);
  1208. if (!batadv_tt_global_to_purge(tt_global, &msg))
  1209. continue;
  1210. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1211. "Deleting global tt entry %pM (vid: %d): %s\n",
  1212. tt_global->common.addr,
  1213. BATADV_PRINT_VID(tt_global->common.vid),
  1214. msg);
  1215. hlist_del_rcu(&tt_common->hash_entry);
  1216. batadv_tt_global_entry_free_ref(tt_global);
  1217. }
  1218. spin_unlock_bh(list_lock);
  1219. }
  1220. }
  1221. static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
  1222. {
  1223. struct batadv_hashtable *hash;
  1224. spinlock_t *list_lock; /* protects write access to the hash lists */
  1225. struct batadv_tt_common_entry *tt_common_entry;
  1226. struct batadv_tt_global_entry *tt_global;
  1227. struct hlist_node *node_tmp;
  1228. struct hlist_head *head;
  1229. uint32_t i;
  1230. if (!bat_priv->tt.global_hash)
  1231. return;
  1232. hash = bat_priv->tt.global_hash;
  1233. for (i = 0; i < hash->size; i++) {
  1234. head = &hash->table[i];
  1235. list_lock = &hash->list_locks[i];
  1236. spin_lock_bh(list_lock);
  1237. hlist_for_each_entry_safe(tt_common_entry, node_tmp,
  1238. head, hash_entry) {
  1239. hlist_del_rcu(&tt_common_entry->hash_entry);
  1240. tt_global = container_of(tt_common_entry,
  1241. struct batadv_tt_global_entry,
  1242. common);
  1243. batadv_tt_global_entry_free_ref(tt_global);
  1244. }
  1245. spin_unlock_bh(list_lock);
  1246. }
  1247. batadv_hash_destroy(hash);
  1248. bat_priv->tt.global_hash = NULL;
  1249. }
  1250. static bool
  1251. _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
  1252. struct batadv_tt_global_entry *tt_global_entry)
  1253. {
  1254. bool ret = false;
  1255. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
  1256. tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
  1257. ret = true;
  1258. return ret;
  1259. }
  1260. /**
  1261. * batadv_transtable_search - get the mesh destination for a given client
  1262. * @bat_priv: the bat priv with all the soft interface information
  1263. * @src: mac address of the source client
  1264. * @addr: mac address of the destination client
  1265. * @vid: VLAN identifier
  1266. *
  1267. * Returns a pointer to the originator that was selected as destination in the
  1268. * mesh for contacting the client 'addr', NULL otherwise.
  1269. * In case of multiple originators serving the same client, the function returns
  1270. * the best one (best in terms of metric towards the destination node).
  1271. *
  1272. * If the two clients are AP isolated the function returns NULL.
  1273. */
  1274. struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
  1275. const uint8_t *src,
  1276. const uint8_t *addr,
  1277. unsigned short vid)
  1278. {
  1279. struct batadv_tt_local_entry *tt_local_entry = NULL;
  1280. struct batadv_tt_global_entry *tt_global_entry = NULL;
  1281. struct batadv_orig_node *orig_node = NULL;
  1282. struct batadv_tt_orig_list_entry *best_entry;
  1283. if (src && atomic_read(&bat_priv->ap_isolation)) {
  1284. tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
  1285. if (!tt_local_entry ||
  1286. (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
  1287. goto out;
  1288. }
  1289. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  1290. if (!tt_global_entry)
  1291. goto out;
  1292. /* check whether the clients should not communicate due to AP
  1293. * isolation
  1294. */
  1295. if (tt_local_entry &&
  1296. _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
  1297. goto out;
  1298. rcu_read_lock();
  1299. best_entry = batadv_transtable_best_orig(tt_global_entry);
  1300. /* found anything? */
  1301. if (best_entry)
  1302. orig_node = best_entry->orig_node;
  1303. if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
  1304. orig_node = NULL;
  1305. rcu_read_unlock();
  1306. out:
  1307. if (tt_global_entry)
  1308. batadv_tt_global_entry_free_ref(tt_global_entry);
  1309. if (tt_local_entry)
  1310. batadv_tt_local_entry_free_ref(tt_local_entry);
  1311. return orig_node;
  1312. }
  1313. /**
  1314. * batadv_tt_global_crc - calculates the checksum of the local table belonging
  1315. * to the given orig_node
  1316. * @bat_priv: the bat priv with all the soft interface information
  1317. * @orig_node: originator for which the CRC should be computed
  1318. *
  1319. * This function computes the checksum for the global table corresponding to a
  1320. * specific originator. In particular, the checksum is computed as follows: For
  1321. * each client connected to the originator the CRC32C of the MAC address and the
  1322. * VID is computed and then all the CRC32Cs of the various clients are xor'ed
  1323. * together.
  1324. *
  1325. * The idea behind is that CRC32C should be used as much as possible in order to
  1326. * produce a unique hash of the table, but since the order which is used to feed
  1327. * the CRC32C function affects the result and since every node in the network
  1328. * probably sorts the clients differently, the hash function cannot be directly
  1329. * computed over the entire table. Hence the CRC32C is used only on
  1330. * the single client entry, while all the results are then xor'ed together
  1331. * because the XOR operation can combine them all while trying to reduce the
  1332. * noise as much as possible.
  1333. *
  1334. * Returns the checksum of the global table of a given originator.
  1335. */
  1336. static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
  1337. struct batadv_orig_node *orig_node)
  1338. {
  1339. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1340. struct batadv_tt_common_entry *tt_common;
  1341. struct batadv_tt_global_entry *tt_global;
  1342. struct hlist_head *head;
  1343. uint32_t i, crc_tmp, crc = 0;
  1344. for (i = 0; i < hash->size; i++) {
  1345. head = &hash->table[i];
  1346. rcu_read_lock();
  1347. hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
  1348. tt_global = container_of(tt_common,
  1349. struct batadv_tt_global_entry,
  1350. common);
  1351. /* Roaming clients are in the global table for
  1352. * consistency only. They don't have to be
  1353. * taken into account while computing the
  1354. * global crc
  1355. */
  1356. if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
  1357. continue;
  1358. /* Temporary clients have not been announced yet, so
  1359. * they have to be skipped while computing the global
  1360. * crc
  1361. */
  1362. if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
  1363. continue;
  1364. /* find out if this global entry is announced by this
  1365. * originator
  1366. */
  1367. if (!batadv_tt_global_entry_has_orig(tt_global,
  1368. orig_node))
  1369. continue;
  1370. crc_tmp = crc32c(0, &tt_common->vid,
  1371. sizeof(tt_common->vid));
  1372. crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
  1373. }
  1374. rcu_read_unlock();
  1375. }
  1376. return crc;
  1377. }
  1378. /**
  1379. * batadv_tt_local_crc - calculates the checksum of the local table
  1380. * @bat_priv: the bat priv with all the soft interface information
  1381. *
  1382. * For details about the computation, please refer to the documentation for
  1383. * batadv_tt_global_crc().
  1384. *
  1385. * Returns the checksum of the local table
  1386. */
  1387. static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
  1388. {
  1389. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  1390. struct batadv_tt_common_entry *tt_common;
  1391. struct hlist_head *head;
  1392. uint32_t i, crc_tmp, crc = 0;
  1393. for (i = 0; i < hash->size; i++) {
  1394. head = &hash->table[i];
  1395. rcu_read_lock();
  1396. hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
  1397. /* not yet committed clients have not to be taken into
  1398. * account while computing the CRC
  1399. */
  1400. if (tt_common->flags & BATADV_TT_CLIENT_NEW)
  1401. continue;
  1402. crc_tmp = crc32c(0, &tt_common->vid,
  1403. sizeof(tt_common->vid));
  1404. crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
  1405. }
  1406. rcu_read_unlock();
  1407. }
  1408. return crc;
  1409. }
  1410. static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
  1411. {
  1412. struct batadv_tt_req_node *node, *safe;
  1413. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1414. list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  1415. list_del(&node->list);
  1416. kfree(node);
  1417. }
  1418. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1419. }
  1420. static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
  1421. struct batadv_orig_node *orig_node,
  1422. const void *tt_buff,
  1423. uint16_t tt_buff_len)
  1424. {
  1425. /* Replace the old buffer only if I received something in the
  1426. * last OGM (the OGM could carry no changes)
  1427. */
  1428. spin_lock_bh(&orig_node->tt_buff_lock);
  1429. if (tt_buff_len > 0) {
  1430. kfree(orig_node->tt_buff);
  1431. orig_node->tt_buff_len = 0;
  1432. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  1433. if (orig_node->tt_buff) {
  1434. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  1435. orig_node->tt_buff_len = tt_buff_len;
  1436. }
  1437. }
  1438. spin_unlock_bh(&orig_node->tt_buff_lock);
  1439. }
  1440. static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
  1441. {
  1442. struct batadv_tt_req_node *node, *safe;
  1443. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1444. list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  1445. if (batadv_has_timed_out(node->issued_at,
  1446. BATADV_TT_REQUEST_TIMEOUT)) {
  1447. list_del(&node->list);
  1448. kfree(node);
  1449. }
  1450. }
  1451. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1452. }
  1453. /* returns the pointer to the new tt_req_node struct if no request
  1454. * has already been issued for this orig_node, NULL otherwise
  1455. */
  1456. static struct batadv_tt_req_node *
  1457. batadv_new_tt_req_node(struct batadv_priv *bat_priv,
  1458. struct batadv_orig_node *orig_node)
  1459. {
  1460. struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
  1461. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1462. list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
  1463. if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
  1464. !batadv_has_timed_out(tt_req_node_tmp->issued_at,
  1465. BATADV_TT_REQUEST_TIMEOUT))
  1466. goto unlock;
  1467. }
  1468. tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
  1469. if (!tt_req_node)
  1470. goto unlock;
  1471. memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
  1472. tt_req_node->issued_at = jiffies;
  1473. list_add(&tt_req_node->list, &bat_priv->tt.req_list);
  1474. unlock:
  1475. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1476. return tt_req_node;
  1477. }
  1478. /**
  1479. * batadv_tt_local_valid - verify that given tt entry is a valid one
  1480. * @entry_ptr: to be checked local tt entry
  1481. * @data_ptr: not used but definition required to satisfy the callback prototype
  1482. *
  1483. * Returns 1 if the entry is a valid, 0 otherwise.
  1484. */
  1485. static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
  1486. {
  1487. const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
  1488. if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
  1489. return 0;
  1490. return 1;
  1491. }
  1492. static int batadv_tt_global_valid(const void *entry_ptr,
  1493. const void *data_ptr)
  1494. {
  1495. const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
  1496. const struct batadv_tt_global_entry *tt_global_entry;
  1497. const struct batadv_orig_node *orig_node = data_ptr;
  1498. if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
  1499. tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
  1500. return 0;
  1501. tt_global_entry = container_of(tt_common_entry,
  1502. struct batadv_tt_global_entry,
  1503. common);
  1504. return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
  1505. }
  1506. /**
  1507. * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
  1508. * tt entries from the specified tt hash
  1509. * @bat_priv: the bat priv with all the soft interface information
  1510. * @hash: hash table containing the tt entries
  1511. * @tt_len: expected tvlv tt data buffer length in number of bytes
  1512. * @valid_cb: function to filter tt change entries
  1513. * @cb_data: data passed to the filter function as argument
  1514. *
  1515. * Returns pointer to allocated tvlv tt data buffer if operation was
  1516. * successful or NULL otherwise.
  1517. */
  1518. static struct batadv_tvlv_tt_data *
  1519. batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
  1520. struct batadv_hashtable *hash, uint16_t tt_len,
  1521. int (*valid_cb)(const void *, const void *),
  1522. void *cb_data)
  1523. {
  1524. struct batadv_tt_common_entry *tt_common_entry;
  1525. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  1526. struct batadv_tvlv_tt_change *tt_change;
  1527. struct hlist_head *head;
  1528. uint16_t tt_tot, tt_num_entries = 0;
  1529. ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
  1530. uint32_t i;
  1531. if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
  1532. tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
  1533. tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
  1534. }
  1535. tt_tot = batadv_tt_entries(tt_len);
  1536. tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
  1537. GFP_ATOMIC);
  1538. if (!tvlv_tt_data)
  1539. goto out;
  1540. tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
  1541. rcu_read_lock();
  1542. for (i = 0; i < hash->size; i++) {
  1543. head = &hash->table[i];
  1544. hlist_for_each_entry_rcu(tt_common_entry,
  1545. head, hash_entry) {
  1546. if (tt_tot == tt_num_entries)
  1547. break;
  1548. if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
  1549. continue;
  1550. memcpy(tt_change->addr, tt_common_entry->addr,
  1551. ETH_ALEN);
  1552. tt_change->flags = tt_common_entry->flags;
  1553. tt_change->vid = htons(tt_common_entry->vid);
  1554. tt_change->reserved = 0;
  1555. tt_num_entries++;
  1556. tt_change++;
  1557. }
  1558. }
  1559. rcu_read_unlock();
  1560. out:
  1561. return tvlv_tt_data;
  1562. }
  1563. /**
  1564. * batadv_send_tt_request - send a TT Request message to a given node
  1565. * @bat_priv: the bat priv with all the soft interface information
  1566. * @dst_orig_node: the destination of the message
  1567. * @ttvn: the version number that the source of the message is looking for
  1568. * @tt_crc: the CRC associated with the version number
  1569. * @full_table: ask for the entire translation table if true, while only for the
  1570. * last TT diff otherwise
  1571. */
  1572. static int batadv_send_tt_request(struct batadv_priv *bat_priv,
  1573. struct batadv_orig_node *dst_orig_node,
  1574. uint8_t ttvn, uint32_t tt_crc,
  1575. bool full_table)
  1576. {
  1577. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  1578. struct batadv_hard_iface *primary_if;
  1579. struct batadv_tt_req_node *tt_req_node = NULL;
  1580. bool ret = false;
  1581. primary_if = batadv_primary_if_get_selected(bat_priv);
  1582. if (!primary_if)
  1583. goto out;
  1584. /* The new tt_req will be issued only if I'm not waiting for a
  1585. * reply from the same orig_node yet
  1586. */
  1587. tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
  1588. if (!tt_req_node)
  1589. goto out;
  1590. tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
  1591. if (!tvlv_tt_data)
  1592. goto out;
  1593. tvlv_tt_data->flags = BATADV_TT_REQUEST;
  1594. tvlv_tt_data->ttvn = ttvn;
  1595. tvlv_tt_data->crc = htonl(tt_crc);
  1596. if (full_table)
  1597. tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
  1598. batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
  1599. dst_orig_node->orig, full_table ? 'F' : '.');
  1600. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
  1601. batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
  1602. dst_orig_node->orig, BATADV_TVLV_TT, 1,
  1603. tvlv_tt_data, sizeof(*tvlv_tt_data));
  1604. ret = true;
  1605. out:
  1606. if (primary_if)
  1607. batadv_hardif_free_ref(primary_if);
  1608. if (ret && tt_req_node) {
  1609. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1610. list_del(&tt_req_node->list);
  1611. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1612. kfree(tt_req_node);
  1613. }
  1614. kfree(tvlv_tt_data);
  1615. return ret;
  1616. }
  1617. /**
  1618. * batadv_send_other_tt_response - send reply to tt request concerning another
  1619. * node's translation table
  1620. * @bat_priv: the bat priv with all the soft interface information
  1621. * @tt_data: tt data containing the tt request information
  1622. * @req_src: mac address of tt request sender
  1623. * @req_dst: mac address of tt request recipient
  1624. *
  1625. * Returns true if tt request reply was sent, false otherwise.
  1626. */
  1627. static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
  1628. struct batadv_tvlv_tt_data *tt_data,
  1629. uint8_t *req_src, uint8_t *req_dst)
  1630. {
  1631. struct batadv_orig_node *req_dst_orig_node;
  1632. struct batadv_orig_node *res_dst_orig_node = NULL;
  1633. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  1634. uint8_t orig_ttvn, req_ttvn;
  1635. uint16_t tt_len;
  1636. bool ret = false, full_table;
  1637. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1638. "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
  1639. req_src, tt_data->ttvn, req_dst,
  1640. (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1641. /* Let's get the orig node of the REAL destination */
  1642. req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
  1643. if (!req_dst_orig_node)
  1644. goto out;
  1645. res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
  1646. if (!res_dst_orig_node)
  1647. goto out;
  1648. orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1649. req_ttvn = tt_data->ttvn;
  1650. /* this node doesn't have the requested data */
  1651. if (orig_ttvn != req_ttvn ||
  1652. tt_data->crc != htonl(req_dst_orig_node->tt_crc))
  1653. goto out;
  1654. /* If the full table has been explicitly requested */
  1655. if (tt_data->flags & BATADV_TT_FULL_TABLE ||
  1656. !req_dst_orig_node->tt_buff)
  1657. full_table = true;
  1658. else
  1659. full_table = false;
  1660. /* TT fragmentation hasn't been implemented yet, so send as many
  1661. * TT entries fit a single packet as possible only
  1662. */
  1663. if (!full_table) {
  1664. spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
  1665. tt_len = req_dst_orig_node->tt_buff_len;
  1666. tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
  1667. GFP_ATOMIC);
  1668. if (!tvlv_tt_data)
  1669. goto unlock;
  1670. /* Copy the last orig_node's OGM buffer */
  1671. memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
  1672. req_dst_orig_node->tt_buff_len);
  1673. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1674. } else {
  1675. tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
  1676. tt_len = batadv_tt_len(tt_len);
  1677. tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
  1678. bat_priv->tt.global_hash,
  1679. tt_len,
  1680. batadv_tt_global_valid,
  1681. req_dst_orig_node);
  1682. if (!tvlv_tt_data)
  1683. goto out;
  1684. }
  1685. tvlv_tt_data->flags = BATADV_TT_RESPONSE;
  1686. tvlv_tt_data->ttvn = req_ttvn;
  1687. if (full_table)
  1688. tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
  1689. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1690. "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
  1691. res_dst_orig_node->orig, req_dst_orig_node->orig,
  1692. full_table ? 'F' : '.', req_ttvn);
  1693. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
  1694. batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
  1695. req_src, BATADV_TVLV_TT, 1,
  1696. tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
  1697. ret = true;
  1698. goto out;
  1699. unlock:
  1700. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1701. out:
  1702. if (res_dst_orig_node)
  1703. batadv_orig_node_free_ref(res_dst_orig_node);
  1704. if (req_dst_orig_node)
  1705. batadv_orig_node_free_ref(req_dst_orig_node);
  1706. kfree(tvlv_tt_data);
  1707. return ret;
  1708. }
  1709. /**
  1710. * batadv_send_my_tt_response - send reply to tt request concerning this node's
  1711. * translation table
  1712. * @bat_priv: the bat priv with all the soft interface information
  1713. * @tt_data: tt data containing the tt request information
  1714. * @req_src: mac address of tt request sender
  1715. *
  1716. * Returns true if tt request reply was sent, false otherwise.
  1717. */
  1718. static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
  1719. struct batadv_tvlv_tt_data *tt_data,
  1720. uint8_t *req_src)
  1721. {
  1722. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  1723. struct batadv_orig_node *orig_node;
  1724. struct batadv_hard_iface *primary_if = NULL;
  1725. uint8_t my_ttvn, req_ttvn;
  1726. bool full_table;
  1727. uint16_t tt_len;
  1728. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1729. "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
  1730. req_src, tt_data->ttvn,
  1731. (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1732. my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  1733. req_ttvn = tt_data->ttvn;
  1734. orig_node = batadv_orig_hash_find(bat_priv, req_src);
  1735. if (!orig_node)
  1736. goto out;
  1737. primary_if = batadv_primary_if_get_selected(bat_priv);
  1738. if (!primary_if)
  1739. goto out;
  1740. /* If the full table has been explicitly requested or the gap
  1741. * is too big send the whole local translation table
  1742. */
  1743. if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
  1744. !bat_priv->tt.last_changeset)
  1745. full_table = true;
  1746. else
  1747. full_table = false;
  1748. /* TT fragmentation hasn't been implemented yet, so send as many
  1749. * TT entries fit a single packet as possible only
  1750. */
  1751. if (!full_table) {
  1752. spin_lock_bh(&bat_priv->tt.last_changeset_lock);
  1753. tt_len = bat_priv->tt.last_changeset_len;
  1754. tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
  1755. GFP_ATOMIC);
  1756. if (!tvlv_tt_data)
  1757. goto unlock;
  1758. /* Copy the last orig_node's OGM buffer */
  1759. memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
  1760. bat_priv->tt.last_changeset_len);
  1761. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  1762. } else {
  1763. tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
  1764. tt_len = batadv_tt_len(tt_len);
  1765. req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  1766. tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
  1767. bat_priv->tt.local_hash,
  1768. tt_len,
  1769. batadv_tt_local_valid,
  1770. NULL);
  1771. if (!tvlv_tt_data)
  1772. goto out;
  1773. }
  1774. tvlv_tt_data->flags = BATADV_TT_RESPONSE;
  1775. tvlv_tt_data->ttvn = req_ttvn;
  1776. if (full_table)
  1777. tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
  1778. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1779. "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
  1780. orig_node->orig, full_table ? 'F' : '.', req_ttvn);
  1781. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
  1782. batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
  1783. req_src, BATADV_TVLV_TT, 1,
  1784. tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
  1785. goto out;
  1786. unlock:
  1787. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  1788. out:
  1789. if (orig_node)
  1790. batadv_orig_node_free_ref(orig_node);
  1791. if (primary_if)
  1792. batadv_hardif_free_ref(primary_if);
  1793. kfree(tvlv_tt_data);
  1794. /* The packet was for this host, so it doesn't need to be re-routed */
  1795. return true;
  1796. }
  1797. /**
  1798. * batadv_send_tt_response - send reply to tt request
  1799. * @bat_priv: the bat priv with all the soft interface information
  1800. * @tt_data: tt data containing the tt request information
  1801. * @req_src: mac address of tt request sender
  1802. * @req_dst: mac address of tt request recipient
  1803. *
  1804. * Returns true if tt request reply was sent, false otherwise.
  1805. */
  1806. static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
  1807. struct batadv_tvlv_tt_data *tt_data,
  1808. uint8_t *req_src, uint8_t *req_dst)
  1809. {
  1810. if (batadv_is_my_mac(bat_priv, req_dst)) {
  1811. /* don't answer backbone gws! */
  1812. if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
  1813. return true;
  1814. return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
  1815. } else {
  1816. return batadv_send_other_tt_response(bat_priv, tt_data,
  1817. req_src, req_dst);
  1818. }
  1819. }
  1820. static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
  1821. struct batadv_orig_node *orig_node,
  1822. struct batadv_tvlv_tt_change *tt_change,
  1823. uint16_t tt_num_changes, uint8_t ttvn)
  1824. {
  1825. int i;
  1826. int roams;
  1827. for (i = 0; i < tt_num_changes; i++) {
  1828. if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
  1829. roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
  1830. batadv_tt_global_del(bat_priv, orig_node,
  1831. (tt_change + i)->addr,
  1832. ntohs((tt_change + i)->vid),
  1833. "tt removed by changes",
  1834. roams);
  1835. } else {
  1836. if (!batadv_tt_global_add(bat_priv, orig_node,
  1837. (tt_change + i)->addr,
  1838. ntohs((tt_change + i)->vid),
  1839. (tt_change + i)->flags, ttvn))
  1840. /* In case of problem while storing a
  1841. * global_entry, we stop the updating
  1842. * procedure without committing the
  1843. * ttvn change. This will avoid to send
  1844. * corrupted data on tt_request
  1845. */
  1846. return;
  1847. }
  1848. }
  1849. orig_node->tt_initialised = true;
  1850. }
  1851. static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
  1852. struct batadv_tvlv_tt_data *tt_data,
  1853. uint8_t *resp_src, uint16_t num_entries)
  1854. {
  1855. struct batadv_orig_node *orig_node;
  1856. orig_node = batadv_orig_hash_find(bat_priv, resp_src);
  1857. if (!orig_node)
  1858. goto out;
  1859. /* Purge the old table first.. */
  1860. batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
  1861. _batadv_tt_update_changes(bat_priv, orig_node,
  1862. (struct batadv_tvlv_tt_change *)(tt_data + 1),
  1863. num_entries, tt_data->ttvn);
  1864. spin_lock_bh(&orig_node->tt_buff_lock);
  1865. kfree(orig_node->tt_buff);
  1866. orig_node->tt_buff_len = 0;
  1867. orig_node->tt_buff = NULL;
  1868. spin_unlock_bh(&orig_node->tt_buff_lock);
  1869. atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
  1870. out:
  1871. if (orig_node)
  1872. batadv_orig_node_free_ref(orig_node);
  1873. }
  1874. static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
  1875. struct batadv_orig_node *orig_node,
  1876. uint16_t tt_num_changes, uint8_t ttvn,
  1877. struct batadv_tvlv_tt_change *tt_change)
  1878. {
  1879. _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
  1880. tt_num_changes, ttvn);
  1881. batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
  1882. batadv_tt_len(tt_num_changes));
  1883. atomic_set(&orig_node->last_ttvn, ttvn);
  1884. }
  1885. /**
  1886. * batadv_is_my_client - check if a client is served by the local node
  1887. * @bat_priv: the bat priv with all the soft interface information
  1888. * @addr: the mac adress of the client to check
  1889. * @vid: VLAN identifier
  1890. *
  1891. * Returns true if the client is served by this node, false otherwise.
  1892. */
  1893. bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
  1894. unsigned short vid)
  1895. {
  1896. struct batadv_tt_local_entry *tt_local_entry;
  1897. bool ret = false;
  1898. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
  1899. if (!tt_local_entry)
  1900. goto out;
  1901. /* Check if the client has been logically deleted (but is kept for
  1902. * consistency purpose)
  1903. */
  1904. if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
  1905. (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
  1906. goto out;
  1907. ret = true;
  1908. out:
  1909. if (tt_local_entry)
  1910. batadv_tt_local_entry_free_ref(tt_local_entry);
  1911. return ret;
  1912. }
  1913. /**
  1914. * batadv_handle_tt_response - process incoming tt reply
  1915. * @bat_priv: the bat priv with all the soft interface information
  1916. * @tt_data: tt data containing the tt request information
  1917. * @resp_src: mac address of tt reply sender
  1918. * @num_entries: number of tt change entries appended to the tt data
  1919. */
  1920. static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
  1921. struct batadv_tvlv_tt_data *tt_data,
  1922. uint8_t *resp_src, uint16_t num_entries)
  1923. {
  1924. struct batadv_tt_req_node *node, *safe;
  1925. struct batadv_orig_node *orig_node = NULL;
  1926. struct batadv_tvlv_tt_change *tt_change;
  1927. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1928. "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
  1929. resp_src, tt_data->ttvn, num_entries,
  1930. (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1931. /* we should have never asked a backbone gw */
  1932. if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
  1933. goto out;
  1934. orig_node = batadv_orig_hash_find(bat_priv, resp_src);
  1935. if (!orig_node)
  1936. goto out;
  1937. if (tt_data->flags & BATADV_TT_FULL_TABLE) {
  1938. batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
  1939. } else {
  1940. tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
  1941. batadv_tt_update_changes(bat_priv, orig_node, num_entries,
  1942. tt_data->ttvn, tt_change);
  1943. }
  1944. /* Delete the tt_req_node from pending tt_requests list */
  1945. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1946. list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  1947. if (!batadv_compare_eth(node->addr, resp_src))
  1948. continue;
  1949. list_del(&node->list);
  1950. kfree(node);
  1951. }
  1952. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1953. /* Recalculate the CRC for this orig_node and store it */
  1954. orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
  1955. out:
  1956. if (orig_node)
  1957. batadv_orig_node_free_ref(orig_node);
  1958. }
  1959. static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
  1960. {
  1961. struct batadv_tt_roam_node *node, *safe;
  1962. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  1963. list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
  1964. list_del(&node->list);
  1965. kfree(node);
  1966. }
  1967. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  1968. }
  1969. static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
  1970. {
  1971. struct batadv_tt_roam_node *node, *safe;
  1972. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  1973. list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
  1974. if (!batadv_has_timed_out(node->first_time,
  1975. BATADV_ROAMING_MAX_TIME))
  1976. continue;
  1977. list_del(&node->list);
  1978. kfree(node);
  1979. }
  1980. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  1981. }
  1982. /* This function checks whether the client already reached the
  1983. * maximum number of possible roaming phases. In this case the ROAMING_ADV
  1984. * will not be sent.
  1985. *
  1986. * returns true if the ROAMING_ADV can be sent, false otherwise
  1987. */
  1988. static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
  1989. uint8_t *client)
  1990. {
  1991. struct batadv_tt_roam_node *tt_roam_node;
  1992. bool ret = false;
  1993. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  1994. /* The new tt_req will be issued only if I'm not waiting for a
  1995. * reply from the same orig_node yet
  1996. */
  1997. list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
  1998. if (!batadv_compare_eth(tt_roam_node->addr, client))
  1999. continue;
  2000. if (batadv_has_timed_out(tt_roam_node->first_time,
  2001. BATADV_ROAMING_MAX_TIME))
  2002. continue;
  2003. if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
  2004. /* Sorry, you roamed too many times! */
  2005. goto unlock;
  2006. ret = true;
  2007. break;
  2008. }
  2009. if (!ret) {
  2010. tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
  2011. if (!tt_roam_node)
  2012. goto unlock;
  2013. tt_roam_node->first_time = jiffies;
  2014. atomic_set(&tt_roam_node->counter,
  2015. BATADV_ROAMING_MAX_COUNT - 1);
  2016. memcpy(tt_roam_node->addr, client, ETH_ALEN);
  2017. list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
  2018. ret = true;
  2019. }
  2020. unlock:
  2021. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  2022. return ret;
  2023. }
  2024. /**
  2025. * batadv_send_roam_adv - send a roaming advertisement message
  2026. * @bat_priv: the bat priv with all the soft interface information
  2027. * @client: mac address of the roaming client
  2028. * @vid: VLAN identifier
  2029. * @orig_node: message destination
  2030. *
  2031. * Send a ROAMING_ADV message to the node which was previously serving this
  2032. * client. This is done to inform the node that from now on all traffic destined
  2033. * for this particular roamed client has to be forwarded to the sender of the
  2034. * roaming message.
  2035. */
  2036. static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
  2037. unsigned short vid,
  2038. struct batadv_orig_node *orig_node)
  2039. {
  2040. struct batadv_hard_iface *primary_if;
  2041. struct batadv_tvlv_roam_adv tvlv_roam;
  2042. primary_if = batadv_primary_if_get_selected(bat_priv);
  2043. if (!primary_if)
  2044. goto out;
  2045. /* before going on we have to check whether the client has
  2046. * already roamed to us too many times
  2047. */
  2048. if (!batadv_tt_check_roam_count(bat_priv, client))
  2049. goto out;
  2050. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2051. "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
  2052. orig_node->orig, client, BATADV_PRINT_VID(vid));
  2053. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
  2054. memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
  2055. tvlv_roam.vid = htons(vid);
  2056. batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
  2057. orig_node->orig, BATADV_TVLV_ROAM, 1,
  2058. &tvlv_roam, sizeof(tvlv_roam));
  2059. out:
  2060. if (primary_if)
  2061. batadv_hardif_free_ref(primary_if);
  2062. }
  2063. static void batadv_tt_purge(struct work_struct *work)
  2064. {
  2065. struct delayed_work *delayed_work;
  2066. struct batadv_priv_tt *priv_tt;
  2067. struct batadv_priv *bat_priv;
  2068. delayed_work = container_of(work, struct delayed_work, work);
  2069. priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
  2070. bat_priv = container_of(priv_tt, struct batadv_priv, tt);
  2071. batadv_tt_local_purge(bat_priv);
  2072. batadv_tt_global_purge(bat_priv);
  2073. batadv_tt_req_purge(bat_priv);
  2074. batadv_tt_roam_purge(bat_priv);
  2075. queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
  2076. msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
  2077. }
  2078. void batadv_tt_free(struct batadv_priv *bat_priv)
  2079. {
  2080. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
  2081. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
  2082. cancel_delayed_work_sync(&bat_priv->tt.work);
  2083. batadv_tt_local_table_free(bat_priv);
  2084. batadv_tt_global_table_free(bat_priv);
  2085. batadv_tt_req_list_free(bat_priv);
  2086. batadv_tt_changes_list_free(bat_priv);
  2087. batadv_tt_roam_list_free(bat_priv);
  2088. kfree(bat_priv->tt.last_changeset);
  2089. }
  2090. /* This function will enable or disable the specified flags for all the entries
  2091. * in the given hash table and returns the number of modified entries
  2092. */
  2093. static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
  2094. uint16_t flags, bool enable)
  2095. {
  2096. uint32_t i;
  2097. uint16_t changed_num = 0;
  2098. struct hlist_head *head;
  2099. struct batadv_tt_common_entry *tt_common_entry;
  2100. if (!hash)
  2101. goto out;
  2102. for (i = 0; i < hash->size; i++) {
  2103. head = &hash->table[i];
  2104. rcu_read_lock();
  2105. hlist_for_each_entry_rcu(tt_common_entry,
  2106. head, hash_entry) {
  2107. if (enable) {
  2108. if ((tt_common_entry->flags & flags) == flags)
  2109. continue;
  2110. tt_common_entry->flags |= flags;
  2111. } else {
  2112. if (!(tt_common_entry->flags & flags))
  2113. continue;
  2114. tt_common_entry->flags &= ~flags;
  2115. }
  2116. changed_num++;
  2117. }
  2118. rcu_read_unlock();
  2119. }
  2120. out:
  2121. return changed_num;
  2122. }
  2123. /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
  2124. static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
  2125. {
  2126. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  2127. struct batadv_tt_common_entry *tt_common;
  2128. struct batadv_tt_local_entry *tt_local;
  2129. struct hlist_node *node_tmp;
  2130. struct hlist_head *head;
  2131. spinlock_t *list_lock; /* protects write access to the hash lists */
  2132. uint32_t i;
  2133. if (!hash)
  2134. return;
  2135. for (i = 0; i < hash->size; i++) {
  2136. head = &hash->table[i];
  2137. list_lock = &hash->list_locks[i];
  2138. spin_lock_bh(list_lock);
  2139. hlist_for_each_entry_safe(tt_common, node_tmp, head,
  2140. hash_entry) {
  2141. if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
  2142. continue;
  2143. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2144. "Deleting local tt entry (%pM, vid: %d): pending\n",
  2145. tt_common->addr,
  2146. BATADV_PRINT_VID(tt_common->vid));
  2147. atomic_dec(&bat_priv->tt.local_entry_num);
  2148. hlist_del_rcu(&tt_common->hash_entry);
  2149. tt_local = container_of(tt_common,
  2150. struct batadv_tt_local_entry,
  2151. common);
  2152. batadv_tt_local_entry_free_ref(tt_local);
  2153. }
  2154. spin_unlock_bh(list_lock);
  2155. }
  2156. }
  2157. /**
  2158. * batadv_tt_local_commit_changes - commit all pending local tt changes which
  2159. * have been queued in the time since the last commit
  2160. * @bat_priv: the bat priv with all the soft interface information
  2161. */
  2162. void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
  2163. {
  2164. uint16_t changed_num = 0;
  2165. if (atomic_read(&bat_priv->tt.local_changes) < 1) {
  2166. if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
  2167. batadv_tt_tvlv_container_update(bat_priv);
  2168. return;
  2169. }
  2170. changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
  2171. BATADV_TT_CLIENT_NEW, false);
  2172. /* all reset entries have to be counted as local entries */
  2173. atomic_add(changed_num, &bat_priv->tt.local_entry_num);
  2174. batadv_tt_local_purge_pending_clients(bat_priv);
  2175. bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
  2176. /* Increment the TTVN only once per OGM interval */
  2177. atomic_inc(&bat_priv->tt.vn);
  2178. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2179. "Local changes committed, updating to ttvn %u\n",
  2180. (uint8_t)atomic_read(&bat_priv->tt.vn));
  2181. /* reset the sending counter */
  2182. atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
  2183. batadv_tt_tvlv_container_update(bat_priv);
  2184. }
  2185. bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
  2186. uint8_t *dst)
  2187. {
  2188. struct batadv_tt_local_entry *tt_local_entry = NULL;
  2189. struct batadv_tt_global_entry *tt_global_entry = NULL;
  2190. bool ret = false;
  2191. if (!atomic_read(&bat_priv->ap_isolation))
  2192. goto out;
  2193. tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst,
  2194. BATADV_NO_FLAGS);
  2195. if (!tt_local_entry)
  2196. goto out;
  2197. tt_global_entry = batadv_tt_global_hash_find(bat_priv, src,
  2198. BATADV_NO_FLAGS);
  2199. if (!tt_global_entry)
  2200. goto out;
  2201. if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
  2202. goto out;
  2203. ret = true;
  2204. out:
  2205. if (tt_global_entry)
  2206. batadv_tt_global_entry_free_ref(tt_global_entry);
  2207. if (tt_local_entry)
  2208. batadv_tt_local_entry_free_ref(tt_local_entry);
  2209. return ret;
  2210. }
  2211. /**
  2212. * batadv_tt_update_orig - update global translation table with new tt
  2213. * information received via ogms
  2214. * @bat_priv: the bat priv with all the soft interface information
  2215. * @orig: the orig_node of the ogm
  2216. * @tt_buff: buffer holding the tt information
  2217. * @tt_num_changes: number of tt changes inside the tt buffer
  2218. * @ttvn: translation table version number of this changeset
  2219. * @tt_crc: crc32 checksum of orig node's translation table
  2220. */
  2221. static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
  2222. struct batadv_orig_node *orig_node,
  2223. const unsigned char *tt_buff,
  2224. uint16_t tt_num_changes, uint8_t ttvn,
  2225. uint32_t tt_crc)
  2226. {
  2227. uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  2228. bool full_table = true;
  2229. struct batadv_tvlv_tt_change *tt_change;
  2230. /* don't care about a backbone gateways updates. */
  2231. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
  2232. return;
  2233. /* orig table not initialised AND first diff is in the OGM OR the ttvn
  2234. * increased by one -> we can apply the attached changes
  2235. */
  2236. if ((!orig_node->tt_initialised && ttvn == 1) ||
  2237. ttvn - orig_ttvn == 1) {
  2238. /* the OGM could not contain the changes due to their size or
  2239. * because they have already been sent BATADV_TT_OGM_APPEND_MAX
  2240. * times.
  2241. * In this case send a tt request
  2242. */
  2243. if (!tt_num_changes) {
  2244. full_table = false;
  2245. goto request_table;
  2246. }
  2247. tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
  2248. batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
  2249. ttvn, tt_change);
  2250. /* Even if we received the precomputed crc with the OGM, we
  2251. * prefer to recompute it to spot any possible inconsistency
  2252. * in the global table
  2253. */
  2254. orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
  2255. /* The ttvn alone is not enough to guarantee consistency
  2256. * because a single value could represent different states
  2257. * (due to the wrap around). Thus a node has to check whether
  2258. * the resulting table (after applying the changes) is still
  2259. * consistent or not. E.g. a node could disconnect while its
  2260. * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
  2261. * checking the CRC value is mandatory to detect the
  2262. * inconsistency
  2263. */
  2264. if (orig_node->tt_crc != tt_crc)
  2265. goto request_table;
  2266. } else {
  2267. /* if we missed more than one change or our tables are not
  2268. * in sync anymore -> request fresh tt data
  2269. */
  2270. if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
  2271. orig_node->tt_crc != tt_crc) {
  2272. request_table:
  2273. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2274. "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.8x last_crc: %#.8x num_changes: %u)\n",
  2275. orig_node->orig, ttvn, orig_ttvn, tt_crc,
  2276. orig_node->tt_crc, tt_num_changes);
  2277. batadv_send_tt_request(bat_priv, orig_node, ttvn,
  2278. tt_crc, full_table);
  2279. return;
  2280. }
  2281. }
  2282. }
  2283. /**
  2284. * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
  2285. * @bat_priv: the bat priv with all the soft interface information
  2286. * @addr: the mac address of the client to check
  2287. * @vid: VLAN identifier
  2288. *
  2289. * Returns true if we know that the client has moved from its old originator
  2290. * to another one. This entry is still kept for consistency purposes and will be
  2291. * deleted later by a DEL or because of timeout
  2292. */
  2293. bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
  2294. uint8_t *addr, unsigned short vid)
  2295. {
  2296. struct batadv_tt_global_entry *tt_global_entry;
  2297. bool ret = false;
  2298. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  2299. if (!tt_global_entry)
  2300. goto out;
  2301. ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
  2302. batadv_tt_global_entry_free_ref(tt_global_entry);
  2303. out:
  2304. return ret;
  2305. }
  2306. /**
  2307. * batadv_tt_local_client_is_roaming - tells whether the client is roaming
  2308. * @bat_priv: the bat priv with all the soft interface information
  2309. * @addr: the mac address of the local client to query
  2310. * @vid: VLAN identifier
  2311. *
  2312. * Returns true if the local client is known to be roaming (it is not served by
  2313. * this node anymore) or not. If yes, the client is still present in the table
  2314. * to keep the latter consistent with the node TTVN
  2315. */
  2316. bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
  2317. uint8_t *addr, unsigned short vid)
  2318. {
  2319. struct batadv_tt_local_entry *tt_local_entry;
  2320. bool ret = false;
  2321. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
  2322. if (!tt_local_entry)
  2323. goto out;
  2324. ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
  2325. batadv_tt_local_entry_free_ref(tt_local_entry);
  2326. out:
  2327. return ret;
  2328. }
  2329. bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
  2330. struct batadv_orig_node *orig_node,
  2331. const unsigned char *addr,
  2332. unsigned short vid)
  2333. {
  2334. bool ret = false;
  2335. /* if the originator is a backbone node (meaning it belongs to the same
  2336. * LAN of this node) the temporary client must not be added because to
  2337. * reach such destination the node must use the LAN instead of the mesh
  2338. */
  2339. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
  2340. goto out;
  2341. if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
  2342. BATADV_TT_CLIENT_TEMP,
  2343. atomic_read(&orig_node->last_ttvn)))
  2344. goto out;
  2345. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2346. "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
  2347. addr, BATADV_PRINT_VID(vid), orig_node->orig);
  2348. ret = true;
  2349. out:
  2350. return ret;
  2351. }
  2352. /**
  2353. * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
  2354. * @bat_priv: the bat priv with all the soft interface information
  2355. * @orig: the orig_node of the ogm
  2356. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  2357. * @tvlv_value: tvlv buffer containing the gateway data
  2358. * @tvlv_value_len: tvlv buffer length
  2359. */
  2360. static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  2361. struct batadv_orig_node *orig,
  2362. uint8_t flags,
  2363. void *tvlv_value,
  2364. uint16_t tvlv_value_len)
  2365. {
  2366. struct batadv_tvlv_tt_data *tt_data;
  2367. uint16_t num_entries;
  2368. if (tvlv_value_len < sizeof(*tt_data))
  2369. return;
  2370. tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
  2371. tvlv_value_len -= sizeof(*tt_data);
  2372. num_entries = batadv_tt_entries(tvlv_value_len);
  2373. batadv_tt_update_orig(bat_priv, orig,
  2374. (unsigned char *)(tt_data + 1),
  2375. num_entries, tt_data->ttvn, ntohl(tt_data->crc));
  2376. }
  2377. /**
  2378. * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
  2379. * container
  2380. * @bat_priv: the bat priv with all the soft interface information
  2381. * @src: mac address of tt tvlv sender
  2382. * @dst: mac address of tt tvlv recipient
  2383. * @tvlv_value: tvlv buffer containing the tt data
  2384. * @tvlv_value_len: tvlv buffer length
  2385. *
  2386. * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
  2387. * otherwise.
  2388. */
  2389. static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
  2390. uint8_t *src, uint8_t *dst,
  2391. void *tvlv_value,
  2392. uint16_t tvlv_value_len)
  2393. {
  2394. struct batadv_tvlv_tt_data *tt_data;
  2395. uint16_t num_entries;
  2396. char tt_flag;
  2397. bool ret;
  2398. if (tvlv_value_len < sizeof(*tt_data))
  2399. return NET_RX_SUCCESS;
  2400. tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
  2401. tvlv_value_len -= sizeof(*tt_data);
  2402. num_entries = batadv_tt_entries(tvlv_value_len);
  2403. switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
  2404. case BATADV_TT_REQUEST:
  2405. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
  2406. /* If this node cannot provide a TT response the tt_request is
  2407. * forwarded
  2408. */
  2409. ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
  2410. if (!ret) {
  2411. if (tt_data->flags & BATADV_TT_FULL_TABLE)
  2412. tt_flag = 'F';
  2413. else
  2414. tt_flag = '.';
  2415. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2416. "Routing TT_REQUEST to %pM [%c]\n",
  2417. dst, tt_flag);
  2418. /* tvlv API will re-route the packet */
  2419. return NET_RX_DROP;
  2420. }
  2421. break;
  2422. case BATADV_TT_RESPONSE:
  2423. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
  2424. if (batadv_is_my_mac(bat_priv, dst)) {
  2425. batadv_handle_tt_response(bat_priv, tt_data,
  2426. src, num_entries);
  2427. return NET_RX_SUCCESS;
  2428. }
  2429. if (tt_data->flags & BATADV_TT_FULL_TABLE)
  2430. tt_flag = 'F';
  2431. else
  2432. tt_flag = '.';
  2433. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2434. "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
  2435. /* tvlv API will re-route the packet */
  2436. return NET_RX_DROP;
  2437. }
  2438. return NET_RX_SUCCESS;
  2439. }
  2440. /**
  2441. * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
  2442. * @bat_priv: the bat priv with all the soft interface information
  2443. * @src: mac address of tt tvlv sender
  2444. * @dst: mac address of tt tvlv recipient
  2445. * @tvlv_value: tvlv buffer containing the tt data
  2446. * @tvlv_value_len: tvlv buffer length
  2447. *
  2448. * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
  2449. * otherwise.
  2450. */
  2451. static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
  2452. uint8_t *src, uint8_t *dst,
  2453. void *tvlv_value,
  2454. uint16_t tvlv_value_len)
  2455. {
  2456. struct batadv_tvlv_roam_adv *roaming_adv;
  2457. struct batadv_orig_node *orig_node = NULL;
  2458. /* If this node is not the intended recipient of the
  2459. * roaming advertisement the packet is forwarded
  2460. * (the tvlv API will re-route the packet).
  2461. */
  2462. if (!batadv_is_my_mac(bat_priv, dst))
  2463. return NET_RX_DROP;
  2464. /* check if it is a backbone gateway. we don't accept
  2465. * roaming advertisement from it, as it has the same
  2466. * entries as we have.
  2467. */
  2468. if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
  2469. goto out;
  2470. if (tvlv_value_len < sizeof(*roaming_adv))
  2471. goto out;
  2472. orig_node = batadv_orig_hash_find(bat_priv, src);
  2473. if (!orig_node)
  2474. goto out;
  2475. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
  2476. roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
  2477. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2478. "Received ROAMING_ADV from %pM (client %pM)\n",
  2479. src, roaming_adv->client);
  2480. batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
  2481. ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
  2482. atomic_read(&orig_node->last_ttvn) + 1);
  2483. out:
  2484. if (orig_node)
  2485. batadv_orig_node_free_ref(orig_node);
  2486. return NET_RX_SUCCESS;
  2487. }
  2488. /**
  2489. * batadv_tt_init - initialise the translation table internals
  2490. * @bat_priv: the bat priv with all the soft interface information
  2491. *
  2492. * Return 0 on success or negative error number in case of failure.
  2493. */
  2494. int batadv_tt_init(struct batadv_priv *bat_priv)
  2495. {
  2496. int ret;
  2497. ret = batadv_tt_local_init(bat_priv);
  2498. if (ret < 0)
  2499. return ret;
  2500. ret = batadv_tt_global_init(bat_priv);
  2501. if (ret < 0)
  2502. return ret;
  2503. batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
  2504. batadv_tt_tvlv_unicast_handler_v1,
  2505. BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
  2506. batadv_tvlv_handler_register(bat_priv, NULL,
  2507. batadv_roam_tvlv_unicast_handler_v1,
  2508. BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
  2509. INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
  2510. queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
  2511. msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
  2512. return 1;
  2513. }