translation-table.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. /*
  2. * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "translation-table.h"
  23. #include "soft-interface.h"
  24. #include "hard-interface.h"
  25. #include "send.h"
  26. #include "hash.h"
  27. #include "originator.h"
  28. #include "routing.h"
  29. #include <linux/crc16.h>
  30. static void _tt_global_del(struct bat_priv *bat_priv,
  31. struct tt_global_entry *tt_global_entry,
  32. const char *message);
  33. static void tt_purge(struct work_struct *work);
  34. /* returns 1 if they are the same mac addr */
  35. static int compare_tt(const struct hlist_node *node, const void *data2)
  36. {
  37. const void *data1 = container_of(node, struct tt_common_entry,
  38. hash_entry);
  39. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  40. }
  41. static void tt_start_timer(struct bat_priv *bat_priv)
  42. {
  43. INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
  44. queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
  45. msecs_to_jiffies(5000));
  46. }
  47. static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash,
  48. const void *data)
  49. {
  50. struct hlist_head *head;
  51. struct hlist_node *node;
  52. struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
  53. uint32_t index;
  54. if (!hash)
  55. return NULL;
  56. index = choose_orig(data, hash->size);
  57. head = &hash->table[index];
  58. rcu_read_lock();
  59. hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
  60. if (!compare_eth(tt_common_entry, data))
  61. continue;
  62. if (!atomic_inc_not_zero(&tt_common_entry->refcount))
  63. continue;
  64. tt_common_entry_tmp = tt_common_entry;
  65. break;
  66. }
  67. rcu_read_unlock();
  68. return tt_common_entry_tmp;
  69. }
  70. static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
  71. const void *data)
  72. {
  73. struct tt_common_entry *tt_common_entry;
  74. struct tt_local_entry *tt_local_entry = NULL;
  75. tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
  76. if (tt_common_entry)
  77. tt_local_entry = container_of(tt_common_entry,
  78. struct tt_local_entry, common);
  79. return tt_local_entry;
  80. }
  81. static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
  82. const void *data)
  83. {
  84. struct tt_common_entry *tt_common_entry;
  85. struct tt_global_entry *tt_global_entry = NULL;
  86. tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
  87. if (tt_common_entry)
  88. tt_global_entry = container_of(tt_common_entry,
  89. struct tt_global_entry, common);
  90. return tt_global_entry;
  91. }
  92. static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
  93. {
  94. if (atomic_dec_and_test(&tt_local_entry->common.refcount))
  95. kfree_rcu(tt_local_entry, common.rcu);
  96. }
  97. static void tt_global_entry_free_rcu(struct rcu_head *rcu)
  98. {
  99. struct tt_common_entry *tt_common_entry;
  100. struct tt_global_entry *tt_global_entry;
  101. tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
  102. tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
  103. common);
  104. if (tt_global_entry->orig_node)
  105. orig_node_free_ref(tt_global_entry->orig_node);
  106. kfree(tt_global_entry);
  107. }
  108. static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
  109. {
  110. if (atomic_dec_and_test(&tt_global_entry->common.refcount))
  111. call_rcu(&tt_global_entry->common.rcu,
  112. tt_global_entry_free_rcu);
  113. }
  114. static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
  115. uint8_t flags)
  116. {
  117. struct tt_change_node *tt_change_node;
  118. tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
  119. if (!tt_change_node)
  120. return;
  121. tt_change_node->change.flags = flags;
  122. memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
  123. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  124. /* track the change in the OGMinterval list */
  125. list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
  126. atomic_inc(&bat_priv->tt_local_changes);
  127. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  128. atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
  129. }
  130. int tt_len(int changes_num)
  131. {
  132. return changes_num * sizeof(struct tt_change);
  133. }
  134. static int tt_local_init(struct bat_priv *bat_priv)
  135. {
  136. if (bat_priv->tt_local_hash)
  137. return 1;
  138. bat_priv->tt_local_hash = hash_new(1024);
  139. if (!bat_priv->tt_local_hash)
  140. return 0;
  141. return 1;
  142. }
  143. void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
  144. int ifindex)
  145. {
  146. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  147. struct tt_local_entry *tt_local_entry = NULL;
  148. struct tt_global_entry *tt_global_entry = NULL;
  149. int hash_added;
  150. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  151. if (tt_local_entry) {
  152. tt_local_entry->last_seen = jiffies;
  153. goto out;
  154. }
  155. tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
  156. if (!tt_local_entry)
  157. goto out;
  158. bat_dbg(DBG_TT, bat_priv,
  159. "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
  160. (uint8_t)atomic_read(&bat_priv->ttvn));
  161. memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
  162. tt_local_entry->common.flags = NO_FLAGS;
  163. if (is_wifi_iface(ifindex))
  164. tt_local_entry->common.flags |= TT_CLIENT_WIFI;
  165. atomic_set(&tt_local_entry->common.refcount, 2);
  166. tt_local_entry->last_seen = jiffies;
  167. /* the batman interface mac address should never be purged */
  168. if (compare_eth(addr, soft_iface->dev_addr))
  169. tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
  170. /* The local entry has to be marked as NEW to avoid to send it in
  171. * a full table response going out before the next ttvn increment
  172. * (consistency check) */
  173. tt_local_entry->common.flags |= TT_CLIENT_NEW;
  174. hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
  175. &tt_local_entry->common,
  176. &tt_local_entry->common.hash_entry);
  177. if (unlikely(hash_added != 0)) {
  178. /* remove the reference for the hash */
  179. tt_local_entry_free_ref(tt_local_entry);
  180. goto out;
  181. }
  182. tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
  183. /* remove address from global hash if present */
  184. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  185. /* Check whether it is a roaming! */
  186. if (tt_global_entry) {
  187. /* This node is probably going to update its tt table */
  188. tt_global_entry->orig_node->tt_poss_change = true;
  189. /* The global entry has to be marked as ROAMING and has to be
  190. * kept for consistency purpose */
  191. tt_global_entry->common.flags |= TT_CLIENT_ROAM;
  192. tt_global_entry->roam_at = jiffies;
  193. send_roam_adv(bat_priv, tt_global_entry->common.addr,
  194. tt_global_entry->orig_node);
  195. }
  196. out:
  197. if (tt_local_entry)
  198. tt_local_entry_free_ref(tt_local_entry);
  199. if (tt_global_entry)
  200. tt_global_entry_free_ref(tt_global_entry);
  201. }
  202. int tt_changes_fill_buffer(struct bat_priv *bat_priv,
  203. unsigned char *buff, int buff_len)
  204. {
  205. int count = 0, tot_changes = 0;
  206. struct tt_change_node *entry, *safe;
  207. if (buff_len > 0)
  208. tot_changes = buff_len / tt_len(1);
  209. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  210. atomic_set(&bat_priv->tt_local_changes, 0);
  211. list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
  212. list) {
  213. if (count < tot_changes) {
  214. memcpy(buff + tt_len(count),
  215. &entry->change, sizeof(struct tt_change));
  216. count++;
  217. }
  218. list_del(&entry->list);
  219. kfree(entry);
  220. }
  221. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  222. /* Keep the buffer for possible tt_request */
  223. spin_lock_bh(&bat_priv->tt_buff_lock);
  224. kfree(bat_priv->tt_buff);
  225. bat_priv->tt_buff_len = 0;
  226. bat_priv->tt_buff = NULL;
  227. /* We check whether this new OGM has no changes due to size
  228. * problems */
  229. if (buff_len > 0) {
  230. /**
  231. * if kmalloc() fails we will reply with the full table
  232. * instead of providing the diff
  233. */
  234. bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
  235. if (bat_priv->tt_buff) {
  236. memcpy(bat_priv->tt_buff, buff, buff_len);
  237. bat_priv->tt_buff_len = buff_len;
  238. }
  239. }
  240. spin_unlock_bh(&bat_priv->tt_buff_lock);
  241. return tot_changes;
  242. }
  243. int tt_local_seq_print_text(struct seq_file *seq, void *offset)
  244. {
  245. struct net_device *net_dev = (struct net_device *)seq->private;
  246. struct bat_priv *bat_priv = netdev_priv(net_dev);
  247. struct hashtable_t *hash = bat_priv->tt_local_hash;
  248. struct tt_common_entry *tt_common_entry;
  249. struct hard_iface *primary_if;
  250. struct hlist_node *node;
  251. struct hlist_head *head;
  252. uint32_t i;
  253. int ret = 0;
  254. primary_if = primary_if_get_selected(bat_priv);
  255. if (!primary_if) {
  256. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  257. "please specify interfaces to enable it\n",
  258. net_dev->name);
  259. goto out;
  260. }
  261. if (primary_if->if_status != IF_ACTIVE) {
  262. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  263. "primary interface not active\n",
  264. net_dev->name);
  265. goto out;
  266. }
  267. seq_printf(seq, "Locally retrieved addresses (from %s) "
  268. "announced via TT (TTVN: %u):\n",
  269. net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
  270. for (i = 0; i < hash->size; i++) {
  271. head = &hash->table[i];
  272. rcu_read_lock();
  273. hlist_for_each_entry_rcu(tt_common_entry, node,
  274. head, hash_entry) {
  275. seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
  276. tt_common_entry->addr,
  277. (tt_common_entry->flags &
  278. TT_CLIENT_ROAM ? 'R' : '.'),
  279. (tt_common_entry->flags &
  280. TT_CLIENT_NOPURGE ? 'P' : '.'),
  281. (tt_common_entry->flags &
  282. TT_CLIENT_NEW ? 'N' : '.'),
  283. (tt_common_entry->flags &
  284. TT_CLIENT_PENDING ? 'X' : '.'),
  285. (tt_common_entry->flags &
  286. TT_CLIENT_WIFI ? 'W' : '.'));
  287. }
  288. rcu_read_unlock();
  289. }
  290. out:
  291. if (primary_if)
  292. hardif_free_ref(primary_if);
  293. return ret;
  294. }
  295. static void tt_local_set_pending(struct bat_priv *bat_priv,
  296. struct tt_local_entry *tt_local_entry,
  297. uint16_t flags, const char *message)
  298. {
  299. tt_local_event(bat_priv, tt_local_entry->common.addr,
  300. tt_local_entry->common.flags | flags);
  301. /* The local client has to be marked as "pending to be removed" but has
  302. * to be kept in the table in order to send it in a full table
  303. * response issued before the net ttvn increment (consistency check) */
  304. tt_local_entry->common.flags |= TT_CLIENT_PENDING;
  305. bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
  306. "%s\n", tt_local_entry->common.addr, message);
  307. }
  308. void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
  309. const char *message, bool roaming)
  310. {
  311. struct tt_local_entry *tt_local_entry = NULL;
  312. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  313. if (!tt_local_entry)
  314. goto out;
  315. tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
  316. (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message);
  317. out:
  318. if (tt_local_entry)
  319. tt_local_entry_free_ref(tt_local_entry);
  320. }
  321. static void tt_local_purge(struct bat_priv *bat_priv)
  322. {
  323. struct hashtable_t *hash = bat_priv->tt_local_hash;
  324. struct tt_local_entry *tt_local_entry;
  325. struct tt_common_entry *tt_common_entry;
  326. struct hlist_node *node, *node_tmp;
  327. struct hlist_head *head;
  328. spinlock_t *list_lock; /* protects write access to the hash lists */
  329. uint32_t i;
  330. for (i = 0; i < hash->size; i++) {
  331. head = &hash->table[i];
  332. list_lock = &hash->list_locks[i];
  333. spin_lock_bh(list_lock);
  334. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  335. head, hash_entry) {
  336. tt_local_entry = container_of(tt_common_entry,
  337. struct tt_local_entry,
  338. common);
  339. if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
  340. continue;
  341. /* entry already marked for deletion */
  342. if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
  343. continue;
  344. if (!has_timed_out(tt_local_entry->last_seen,
  345. TT_LOCAL_TIMEOUT))
  346. continue;
  347. tt_local_set_pending(bat_priv, tt_local_entry,
  348. TT_CLIENT_DEL, "timed out");
  349. }
  350. spin_unlock_bh(list_lock);
  351. }
  352. }
  353. static void tt_local_table_free(struct bat_priv *bat_priv)
  354. {
  355. struct hashtable_t *hash;
  356. spinlock_t *list_lock; /* protects write access to the hash lists */
  357. struct tt_common_entry *tt_common_entry;
  358. struct tt_local_entry *tt_local_entry;
  359. struct hlist_node *node, *node_tmp;
  360. struct hlist_head *head;
  361. uint32_t i;
  362. if (!bat_priv->tt_local_hash)
  363. return;
  364. hash = bat_priv->tt_local_hash;
  365. for (i = 0; i < hash->size; i++) {
  366. head = &hash->table[i];
  367. list_lock = &hash->list_locks[i];
  368. spin_lock_bh(list_lock);
  369. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  370. head, hash_entry) {
  371. hlist_del_rcu(node);
  372. tt_local_entry = container_of(tt_common_entry,
  373. struct tt_local_entry,
  374. common);
  375. tt_local_entry_free_ref(tt_local_entry);
  376. }
  377. spin_unlock_bh(list_lock);
  378. }
  379. hash_destroy(hash);
  380. bat_priv->tt_local_hash = NULL;
  381. }
  382. static int tt_global_init(struct bat_priv *bat_priv)
  383. {
  384. if (bat_priv->tt_global_hash)
  385. return 1;
  386. bat_priv->tt_global_hash = hash_new(1024);
  387. if (!bat_priv->tt_global_hash)
  388. return 0;
  389. return 1;
  390. }
  391. static void tt_changes_list_free(struct bat_priv *bat_priv)
  392. {
  393. struct tt_change_node *entry, *safe;
  394. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  395. list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
  396. list) {
  397. list_del(&entry->list);
  398. kfree(entry);
  399. }
  400. atomic_set(&bat_priv->tt_local_changes, 0);
  401. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  402. }
  403. /* caller must hold orig_node refcount */
  404. int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
  405. const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
  406. bool wifi)
  407. {
  408. struct tt_global_entry *tt_global_entry;
  409. struct orig_node *orig_node_tmp;
  410. int ret = 0;
  411. int hash_added;
  412. tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
  413. if (!tt_global_entry) {
  414. tt_global_entry =
  415. kmalloc(sizeof(*tt_global_entry),
  416. GFP_ATOMIC);
  417. if (!tt_global_entry)
  418. goto out;
  419. memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
  420. tt_global_entry->common.flags = NO_FLAGS;
  421. atomic_set(&tt_global_entry->common.refcount, 2);
  422. /* Assign the new orig_node */
  423. atomic_inc(&orig_node->refcount);
  424. tt_global_entry->orig_node = orig_node;
  425. tt_global_entry->ttvn = ttvn;
  426. tt_global_entry->roam_at = 0;
  427. hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
  428. choose_orig, &tt_global_entry->common,
  429. &tt_global_entry->common.hash_entry);
  430. if (unlikely(hash_added != 0)) {
  431. /* remove the reference for the hash */
  432. tt_global_entry_free_ref(tt_global_entry);
  433. goto out_remove;
  434. }
  435. atomic_inc(&orig_node->tt_size);
  436. } else {
  437. if (tt_global_entry->orig_node != orig_node) {
  438. atomic_dec(&tt_global_entry->orig_node->tt_size);
  439. orig_node_tmp = tt_global_entry->orig_node;
  440. atomic_inc(&orig_node->refcount);
  441. tt_global_entry->orig_node = orig_node;
  442. orig_node_free_ref(orig_node_tmp);
  443. atomic_inc(&orig_node->tt_size);
  444. }
  445. tt_global_entry->common.flags = NO_FLAGS;
  446. tt_global_entry->ttvn = ttvn;
  447. tt_global_entry->roam_at = 0;
  448. }
  449. if (wifi)
  450. tt_global_entry->common.flags |= TT_CLIENT_WIFI;
  451. bat_dbg(DBG_TT, bat_priv,
  452. "Creating new global tt entry: %pM (via %pM)\n",
  453. tt_global_entry->common.addr, orig_node->orig);
  454. out_remove:
  455. /* remove address from local hash if present */
  456. tt_local_remove(bat_priv, tt_global_entry->common.addr,
  457. "global tt received", roaming);
  458. ret = 1;
  459. out:
  460. if (tt_global_entry)
  461. tt_global_entry_free_ref(tt_global_entry);
  462. return ret;
  463. }
  464. int tt_global_seq_print_text(struct seq_file *seq, void *offset)
  465. {
  466. struct net_device *net_dev = (struct net_device *)seq->private;
  467. struct bat_priv *bat_priv = netdev_priv(net_dev);
  468. struct hashtable_t *hash = bat_priv->tt_global_hash;
  469. struct tt_common_entry *tt_common_entry;
  470. struct tt_global_entry *tt_global_entry;
  471. struct hard_iface *primary_if;
  472. struct hlist_node *node;
  473. struct hlist_head *head;
  474. uint32_t i;
  475. int ret = 0;
  476. primary_if = primary_if_get_selected(bat_priv);
  477. if (!primary_if) {
  478. ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
  479. "specify interfaces to enable it\n",
  480. net_dev->name);
  481. goto out;
  482. }
  483. if (primary_if->if_status != IF_ACTIVE) {
  484. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  485. "primary interface not active\n",
  486. net_dev->name);
  487. goto out;
  488. }
  489. seq_printf(seq,
  490. "Globally announced TT entries received via the mesh %s\n",
  491. net_dev->name);
  492. seq_printf(seq, " %-13s %s %-15s %s %s\n",
  493. "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
  494. for (i = 0; i < hash->size; i++) {
  495. head = &hash->table[i];
  496. rcu_read_lock();
  497. hlist_for_each_entry_rcu(tt_common_entry, node,
  498. head, hash_entry) {
  499. tt_global_entry = container_of(tt_common_entry,
  500. struct tt_global_entry,
  501. common);
  502. seq_printf(seq, " * %pM (%3u) via %pM (%3u) "
  503. "[%c%c]\n",
  504. tt_global_entry->common.addr,
  505. tt_global_entry->ttvn,
  506. tt_global_entry->orig_node->orig,
  507. (uint8_t) atomic_read(
  508. &tt_global_entry->orig_node->
  509. last_ttvn),
  510. (tt_global_entry->common.flags &
  511. TT_CLIENT_ROAM ? 'R' : '.'),
  512. (tt_global_entry->common.flags &
  513. TT_CLIENT_WIFI ? 'W' : '.'));
  514. }
  515. rcu_read_unlock();
  516. }
  517. out:
  518. if (primary_if)
  519. hardif_free_ref(primary_if);
  520. return ret;
  521. }
  522. static void _tt_global_del(struct bat_priv *bat_priv,
  523. struct tt_global_entry *tt_global_entry,
  524. const char *message)
  525. {
  526. if (!tt_global_entry)
  527. goto out;
  528. bat_dbg(DBG_TT, bat_priv,
  529. "Deleting global tt entry %pM (via %pM): %s\n",
  530. tt_global_entry->common.addr, tt_global_entry->orig_node->orig,
  531. message);
  532. atomic_dec(&tt_global_entry->orig_node->tt_size);
  533. hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
  534. tt_global_entry->common.addr);
  535. out:
  536. if (tt_global_entry)
  537. tt_global_entry_free_ref(tt_global_entry);
  538. }
  539. void tt_global_del(struct bat_priv *bat_priv,
  540. struct orig_node *orig_node, const unsigned char *addr,
  541. const char *message, bool roaming)
  542. {
  543. struct tt_global_entry *tt_global_entry = NULL;
  544. struct tt_local_entry *tt_local_entry = NULL;
  545. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  546. if (!tt_global_entry || tt_global_entry->orig_node != orig_node)
  547. goto out;
  548. if (!roaming)
  549. goto out_del;
  550. /* if we are deleting a global entry due to a roam
  551. * event, there are two possibilities:
  552. * 1) the client roamed from node A to node B => we mark
  553. * it with TT_CLIENT_ROAM, we start a timer and we
  554. * wait for node B to claim it. In case of timeout
  555. * the entry is purged.
  556. * 2) the client roamed to us => we can directly delete
  557. * the global entry, since it is useless now. */
  558. tt_local_entry = tt_local_hash_find(bat_priv,
  559. tt_global_entry->common.addr);
  560. if (!tt_local_entry) {
  561. tt_global_entry->common.flags |= TT_CLIENT_ROAM;
  562. tt_global_entry->roam_at = jiffies;
  563. goto out;
  564. }
  565. out_del:
  566. _tt_global_del(bat_priv, tt_global_entry, message);
  567. out:
  568. if (tt_global_entry)
  569. tt_global_entry_free_ref(tt_global_entry);
  570. if (tt_local_entry)
  571. tt_local_entry_free_ref(tt_local_entry);
  572. }
  573. void tt_global_del_orig(struct bat_priv *bat_priv,
  574. struct orig_node *orig_node, const char *message)
  575. {
  576. struct tt_global_entry *tt_global_entry;
  577. struct tt_common_entry *tt_common_entry;
  578. uint32_t i;
  579. struct hashtable_t *hash = bat_priv->tt_global_hash;
  580. struct hlist_node *node, *safe;
  581. struct hlist_head *head;
  582. spinlock_t *list_lock; /* protects write access to the hash lists */
  583. if (!hash)
  584. return;
  585. for (i = 0; i < hash->size; i++) {
  586. head = &hash->table[i];
  587. list_lock = &hash->list_locks[i];
  588. spin_lock_bh(list_lock);
  589. hlist_for_each_entry_safe(tt_common_entry, node, safe,
  590. head, hash_entry) {
  591. tt_global_entry = container_of(tt_common_entry,
  592. struct tt_global_entry,
  593. common);
  594. if (tt_global_entry->orig_node == orig_node) {
  595. bat_dbg(DBG_TT, bat_priv,
  596. "Deleting global tt entry %pM "
  597. "(via %pM): %s\n",
  598. tt_global_entry->common.addr,
  599. tt_global_entry->orig_node->orig,
  600. message);
  601. hlist_del_rcu(node);
  602. tt_global_entry_free_ref(tt_global_entry);
  603. }
  604. }
  605. spin_unlock_bh(list_lock);
  606. }
  607. atomic_set(&orig_node->tt_size, 0);
  608. orig_node->tt_initialised = false;
  609. }
  610. static void tt_global_roam_purge(struct bat_priv *bat_priv)
  611. {
  612. struct hashtable_t *hash = bat_priv->tt_global_hash;
  613. struct tt_common_entry *tt_common_entry;
  614. struct tt_global_entry *tt_global_entry;
  615. struct hlist_node *node, *node_tmp;
  616. struct hlist_head *head;
  617. spinlock_t *list_lock; /* protects write access to the hash lists */
  618. uint32_t i;
  619. for (i = 0; i < hash->size; i++) {
  620. head = &hash->table[i];
  621. list_lock = &hash->list_locks[i];
  622. spin_lock_bh(list_lock);
  623. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  624. head, hash_entry) {
  625. tt_global_entry = container_of(tt_common_entry,
  626. struct tt_global_entry,
  627. common);
  628. if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
  629. continue;
  630. if (!has_timed_out(tt_global_entry->roam_at,
  631. TT_CLIENT_ROAM_TIMEOUT))
  632. continue;
  633. bat_dbg(DBG_TT, bat_priv, "Deleting global "
  634. "tt entry (%pM): Roaming timeout\n",
  635. tt_global_entry->common.addr);
  636. atomic_dec(&tt_global_entry->orig_node->tt_size);
  637. hlist_del_rcu(node);
  638. tt_global_entry_free_ref(tt_global_entry);
  639. }
  640. spin_unlock_bh(list_lock);
  641. }
  642. }
  643. static void tt_global_table_free(struct bat_priv *bat_priv)
  644. {
  645. struct hashtable_t *hash;
  646. spinlock_t *list_lock; /* protects write access to the hash lists */
  647. struct tt_common_entry *tt_common_entry;
  648. struct tt_global_entry *tt_global_entry;
  649. struct hlist_node *node, *node_tmp;
  650. struct hlist_head *head;
  651. uint32_t i;
  652. if (!bat_priv->tt_global_hash)
  653. return;
  654. hash = bat_priv->tt_global_hash;
  655. for (i = 0; i < hash->size; i++) {
  656. head = &hash->table[i];
  657. list_lock = &hash->list_locks[i];
  658. spin_lock_bh(list_lock);
  659. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  660. head, hash_entry) {
  661. hlist_del_rcu(node);
  662. tt_global_entry = container_of(tt_common_entry,
  663. struct tt_global_entry,
  664. common);
  665. tt_global_entry_free_ref(tt_global_entry);
  666. }
  667. spin_unlock_bh(list_lock);
  668. }
  669. hash_destroy(hash);
  670. bat_priv->tt_global_hash = NULL;
  671. }
  672. static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
  673. struct tt_global_entry *tt_global_entry)
  674. {
  675. bool ret = false;
  676. if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
  677. tt_global_entry->common.flags & TT_CLIENT_WIFI)
  678. ret = true;
  679. return ret;
  680. }
  681. struct orig_node *transtable_search(struct bat_priv *bat_priv,
  682. const uint8_t *src, const uint8_t *addr)
  683. {
  684. struct tt_local_entry *tt_local_entry = NULL;
  685. struct tt_global_entry *tt_global_entry = NULL;
  686. struct orig_node *orig_node = NULL;
  687. if (src && atomic_read(&bat_priv->ap_isolation)) {
  688. tt_local_entry = tt_local_hash_find(bat_priv, src);
  689. if (!tt_local_entry)
  690. goto out;
  691. }
  692. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  693. if (!tt_global_entry)
  694. goto out;
  695. /* check whether the clients should not communicate due to AP
  696. * isolation */
  697. if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
  698. goto out;
  699. if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
  700. goto out;
  701. orig_node = tt_global_entry->orig_node;
  702. out:
  703. if (tt_global_entry)
  704. tt_global_entry_free_ref(tt_global_entry);
  705. if (tt_local_entry)
  706. tt_local_entry_free_ref(tt_local_entry);
  707. return orig_node;
  708. }
  709. /* Calculates the checksum of the local table of a given orig_node */
  710. uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
  711. {
  712. uint16_t total = 0, total_one;
  713. struct hashtable_t *hash = bat_priv->tt_global_hash;
  714. struct tt_common_entry *tt_common_entry;
  715. struct tt_global_entry *tt_global_entry;
  716. struct hlist_node *node;
  717. struct hlist_head *head;
  718. uint32_t i;
  719. int j;
  720. for (i = 0; i < hash->size; i++) {
  721. head = &hash->table[i];
  722. rcu_read_lock();
  723. hlist_for_each_entry_rcu(tt_common_entry, node,
  724. head, hash_entry) {
  725. tt_global_entry = container_of(tt_common_entry,
  726. struct tt_global_entry,
  727. common);
  728. if (compare_eth(tt_global_entry->orig_node,
  729. orig_node)) {
  730. /* Roaming clients are in the global table for
  731. * consistency only. They don't have to be
  732. * taken into account while computing the
  733. * global crc */
  734. if (tt_common_entry->flags & TT_CLIENT_ROAM)
  735. continue;
  736. total_one = 0;
  737. for (j = 0; j < ETH_ALEN; j++)
  738. total_one = crc16_byte(total_one,
  739. tt_common_entry->addr[j]);
  740. total ^= total_one;
  741. }
  742. }
  743. rcu_read_unlock();
  744. }
  745. return total;
  746. }
  747. /* Calculates the checksum of the local table */
  748. uint16_t tt_local_crc(struct bat_priv *bat_priv)
  749. {
  750. uint16_t total = 0, total_one;
  751. struct hashtable_t *hash = bat_priv->tt_local_hash;
  752. struct tt_common_entry *tt_common_entry;
  753. struct hlist_node *node;
  754. struct hlist_head *head;
  755. uint32_t i;
  756. int j;
  757. for (i = 0; i < hash->size; i++) {
  758. head = &hash->table[i];
  759. rcu_read_lock();
  760. hlist_for_each_entry_rcu(tt_common_entry, node,
  761. head, hash_entry) {
  762. /* not yet committed clients have not to be taken into
  763. * account while computing the CRC */
  764. if (tt_common_entry->flags & TT_CLIENT_NEW)
  765. continue;
  766. total_one = 0;
  767. for (j = 0; j < ETH_ALEN; j++)
  768. total_one = crc16_byte(total_one,
  769. tt_common_entry->addr[j]);
  770. total ^= total_one;
  771. }
  772. rcu_read_unlock();
  773. }
  774. return total;
  775. }
  776. static void tt_req_list_free(struct bat_priv *bat_priv)
  777. {
  778. struct tt_req_node *node, *safe;
  779. spin_lock_bh(&bat_priv->tt_req_list_lock);
  780. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  781. list_del(&node->list);
  782. kfree(node);
  783. }
  784. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  785. }
  786. void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
  787. const unsigned char *tt_buff, uint8_t tt_num_changes)
  788. {
  789. uint16_t tt_buff_len = tt_len(tt_num_changes);
  790. /* Replace the old buffer only if I received something in the
  791. * last OGM (the OGM could carry no changes) */
  792. spin_lock_bh(&orig_node->tt_buff_lock);
  793. if (tt_buff_len > 0) {
  794. kfree(orig_node->tt_buff);
  795. orig_node->tt_buff_len = 0;
  796. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  797. if (orig_node->tt_buff) {
  798. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  799. orig_node->tt_buff_len = tt_buff_len;
  800. }
  801. }
  802. spin_unlock_bh(&orig_node->tt_buff_lock);
  803. }
  804. static void tt_req_purge(struct bat_priv *bat_priv)
  805. {
  806. struct tt_req_node *node, *safe;
  807. spin_lock_bh(&bat_priv->tt_req_list_lock);
  808. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  809. if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
  810. list_del(&node->list);
  811. kfree(node);
  812. }
  813. }
  814. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  815. }
  816. /* returns the pointer to the new tt_req_node struct if no request
  817. * has already been issued for this orig_node, NULL otherwise */
  818. static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
  819. struct orig_node *orig_node)
  820. {
  821. struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
  822. spin_lock_bh(&bat_priv->tt_req_list_lock);
  823. list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
  824. if (compare_eth(tt_req_node_tmp, orig_node) &&
  825. !has_timed_out(tt_req_node_tmp->issued_at,
  826. TT_REQUEST_TIMEOUT))
  827. goto unlock;
  828. }
  829. tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
  830. if (!tt_req_node)
  831. goto unlock;
  832. memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
  833. tt_req_node->issued_at = jiffies;
  834. list_add(&tt_req_node->list, &bat_priv->tt_req_list);
  835. unlock:
  836. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  837. return tt_req_node;
  838. }
  839. /* data_ptr is useless here, but has to be kept to respect the prototype */
  840. static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
  841. {
  842. const struct tt_common_entry *tt_common_entry = entry_ptr;
  843. if (tt_common_entry->flags & TT_CLIENT_NEW)
  844. return 0;
  845. return 1;
  846. }
  847. static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
  848. {
  849. const struct tt_common_entry *tt_common_entry = entry_ptr;
  850. const struct tt_global_entry *tt_global_entry;
  851. const struct orig_node *orig_node = data_ptr;
  852. if (tt_common_entry->flags & TT_CLIENT_ROAM)
  853. return 0;
  854. tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
  855. common);
  856. return (tt_global_entry->orig_node == orig_node);
  857. }
  858. static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
  859. struct hashtable_t *hash,
  860. struct hard_iface *primary_if,
  861. int (*valid_cb)(const void *,
  862. const void *),
  863. void *cb_data)
  864. {
  865. struct tt_common_entry *tt_common_entry;
  866. struct tt_query_packet *tt_response;
  867. struct tt_change *tt_change;
  868. struct hlist_node *node;
  869. struct hlist_head *head;
  870. struct sk_buff *skb = NULL;
  871. uint16_t tt_tot, tt_count;
  872. ssize_t tt_query_size = sizeof(struct tt_query_packet);
  873. uint32_t i;
  874. if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
  875. tt_len = primary_if->soft_iface->mtu - tt_query_size;
  876. tt_len -= tt_len % sizeof(struct tt_change);
  877. }
  878. tt_tot = tt_len / sizeof(struct tt_change);
  879. skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
  880. if (!skb)
  881. goto out;
  882. skb_reserve(skb, ETH_HLEN);
  883. tt_response = (struct tt_query_packet *)skb_put(skb,
  884. tt_query_size + tt_len);
  885. tt_response->ttvn = ttvn;
  886. tt_change = (struct tt_change *)(skb->data + tt_query_size);
  887. tt_count = 0;
  888. rcu_read_lock();
  889. for (i = 0; i < hash->size; i++) {
  890. head = &hash->table[i];
  891. hlist_for_each_entry_rcu(tt_common_entry, node,
  892. head, hash_entry) {
  893. if (tt_count == tt_tot)
  894. break;
  895. if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
  896. continue;
  897. memcpy(tt_change->addr, tt_common_entry->addr,
  898. ETH_ALEN);
  899. tt_change->flags = NO_FLAGS;
  900. tt_count++;
  901. tt_change++;
  902. }
  903. }
  904. rcu_read_unlock();
  905. /* store in the message the number of entries we have successfully
  906. * copied */
  907. tt_response->tt_data = htons(tt_count);
  908. out:
  909. return skb;
  910. }
  911. static int send_tt_request(struct bat_priv *bat_priv,
  912. struct orig_node *dst_orig_node,
  913. uint8_t ttvn, uint16_t tt_crc, bool full_table)
  914. {
  915. struct sk_buff *skb = NULL;
  916. struct tt_query_packet *tt_request;
  917. struct neigh_node *neigh_node = NULL;
  918. struct hard_iface *primary_if;
  919. struct tt_req_node *tt_req_node = NULL;
  920. int ret = 1;
  921. primary_if = primary_if_get_selected(bat_priv);
  922. if (!primary_if)
  923. goto out;
  924. /* The new tt_req will be issued only if I'm not waiting for a
  925. * reply from the same orig_node yet */
  926. tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
  927. if (!tt_req_node)
  928. goto out;
  929. skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
  930. if (!skb)
  931. goto out;
  932. skb_reserve(skb, ETH_HLEN);
  933. tt_request = (struct tt_query_packet *)skb_put(skb,
  934. sizeof(struct tt_query_packet));
  935. tt_request->header.packet_type = BAT_TT_QUERY;
  936. tt_request->header.version = COMPAT_VERSION;
  937. memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  938. memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
  939. tt_request->header.ttl = TTL;
  940. tt_request->ttvn = ttvn;
  941. tt_request->tt_data = tt_crc;
  942. tt_request->flags = TT_REQUEST;
  943. if (full_table)
  944. tt_request->flags |= TT_FULL_TABLE;
  945. neigh_node = orig_node_get_router(dst_orig_node);
  946. if (!neigh_node)
  947. goto out;
  948. bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
  949. "[%c]\n", dst_orig_node->orig, neigh_node->addr,
  950. (full_table ? 'F' : '.'));
  951. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  952. ret = 0;
  953. out:
  954. if (neigh_node)
  955. neigh_node_free_ref(neigh_node);
  956. if (primary_if)
  957. hardif_free_ref(primary_if);
  958. if (ret)
  959. kfree_skb(skb);
  960. if (ret && tt_req_node) {
  961. spin_lock_bh(&bat_priv->tt_req_list_lock);
  962. list_del(&tt_req_node->list);
  963. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  964. kfree(tt_req_node);
  965. }
  966. return ret;
  967. }
  968. static bool send_other_tt_response(struct bat_priv *bat_priv,
  969. struct tt_query_packet *tt_request)
  970. {
  971. struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
  972. struct neigh_node *neigh_node = NULL;
  973. struct hard_iface *primary_if = NULL;
  974. uint8_t orig_ttvn, req_ttvn, ttvn;
  975. int ret = false;
  976. unsigned char *tt_buff;
  977. bool full_table;
  978. uint16_t tt_len, tt_tot;
  979. struct sk_buff *skb = NULL;
  980. struct tt_query_packet *tt_response;
  981. bat_dbg(DBG_TT, bat_priv,
  982. "Received TT_REQUEST from %pM for "
  983. "ttvn: %u (%pM) [%c]\n", tt_request->src,
  984. tt_request->ttvn, tt_request->dst,
  985. (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
  986. /* Let's get the orig node of the REAL destination */
  987. req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
  988. if (!req_dst_orig_node)
  989. goto out;
  990. res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
  991. if (!res_dst_orig_node)
  992. goto out;
  993. neigh_node = orig_node_get_router(res_dst_orig_node);
  994. if (!neigh_node)
  995. goto out;
  996. primary_if = primary_if_get_selected(bat_priv);
  997. if (!primary_if)
  998. goto out;
  999. orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1000. req_ttvn = tt_request->ttvn;
  1001. /* I don't have the requested data */
  1002. if (orig_ttvn != req_ttvn ||
  1003. tt_request->tt_data != req_dst_orig_node->tt_crc)
  1004. goto out;
  1005. /* If the full table has been explicitly requested */
  1006. if (tt_request->flags & TT_FULL_TABLE ||
  1007. !req_dst_orig_node->tt_buff)
  1008. full_table = true;
  1009. else
  1010. full_table = false;
  1011. /* In this version, fragmentation is not implemented, then
  1012. * I'll send only one packet with as much TT entries as I can */
  1013. if (!full_table) {
  1014. spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
  1015. tt_len = req_dst_orig_node->tt_buff_len;
  1016. tt_tot = tt_len / sizeof(struct tt_change);
  1017. skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
  1018. tt_len + ETH_HLEN);
  1019. if (!skb)
  1020. goto unlock;
  1021. skb_reserve(skb, ETH_HLEN);
  1022. tt_response = (struct tt_query_packet *)skb_put(skb,
  1023. sizeof(struct tt_query_packet) + tt_len);
  1024. tt_response->ttvn = req_ttvn;
  1025. tt_response->tt_data = htons(tt_tot);
  1026. tt_buff = skb->data + sizeof(struct tt_query_packet);
  1027. /* Copy the last orig_node's OGM buffer */
  1028. memcpy(tt_buff, req_dst_orig_node->tt_buff,
  1029. req_dst_orig_node->tt_buff_len);
  1030. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1031. } else {
  1032. tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
  1033. sizeof(struct tt_change);
  1034. ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1035. skb = tt_response_fill_table(tt_len, ttvn,
  1036. bat_priv->tt_global_hash,
  1037. primary_if, tt_global_valid_entry,
  1038. req_dst_orig_node);
  1039. if (!skb)
  1040. goto out;
  1041. tt_response = (struct tt_query_packet *)skb->data;
  1042. }
  1043. tt_response->header.packet_type = BAT_TT_QUERY;
  1044. tt_response->header.version = COMPAT_VERSION;
  1045. tt_response->header.ttl = TTL;
  1046. memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
  1047. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1048. tt_response->flags = TT_RESPONSE;
  1049. if (full_table)
  1050. tt_response->flags |= TT_FULL_TABLE;
  1051. bat_dbg(DBG_TT, bat_priv,
  1052. "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
  1053. res_dst_orig_node->orig, neigh_node->addr,
  1054. req_dst_orig_node->orig, req_ttvn);
  1055. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1056. ret = true;
  1057. goto out;
  1058. unlock:
  1059. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1060. out:
  1061. if (res_dst_orig_node)
  1062. orig_node_free_ref(res_dst_orig_node);
  1063. if (req_dst_orig_node)
  1064. orig_node_free_ref(req_dst_orig_node);
  1065. if (neigh_node)
  1066. neigh_node_free_ref(neigh_node);
  1067. if (primary_if)
  1068. hardif_free_ref(primary_if);
  1069. if (!ret)
  1070. kfree_skb(skb);
  1071. return ret;
  1072. }
  1073. static bool send_my_tt_response(struct bat_priv *bat_priv,
  1074. struct tt_query_packet *tt_request)
  1075. {
  1076. struct orig_node *orig_node = NULL;
  1077. struct neigh_node *neigh_node = NULL;
  1078. struct hard_iface *primary_if = NULL;
  1079. uint8_t my_ttvn, req_ttvn, ttvn;
  1080. int ret = false;
  1081. unsigned char *tt_buff;
  1082. bool full_table;
  1083. uint16_t tt_len, tt_tot;
  1084. struct sk_buff *skb = NULL;
  1085. struct tt_query_packet *tt_response;
  1086. bat_dbg(DBG_TT, bat_priv,
  1087. "Received TT_REQUEST from %pM for "
  1088. "ttvn: %u (me) [%c]\n", tt_request->src,
  1089. tt_request->ttvn,
  1090. (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
  1091. my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1092. req_ttvn = tt_request->ttvn;
  1093. orig_node = orig_hash_find(bat_priv, tt_request->src);
  1094. if (!orig_node)
  1095. goto out;
  1096. neigh_node = orig_node_get_router(orig_node);
  1097. if (!neigh_node)
  1098. goto out;
  1099. primary_if = primary_if_get_selected(bat_priv);
  1100. if (!primary_if)
  1101. goto out;
  1102. /* If the full table has been explicitly requested or the gap
  1103. * is too big send the whole local translation table */
  1104. if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
  1105. !bat_priv->tt_buff)
  1106. full_table = true;
  1107. else
  1108. full_table = false;
  1109. /* In this version, fragmentation is not implemented, then
  1110. * I'll send only one packet with as much TT entries as I can */
  1111. if (!full_table) {
  1112. spin_lock_bh(&bat_priv->tt_buff_lock);
  1113. tt_len = bat_priv->tt_buff_len;
  1114. tt_tot = tt_len / sizeof(struct tt_change);
  1115. skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
  1116. tt_len + ETH_HLEN);
  1117. if (!skb)
  1118. goto unlock;
  1119. skb_reserve(skb, ETH_HLEN);
  1120. tt_response = (struct tt_query_packet *)skb_put(skb,
  1121. sizeof(struct tt_query_packet) + tt_len);
  1122. tt_response->ttvn = req_ttvn;
  1123. tt_response->tt_data = htons(tt_tot);
  1124. tt_buff = skb->data + sizeof(struct tt_query_packet);
  1125. memcpy(tt_buff, bat_priv->tt_buff,
  1126. bat_priv->tt_buff_len);
  1127. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1128. } else {
  1129. tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
  1130. sizeof(struct tt_change);
  1131. ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1132. skb = tt_response_fill_table(tt_len, ttvn,
  1133. bat_priv->tt_local_hash,
  1134. primary_if, tt_local_valid_entry,
  1135. NULL);
  1136. if (!skb)
  1137. goto out;
  1138. tt_response = (struct tt_query_packet *)skb->data;
  1139. }
  1140. tt_response->header.packet_type = BAT_TT_QUERY;
  1141. tt_response->header.version = COMPAT_VERSION;
  1142. tt_response->header.ttl = TTL;
  1143. memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1144. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1145. tt_response->flags = TT_RESPONSE;
  1146. if (full_table)
  1147. tt_response->flags |= TT_FULL_TABLE;
  1148. bat_dbg(DBG_TT, bat_priv,
  1149. "Sending TT_RESPONSE to %pM via %pM [%c]\n",
  1150. orig_node->orig, neigh_node->addr,
  1151. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1152. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1153. ret = true;
  1154. goto out;
  1155. unlock:
  1156. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1157. out:
  1158. if (orig_node)
  1159. orig_node_free_ref(orig_node);
  1160. if (neigh_node)
  1161. neigh_node_free_ref(neigh_node);
  1162. if (primary_if)
  1163. hardif_free_ref(primary_if);
  1164. if (!ret)
  1165. kfree_skb(skb);
  1166. /* This packet was for me, so it doesn't need to be re-routed */
  1167. return true;
  1168. }
  1169. bool send_tt_response(struct bat_priv *bat_priv,
  1170. struct tt_query_packet *tt_request)
  1171. {
  1172. if (is_my_mac(tt_request->dst))
  1173. return send_my_tt_response(bat_priv, tt_request);
  1174. else
  1175. return send_other_tt_response(bat_priv, tt_request);
  1176. }
  1177. static void _tt_update_changes(struct bat_priv *bat_priv,
  1178. struct orig_node *orig_node,
  1179. struct tt_change *tt_change,
  1180. uint16_t tt_num_changes, uint8_t ttvn)
  1181. {
  1182. int i;
  1183. for (i = 0; i < tt_num_changes; i++) {
  1184. if ((tt_change + i)->flags & TT_CLIENT_DEL)
  1185. tt_global_del(bat_priv, orig_node,
  1186. (tt_change + i)->addr,
  1187. "tt removed by changes",
  1188. (tt_change + i)->flags & TT_CLIENT_ROAM);
  1189. else
  1190. if (!tt_global_add(bat_priv, orig_node,
  1191. (tt_change + i)->addr, ttvn, false,
  1192. (tt_change + i)->flags &
  1193. TT_CLIENT_WIFI))
  1194. /* In case of problem while storing a
  1195. * global_entry, we stop the updating
  1196. * procedure without committing the
  1197. * ttvn change. This will avoid to send
  1198. * corrupted data on tt_request
  1199. */
  1200. return;
  1201. }
  1202. orig_node->tt_initialised = true;
  1203. }
  1204. static void tt_fill_gtable(struct bat_priv *bat_priv,
  1205. struct tt_query_packet *tt_response)
  1206. {
  1207. struct orig_node *orig_node = NULL;
  1208. orig_node = orig_hash_find(bat_priv, tt_response->src);
  1209. if (!orig_node)
  1210. goto out;
  1211. /* Purge the old table first.. */
  1212. tt_global_del_orig(bat_priv, orig_node, "Received full table");
  1213. _tt_update_changes(bat_priv, orig_node,
  1214. (struct tt_change *)(tt_response + 1),
  1215. tt_response->tt_data, tt_response->ttvn);
  1216. spin_lock_bh(&orig_node->tt_buff_lock);
  1217. kfree(orig_node->tt_buff);
  1218. orig_node->tt_buff_len = 0;
  1219. orig_node->tt_buff = NULL;
  1220. spin_unlock_bh(&orig_node->tt_buff_lock);
  1221. atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
  1222. out:
  1223. if (orig_node)
  1224. orig_node_free_ref(orig_node);
  1225. }
  1226. static void tt_update_changes(struct bat_priv *bat_priv,
  1227. struct orig_node *orig_node,
  1228. uint16_t tt_num_changes, uint8_t ttvn,
  1229. struct tt_change *tt_change)
  1230. {
  1231. _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
  1232. ttvn);
  1233. tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
  1234. tt_num_changes);
  1235. atomic_set(&orig_node->last_ttvn, ttvn);
  1236. }
  1237. bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
  1238. {
  1239. struct tt_local_entry *tt_local_entry = NULL;
  1240. bool ret = false;
  1241. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  1242. if (!tt_local_entry)
  1243. goto out;
  1244. /* Check if the client has been logically deleted (but is kept for
  1245. * consistency purpose) */
  1246. if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
  1247. goto out;
  1248. ret = true;
  1249. out:
  1250. if (tt_local_entry)
  1251. tt_local_entry_free_ref(tt_local_entry);
  1252. return ret;
  1253. }
  1254. void handle_tt_response(struct bat_priv *bat_priv,
  1255. struct tt_query_packet *tt_response)
  1256. {
  1257. struct tt_req_node *node, *safe;
  1258. struct orig_node *orig_node = NULL;
  1259. bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
  1260. "ttvn %d t_size: %d [%c]\n",
  1261. tt_response->src, tt_response->ttvn,
  1262. tt_response->tt_data,
  1263. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1264. orig_node = orig_hash_find(bat_priv, tt_response->src);
  1265. if (!orig_node)
  1266. goto out;
  1267. if (tt_response->flags & TT_FULL_TABLE)
  1268. tt_fill_gtable(bat_priv, tt_response);
  1269. else
  1270. tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
  1271. tt_response->ttvn,
  1272. (struct tt_change *)(tt_response + 1));
  1273. /* Delete the tt_req_node from pending tt_requests list */
  1274. spin_lock_bh(&bat_priv->tt_req_list_lock);
  1275. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  1276. if (!compare_eth(node->addr, tt_response->src))
  1277. continue;
  1278. list_del(&node->list);
  1279. kfree(node);
  1280. }
  1281. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  1282. /* Recalculate the CRC for this orig_node and store it */
  1283. orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
  1284. /* Roaming phase is over: tables are in sync again. I can
  1285. * unset the flag */
  1286. orig_node->tt_poss_change = false;
  1287. out:
  1288. if (orig_node)
  1289. orig_node_free_ref(orig_node);
  1290. }
  1291. int tt_init(struct bat_priv *bat_priv)
  1292. {
  1293. if (!tt_local_init(bat_priv))
  1294. return 0;
  1295. if (!tt_global_init(bat_priv))
  1296. return 0;
  1297. tt_start_timer(bat_priv);
  1298. return 1;
  1299. }
  1300. static void tt_roam_list_free(struct bat_priv *bat_priv)
  1301. {
  1302. struct tt_roam_node *node, *safe;
  1303. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1304. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1305. list_del(&node->list);
  1306. kfree(node);
  1307. }
  1308. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1309. }
  1310. static void tt_roam_purge(struct bat_priv *bat_priv)
  1311. {
  1312. struct tt_roam_node *node, *safe;
  1313. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1314. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1315. if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
  1316. continue;
  1317. list_del(&node->list);
  1318. kfree(node);
  1319. }
  1320. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1321. }
  1322. /* This function checks whether the client already reached the
  1323. * maximum number of possible roaming phases. In this case the ROAMING_ADV
  1324. * will not be sent.
  1325. *
  1326. * returns true if the ROAMING_ADV can be sent, false otherwise */
  1327. static bool tt_check_roam_count(struct bat_priv *bat_priv,
  1328. uint8_t *client)
  1329. {
  1330. struct tt_roam_node *tt_roam_node;
  1331. bool ret = false;
  1332. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1333. /* The new tt_req will be issued only if I'm not waiting for a
  1334. * reply from the same orig_node yet */
  1335. list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
  1336. if (!compare_eth(tt_roam_node->addr, client))
  1337. continue;
  1338. if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
  1339. continue;
  1340. if (!atomic_dec_not_zero(&tt_roam_node->counter))
  1341. /* Sorry, you roamed too many times! */
  1342. goto unlock;
  1343. ret = true;
  1344. break;
  1345. }
  1346. if (!ret) {
  1347. tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
  1348. if (!tt_roam_node)
  1349. goto unlock;
  1350. tt_roam_node->first_time = jiffies;
  1351. atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
  1352. memcpy(tt_roam_node->addr, client, ETH_ALEN);
  1353. list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
  1354. ret = true;
  1355. }
  1356. unlock:
  1357. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1358. return ret;
  1359. }
  1360. void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
  1361. struct orig_node *orig_node)
  1362. {
  1363. struct neigh_node *neigh_node = NULL;
  1364. struct sk_buff *skb = NULL;
  1365. struct roam_adv_packet *roam_adv_packet;
  1366. int ret = 1;
  1367. struct hard_iface *primary_if;
  1368. /* before going on we have to check whether the client has
  1369. * already roamed to us too many times */
  1370. if (!tt_check_roam_count(bat_priv, client))
  1371. goto out;
  1372. skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
  1373. if (!skb)
  1374. goto out;
  1375. skb_reserve(skb, ETH_HLEN);
  1376. roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
  1377. sizeof(struct roam_adv_packet));
  1378. roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
  1379. roam_adv_packet->header.version = COMPAT_VERSION;
  1380. roam_adv_packet->header.ttl = TTL;
  1381. primary_if = primary_if_get_selected(bat_priv);
  1382. if (!primary_if)
  1383. goto out;
  1384. memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1385. hardif_free_ref(primary_if);
  1386. memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
  1387. memcpy(roam_adv_packet->client, client, ETH_ALEN);
  1388. neigh_node = orig_node_get_router(orig_node);
  1389. if (!neigh_node)
  1390. goto out;
  1391. bat_dbg(DBG_TT, bat_priv,
  1392. "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
  1393. orig_node->orig, client, neigh_node->addr);
  1394. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1395. ret = 0;
  1396. out:
  1397. if (neigh_node)
  1398. neigh_node_free_ref(neigh_node);
  1399. if (ret)
  1400. kfree_skb(skb);
  1401. return;
  1402. }
  1403. static void tt_purge(struct work_struct *work)
  1404. {
  1405. struct delayed_work *delayed_work =
  1406. container_of(work, struct delayed_work, work);
  1407. struct bat_priv *bat_priv =
  1408. container_of(delayed_work, struct bat_priv, tt_work);
  1409. tt_local_purge(bat_priv);
  1410. tt_global_roam_purge(bat_priv);
  1411. tt_req_purge(bat_priv);
  1412. tt_roam_purge(bat_priv);
  1413. tt_start_timer(bat_priv);
  1414. }
  1415. void tt_free(struct bat_priv *bat_priv)
  1416. {
  1417. cancel_delayed_work_sync(&bat_priv->tt_work);
  1418. tt_local_table_free(bat_priv);
  1419. tt_global_table_free(bat_priv);
  1420. tt_req_list_free(bat_priv);
  1421. tt_changes_list_free(bat_priv);
  1422. tt_roam_list_free(bat_priv);
  1423. kfree(bat_priv->tt_buff);
  1424. }
  1425. /* This function will enable or disable the specified flags for all the entries
  1426. * in the given hash table and returns the number of modified entries */
  1427. static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
  1428. bool enable)
  1429. {
  1430. uint32_t i;
  1431. uint16_t changed_num = 0;
  1432. struct hlist_head *head;
  1433. struct hlist_node *node;
  1434. struct tt_common_entry *tt_common_entry;
  1435. if (!hash)
  1436. goto out;
  1437. for (i = 0; i < hash->size; i++) {
  1438. head = &hash->table[i];
  1439. rcu_read_lock();
  1440. hlist_for_each_entry_rcu(tt_common_entry, node,
  1441. head, hash_entry) {
  1442. if (enable) {
  1443. if ((tt_common_entry->flags & flags) == flags)
  1444. continue;
  1445. tt_common_entry->flags |= flags;
  1446. } else {
  1447. if (!(tt_common_entry->flags & flags))
  1448. continue;
  1449. tt_common_entry->flags &= ~flags;
  1450. }
  1451. changed_num++;
  1452. }
  1453. rcu_read_unlock();
  1454. }
  1455. out:
  1456. return changed_num;
  1457. }
  1458. /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
  1459. static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
  1460. {
  1461. struct hashtable_t *hash = bat_priv->tt_local_hash;
  1462. struct tt_common_entry *tt_common_entry;
  1463. struct tt_local_entry *tt_local_entry;
  1464. struct hlist_node *node, *node_tmp;
  1465. struct hlist_head *head;
  1466. spinlock_t *list_lock; /* protects write access to the hash lists */
  1467. uint32_t i;
  1468. if (!hash)
  1469. return;
  1470. for (i = 0; i < hash->size; i++) {
  1471. head = &hash->table[i];
  1472. list_lock = &hash->list_locks[i];
  1473. spin_lock_bh(list_lock);
  1474. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  1475. head, hash_entry) {
  1476. if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
  1477. continue;
  1478. bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
  1479. "(%pM): pending\n", tt_common_entry->addr);
  1480. atomic_dec(&bat_priv->num_local_tt);
  1481. hlist_del_rcu(node);
  1482. tt_local_entry = container_of(tt_common_entry,
  1483. struct tt_local_entry,
  1484. common);
  1485. tt_local_entry_free_ref(tt_local_entry);
  1486. }
  1487. spin_unlock_bh(list_lock);
  1488. }
  1489. }
  1490. void tt_commit_changes(struct bat_priv *bat_priv)
  1491. {
  1492. uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
  1493. TT_CLIENT_NEW, false);
  1494. /* all the reset entries have now to be effectively counted as local
  1495. * entries */
  1496. atomic_add(changed_num, &bat_priv->num_local_tt);
  1497. tt_local_purge_pending_clients(bat_priv);
  1498. /* Increment the TTVN only once per OGM interval */
  1499. atomic_inc(&bat_priv->ttvn);
  1500. bat_priv->tt_poss_change = false;
  1501. }
  1502. bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
  1503. {
  1504. struct tt_local_entry *tt_local_entry = NULL;
  1505. struct tt_global_entry *tt_global_entry = NULL;
  1506. bool ret = true;
  1507. if (!atomic_read(&bat_priv->ap_isolation))
  1508. return false;
  1509. tt_local_entry = tt_local_hash_find(bat_priv, dst);
  1510. if (!tt_local_entry)
  1511. goto out;
  1512. tt_global_entry = tt_global_hash_find(bat_priv, src);
  1513. if (!tt_global_entry)
  1514. goto out;
  1515. if (_is_ap_isolated(tt_local_entry, tt_global_entry))
  1516. goto out;
  1517. ret = false;
  1518. out:
  1519. if (tt_global_entry)
  1520. tt_global_entry_free_ref(tt_global_entry);
  1521. if (tt_local_entry)
  1522. tt_local_entry_free_ref(tt_local_entry);
  1523. return ret;
  1524. }
  1525. void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
  1526. const unsigned char *tt_buff, uint8_t tt_num_changes,
  1527. uint8_t ttvn, uint16_t tt_crc)
  1528. {
  1529. uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  1530. bool full_table = true;
  1531. /* orig table not initialised AND first diff is in the OGM OR the ttvn
  1532. * increased by one -> we can apply the attached changes */
  1533. if ((!orig_node->tt_initialised && ttvn == 1) ||
  1534. ttvn - orig_ttvn == 1) {
  1535. /* the OGM could not contain the changes due to their size or
  1536. * because they have already been sent TT_OGM_APPEND_MAX times.
  1537. * In this case send a tt request */
  1538. if (!tt_num_changes) {
  1539. full_table = false;
  1540. goto request_table;
  1541. }
  1542. tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
  1543. (struct tt_change *)tt_buff);
  1544. /* Even if we received the precomputed crc with the OGM, we
  1545. * prefer to recompute it to spot any possible inconsistency
  1546. * in the global table */
  1547. orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
  1548. /* The ttvn alone is not enough to guarantee consistency
  1549. * because a single value could represent different states
  1550. * (due to the wrap around). Thus a node has to check whether
  1551. * the resulting table (after applying the changes) is still
  1552. * consistent or not. E.g. a node could disconnect while its
  1553. * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
  1554. * checking the CRC value is mandatory to detect the
  1555. * inconsistency */
  1556. if (orig_node->tt_crc != tt_crc)
  1557. goto request_table;
  1558. /* Roaming phase is over: tables are in sync again. I can
  1559. * unset the flag */
  1560. orig_node->tt_poss_change = false;
  1561. } else {
  1562. /* if we missed more than one change or our tables are not
  1563. * in sync anymore -> request fresh tt data */
  1564. if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
  1565. orig_node->tt_crc != tt_crc) {
  1566. request_table:
  1567. bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
  1568. "Need to retrieve the correct information "
  1569. "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
  1570. "%u num_changes: %u)\n", orig_node->orig, ttvn,
  1571. orig_ttvn, tt_crc, orig_node->tt_crc,
  1572. tt_num_changes);
  1573. send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
  1574. full_table);
  1575. return;
  1576. }
  1577. }
  1578. }