translation-table.c 13 KB

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