translation-table.c 41 KB

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