2 * Copyright (C) 2007-2011 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"
26 #include "originator.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(1024);
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;
62 spin_lock_bh(&bat_priv->hna_lhash_lock);
65 ((struct hna_local_entry *)hash_find(bat_priv->hna_local_hash,
66 compare_orig, choose_orig,
69 spin_unlock_bh(&bat_priv->hna_lhash_lock);
71 if (hna_local_entry) {
72 hna_local_entry->last_seen = jiffies;
76 /* only announce as many hosts as possible in the batman-packet and
77 space in batman_packet->num_hna That also should give a limit to
79 required_bytes = (bat_priv->num_local_hna + 1) * ETH_ALEN;
80 required_bytes += BAT_PACKET_LEN;
82 if ((required_bytes > ETH_DATA_LEN) ||
83 (atomic_read(&bat_priv->aggregated_ogms) &&
84 required_bytes > MAX_AGGREGATION_BYTES) ||
85 (bat_priv->num_local_hna + 1 > 255)) {
86 bat_dbg(DBG_ROUTES, bat_priv,
87 "Can't add new local hna entry (%pM): "
88 "number of local hna entries exceeds packet size\n",
93 bat_dbg(DBG_ROUTES, bat_priv,
94 "Creating new local hna entry: %pM\n", addr);
96 hna_local_entry = kmalloc(sizeof(struct hna_local_entry), GFP_ATOMIC);
100 memcpy(hna_local_entry->addr, addr, ETH_ALEN);
101 hna_local_entry->last_seen = jiffies;
103 /* the batman interface mac address should never be purged */
104 if (compare_orig(addr, soft_iface->dev_addr))
105 hna_local_entry->never_purge = 1;
107 hna_local_entry->never_purge = 0;
109 spin_lock_bh(&bat_priv->hna_lhash_lock);
111 hash_add(bat_priv->hna_local_hash, compare_orig, choose_orig,
113 bat_priv->num_local_hna++;
114 atomic_set(&bat_priv->hna_local_changed, 1);
116 spin_unlock_bh(&bat_priv->hna_lhash_lock);
118 /* remove address from global hash if present */
119 spin_lock_bh(&bat_priv->hna_ghash_lock);
122 hna_global_entry = ((struct hna_global_entry *)
123 hash_find(bat_priv->hna_global_hash,
124 compare_orig, choose_orig, addr));
127 if (hna_global_entry)
128 _hna_global_del_orig(bat_priv, hna_global_entry,
129 "local hna received");
131 spin_unlock_bh(&bat_priv->hna_ghash_lock);
134 int hna_local_fill_buffer(struct bat_priv *bat_priv,
135 unsigned char *buff, int buff_len)
137 struct hashtable_t *hash = bat_priv->hna_local_hash;
138 struct hna_local_entry *hna_local_entry;
139 struct element_t *bucket;
141 struct hlist_node *walk;
142 struct hlist_head *head;
145 spin_lock_bh(&bat_priv->hna_lhash_lock);
147 for (i = 0; i < hash->size; i++) {
148 head = &hash->table[i];
150 hlist_for_each_entry(bucket, walk, head, hlist) {
152 if (buff_len < (count + 1) * ETH_ALEN)
155 hna_local_entry = bucket->data;
156 memcpy(buff + (count * ETH_ALEN), hna_local_entry->addr,
163 /* if we did not get all new local hnas see you next time ;-) */
164 if (count == bat_priv->num_local_hna)
165 atomic_set(&bat_priv->hna_local_changed, 0);
167 spin_unlock_bh(&bat_priv->hna_lhash_lock);
171 int hna_local_seq_print_text(struct seq_file *seq, void *offset)
173 struct net_device *net_dev = (struct net_device *)seq->private;
174 struct bat_priv *bat_priv = netdev_priv(net_dev);
175 struct hashtable_t *hash = bat_priv->hna_local_hash;
176 struct hna_local_entry *hna_local_entry;
178 struct hlist_node *walk;
179 struct hlist_head *head;
180 struct element_t *bucket;
181 size_t buf_size, pos;
184 if (!bat_priv->primary_if) {
185 return seq_printf(seq, "BATMAN mesh %s disabled - "
186 "please specify interfaces to enable it\n",
190 seq_printf(seq, "Locally retrieved addresses (from %s) "
191 "announced via HNA:\n",
194 spin_lock_bh(&bat_priv->hna_lhash_lock);
197 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
198 for (i = 0; i < hash->size; i++) {
199 head = &hash->table[i];
201 hlist_for_each(walk, head)
205 buff = kmalloc(buf_size, GFP_ATOMIC);
207 spin_unlock_bh(&bat_priv->hna_lhash_lock);
213 for (i = 0; i < hash->size; i++) {
214 head = &hash->table[i];
216 hlist_for_each_entry(bucket, walk, head, hlist) {
217 hna_local_entry = bucket->data;
219 pos += snprintf(buff + pos, 22, " * %pM\n",
220 hna_local_entry->addr);
224 spin_unlock_bh(&bat_priv->hna_lhash_lock);
226 seq_printf(seq, "%s", buff);
231 static void _hna_local_del(void *data, void *arg)
233 struct bat_priv *bat_priv = (struct bat_priv *)arg;
236 bat_priv->num_local_hna--;
237 atomic_set(&bat_priv->hna_local_changed, 1);
240 static void hna_local_del(struct bat_priv *bat_priv,
241 struct hna_local_entry *hna_local_entry,
244 bat_dbg(DBG_ROUTES, bat_priv, "Deleting local hna entry (%pM): %s\n",
245 hna_local_entry->addr, message);
247 hash_remove(bat_priv->hna_local_hash, compare_orig, choose_orig,
248 hna_local_entry->addr);
249 _hna_local_del(hna_local_entry, bat_priv);
252 void hna_local_remove(struct bat_priv *bat_priv,
253 uint8_t *addr, char *message)
255 struct hna_local_entry *hna_local_entry;
257 spin_lock_bh(&bat_priv->hna_lhash_lock);
260 hna_local_entry = (struct hna_local_entry *)
261 hash_find(bat_priv->hna_local_hash, compare_orig, choose_orig,
266 hna_local_del(bat_priv, hna_local_entry, message);
268 spin_unlock_bh(&bat_priv->hna_lhash_lock);
271 static void hna_local_purge(struct work_struct *work)
273 struct delayed_work *delayed_work =
274 container_of(work, struct delayed_work, work);
275 struct bat_priv *bat_priv =
276 container_of(delayed_work, struct bat_priv, hna_work);
277 struct hashtable_t *hash = bat_priv->hna_local_hash;
278 struct hna_local_entry *hna_local_entry;
280 struct hlist_node *walk, *safe;
281 struct hlist_head *head;
282 struct element_t *bucket;
283 unsigned long timeout;
285 spin_lock_bh(&bat_priv->hna_lhash_lock);
287 for (i = 0; i < hash->size; i++) {
288 head = &hash->table[i];
290 hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
291 hna_local_entry = bucket->data;
293 timeout = hna_local_entry->last_seen;
294 timeout += LOCAL_HNA_TIMEOUT * HZ;
296 if ((!hna_local_entry->never_purge) &&
297 time_after(jiffies, timeout))
298 hna_local_del(bat_priv, hna_local_entry,
299 "address timed out");
303 spin_unlock_bh(&bat_priv->hna_lhash_lock);
304 hna_local_start_timer(bat_priv);
307 void hna_local_free(struct bat_priv *bat_priv)
309 if (!bat_priv->hna_local_hash)
312 cancel_delayed_work_sync(&bat_priv->hna_work);
313 hash_delete(bat_priv->hna_local_hash, _hna_local_del, bat_priv);
314 bat_priv->hna_local_hash = NULL;
317 int hna_global_init(struct bat_priv *bat_priv)
319 if (bat_priv->hna_global_hash)
322 bat_priv->hna_global_hash = hash_new(1024);
324 if (!bat_priv->hna_global_hash)
330 void hna_global_add_orig(struct bat_priv *bat_priv,
331 struct orig_node *orig_node,
332 unsigned char *hna_buff, int hna_buff_len)
334 struct hna_global_entry *hna_global_entry;
335 struct hna_local_entry *hna_local_entry;
336 int hna_buff_count = 0;
337 unsigned char *hna_ptr;
339 while ((hna_buff_count + 1) * ETH_ALEN <= hna_buff_len) {
340 spin_lock_bh(&bat_priv->hna_ghash_lock);
342 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
344 hna_global_entry = (struct hna_global_entry *)
345 hash_find(bat_priv->hna_global_hash, compare_orig,
346 choose_orig, hna_ptr);
349 if (!hna_global_entry) {
350 spin_unlock_bh(&bat_priv->hna_ghash_lock);
353 kmalloc(sizeof(struct hna_global_entry),
356 if (!hna_global_entry)
359 memcpy(hna_global_entry->addr, hna_ptr, ETH_ALEN);
361 bat_dbg(DBG_ROUTES, bat_priv,
362 "Creating new global hna entry: "
364 hna_global_entry->addr, orig_node->orig);
366 spin_lock_bh(&bat_priv->hna_ghash_lock);
367 hash_add(bat_priv->hna_global_hash, compare_orig,
368 choose_orig, hna_global_entry);
372 hna_global_entry->orig_node = orig_node;
373 spin_unlock_bh(&bat_priv->hna_ghash_lock);
375 /* remove address from local hash if present */
376 spin_lock_bh(&bat_priv->hna_lhash_lock);
378 hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
380 hna_local_entry = (struct hna_local_entry *)
381 hash_find(bat_priv->hna_local_hash, compare_orig,
382 choose_orig, hna_ptr);
386 hna_local_del(bat_priv, hna_local_entry,
387 "global hna received");
389 spin_unlock_bh(&bat_priv->hna_lhash_lock);
394 /* initialize, and overwrite if malloc succeeds */
395 orig_node->hna_buff = NULL;
396 orig_node->hna_buff_len = 0;
398 if (hna_buff_len > 0) {
399 orig_node->hna_buff = kmalloc(hna_buff_len, GFP_ATOMIC);
400 if (orig_node->hna_buff) {
401 memcpy(orig_node->hna_buff, hna_buff, hna_buff_len);
402 orig_node->hna_buff_len = hna_buff_len;
407 int hna_global_seq_print_text(struct seq_file *seq, void *offset)
409 struct net_device *net_dev = (struct net_device *)seq->private;
410 struct bat_priv *bat_priv = netdev_priv(net_dev);
411 struct hashtable_t *hash = bat_priv->hna_global_hash;
412 struct hna_global_entry *hna_global_entry;
414 struct hlist_node *walk;
415 struct hlist_head *head;
416 struct element_t *bucket;
417 size_t buf_size, pos;
420 if (!bat_priv->primary_if) {
421 return seq_printf(seq, "BATMAN mesh %s disabled - "
422 "please specify interfaces to enable it\n",
426 seq_printf(seq, "Globally announced HNAs received via the mesh %s\n",
429 spin_lock_bh(&bat_priv->hna_ghash_lock);
432 /* Estimate length for: " * xx:xx:xx:xx:xx:xx via xx:xx:xx:xx:xx:xx\n"*/
433 for (i = 0; i < hash->size; i++) {
434 head = &hash->table[i];
436 hlist_for_each(walk, head)
440 buff = kmalloc(buf_size, GFP_ATOMIC);
442 spin_unlock_bh(&bat_priv->hna_ghash_lock);
448 for (i = 0; i < hash->size; i++) {
449 head = &hash->table[i];
451 hlist_for_each_entry(bucket, walk, head, hlist) {
452 hna_global_entry = bucket->data;
454 pos += snprintf(buff + pos, 44,
456 hna_global_entry->addr,
457 hna_global_entry->orig_node->orig);
461 spin_unlock_bh(&bat_priv->hna_ghash_lock);
463 seq_printf(seq, "%s", buff);
468 static void _hna_global_del_orig(struct bat_priv *bat_priv,
469 struct hna_global_entry *hna_global_entry,
472 bat_dbg(DBG_ROUTES, bat_priv,
473 "Deleting global hna entry %pM (via %pM): %s\n",
474 hna_global_entry->addr, hna_global_entry->orig_node->orig,
477 hash_remove(bat_priv->hna_global_hash, compare_orig, choose_orig,
478 hna_global_entry->addr);
479 kfree(hna_global_entry);
482 void hna_global_del_orig(struct bat_priv *bat_priv,
483 struct orig_node *orig_node, char *message)
485 struct hna_global_entry *hna_global_entry;
486 int hna_buff_count = 0;
487 unsigned char *hna_ptr;
489 if (orig_node->hna_buff_len == 0)
492 spin_lock_bh(&bat_priv->hna_ghash_lock);
494 while ((hna_buff_count + 1) * ETH_ALEN <= orig_node->hna_buff_len) {
495 hna_ptr = orig_node->hna_buff + (hna_buff_count * ETH_ALEN);
497 hna_global_entry = (struct hna_global_entry *)
498 hash_find(bat_priv->hna_global_hash, compare_orig,
499 choose_orig, hna_ptr);
502 if ((hna_global_entry) &&
503 (hna_global_entry->orig_node == orig_node))
504 _hna_global_del_orig(bat_priv, hna_global_entry,
510 spin_unlock_bh(&bat_priv->hna_ghash_lock);
512 orig_node->hna_buff_len = 0;
513 kfree(orig_node->hna_buff);
514 orig_node->hna_buff = NULL;
517 static void hna_global_del(void *data, void *arg)
522 void hna_global_free(struct bat_priv *bat_priv)
524 if (!bat_priv->hna_global_hash)
527 hash_delete(bat_priv->hna_global_hash, hna_global_del, NULL);
528 bat_priv->hna_global_hash = NULL;
531 struct orig_node *transtable_search(struct bat_priv *bat_priv, uint8_t *addr)
533 struct hna_global_entry *hna_global_entry;
535 spin_lock_bh(&bat_priv->hna_ghash_lock);
537 hna_global_entry = (struct hna_global_entry *)
538 hash_find(bat_priv->hna_global_hash,
539 compare_orig, choose_orig, addr);
541 spin_unlock_bh(&bat_priv->hna_ghash_lock);
543 if (!hna_global_entry)
546 return hna_global_entry->orig_node;