translation-table.c 51 KB

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