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 static void hna_local_purge(struct work_struct *work);
29 static void _hna_global_del_orig(struct bat_priv *bat_priv,
30 struct hna_global_entry *hna_global_entry,
33 static void hna_local_start_timer(struct bat_priv *bat_priv)
35 INIT_DELAYED_WORK(&bat_priv->hna_work, hna_local_purge);
36 queue_delayed_work(bat_event_workqueue, &bat_priv->hna_work, 10 * HZ);
39 int hna_local_init(struct bat_priv *bat_priv)
41 if (bat_priv->hna_local_hash)
44 bat_priv->hna_local_hash = hash_new(128, compare_orig, choose_orig);
46 if (!bat_priv->hna_local_hash)
49 atomic_set(&bat_priv->hna_local_changed, 0);
50 hna_local_start_timer(bat_priv);
55 void hna_local_add(struct net_device *soft_iface, uint8_t *addr)
57 struct bat_priv *bat_priv = netdev_priv(soft_iface);
58 struct hna_local_entry *hna_local_entry;
59 struct hna_global_entry *hna_global_entry;
60 struct hashtable_t *swaphash;
63 spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
65 ((struct hna_local_entry *)hash_find(bat_priv->hna_local_hash,
67 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
69 if (hna_local_entry) {
70 hna_local_entry->last_seen = jiffies;
74 /* only announce as many hosts as possible in the batman-packet and
75 space in batman_packet->num_hna That also should give a limit to
77 if ((bat_priv->num_local_hna + 1 > (ETH_DATA_LEN - BAT_PACKET_LEN)
79 (bat_priv->num_local_hna + 1 > 255)) {
80 bat_dbg(DBG_ROUTES, bat_priv,
81 "Can't add new local hna entry (%pM): "
82 "number of local hna entries exceeds packet size\n",
87 bat_dbg(DBG_ROUTES, bat_priv,
88 "Creating new local hna entry: %pM\n", addr);
90 hna_local_entry = kmalloc(sizeof(struct hna_local_entry), GFP_ATOMIC);
94 memcpy(hna_local_entry->addr, addr, ETH_ALEN);
95 hna_local_entry->last_seen = jiffies;
97 /* the batman interface mac address should never be purged */
98 if (compare_orig(addr, soft_iface->dev_addr))
99 hna_local_entry->never_purge = 1;
101 hna_local_entry->never_purge = 0;
103 spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
105 hash_add(bat_priv->hna_local_hash, hna_local_entry);
106 bat_priv->num_local_hna++;
107 atomic_set(&bat_priv->hna_local_changed, 1);
109 if (bat_priv->hna_local_hash->elements * 4 >
110 bat_priv->hna_local_hash->size) {
111 swaphash = hash_resize(bat_priv->hna_local_hash,
112 bat_priv->hna_local_hash->size * 2);
115 pr_err("Couldn't resize local hna hash table\n");
117 bat_priv->hna_local_hash = swaphash;
120 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
122 /* remove address from global hash if present */
123 spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
125 hna_global_entry = ((struct hna_global_entry *)
126 hash_find(bat_priv->hna_global_hash, addr));
128 if (hna_global_entry)
129 _hna_global_del_orig(bat_priv, hna_global_entry,
130 "local hna received");
132 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
135 int hna_local_fill_buffer(struct bat_priv *bat_priv,
136 unsigned char *buff, int buff_len)
138 struct hna_local_entry *hna_local_entry;
143 spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
145 while (hash_iterate(bat_priv->hna_local_hash, &hashit)) {
147 if (buff_len < (i + 1) * ETH_ALEN)
150 hna_local_entry = hashit.bucket->data;
151 memcpy(buff + (i * ETH_ALEN), hna_local_entry->addr, ETH_ALEN);
156 /* if we did not get all new local hnas see you next time ;-) */
157 if (i == bat_priv->num_local_hna)
158 atomic_set(&bat_priv->hna_local_changed, 0);
160 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
164 int hna_local_seq_print_text(struct seq_file *seq, void *offset)
166 struct net_device *net_dev = (struct net_device *)seq->private;
167 struct bat_priv *bat_priv = netdev_priv(net_dev);
168 struct hna_local_entry *hna_local_entry;
170 HASHIT(hashit_count);
172 size_t buf_size, pos;
175 if (!bat_priv->primary_if) {
176 return seq_printf(seq, "BATMAN mesh %s disabled - "
177 "please specify interfaces to enable it\n",
181 seq_printf(seq, "Locally retrieved addresses (from %s) "
182 "announced via HNA:\n",
185 spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
188 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
189 while (hash_iterate(bat_priv->hna_local_hash, &hashit_count))
192 buff = kmalloc(buf_size, GFP_ATOMIC);
194 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
200 while (hash_iterate(bat_priv->hna_local_hash, &hashit)) {
201 hna_local_entry = hashit.bucket->data;
203 pos += snprintf(buff + pos, 22, " * %pM\n",
204 hna_local_entry->addr);
207 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
209 seq_printf(seq, "%s", buff);
214 static void _hna_local_del(void *data, void *arg)
216 struct bat_priv *bat_priv = (struct bat_priv *)arg;
219 bat_priv->num_local_hna--;
220 atomic_set(&bat_priv->hna_local_changed, 1);
223 static void hna_local_del(struct bat_priv *bat_priv,
224 struct hna_local_entry *hna_local_entry,
227 bat_dbg(DBG_ROUTES, bat_priv, "Deleting local hna entry (%pM): %s\n",
228 hna_local_entry->addr, message);
230 hash_remove(bat_priv->hna_local_hash, hna_local_entry->addr);
231 _hna_local_del(hna_local_entry, bat_priv);
234 void hna_local_remove(struct bat_priv *bat_priv,
235 uint8_t *addr, char *message)
237 struct hna_local_entry *hna_local_entry;
240 spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
242 hna_local_entry = (struct hna_local_entry *)
243 hash_find(bat_priv->hna_local_hash, addr);
245 hna_local_del(bat_priv, hna_local_entry, message);
247 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
250 static void hna_local_purge(struct work_struct *work)
252 struct delayed_work *delayed_work =
253 container_of(work, struct delayed_work, work);
254 struct bat_priv *bat_priv =
255 container_of(delayed_work, struct bat_priv, hna_work);
256 struct hna_local_entry *hna_local_entry;
259 unsigned long timeout;
261 spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
263 while (hash_iterate(bat_priv->hna_local_hash, &hashit)) {
264 hna_local_entry = hashit.bucket->data;
266 timeout = hna_local_entry->last_seen + LOCAL_HNA_TIMEOUT * HZ;
268 if ((!hna_local_entry->never_purge) &&
269 time_after(jiffies, timeout))
270 hna_local_del(bat_priv, hna_local_entry,
271 "address timed out");
274 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
275 hna_local_start_timer(bat_priv);
278 void hna_local_free(struct bat_priv *bat_priv)
280 if (!bat_priv->hna_local_hash)
283 cancel_delayed_work_sync(&bat_priv->hna_work);
284 hash_delete(bat_priv->hna_local_hash, _hna_local_del, bat_priv);
285 bat_priv->hna_local_hash = NULL;
288 int hna_global_init(struct bat_priv *bat_priv)
290 if (bat_priv->hna_global_hash)
293 bat_priv->hna_global_hash = hash_new(128, compare_orig, choose_orig);
295 if (!bat_priv->hna_global_hash)
301 void hna_global_add_orig(struct bat_priv *bat_priv,
302 struct orig_node *orig_node,
303 unsigned char *hna_buff, int hna_buff_len)
305 struct hna_global_entry *hna_global_entry;
306 struct hna_local_entry *hna_local_entry;
307 struct hashtable_t *swaphash;
308 int hna_buff_count = 0;
310 unsigned char *hna_ptr;
312 while ((hna_buff_count + 1) * ETH_ALEN <= hna_buff_len) {
313 spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
315 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
316 hna_global_entry = (struct hna_global_entry *)
317 hash_find(bat_priv->hna_global_hash, hna_ptr);
319 if (!hna_global_entry) {
320 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock,
324 kmalloc(sizeof(struct hna_global_entry),
327 if (!hna_global_entry)
330 memcpy(hna_global_entry->addr, hna_ptr, ETH_ALEN);
332 bat_dbg(DBG_ROUTES, bat_priv,
333 "Creating new global hna entry: "
335 hna_global_entry->addr, orig_node->orig);
337 spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
338 hash_add(bat_priv->hna_global_hash, hna_global_entry);
342 hna_global_entry->orig_node = orig_node;
343 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
345 /* remove address from local hash if present */
346 spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
348 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
349 hna_local_entry = (struct hna_local_entry *)
350 hash_find(bat_priv->hna_local_hash, hna_ptr);
353 hna_local_del(bat_priv, hna_local_entry,
354 "global hna received");
356 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
361 /* initialize, and overwrite if malloc succeeds */
362 orig_node->hna_buff = NULL;
363 orig_node->hna_buff_len = 0;
365 if (hna_buff_len > 0) {
366 orig_node->hna_buff = kmalloc(hna_buff_len, GFP_ATOMIC);
367 if (orig_node->hna_buff) {
368 memcpy(orig_node->hna_buff, hna_buff, hna_buff_len);
369 orig_node->hna_buff_len = hna_buff_len;
373 spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
375 if (bat_priv->hna_global_hash->elements * 4 >
376 bat_priv->hna_global_hash->size) {
377 swaphash = hash_resize(bat_priv->hna_global_hash,
378 bat_priv->hna_global_hash->size * 2);
381 pr_err("Couldn't resize global hna hash table\n");
383 bat_priv->hna_global_hash = swaphash;
386 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
389 int hna_global_seq_print_text(struct seq_file *seq, void *offset)
391 struct net_device *net_dev = (struct net_device *)seq->private;
392 struct bat_priv *bat_priv = netdev_priv(net_dev);
393 struct hna_global_entry *hna_global_entry;
395 HASHIT(hashit_count);
397 size_t buf_size, pos;
400 if (!bat_priv->primary_if) {
401 return seq_printf(seq, "BATMAN mesh %s disabled - "
402 "please specify interfaces to enable it\n",
406 seq_printf(seq, "Globally announced HNAs received via the mesh %s\n",
409 spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
412 /* Estimate length for: " * xx:xx:xx:xx:xx:xx via xx:xx:xx:xx:xx:xx\n"*/
413 while (hash_iterate(bat_priv->hna_global_hash, &hashit_count))
416 buff = kmalloc(buf_size, GFP_ATOMIC);
418 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
424 while (hash_iterate(bat_priv->hna_global_hash, &hashit)) {
425 hna_global_entry = hashit.bucket->data;
427 pos += snprintf(buff + pos, 44,
428 " * %pM via %pM\n", hna_global_entry->addr,
429 hna_global_entry->orig_node->orig);
432 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
434 seq_printf(seq, "%s", buff);
439 static void _hna_global_del_orig(struct bat_priv *bat_priv,
440 struct hna_global_entry *hna_global_entry,
443 bat_dbg(DBG_ROUTES, bat_priv,
444 "Deleting global hna entry %pM (via %pM): %s\n",
445 hna_global_entry->addr, hna_global_entry->orig_node->orig,
448 hash_remove(bat_priv->hna_global_hash, hna_global_entry->addr);
449 kfree(hna_global_entry);
452 void hna_global_del_orig(struct bat_priv *bat_priv,
453 struct orig_node *orig_node, char *message)
455 struct hna_global_entry *hna_global_entry;
456 int hna_buff_count = 0;
458 unsigned char *hna_ptr;
460 if (orig_node->hna_buff_len == 0)
463 spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
465 while ((hna_buff_count + 1) * ETH_ALEN <= orig_node->hna_buff_len) {
466 hna_ptr = orig_node->hna_buff + (hna_buff_count * ETH_ALEN);
467 hna_global_entry = (struct hna_global_entry *)
468 hash_find(bat_priv->hna_global_hash, hna_ptr);
470 if ((hna_global_entry) &&
471 (hna_global_entry->orig_node == orig_node))
472 _hna_global_del_orig(bat_priv, hna_global_entry,
478 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
480 orig_node->hna_buff_len = 0;
481 kfree(orig_node->hna_buff);
482 orig_node->hna_buff = NULL;
485 static void hna_global_del(void *data, void *arg)
490 void hna_global_free(struct bat_priv *bat_priv)
492 if (!bat_priv->hna_global_hash)
495 hash_delete(bat_priv->hna_global_hash, hna_global_del, NULL);
496 bat_priv->hna_global_hash = NULL;
499 struct orig_node *transtable_search(struct bat_priv *bat_priv, uint8_t *addr)
501 struct hna_global_entry *hna_global_entry;
504 spin_lock_irqsave(&bat_priv->hna_ghash_lock, flags);
505 hna_global_entry = (struct hna_global_entry *)
506 hash_find(bat_priv->hna_global_hash, addr);
507 spin_unlock_irqrestore(&bat_priv->hna_ghash_lock, flags);
509 if (!hna_global_entry)
512 return hna_global_entry->orig_node;