2 * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
23 #include "translation-table.h"
24 #include "soft-interface.h"
29 struct hashtable_t *hna_local_hash;
30 static struct hashtable_t *hna_global_hash;
31 atomic_t hna_local_changed;
33 DEFINE_SPINLOCK(hna_local_hash_lock);
34 static DEFINE_SPINLOCK(hna_global_hash_lock);
36 static DECLARE_DELAYED_WORK(hna_local_purge_wq, hna_local_purge);
38 static void hna_local_start_timer(void)
40 queue_delayed_work(bat_event_workqueue, &hna_local_purge_wq, 10 * HZ);
43 int hna_local_init(void)
48 hna_local_hash = hash_new(128, compare_orig, choose_orig);
53 atomic_set(&hna_local_changed, 0);
54 hna_local_start_timer();
59 void hna_local_add(uint8_t *addr)
61 struct hna_local_entry *hna_local_entry;
62 struct hna_global_entry *hna_global_entry;
63 struct hashtable_t *swaphash;
64 char hna_str[ETH_STR_LEN];
67 spin_lock_irqsave(&hna_local_hash_lock, flags);
69 ((struct hna_local_entry *)hash_find(hna_local_hash, addr));
70 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
72 if (hna_local_entry != NULL) {
73 hna_local_entry->last_seen = jiffies;
77 addr_to_string(hna_str, addr);
79 /* only announce as many hosts as possible in the batman-packet and
80 space in batman_packet->num_hna That also should give a limit to
82 if ((num_hna + 1 > (ETH_DATA_LEN - BAT_PACKET_LEN) / ETH_ALEN) ||
83 (num_hna + 1 > 255)) {
84 bat_dbg(DBG_ROUTES, "Can't add new local hna entry (%s): number of local hna entries exceeds packet size \n", hna_str);
88 bat_dbg(DBG_ROUTES, "Creating new local hna entry: %s \n",
91 hna_local_entry = kmalloc(sizeof(struct hna_local_entry), GFP_ATOMIC);
95 memcpy(hna_local_entry->addr, addr, ETH_ALEN);
96 hna_local_entry->last_seen = jiffies;
98 /* the batman interface mac address should never be purged */
99 if (compare_orig(addr, soft_device->dev_addr))
100 hna_local_entry->never_purge = 1;
102 hna_local_entry->never_purge = 0;
104 spin_lock_irqsave(&hna_local_hash_lock, flags);
106 hash_add(hna_local_hash, hna_local_entry);
108 atomic_set(&hna_local_changed, 1);
110 if (hna_local_hash->elements * 4 > hna_local_hash->size) {
111 swaphash = hash_resize(hna_local_hash,
112 hna_local_hash->size * 2);
114 if (swaphash == NULL)
115 printk(KERN_ERR "batman-adv:Couldn't resize local hna hash table \n");
117 hna_local_hash = swaphash;
120 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
122 /* remove address from global hash if present */
123 spin_lock_irqsave(&hna_global_hash_lock, flags);
126 ((struct hna_global_entry *)hash_find(hna_global_hash, addr));
128 if (hna_global_entry != NULL)
129 _hna_global_del_orig(hna_global_entry, "local hna received");
131 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
134 int hna_local_fill_buffer(unsigned char *buff, int buff_len)
136 struct hna_local_entry *hna_local_entry;
141 spin_lock_irqsave(&hna_local_hash_lock, flags);
143 while (hash_iterate(hna_local_hash, &hashit)) {
145 if (buff_len < (i + 1) * ETH_ALEN)
148 hna_local_entry = hashit.bucket->data;
149 memcpy(buff + (i * ETH_ALEN), hna_local_entry->addr, ETH_ALEN);
154 /* if we did not get all new local hnas see you next time ;-) */
156 atomic_set(&hna_local_changed, 0);
158 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
163 int hna_local_fill_buffer_text(unsigned char *buff, int buff_len)
165 struct hna_local_entry *hna_local_entry;
167 int bytes_written = 0;
170 spin_lock_irqsave(&hna_local_hash_lock, flags);
172 while (hash_iterate(hna_local_hash, &hashit)) {
174 if (buff_len < bytes_written + ETH_STR_LEN + 4)
177 hna_local_entry = hashit.bucket->data;
179 bytes_written += snprintf(buff + bytes_written, ETH_STR_LEN + 4,
180 " * %02x:%02x:%02x:%02x:%02x:%02x\n",
181 hna_local_entry->addr[0],
182 hna_local_entry->addr[1],
183 hna_local_entry->addr[2],
184 hna_local_entry->addr[3],
185 hna_local_entry->addr[4],
186 hna_local_entry->addr[5]);
189 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
191 return bytes_written;
194 static void _hna_local_del(void *data)
198 atomic_set(&hna_local_changed, 1);
201 static void hna_local_del(struct hna_local_entry *hna_local_entry,
204 char hna_str[ETH_STR_LEN];
206 addr_to_string(hna_str, hna_local_entry->addr);
207 bat_dbg(DBG_ROUTES, "Deleting local hna entry (%s): %s \n",
210 hash_remove(hna_local_hash, hna_local_entry->addr);
211 _hna_local_del(hna_local_entry);
214 void hna_local_purge(struct work_struct *work)
216 struct hna_local_entry *hna_local_entry;
219 unsigned long timeout;
221 spin_lock_irqsave(&hna_local_hash_lock, flags);
223 while (hash_iterate(hna_local_hash, &hashit)) {
224 hna_local_entry = hashit.bucket->data;
226 timeout = hna_local_entry->last_seen +
227 ((LOCAL_HNA_TIMEOUT / 1000) * HZ);
228 if ((!hna_local_entry->never_purge) &&
229 time_after(jiffies, timeout))
230 hna_local_del(hna_local_entry, "address timed out");
233 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
234 hna_local_start_timer();
237 void hna_local_free(void)
242 cancel_delayed_work_sync(&hna_local_purge_wq);
243 hash_delete(hna_local_hash, _hna_local_del);
244 hna_local_hash = NULL;
247 int hna_global_init(void)
252 hna_global_hash = hash_new(128, compare_orig, choose_orig);
254 if (!hna_global_hash)
260 void hna_global_add_orig(struct orig_node *orig_node,
261 unsigned char *hna_buff, int hna_buff_len)
263 struct hna_global_entry *hna_global_entry;
264 struct hna_local_entry *hna_local_entry;
265 struct hashtable_t *swaphash;
266 char hna_str[ETH_STR_LEN], orig_str[ETH_STR_LEN];
267 int hna_buff_count = 0;
269 unsigned char *hna_ptr;
271 addr_to_string(orig_str, orig_node->orig);
273 while ((hna_buff_count + 1) * ETH_ALEN <= hna_buff_len) {
274 spin_lock_irqsave(&hna_global_hash_lock, flags);
276 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
277 hna_global_entry = (struct hna_global_entry *)
278 hash_find(hna_global_hash, hna_ptr);
280 if (hna_global_entry == NULL) {
281 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
284 kmalloc(sizeof(struct hna_global_entry),
287 if (!hna_global_entry)
290 memcpy(hna_global_entry->addr, hna_ptr, ETH_ALEN);
292 addr_to_string(hna_str, hna_global_entry->addr);
294 "Creating new global hna entry: %s (via %s)\n",
297 spin_lock_irqsave(&hna_global_hash_lock, flags);
298 hash_add(hna_global_hash, hna_global_entry);
302 hna_global_entry->orig_node = orig_node;
303 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
305 /* remove address from local hash if present */
306 spin_lock_irqsave(&hna_local_hash_lock, flags);
308 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
309 hna_local_entry = (struct hna_local_entry *)
310 hash_find(hna_local_hash, hna_ptr);
312 if (hna_local_entry != NULL)
313 hna_local_del(hna_local_entry, "global hna received");
315 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
320 /* initialize, and overwrite if malloc succeeds */
321 orig_node->hna_buff = NULL;
322 orig_node->hna_buff_len = 0;
324 if (hna_buff_len > 0) {
325 orig_node->hna_buff = kmalloc(hna_buff_len, GFP_ATOMIC);
326 if (orig_node->hna_buff) {
327 memcpy(orig_node->hna_buff, hna_buff, hna_buff_len);
328 orig_node->hna_buff_len = hna_buff_len;
332 spin_lock_irqsave(&hna_global_hash_lock, flags);
334 if (hna_global_hash->elements * 4 > hna_global_hash->size) {
335 swaphash = hash_resize(hna_global_hash,
336 hna_global_hash->size * 2);
338 if (swaphash == NULL)
339 printk(KERN_ERR "batman-adv:Couldn't resize global hna hash table \n");
341 hna_global_hash = swaphash;
344 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
347 int hna_global_fill_buffer_text(unsigned char *buff, int buff_len)
349 struct hna_global_entry *hna_global_entry;
351 int bytes_written = 0;
354 spin_lock_irqsave(&hna_global_hash_lock, flags);
356 while (hash_iterate(hna_global_hash, &hashit)) {
357 if (buff_len < bytes_written + (2 * ETH_STR_LEN) + 10)
360 hna_global_entry = hashit.bucket->data;
362 bytes_written += snprintf(buff + bytes_written,
363 (2 * ETH_STR_LEN) + 10,
364 " * %02x:%02x:%02x:%02x:%02x:%02x via %02x:%02x:%02x:%02x:%02x:%02x \n",
365 hna_global_entry->addr[0],
366 hna_global_entry->addr[1],
367 hna_global_entry->addr[2],
368 hna_global_entry->addr[3],
369 hna_global_entry->addr[4],
370 hna_global_entry->addr[5],
371 hna_global_entry->orig_node->orig[0],
372 hna_global_entry->orig_node->orig[1],
373 hna_global_entry->orig_node->orig[2],
374 hna_global_entry->orig_node->orig[3],
375 hna_global_entry->orig_node->orig[4],
376 hna_global_entry->orig_node->orig[5]);
379 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
381 return bytes_written;
384 void _hna_global_del_orig(struct hna_global_entry *hna_global_entry,
387 char hna_str[ETH_STR_LEN], orig_str[ETH_STR_LEN];
389 addr_to_string(orig_str, hna_global_entry->orig_node->orig);
390 addr_to_string(hna_str, hna_global_entry->addr);
392 bat_dbg(DBG_ROUTES, "Deleting global hna entry %s (via %s): %s \n",
393 hna_str, orig_str, message);
395 hash_remove(hna_global_hash, hna_global_entry->addr);
396 kfree(hna_global_entry);
399 void hna_global_del_orig(struct orig_node *orig_node, char *message)
401 struct hna_global_entry *hna_global_entry;
402 int hna_buff_count = 0;
404 unsigned char *hna_ptr;
406 if (orig_node->hna_buff_len == 0)
409 spin_lock_irqsave(&hna_global_hash_lock, flags);
411 while ((hna_buff_count + 1) * ETH_ALEN <= orig_node->hna_buff_len) {
412 hna_ptr = orig_node->hna_buff + (hna_buff_count * ETH_ALEN);
413 hna_global_entry = (struct hna_global_entry *)
414 hash_find(hna_global_hash, hna_ptr);
416 if ((hna_global_entry != NULL) &&
417 (hna_global_entry->orig_node == orig_node))
418 _hna_global_del_orig(hna_global_entry, message);
423 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
425 orig_node->hna_buff_len = 0;
426 kfree(orig_node->hna_buff);
427 orig_node->hna_buff = NULL;
430 static void hna_global_del(void *data)
435 void hna_global_free(void)
437 if (!hna_global_hash)
440 hash_delete(hna_global_hash, hna_global_del);
441 hna_global_hash = NULL;
444 struct orig_node *transtable_search(uint8_t *addr)
446 struct hna_global_entry *hna_global_entry;
449 spin_lock_irqsave(&hna_global_hash_lock, flags);
450 hna_global_entry = (struct hna_global_entry *)
451 hash_find(hna_global_hash, addr);
452 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
454 if (hna_global_entry == NULL)
457 return hna_global_entry->orig_node;