translation-table.c 80 KB

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