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->dev);
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;
318 char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
320 if ((!bat_priv->primary_if) ||
321 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
322 if (!bat_priv->primary_if)
323 return seq_printf(seq, "BATMAN mesh %s disabled - "
324 "please specify interfaces to enable it\n",
327 return seq_printf(seq, "BATMAN mesh %s "
328 "disabled - primary interface not active\n",
333 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s (%s)]\n",
334 SOURCE_VERSION, REVISION_VERSION_STR,
335 bat_priv->primary_if->dev, bat_priv->primary_if->addr_str,
337 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
338 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
339 "outgoingIF", "Potential nexthops");
342 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
344 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
346 orig_node = hashit.bucket->data;
348 if (!orig_node->router)
351 if (orig_node->router->tq_avg == 0)
354 addr_to_string(orig_str, orig_node->orig);
355 addr_to_string(router_str, orig_node->router->addr);
356 last_seen_secs = jiffies_to_msecs(jiffies -
357 orig_node->last_valid) / 1000;
358 last_seen_msecs = jiffies_to_msecs(jiffies -
359 orig_node->last_valid) % 1000;
361 seq_printf(seq, "%-17s %4i.%03is (%3i) %17s [%10s]:",
362 orig_str, last_seen_secs, last_seen_msecs,
363 orig_node->router->tq_avg, router_str,
364 orig_node->router->if_incoming->dev);
366 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
367 addr_to_string(orig_str, neigh_node->addr);
368 seq_printf(seq, " %17s (%3i)", orig_str,
372 seq_printf(seq, "\n");
376 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
378 if ((batman_count == 0))
379 seq_printf(seq, "No batman nodes in range ...\n");
384 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
388 data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
391 pr_err("Can't resize orig: out of memory\n");
395 memcpy(data_ptr, orig_node->bcast_own,
396 (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
397 kfree(orig_node->bcast_own);
398 orig_node->bcast_own = data_ptr;
400 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
402 pr_err("Can't resize orig: out of memory\n");
406 memcpy(data_ptr, orig_node->bcast_own_sum,
407 (max_if_num - 1) * sizeof(uint8_t));
408 kfree(orig_node->bcast_own_sum);
409 orig_node->bcast_own_sum = data_ptr;
414 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
416 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
417 struct orig_node *orig_node;
421 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
423 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
425 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
426 orig_node = hashit.bucket->data;
428 if (orig_node_add_if(orig_node, max_if_num) == -1)
432 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
436 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
440 static int orig_node_del_if(struct orig_node *orig_node,
441 int max_if_num, int del_if_num)
443 void *data_ptr = NULL;
446 /* last interface was removed */
450 chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
451 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
453 pr_err("Can't resize orig: out of memory\n");
457 /* copy first part */
458 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
460 /* copy second part */
462 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
463 (max_if_num - del_if_num) * chunk_size);
466 kfree(orig_node->bcast_own);
467 orig_node->bcast_own = data_ptr;
472 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
474 pr_err("Can't resize orig: out of memory\n");
478 memcpy(data_ptr, orig_node->bcast_own_sum,
479 del_if_num * sizeof(uint8_t));
482 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
483 (max_if_num - del_if_num) * sizeof(uint8_t));
486 kfree(orig_node->bcast_own_sum);
487 orig_node->bcast_own_sum = data_ptr;
492 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
494 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
495 struct batman_if *batman_if_tmp;
496 struct orig_node *orig_node;
501 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
503 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
505 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
506 orig_node = hashit.bucket->data;
508 ret = orig_node_del_if(orig_node, max_if_num,
515 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
517 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
518 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
521 if (batman_if == batman_if_tmp)
524 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
527 if (batman_if_tmp->if_num > batman_if->if_num)
528 batman_if_tmp->if_num--;
532 batman_if->if_num = -1;
533 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
537 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);