2 * Copyright (C) 2007-2010 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"
28 struct hashtable_t *hna_local_hash;
29 static struct hashtable_t *hna_global_hash;
30 atomic_t hna_local_changed;
32 DEFINE_SPINLOCK(hna_local_hash_lock);
33 static DEFINE_SPINLOCK(hna_global_hash_lock);
35 static void hna_local_purge(struct work_struct *work);
36 static DECLARE_DELAYED_WORK(hna_local_purge_wq, hna_local_purge);
37 static void _hna_global_del_orig(struct hna_global_entry *hna_global_entry,
40 static void hna_local_start_timer(void)
42 queue_delayed_work(bat_event_workqueue, &hna_local_purge_wq, 10 * HZ);
45 int hna_local_init(void)
50 hna_local_hash = hash_new(128, compare_orig, choose_orig);
55 atomic_set(&hna_local_changed, 0);
56 hna_local_start_timer();
61 void hna_local_add(uint8_t *addr)
63 struct hna_local_entry *hna_local_entry;
64 struct hna_global_entry *hna_global_entry;
65 struct hashtable_t *swaphash;
68 spin_lock_irqsave(&hna_local_hash_lock, flags);
70 ((struct hna_local_entry *)hash_find(hna_local_hash, addr));
71 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
73 if (hna_local_entry != NULL) {
74 hna_local_entry->last_seen = jiffies;
78 /* only announce as many hosts as possible in the batman-packet and
79 space in batman_packet->num_hna That also should give a limit to
81 if ((num_hna + 1 > (ETH_DATA_LEN - BAT_PACKET_LEN) / ETH_ALEN) ||
82 (num_hna + 1 > 255)) {
84 "Can't add new local hna entry (%pM): "
85 "number of local hna entries exceeds packet size\n",
90 bat_dbg(DBG_ROUTES, "Creating new local hna entry: %pM\n",
93 hna_local_entry = kmalloc(sizeof(struct hna_local_entry), GFP_ATOMIC);
97 memcpy(hna_local_entry->addr, addr, ETH_ALEN);
98 hna_local_entry->last_seen = jiffies;
100 /* the batman interface mac address should never be purged */
101 if (compare_orig(addr, soft_device->dev_addr))
102 hna_local_entry->never_purge = 1;
104 hna_local_entry->never_purge = 0;
106 spin_lock_irqsave(&hna_local_hash_lock, flags);
108 hash_add(hna_local_hash, hna_local_entry);
110 atomic_set(&hna_local_changed, 1);
112 if (hna_local_hash->elements * 4 > hna_local_hash->size) {
113 swaphash = hash_resize(hna_local_hash,
114 hna_local_hash->size * 2);
116 if (swaphash == NULL)
117 printk(KERN_ERR "batman-adv:"
118 "Couldn't resize local hna hash table\n");
120 hna_local_hash = swaphash;
123 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
125 /* remove address from global hash if present */
126 spin_lock_irqsave(&hna_global_hash_lock, flags);
129 ((struct hna_global_entry *)hash_find(hna_global_hash, addr));
131 if (hna_global_entry != NULL)
132 _hna_global_del_orig(hna_global_entry, "local hna received");
134 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
137 int hna_local_fill_buffer(unsigned char *buff, int buff_len)
139 struct hna_local_entry *hna_local_entry;
144 spin_lock_irqsave(&hna_local_hash_lock, flags);
146 while (hash_iterate(hna_local_hash, &hashit)) {
148 if (buff_len < (i + 1) * ETH_ALEN)
151 hna_local_entry = hashit.bucket->data;
152 memcpy(buff + (i * ETH_ALEN), hna_local_entry->addr, ETH_ALEN);
157 /* if we did not get all new local hnas see you next time ;-) */
159 atomic_set(&hna_local_changed, 0);
161 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
166 int hna_local_seq_print_text(struct seq_file *seq, void *offset)
168 struct net_device *net_dev = (struct net_device *)seq->private;
169 struct bat_priv *bat_priv = netdev_priv(net_dev);
170 struct hna_local_entry *hna_local_entry;
172 HASHIT(hashit_count);
174 size_t buf_size, pos;
177 if (!bat_priv->primary_if) {
178 return seq_printf(seq, "BATMAN mesh %s disabled - "
179 "please specify interfaces to enable it\n",
183 seq_printf(seq, "Locally retrieved addresses (from %s) "
184 "announced via HNA:\n",
187 spin_lock_irqsave(&hna_local_hash_lock, flags);
190 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
191 while (hash_iterate(hna_local_hash, &hashit_count))
194 buff = kmalloc(buf_size, GFP_ATOMIC);
196 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
202 while (hash_iterate(hna_local_hash, &hashit)) {
203 hna_local_entry = hashit.bucket->data;
205 pos += snprintf(buff + pos, 22, " * %pM\n",
206 hna_local_entry->addr);
209 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
211 seq_printf(seq, "%s", buff);
216 static void _hna_local_del(void *data)
220 atomic_set(&hna_local_changed, 1);
223 static void hna_local_del(struct hna_local_entry *hna_local_entry,
226 bat_dbg(DBG_ROUTES, "Deleting local hna entry (%pM): %s\n",
227 hna_local_entry->addr, message);
229 hash_remove(hna_local_hash, hna_local_entry->addr);
230 _hna_local_del(hna_local_entry);
233 void hna_local_remove(uint8_t *addr, char *message)
235 struct hna_local_entry *hna_local_entry;
238 spin_lock_irqsave(&hna_local_hash_lock, flags);
240 hna_local_entry = (struct hna_local_entry *)
241 hash_find(hna_local_hash, addr);
243 hna_local_del(hna_local_entry, message);
245 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
248 static void hna_local_purge(struct work_struct *work)
250 struct hna_local_entry *hna_local_entry;
253 unsigned long timeout;
255 spin_lock_irqsave(&hna_local_hash_lock, flags);
257 while (hash_iterate(hna_local_hash, &hashit)) {
258 hna_local_entry = hashit.bucket->data;
260 timeout = hna_local_entry->last_seen +
261 ((LOCAL_HNA_TIMEOUT / 1000) * HZ);
262 if ((!hna_local_entry->never_purge) &&
263 time_after(jiffies, timeout))
264 hna_local_del(hna_local_entry, "address timed out");
267 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
268 hna_local_start_timer();
271 void hna_local_free(void)
276 cancel_delayed_work_sync(&hna_local_purge_wq);
277 hash_delete(hna_local_hash, _hna_local_del);
278 hna_local_hash = NULL;
281 int hna_global_init(void)
286 hna_global_hash = hash_new(128, compare_orig, choose_orig);
288 if (!hna_global_hash)
294 void hna_global_add_orig(struct orig_node *orig_node,
295 unsigned char *hna_buff, int hna_buff_len)
297 struct hna_global_entry *hna_global_entry;
298 struct hna_local_entry *hna_local_entry;
299 struct hashtable_t *swaphash;
300 int hna_buff_count = 0;
302 unsigned char *hna_ptr;
304 while ((hna_buff_count + 1) * ETH_ALEN <= hna_buff_len) {
305 spin_lock_irqsave(&hna_global_hash_lock, flags);
307 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
308 hna_global_entry = (struct hna_global_entry *)
309 hash_find(hna_global_hash, hna_ptr);
311 if (hna_global_entry == NULL) {
312 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
315 kmalloc(sizeof(struct hna_global_entry),
318 if (!hna_global_entry)
321 memcpy(hna_global_entry->addr, hna_ptr, ETH_ALEN);
324 "Creating new global hna entry: "
326 hna_global_entry->addr, orig_node->orig);
328 spin_lock_irqsave(&hna_global_hash_lock, flags);
329 hash_add(hna_global_hash, hna_global_entry);
333 hna_global_entry->orig_node = orig_node;
334 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
336 /* remove address from local hash if present */
337 spin_lock_irqsave(&hna_local_hash_lock, flags);
339 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
340 hna_local_entry = (struct hna_local_entry *)
341 hash_find(hna_local_hash, hna_ptr);
343 if (hna_local_entry != NULL)
344 hna_local_del(hna_local_entry, "global hna received");
346 spin_unlock_irqrestore(&hna_local_hash_lock, flags);
351 /* initialize, and overwrite if malloc succeeds */
352 orig_node->hna_buff = NULL;
353 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;
363 spin_lock_irqsave(&hna_global_hash_lock, flags);
365 if (hna_global_hash->elements * 4 > hna_global_hash->size) {
366 swaphash = hash_resize(hna_global_hash,
367 hna_global_hash->size * 2);
369 if (swaphash == NULL)
370 printk(KERN_ERR "batman-adv:"
371 "Couldn't resize global hna hash table\n");
373 hna_global_hash = swaphash;
376 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
379 int hna_global_seq_print_text(struct seq_file *seq, void *offset)
381 struct net_device *net_dev = (struct net_device *)seq->private;
382 struct bat_priv *bat_priv = netdev_priv(net_dev);
383 struct hna_global_entry *hna_global_entry;
385 HASHIT(hashit_count);
387 size_t buf_size, pos;
390 if (!bat_priv->primary_if) {
391 return seq_printf(seq, "BATMAN mesh %s disabled - "
392 "please specify interfaces to enable it\n",
396 seq_printf(seq, "Globally announced HNAs received via the mesh %s\n",
399 spin_lock_irqsave(&hna_global_hash_lock, flags);
402 /* Estimate length for: " * xx:xx:xx:xx:xx:xx via xx:xx:xx:xx:xx:xx\n"*/
403 while (hash_iterate(hna_global_hash, &hashit_count))
406 buff = kmalloc(buf_size, GFP_ATOMIC);
408 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
414 while (hash_iterate(hna_global_hash, &hashit)) {
415 hna_global_entry = hashit.bucket->data;
417 pos += snprintf(buff + pos, 44,
418 " * %pM via %pM\n", hna_global_entry->addr,
419 hna_global_entry->orig_node->orig);
422 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
424 seq_printf(seq, "%s", buff);
429 static void _hna_global_del_orig(struct hna_global_entry *hna_global_entry,
432 bat_dbg(DBG_ROUTES, "Deleting global hna entry %pM (via %pM): %s\n",
433 hna_global_entry->addr, hna_global_entry->orig_node->orig,
436 hash_remove(hna_global_hash, hna_global_entry->addr);
437 kfree(hna_global_entry);
440 void hna_global_del_orig(struct orig_node *orig_node, char *message)
442 struct hna_global_entry *hna_global_entry;
443 int hna_buff_count = 0;
445 unsigned char *hna_ptr;
447 if (orig_node->hna_buff_len == 0)
450 spin_lock_irqsave(&hna_global_hash_lock, flags);
452 while ((hna_buff_count + 1) * ETH_ALEN <= orig_node->hna_buff_len) {
453 hna_ptr = orig_node->hna_buff + (hna_buff_count * ETH_ALEN);
454 hna_global_entry = (struct hna_global_entry *)
455 hash_find(hna_global_hash, hna_ptr);
457 if ((hna_global_entry != NULL) &&
458 (hna_global_entry->orig_node == orig_node))
459 _hna_global_del_orig(hna_global_entry, message);
464 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
466 orig_node->hna_buff_len = 0;
467 kfree(orig_node->hna_buff);
468 orig_node->hna_buff = NULL;
471 static void hna_global_del(void *data)
476 void hna_global_free(void)
478 if (!hna_global_hash)
481 hash_delete(hna_global_hash, hna_global_del);
482 hna_global_hash = NULL;
485 struct orig_node *transtable_search(uint8_t *addr)
487 struct hna_global_entry *hna_global_entry;
490 spin_lock_irqsave(&hna_global_hash_lock, flags);
491 hna_global_entry = (struct hna_global_entry *)
492 hash_find(hna_global_hash, addr);
493 spin_unlock_irqrestore(&hna_global_hash_lock, flags);
495 if (hna_global_entry == NULL)
498 return hna_global_entry->orig_node;