translation-table.c 74 KB

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