translation-table.c 16 KB

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