translation-table.c 13 KB

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