translation-table.c 42 KB

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