translation-table.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 "hash.h"
  26. #include "originator.h"
  27. static void tt_local_purge(struct work_struct *work);
  28. static void _tt_global_del_orig(struct bat_priv *bat_priv,
  29. struct tt_global_entry *tt_global_entry,
  30. const char *message);
  31. /* returns 1 if they are the same mac addr */
  32. static int compare_ltt(const struct hlist_node *node, const void *data2)
  33. {
  34. const void *data1 = container_of(node, struct tt_local_entry,
  35. hash_entry);
  36. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  37. }
  38. /* returns 1 if they are the same mac addr */
  39. static int compare_gtt(const struct hlist_node *node, const void *data2)
  40. {
  41. const void *data1 = container_of(node, struct tt_global_entry,
  42. hash_entry);
  43. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  44. }
  45. static void tt_local_start_timer(struct bat_priv *bat_priv)
  46. {
  47. INIT_DELAYED_WORK(&bat_priv->tt_work, tt_local_purge);
  48. queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work, 10 * HZ);
  49. }
  50. static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
  51. const void *data)
  52. {
  53. struct hashtable_t *hash = bat_priv->tt_local_hash;
  54. struct hlist_head *head;
  55. struct hlist_node *node;
  56. struct tt_local_entry *tt_local_entry, *tt_local_entry_tmp = NULL;
  57. int index;
  58. if (!hash)
  59. return NULL;
  60. index = choose_orig(data, hash->size);
  61. head = &hash->table[index];
  62. rcu_read_lock();
  63. hlist_for_each_entry_rcu(tt_local_entry, node, head, hash_entry) {
  64. if (!compare_eth(tt_local_entry, data))
  65. continue;
  66. tt_local_entry_tmp = tt_local_entry;
  67. break;
  68. }
  69. rcu_read_unlock();
  70. return tt_local_entry_tmp;
  71. }
  72. static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
  73. const void *data)
  74. {
  75. struct hashtable_t *hash = bat_priv->tt_global_hash;
  76. struct hlist_head *head;
  77. struct hlist_node *node;
  78. struct tt_global_entry *tt_global_entry;
  79. struct tt_global_entry *tt_global_entry_tmp = NULL;
  80. int index;
  81. if (!hash)
  82. return NULL;
  83. index = choose_orig(data, hash->size);
  84. head = &hash->table[index];
  85. rcu_read_lock();
  86. hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
  87. if (!compare_eth(tt_global_entry, data))
  88. continue;
  89. tt_global_entry_tmp = tt_global_entry;
  90. break;
  91. }
  92. rcu_read_unlock();
  93. return tt_global_entry_tmp;
  94. }
  95. int tt_local_init(struct bat_priv *bat_priv)
  96. {
  97. if (bat_priv->tt_local_hash)
  98. return 1;
  99. bat_priv->tt_local_hash = hash_new(1024);
  100. if (!bat_priv->tt_local_hash)
  101. return 0;
  102. atomic_set(&bat_priv->tt_local_changed, 0);
  103. tt_local_start_timer(bat_priv);
  104. return 1;
  105. }
  106. void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
  107. {
  108. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  109. struct tt_local_entry *tt_local_entry;
  110. struct tt_global_entry *tt_global_entry;
  111. int required_bytes;
  112. spin_lock_bh(&bat_priv->tt_lhash_lock);
  113. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  114. spin_unlock_bh(&bat_priv->tt_lhash_lock);
  115. if (tt_local_entry) {
  116. tt_local_entry->last_seen = jiffies;
  117. return;
  118. }
  119. /* only announce as many hosts as possible in the batman-packet and
  120. space in batman_packet->num_tt That also should give a limit to
  121. MAC-flooding. */
  122. required_bytes = (bat_priv->num_local_tt + 1) * ETH_ALEN;
  123. required_bytes += BAT_PACKET_LEN;
  124. if ((required_bytes > ETH_DATA_LEN) ||
  125. (atomic_read(&bat_priv->aggregated_ogms) &&
  126. required_bytes > MAX_AGGREGATION_BYTES) ||
  127. (bat_priv->num_local_tt + 1 > 255)) {
  128. bat_dbg(DBG_ROUTES, bat_priv,
  129. "Can't add new local tt entry (%pM): "
  130. "number of local tt entries exceeds packet size\n",
  131. addr);
  132. return;
  133. }
  134. bat_dbg(DBG_ROUTES, bat_priv,
  135. "Creating new local tt entry: %pM\n", addr);
  136. tt_local_entry = kmalloc(sizeof(struct tt_local_entry), GFP_ATOMIC);
  137. if (!tt_local_entry)
  138. return;
  139. memcpy(tt_local_entry->addr, addr, ETH_ALEN);
  140. tt_local_entry->last_seen = jiffies;
  141. /* the batman interface mac address should never be purged */
  142. if (compare_eth(addr, soft_iface->dev_addr))
  143. tt_local_entry->never_purge = 1;
  144. else
  145. tt_local_entry->never_purge = 0;
  146. spin_lock_bh(&bat_priv->tt_lhash_lock);
  147. hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
  148. tt_local_entry, &tt_local_entry->hash_entry);
  149. bat_priv->num_local_tt++;
  150. atomic_set(&bat_priv->tt_local_changed, 1);
  151. spin_unlock_bh(&bat_priv->tt_lhash_lock);
  152. /* remove address from global hash if present */
  153. spin_lock_bh(&bat_priv->tt_ghash_lock);
  154. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  155. if (tt_global_entry)
  156. _tt_global_del_orig(bat_priv, tt_global_entry,
  157. "local tt received");
  158. spin_unlock_bh(&bat_priv->tt_ghash_lock);
  159. }
  160. int tt_local_fill_buffer(struct bat_priv *bat_priv,
  161. unsigned char *buff, int buff_len)
  162. {
  163. struct hashtable_t *hash = bat_priv->tt_local_hash;
  164. struct tt_local_entry *tt_local_entry;
  165. struct hlist_node *node;
  166. struct hlist_head *head;
  167. int i, count = 0;
  168. spin_lock_bh(&bat_priv->tt_lhash_lock);
  169. for (i = 0; i < hash->size; i++) {
  170. head = &hash->table[i];
  171. rcu_read_lock();
  172. hlist_for_each_entry_rcu(tt_local_entry, node,
  173. head, hash_entry) {
  174. if (buff_len < (count + 1) * ETH_ALEN)
  175. break;
  176. memcpy(buff + (count * ETH_ALEN), tt_local_entry->addr,
  177. ETH_ALEN);
  178. count++;
  179. }
  180. rcu_read_unlock();
  181. }
  182. /* if we did not get all new local tts see you next time ;-) */
  183. if (count == bat_priv->num_local_tt)
  184. atomic_set(&bat_priv->tt_local_changed, 0);
  185. spin_unlock_bh(&bat_priv->tt_lhash_lock);
  186. return count;
  187. }
  188. int tt_local_seq_print_text(struct seq_file *seq, void *offset)
  189. {
  190. struct net_device *net_dev = (struct net_device *)seq->private;
  191. struct bat_priv *bat_priv = netdev_priv(net_dev);
  192. struct hashtable_t *hash = bat_priv->tt_local_hash;
  193. struct tt_local_entry *tt_local_entry;
  194. struct hard_iface *primary_if;
  195. struct hlist_node *node;
  196. struct hlist_head *head;
  197. size_t buf_size, pos;
  198. char *buff;
  199. int i, ret = 0;
  200. primary_if = primary_if_get_selected(bat_priv);
  201. if (!primary_if) {
  202. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  203. "please specify interfaces to enable it\n",
  204. net_dev->name);
  205. goto out;
  206. }
  207. if (primary_if->if_status != IF_ACTIVE) {
  208. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  209. "primary interface not active\n",
  210. net_dev->name);
  211. goto out;
  212. }
  213. seq_printf(seq, "Locally retrieved addresses (from %s) "
  214. "announced via TT:\n",
  215. net_dev->name);
  216. spin_lock_bh(&bat_priv->tt_lhash_lock);
  217. buf_size = 1;
  218. /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
  219. for (i = 0; i < hash->size; i++) {
  220. head = &hash->table[i];
  221. rcu_read_lock();
  222. __hlist_for_each_rcu(node, head)
  223. buf_size += 21;
  224. rcu_read_unlock();
  225. }
  226. buff = kmalloc(buf_size, GFP_ATOMIC);
  227. if (!buff) {
  228. spin_unlock_bh(&bat_priv->tt_lhash_lock);
  229. ret = -ENOMEM;
  230. goto out;
  231. }
  232. buff[0] = '\0';
  233. pos = 0;
  234. for (i = 0; i < hash->size; i++) {
  235. head = &hash->table[i];
  236. rcu_read_lock();
  237. hlist_for_each_entry_rcu(tt_local_entry, node,
  238. head, hash_entry) {
  239. pos += snprintf(buff + pos, 22, " * %pM\n",
  240. tt_local_entry->addr);
  241. }
  242. rcu_read_unlock();
  243. }
  244. spin_unlock_bh(&bat_priv->tt_lhash_lock);
  245. seq_printf(seq, "%s", buff);
  246. kfree(buff);
  247. out:
  248. if (primary_if)
  249. hardif_free_ref(primary_if);
  250. return ret;
  251. }
  252. static void _tt_local_del(struct hlist_node *node, void *arg)
  253. {
  254. struct bat_priv *bat_priv = arg;
  255. void *data = container_of(node, struct tt_local_entry, hash_entry);
  256. kfree(data);
  257. bat_priv->num_local_tt--;
  258. atomic_set(&bat_priv->tt_local_changed, 1);
  259. }
  260. static void tt_local_del(struct bat_priv *bat_priv,
  261. struct tt_local_entry *tt_local_entry,
  262. const char *message)
  263. {
  264. bat_dbg(DBG_ROUTES, bat_priv, "Deleting local tt entry (%pM): %s\n",
  265. tt_local_entry->addr, message);
  266. hash_remove(bat_priv->tt_local_hash, compare_ltt, choose_orig,
  267. tt_local_entry->addr);
  268. _tt_local_del(&tt_local_entry->hash_entry, bat_priv);
  269. }
  270. void tt_local_remove(struct bat_priv *bat_priv,
  271. const uint8_t *addr, const char *message)
  272. {
  273. struct tt_local_entry *tt_local_entry;
  274. spin_lock_bh(&bat_priv->tt_lhash_lock);
  275. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  276. if (tt_local_entry)
  277. tt_local_del(bat_priv, tt_local_entry, message);
  278. spin_unlock_bh(&bat_priv->tt_lhash_lock);
  279. }
  280. static void tt_local_purge(struct work_struct *work)
  281. {
  282. struct delayed_work *delayed_work =
  283. container_of(work, struct delayed_work, work);
  284. struct bat_priv *bat_priv =
  285. container_of(delayed_work, struct bat_priv, tt_work);
  286. struct hashtable_t *hash = bat_priv->tt_local_hash;
  287. struct tt_local_entry *tt_local_entry;
  288. struct hlist_node *node, *node_tmp;
  289. struct hlist_head *head;
  290. unsigned long timeout;
  291. int i;
  292. spin_lock_bh(&bat_priv->tt_lhash_lock);
  293. for (i = 0; i < hash->size; i++) {
  294. head = &hash->table[i];
  295. hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
  296. head, hash_entry) {
  297. if (tt_local_entry->never_purge)
  298. continue;
  299. timeout = tt_local_entry->last_seen;
  300. timeout += TT_LOCAL_TIMEOUT * HZ;
  301. if (time_before(jiffies, timeout))
  302. continue;
  303. tt_local_del(bat_priv, tt_local_entry,
  304. "address timed out");
  305. }
  306. }
  307. spin_unlock_bh(&bat_priv->tt_lhash_lock);
  308. tt_local_start_timer(bat_priv);
  309. }
  310. void tt_local_free(struct bat_priv *bat_priv)
  311. {
  312. if (!bat_priv->tt_local_hash)
  313. return;
  314. cancel_delayed_work_sync(&bat_priv->tt_work);
  315. hash_delete(bat_priv->tt_local_hash, _tt_local_del, bat_priv);
  316. bat_priv->tt_local_hash = NULL;
  317. }
  318. int tt_global_init(struct bat_priv *bat_priv)
  319. {
  320. if (bat_priv->tt_global_hash)
  321. return 1;
  322. bat_priv->tt_global_hash = hash_new(1024);
  323. if (!bat_priv->tt_global_hash)
  324. return 0;
  325. return 1;
  326. }
  327. void tt_global_add_orig(struct bat_priv *bat_priv,
  328. struct orig_node *orig_node,
  329. const unsigned char *tt_buff, int tt_buff_len)
  330. {
  331. struct tt_global_entry *tt_global_entry;
  332. struct tt_local_entry *tt_local_entry;
  333. int tt_buff_count = 0;
  334. const unsigned char *tt_ptr;
  335. while ((tt_buff_count + 1) * ETH_ALEN <= tt_buff_len) {
  336. spin_lock_bh(&bat_priv->tt_ghash_lock);
  337. tt_ptr = tt_buff + (tt_buff_count * ETH_ALEN);
  338. tt_global_entry = tt_global_hash_find(bat_priv, tt_ptr);
  339. if (!tt_global_entry) {
  340. spin_unlock_bh(&bat_priv->tt_ghash_lock);
  341. tt_global_entry =
  342. kmalloc(sizeof(struct tt_global_entry),
  343. GFP_ATOMIC);
  344. if (!tt_global_entry)
  345. break;
  346. memcpy(tt_global_entry->addr, tt_ptr, ETH_ALEN);
  347. bat_dbg(DBG_ROUTES, bat_priv,
  348. "Creating new global tt entry: "
  349. "%pM (via %pM)\n",
  350. tt_global_entry->addr, orig_node->orig);
  351. spin_lock_bh(&bat_priv->tt_ghash_lock);
  352. hash_add(bat_priv->tt_global_hash, compare_gtt,
  353. choose_orig, tt_global_entry,
  354. &tt_global_entry->hash_entry);
  355. }
  356. tt_global_entry->orig_node = orig_node;
  357. spin_unlock_bh(&bat_priv->tt_ghash_lock);
  358. /* remove address from local hash if present */
  359. spin_lock_bh(&bat_priv->tt_lhash_lock);
  360. tt_ptr = tt_buff + (tt_buff_count * ETH_ALEN);
  361. tt_local_entry = tt_local_hash_find(bat_priv, tt_ptr);
  362. if (tt_local_entry)
  363. tt_local_del(bat_priv, tt_local_entry,
  364. "global tt received");
  365. spin_unlock_bh(&bat_priv->tt_lhash_lock);
  366. tt_buff_count++;
  367. }
  368. /* initialize, and overwrite if malloc succeeds */
  369. orig_node->tt_buff = NULL;
  370. orig_node->tt_buff_len = 0;
  371. if (tt_buff_len > 0) {
  372. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  373. if (orig_node->tt_buff) {
  374. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  375. orig_node->tt_buff_len = tt_buff_len;
  376. }
  377. }
  378. }
  379. int tt_global_seq_print_text(struct seq_file *seq, void *offset)
  380. {
  381. struct net_device *net_dev = (struct net_device *)seq->private;
  382. struct bat_priv *bat_priv = netdev_priv(net_dev);
  383. struct hashtable_t *hash = bat_priv->tt_global_hash;
  384. struct tt_global_entry *tt_global_entry;
  385. struct hard_iface *primary_if;
  386. struct hlist_node *node;
  387. struct hlist_head *head;
  388. size_t buf_size, pos;
  389. char *buff;
  390. int i, ret = 0;
  391. primary_if = primary_if_get_selected(bat_priv);
  392. if (!primary_if) {
  393. ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
  394. "specify interfaces to enable it\n",
  395. net_dev->name);
  396. goto out;
  397. }
  398. if (primary_if->if_status != IF_ACTIVE) {
  399. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  400. "primary interface not active\n",
  401. net_dev->name);
  402. goto out;
  403. }
  404. seq_printf(seq,
  405. "Globally announced TT entries received via the mesh %s\n",
  406. net_dev->name);
  407. spin_lock_bh(&bat_priv->tt_ghash_lock);
  408. buf_size = 1;
  409. /* Estimate length for: " * xx:xx:xx:xx:xx:xx via xx:xx:xx:xx:xx:xx\n"*/
  410. for (i = 0; i < hash->size; i++) {
  411. head = &hash->table[i];
  412. rcu_read_lock();
  413. __hlist_for_each_rcu(node, head)
  414. buf_size += 43;
  415. rcu_read_unlock();
  416. }
  417. buff = kmalloc(buf_size, GFP_ATOMIC);
  418. if (!buff) {
  419. spin_unlock_bh(&bat_priv->tt_ghash_lock);
  420. ret = -ENOMEM;
  421. goto out;
  422. }
  423. buff[0] = '\0';
  424. pos = 0;
  425. for (i = 0; i < hash->size; i++) {
  426. head = &hash->table[i];
  427. rcu_read_lock();
  428. hlist_for_each_entry_rcu(tt_global_entry, node,
  429. head, hash_entry) {
  430. pos += snprintf(buff + pos, 44,
  431. " * %pM via %pM\n",
  432. tt_global_entry->addr,
  433. tt_global_entry->orig_node->orig);
  434. }
  435. rcu_read_unlock();
  436. }
  437. spin_unlock_bh(&bat_priv->tt_ghash_lock);
  438. seq_printf(seq, "%s", buff);
  439. kfree(buff);
  440. out:
  441. if (primary_if)
  442. hardif_free_ref(primary_if);
  443. return ret;
  444. }
  445. static void _tt_global_del_orig(struct bat_priv *bat_priv,
  446. struct tt_global_entry *tt_global_entry,
  447. const char *message)
  448. {
  449. bat_dbg(DBG_ROUTES, bat_priv,
  450. "Deleting global tt entry %pM (via %pM): %s\n",
  451. tt_global_entry->addr, tt_global_entry->orig_node->orig,
  452. message);
  453. hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
  454. tt_global_entry->addr);
  455. kfree(tt_global_entry);
  456. }
  457. void tt_global_del_orig(struct bat_priv *bat_priv,
  458. struct orig_node *orig_node, const char *message)
  459. {
  460. struct tt_global_entry *tt_global_entry;
  461. int tt_buff_count = 0;
  462. unsigned char *tt_ptr;
  463. if (orig_node->tt_buff_len == 0)
  464. return;
  465. spin_lock_bh(&bat_priv->tt_ghash_lock);
  466. while ((tt_buff_count + 1) * ETH_ALEN <= orig_node->tt_buff_len) {
  467. tt_ptr = orig_node->tt_buff + (tt_buff_count * ETH_ALEN);
  468. tt_global_entry = tt_global_hash_find(bat_priv, tt_ptr);
  469. if ((tt_global_entry) &&
  470. (tt_global_entry->orig_node == orig_node))
  471. _tt_global_del_orig(bat_priv, tt_global_entry,
  472. message);
  473. tt_buff_count++;
  474. }
  475. spin_unlock_bh(&bat_priv->tt_ghash_lock);
  476. orig_node->tt_buff_len = 0;
  477. kfree(orig_node->tt_buff);
  478. orig_node->tt_buff = NULL;
  479. }
  480. static void tt_global_del(struct hlist_node *node, void *arg)
  481. {
  482. void *data = container_of(node, struct tt_global_entry, hash_entry);
  483. kfree(data);
  484. }
  485. void tt_global_free(struct bat_priv *bat_priv)
  486. {
  487. if (!bat_priv->tt_global_hash)
  488. return;
  489. hash_delete(bat_priv->tt_global_hash, tt_global_del, NULL);
  490. bat_priv->tt_global_hash = NULL;
  491. }
  492. struct orig_node *transtable_search(struct bat_priv *bat_priv,
  493. const uint8_t *addr)
  494. {
  495. struct tt_global_entry *tt_global_entry;
  496. struct orig_node *orig_node = NULL;
  497. spin_lock_bh(&bat_priv->tt_ghash_lock);
  498. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  499. if (!tt_global_entry)
  500. goto out;
  501. if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
  502. goto out;
  503. orig_node = tt_global_entry->orig_node;
  504. out:
  505. spin_unlock_bh(&bat_priv->tt_ghash_lock);
  506. return orig_node;
  507. }