2 * Copyright (C) 2009-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
22 /* increase the reference counter for this originator */
25 #include "originator.h"
27 #include "translation-table.h"
29 #include "hard-interface.h"
32 static void purge_orig(struct work_struct *work);
34 static void start_purge_timer(struct bat_priv *bat_priv)
36 INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
37 queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
40 int originator_init(struct bat_priv *bat_priv)
43 if (bat_priv->orig_hash)
46 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
47 bat_priv->orig_hash = hash_new(128, compare_orig, choose_orig);
49 if (!bat_priv->orig_hash)
52 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
53 start_purge_timer(bat_priv);
57 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
62 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
63 uint8_t *neigh, struct batman_if *if_incoming)
65 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
66 struct neigh_node *neigh_node;
68 bat_dbg(DBG_BATMAN, bat_priv,
69 "Creating new last-hop neighbor of originator\n");
71 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
75 INIT_LIST_HEAD(&neigh_node->list);
77 memcpy(neigh_node->addr, neigh, ETH_ALEN);
78 neigh_node->orig_node = orig_neigh_node;
79 neigh_node->if_incoming = if_incoming;
81 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
85 static void free_orig_node(void *data, void *arg)
87 struct list_head *list_pos, *list_pos_tmp;
88 struct neigh_node *neigh_node;
89 struct orig_node *orig_node = (struct orig_node *)data;
90 struct bat_priv *bat_priv = (struct bat_priv *)arg;
92 /* for all neighbors towards this originator ... */
93 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
94 neigh_node = list_entry(list_pos, struct neigh_node, list);
100 frag_list_free(&orig_node->frag_list);
101 hna_global_del_orig(bat_priv, orig_node, "originator timed out");
103 kfree(orig_node->bcast_own);
104 kfree(orig_node->bcast_own_sum);
108 void originator_free(struct bat_priv *bat_priv)
112 if (!bat_priv->orig_hash)
115 cancel_delayed_work_sync(&bat_priv->orig_work);
117 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
118 hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv);
119 bat_priv->orig_hash = NULL;
120 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
123 /* this function finds or creates an originator entry for the given
124 * address if it does not exits */
125 struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
127 struct orig_node *orig_node;
128 struct hashtable_t *swaphash;
131 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash, addr));
136 bat_dbg(DBG_BATMAN, bat_priv,
137 "Creating new originator: %pM\n", addr);
139 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
143 INIT_LIST_HEAD(&orig_node->neigh_list);
145 memcpy(orig_node->orig, addr, ETH_ALEN);
146 orig_node->router = NULL;
147 orig_node->hna_buff = NULL;
148 orig_node->bcast_seqno_reset = jiffies - 1
149 - msecs_to_jiffies(RESET_PROTECTION_MS);
150 orig_node->batman_seqno_reset = jiffies - 1
151 - msecs_to_jiffies(RESET_PROTECTION_MS);
153 size = bat_priv->num_ifaces * sizeof(TYPE_OF_WORD) * NUM_WORDS;
155 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
156 if (!orig_node->bcast_own)
159 size = bat_priv->num_ifaces * sizeof(uint8_t);
160 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
162 INIT_LIST_HEAD(&orig_node->frag_list);
163 orig_node->last_frag_packet = 0;
165 if (!orig_node->bcast_own_sum)
168 if (hash_add(bat_priv->orig_hash, orig_node) < 0)
169 goto free_bcast_own_sum;
171 if (bat_priv->orig_hash->elements * 4 > bat_priv->orig_hash->size) {
172 swaphash = hash_resize(bat_priv->orig_hash,
173 bat_priv->orig_hash->size * 2);
176 bat_dbg(DBG_BATMAN, bat_priv,
177 "Couldn't resize orig hash table\n");
179 bat_priv->orig_hash = swaphash;
184 kfree(orig_node->bcast_own_sum);
186 kfree(orig_node->bcast_own);
192 static bool purge_orig_neighbors(struct bat_priv *bat_priv,
193 struct orig_node *orig_node,
194 struct neigh_node **best_neigh_node)
196 struct list_head *list_pos, *list_pos_tmp;
197 struct neigh_node *neigh_node;
198 bool neigh_purged = false;
200 *best_neigh_node = NULL;
202 /* for all neighbors towards this originator ... */
203 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
204 neigh_node = list_entry(list_pos, struct neigh_node, list);
206 if ((time_after(jiffies,
207 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
208 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
209 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
211 if (neigh_node->if_incoming->if_status ==
213 bat_dbg(DBG_BATMAN, bat_priv,
214 "neighbor purge: originator %pM, "
215 "neighbor: %pM, iface: %s\n",
216 orig_node->orig, neigh_node->addr,
217 neigh_node->if_incoming->net_dev->name);
219 bat_dbg(DBG_BATMAN, bat_priv,
220 "neighbor timeout: originator %pM, "
221 "neighbor: %pM, last_valid: %lu\n",
222 orig_node->orig, neigh_node->addr,
223 (neigh_node->last_valid / HZ));
229 if ((*best_neigh_node == NULL) ||
230 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
231 *best_neigh_node = neigh_node;
237 static bool purge_orig_node(struct bat_priv *bat_priv,
238 struct orig_node *orig_node)
240 struct neigh_node *best_neigh_node;
242 if (time_after(jiffies,
243 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
245 bat_dbg(DBG_BATMAN, bat_priv,
246 "Originator timeout: originator %pM, last_valid %lu\n",
247 orig_node->orig, (orig_node->last_valid / HZ));
250 if (purge_orig_neighbors(bat_priv, orig_node,
252 update_routes(bat_priv, orig_node,
255 orig_node->hna_buff_len);
256 /* update bonding candidates, we could have lost
257 * some candidates. */
258 update_bonding_candidates(bat_priv, orig_node);
265 static void _purge_orig(struct bat_priv *bat_priv)
268 struct orig_node *orig_node;
271 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
273 /* for all origins... */
274 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
275 orig_node = hashit.bucket->data;
277 if (purge_orig_node(bat_priv, orig_node)) {
278 hash_remove_bucket(bat_priv->orig_hash, &hashit);
279 free_orig_node(orig_node, bat_priv);
282 if (time_after(jiffies, (orig_node->last_frag_packet +
283 msecs_to_jiffies(FRAG_TIMEOUT))))
284 frag_list_free(&orig_node->frag_list);
287 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
291 static void purge_orig(struct work_struct *work)
293 struct delayed_work *delayed_work =
294 container_of(work, struct delayed_work, work);
295 struct bat_priv *bat_priv =
296 container_of(delayed_work, struct bat_priv, orig_work);
298 _purge_orig(bat_priv);
299 start_purge_timer(bat_priv);
302 void purge_orig_ref(struct bat_priv *bat_priv)
304 _purge_orig(bat_priv);
307 int orig_seq_print_text(struct seq_file *seq, void *offset)
310 struct net_device *net_dev = (struct net_device *)seq->private;
311 struct bat_priv *bat_priv = netdev_priv(net_dev);
312 struct orig_node *orig_node;
313 struct neigh_node *neigh_node;
314 int batman_count = 0;
319 if ((!bat_priv->primary_if) ||
320 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
321 if (!bat_priv->primary_if)
322 return seq_printf(seq, "BATMAN mesh %s disabled - "
323 "please specify interfaces to enable it\n",
326 return seq_printf(seq, "BATMAN mesh %s "
327 "disabled - primary interface not active\n",
331 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
332 SOURCE_VERSION, REVISION_VERSION_STR,
333 bat_priv->primary_if->net_dev->name,
334 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
335 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
336 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
337 "outgoingIF", "Potential nexthops");
339 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
341 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
343 orig_node = hashit.bucket->data;
345 if (!orig_node->router)
348 if (orig_node->router->tq_avg == 0)
351 last_seen_secs = jiffies_to_msecs(jiffies -
352 orig_node->last_valid) / 1000;
353 last_seen_msecs = jiffies_to_msecs(jiffies -
354 orig_node->last_valid) % 1000;
356 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
357 orig_node->orig, last_seen_secs, last_seen_msecs,
358 orig_node->router->tq_avg, orig_node->router->addr,
359 orig_node->router->if_incoming->net_dev->name);
361 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
362 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
366 seq_printf(seq, "\n");
370 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
372 if ((batman_count == 0))
373 seq_printf(seq, "No batman nodes in range ...\n");
378 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
382 data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
385 pr_err("Can't resize orig: out of memory\n");
389 memcpy(data_ptr, orig_node->bcast_own,
390 (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
391 kfree(orig_node->bcast_own);
392 orig_node->bcast_own = data_ptr;
394 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
396 pr_err("Can't resize orig: out of memory\n");
400 memcpy(data_ptr, orig_node->bcast_own_sum,
401 (max_if_num - 1) * sizeof(uint8_t));
402 kfree(orig_node->bcast_own_sum);
403 orig_node->bcast_own_sum = data_ptr;
408 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
410 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
411 struct orig_node *orig_node;
415 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
417 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
419 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
420 orig_node = hashit.bucket->data;
422 if (orig_node_add_if(orig_node, max_if_num) == -1)
426 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
430 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
434 static int orig_node_del_if(struct orig_node *orig_node,
435 int max_if_num, int del_if_num)
437 void *data_ptr = NULL;
440 /* last interface was removed */
444 chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
445 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
447 pr_err("Can't resize orig: out of memory\n");
451 /* copy first part */
452 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
454 /* copy second part */
455 memcpy(data_ptr + del_if_num * chunk_size,
456 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
457 (max_if_num - del_if_num) * chunk_size);
460 kfree(orig_node->bcast_own);
461 orig_node->bcast_own = data_ptr;
466 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
468 pr_err("Can't resize orig: out of memory\n");
472 memcpy(data_ptr, orig_node->bcast_own_sum,
473 del_if_num * sizeof(uint8_t));
475 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
476 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
477 (max_if_num - del_if_num) * sizeof(uint8_t));
480 kfree(orig_node->bcast_own_sum);
481 orig_node->bcast_own_sum = data_ptr;
486 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
488 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
489 struct batman_if *batman_if_tmp;
490 struct orig_node *orig_node;
495 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
497 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
499 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
500 orig_node = hashit.bucket->data;
502 ret = orig_node_del_if(orig_node, max_if_num,
509 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
511 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
512 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
515 if (batman_if == batman_if_tmp)
518 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
521 if (batman_if_tmp->if_num > batman_if->if_num)
522 batman_if_tmp->if_num--;
526 batman_if->if_num = -1;
527 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
531 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);