translation-table.c 15 KB

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