translation-table.c 50 KB

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