translation-table.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 hna_local_purge(struct work_struct *work);
  28. static void _hna_global_del_orig(struct bat_priv *bat_priv,
  29. struct hna_global_entry *hna_global_entry,
  30. char *message);
  31. /* returns 1 if they are the same mac addr */
  32. static int compare_lhna(struct hlist_node *node, void *data2)
  33. {
  34. void *data1 = container_of(node, struct hna_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_ghna(struct hlist_node *node, void *data2)
  39. {
  40. void *data1 = container_of(node, struct hna_global_entry, hash_entry);
  41. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  42. }
  43. static void hna_local_start_timer(struct bat_priv *bat_priv)
  44. {
  45. INIT_DELAYED_WORK(&bat_priv->hna_work, hna_local_purge);
  46. queue_delayed_work(bat_event_workqueue, &bat_priv->hna_work, 10 * HZ);
  47. }
  48. static struct hna_local_entry *hna_local_hash_find(struct bat_priv *bat_priv,
  49. void *data)
  50. {
  51. struct hashtable_t *hash = bat_priv->hna_local_hash;
  52. struct hlist_head *head;
  53. struct hlist_node *node;
  54. struct hna_local_entry *hna_local_entry, *hna_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(hna_local_entry, node, head, hash_entry) {
  62. if (!compare_eth(hna_local_entry, data))
  63. continue;
  64. hna_local_entry_tmp = hna_local_entry;
  65. break;
  66. }
  67. rcu_read_unlock();
  68. return hna_local_entry_tmp;
  69. }
  70. static struct hna_global_entry *hna_global_hash_find(struct bat_priv *bat_priv,
  71. void *data)
  72. {
  73. struct hashtable_t *hash = bat_priv->hna_global_hash;
  74. struct hlist_head *head;
  75. struct hlist_node *node;
  76. struct hna_global_entry *hna_global_entry;
  77. struct hna_global_entry *hna_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(hna_global_entry, node, head, hash_entry) {
  85. if (!compare_eth(hna_global_entry, data))
  86. continue;
  87. hna_global_entry_tmp = hna_global_entry;
  88. break;
  89. }
  90. rcu_read_unlock();
  91. return hna_global_entry_tmp;
  92. }
  93. int hna_local_init(struct bat_priv *bat_priv)
  94. {
  95. if (bat_priv->hna_local_hash)
  96. return 1;
  97. bat_priv->hna_local_hash = hash_new(1024);
  98. if (!bat_priv->hna_local_hash)
  99. return 0;
  100. atomic_set(&bat_priv->hna_local_changed, 0);
  101. hna_local_start_timer(bat_priv);
  102. return 1;
  103. }
  104. void hna_local_add(struct net_device *soft_iface, uint8_t *addr)
  105. {
  106. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  107. struct hna_local_entry *hna_local_entry;
  108. struct hna_global_entry *hna_global_entry;
  109. int required_bytes;
  110. spin_lock_bh(&bat_priv->hna_lhash_lock);
  111. hna_local_entry = hna_local_hash_find(bat_priv, addr);
  112. spin_unlock_bh(&bat_priv->hna_lhash_lock);
  113. if (hna_local_entry) {
  114. hna_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_hna That also should give a limit to
  119. MAC-flooding. */
  120. required_bytes = (bat_priv->num_local_hna + 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_hna + 1 > 255)) {
  126. bat_dbg(DBG_ROUTES, bat_priv,
  127. "Can't add new local hna entry (%pM): "
  128. "number of local hna entries exceeds packet size\n",
  129. addr);
  130. return;
  131. }
  132. bat_dbg(DBG_ROUTES, bat_priv,
  133. "Creating new local hna entry: %pM\n", addr);
  134. hna_local_entry = kmalloc(sizeof(struct hna_local_entry), GFP_ATOMIC);
  135. if (!hna_local_entry)
  136. return;
  137. memcpy(hna_local_entry->addr, addr, ETH_ALEN);
  138. hna_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. hna_local_entry->never_purge = 1;
  142. else
  143. hna_local_entry->never_purge = 0;
  144. spin_lock_bh(&bat_priv->hna_lhash_lock);
  145. hash_add(bat_priv->hna_local_hash, compare_lhna, choose_orig,
  146. hna_local_entry, &hna_local_entry->hash_entry);
  147. bat_priv->num_local_hna++;
  148. atomic_set(&bat_priv->hna_local_changed, 1);
  149. spin_unlock_bh(&bat_priv->hna_lhash_lock);
  150. /* remove address from global hash if present */
  151. spin_lock_bh(&bat_priv->hna_ghash_lock);
  152. hna_global_entry = hna_global_hash_find(bat_priv, addr);
  153. if (hna_global_entry)
  154. _hna_global_del_orig(bat_priv, hna_global_entry,
  155. "local hna received");
  156. spin_unlock_bh(&bat_priv->hna_ghash_lock);
  157. }
  158. int hna_local_fill_buffer(struct bat_priv *bat_priv,
  159. unsigned char *buff, int buff_len)
  160. {
  161. struct hashtable_t *hash = bat_priv->hna_local_hash;
  162. struct hna_local_entry *hna_local_entry;
  163. struct hlist_node *node;
  164. struct hlist_head *head;
  165. int i, count = 0;
  166. spin_lock_bh(&bat_priv->hna_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(hna_local_entry, node,
  171. head, hash_entry) {
  172. if (buff_len < (count + 1) * ETH_ALEN)
  173. break;
  174. memcpy(buff + (count * ETH_ALEN), hna_local_entry->addr,
  175. ETH_ALEN);
  176. count++;
  177. }
  178. rcu_read_unlock();
  179. }
  180. /* if we did not get all new local hnas see you next time ;-) */
  181. if (count == bat_priv->num_local_hna)
  182. atomic_set(&bat_priv->hna_local_changed, 0);
  183. spin_unlock_bh(&bat_priv->hna_lhash_lock);
  184. return count;
  185. }
  186. int hna_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->hna_local_hash;
  191. struct hna_local_entry *hna_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 HNA:\n",
  213. net_dev->name);
  214. spin_lock_bh(&bat_priv->hna_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->hna_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(hna_local_entry, node,
  236. head, hash_entry) {
  237. pos += snprintf(buff + pos, 22, " * %pM\n",
  238. hna_local_entry->addr);
  239. }
  240. rcu_read_unlock();
  241. }
  242. spin_unlock_bh(&bat_priv->hna_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 _hna_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 hna_local_entry, hash_entry);
  254. kfree(data);
  255. bat_priv->num_local_hna--;
  256. atomic_set(&bat_priv->hna_local_changed, 1);
  257. }
  258. static void hna_local_del(struct bat_priv *bat_priv,
  259. struct hna_local_entry *hna_local_entry,
  260. char *message)
  261. {
  262. bat_dbg(DBG_ROUTES, bat_priv, "Deleting local hna entry (%pM): %s\n",
  263. hna_local_entry->addr, message);
  264. hash_remove(bat_priv->hna_local_hash, compare_lhna, choose_orig,
  265. hna_local_entry->addr);
  266. _hna_local_del(&hna_local_entry->hash_entry, bat_priv);
  267. }
  268. void hna_local_remove(struct bat_priv *bat_priv,
  269. uint8_t *addr, char *message)
  270. {
  271. struct hna_local_entry *hna_local_entry;
  272. spin_lock_bh(&bat_priv->hna_lhash_lock);
  273. hna_local_entry = hna_local_hash_find(bat_priv, addr);
  274. if (hna_local_entry)
  275. hna_local_del(bat_priv, hna_local_entry, message);
  276. spin_unlock_bh(&bat_priv->hna_lhash_lock);
  277. }
  278. static void hna_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, hna_work);
  284. struct hashtable_t *hash = bat_priv->hna_local_hash;
  285. struct hna_local_entry *hna_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->hna_lhash_lock);
  291. for (i = 0; i < hash->size; i++) {
  292. head = &hash->table[i];
  293. hlist_for_each_entry_safe(hna_local_entry, node, node_tmp,
  294. head, hash_entry) {
  295. if (hna_local_entry->never_purge)
  296. continue;
  297. timeout = hna_local_entry->last_seen;
  298. timeout += LOCAL_HNA_TIMEOUT * HZ;
  299. if (time_before(jiffies, timeout))
  300. continue;
  301. hna_local_del(bat_priv, hna_local_entry,
  302. "address timed out");
  303. }
  304. }
  305. spin_unlock_bh(&bat_priv->hna_lhash_lock);
  306. hna_local_start_timer(bat_priv);
  307. }
  308. void hna_local_free(struct bat_priv *bat_priv)
  309. {
  310. if (!bat_priv->hna_local_hash)
  311. return;
  312. cancel_delayed_work_sync(&bat_priv->hna_work);
  313. hash_delete(bat_priv->hna_local_hash, _hna_local_del, bat_priv);
  314. bat_priv->hna_local_hash = NULL;
  315. }
  316. int hna_global_init(struct bat_priv *bat_priv)
  317. {
  318. if (bat_priv->hna_global_hash)
  319. return 1;
  320. bat_priv->hna_global_hash = hash_new(1024);
  321. if (!bat_priv->hna_global_hash)
  322. return 0;
  323. return 1;
  324. }
  325. void hna_global_add_orig(struct bat_priv *bat_priv,
  326. struct orig_node *orig_node,
  327. unsigned char *hna_buff, int hna_buff_len)
  328. {
  329. struct hna_global_entry *hna_global_entry;
  330. struct hna_local_entry *hna_local_entry;
  331. int hna_buff_count = 0;
  332. unsigned char *hna_ptr;
  333. while ((hna_buff_count + 1) * ETH_ALEN <= hna_buff_len) {
  334. spin_lock_bh(&bat_priv->hna_ghash_lock);
  335. hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
  336. hna_global_entry = hna_global_hash_find(bat_priv, hna_ptr);
  337. if (!hna_global_entry) {
  338. spin_unlock_bh(&bat_priv->hna_ghash_lock);
  339. hna_global_entry =
  340. kmalloc(sizeof(struct hna_global_entry),
  341. GFP_ATOMIC);
  342. if (!hna_global_entry)
  343. break;
  344. memcpy(hna_global_entry->addr, hna_ptr, ETH_ALEN);
  345. bat_dbg(DBG_ROUTES, bat_priv,
  346. "Creating new global hna entry: "
  347. "%pM (via %pM)\n",
  348. hna_global_entry->addr, orig_node->orig);
  349. spin_lock_bh(&bat_priv->hna_ghash_lock);
  350. hash_add(bat_priv->hna_global_hash, compare_ghna,
  351. choose_orig, hna_global_entry,
  352. &hna_global_entry->hash_entry);
  353. }
  354. hna_global_entry->orig_node = orig_node;
  355. spin_unlock_bh(&bat_priv->hna_ghash_lock);
  356. /* remove address from local hash if present */
  357. spin_lock_bh(&bat_priv->hna_lhash_lock);
  358. hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
  359. hna_local_entry = hna_local_hash_find(bat_priv, hna_ptr);
  360. if (hna_local_entry)
  361. hna_local_del(bat_priv, hna_local_entry,
  362. "global hna received");
  363. spin_unlock_bh(&bat_priv->hna_lhash_lock);
  364. hna_buff_count++;
  365. }
  366. /* initialize, and overwrite if malloc succeeds */
  367. orig_node->hna_buff = NULL;
  368. orig_node->hna_buff_len = 0;
  369. if (hna_buff_len > 0) {
  370. orig_node->hna_buff = kmalloc(hna_buff_len, GFP_ATOMIC);
  371. if (orig_node->hna_buff) {
  372. memcpy(orig_node->hna_buff, hna_buff, hna_buff_len);
  373. orig_node->hna_buff_len = hna_buff_len;
  374. }
  375. }
  376. }
  377. int hna_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->hna_global_hash;
  382. struct hna_global_entry *hna_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, "Globally announced HNAs received via the mesh %s\n",
  403. net_dev->name);
  404. spin_lock_bh(&bat_priv->hna_ghash_lock);
  405. buf_size = 1;
  406. /* Estimate length for: " * xx:xx:xx:xx:xx:xx via xx:xx:xx:xx:xx:xx\n"*/
  407. for (i = 0; i < hash->size; i++) {
  408. head = &hash->table[i];
  409. rcu_read_lock();
  410. __hlist_for_each_rcu(node, head)
  411. buf_size += 43;
  412. rcu_read_unlock();
  413. }
  414. buff = kmalloc(buf_size, GFP_ATOMIC);
  415. if (!buff) {
  416. spin_unlock_bh(&bat_priv->hna_ghash_lock);
  417. ret = -ENOMEM;
  418. goto out;
  419. }
  420. buff[0] = '\0';
  421. pos = 0;
  422. for (i = 0; i < hash->size; i++) {
  423. head = &hash->table[i];
  424. rcu_read_lock();
  425. hlist_for_each_entry_rcu(hna_global_entry, node,
  426. head, hash_entry) {
  427. pos += snprintf(buff + pos, 44,
  428. " * %pM via %pM\n",
  429. hna_global_entry->addr,
  430. hna_global_entry->orig_node->orig);
  431. }
  432. rcu_read_unlock();
  433. }
  434. spin_unlock_bh(&bat_priv->hna_ghash_lock);
  435. seq_printf(seq, "%s", buff);
  436. kfree(buff);
  437. out:
  438. if (primary_if)
  439. hardif_free_ref(primary_if);
  440. return ret;
  441. }
  442. static void _hna_global_del_orig(struct bat_priv *bat_priv,
  443. struct hna_global_entry *hna_global_entry,
  444. char *message)
  445. {
  446. bat_dbg(DBG_ROUTES, bat_priv,
  447. "Deleting global hna entry %pM (via %pM): %s\n",
  448. hna_global_entry->addr, hna_global_entry->orig_node->orig,
  449. message);
  450. hash_remove(bat_priv->hna_global_hash, compare_ghna, choose_orig,
  451. hna_global_entry->addr);
  452. kfree(hna_global_entry);
  453. }
  454. void hna_global_del_orig(struct bat_priv *bat_priv,
  455. struct orig_node *orig_node, char *message)
  456. {
  457. struct hna_global_entry *hna_global_entry;
  458. int hna_buff_count = 0;
  459. unsigned char *hna_ptr;
  460. if (orig_node->hna_buff_len == 0)
  461. return;
  462. spin_lock_bh(&bat_priv->hna_ghash_lock);
  463. while ((hna_buff_count + 1) * ETH_ALEN <= orig_node->hna_buff_len) {
  464. hna_ptr = orig_node->hna_buff + (hna_buff_count * ETH_ALEN);
  465. hna_global_entry = hna_global_hash_find(bat_priv, hna_ptr);
  466. if ((hna_global_entry) &&
  467. (hna_global_entry->orig_node == orig_node))
  468. _hna_global_del_orig(bat_priv, hna_global_entry,
  469. message);
  470. hna_buff_count++;
  471. }
  472. spin_unlock_bh(&bat_priv->hna_ghash_lock);
  473. orig_node->hna_buff_len = 0;
  474. kfree(orig_node->hna_buff);
  475. orig_node->hna_buff = NULL;
  476. }
  477. static void hna_global_del(struct hlist_node *node, void *arg)
  478. {
  479. void *data = container_of(node, struct hna_global_entry, hash_entry);
  480. kfree(data);
  481. }
  482. void hna_global_free(struct bat_priv *bat_priv)
  483. {
  484. if (!bat_priv->hna_global_hash)
  485. return;
  486. hash_delete(bat_priv->hna_global_hash, hna_global_del, NULL);
  487. bat_priv->hna_global_hash = NULL;
  488. }
  489. struct orig_node *transtable_search(struct bat_priv *bat_priv, uint8_t *addr)
  490. {
  491. struct hna_global_entry *hna_global_entry;
  492. struct orig_node *orig_node = NULL;
  493. spin_lock_bh(&bat_priv->hna_ghash_lock);
  494. hna_global_entry = hna_global_hash_find(bat_priv, addr);
  495. if (!hna_global_entry)
  496. goto out;
  497. if (!atomic_inc_not_zero(&hna_global_entry->orig_node->refcount))
  498. goto out;
  499. orig_node = hna_global_entry->orig_node;
  500. out:
  501. spin_unlock_bh(&bat_priv->hna_ghash_lock);
  502. return orig_node;
  503. }