translation-table.c 69 KB

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