translation-table.c 50 KB

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