translation-table.c 74 KB

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