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"
31 #include "soft-interface.h"
33 static void purge_orig(struct work_struct *work);
35 static void start_purge_timer(struct bat_priv *bat_priv)
37 INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
38 queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
41 int originator_init(struct bat_priv *bat_priv)
44 if (bat_priv->orig_hash)
47 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
48 bat_priv->orig_hash = hash_new(128, compare_orig, choose_orig);
50 if (!bat_priv->orig_hash)
53 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
54 start_purge_timer(bat_priv);
58 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
63 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
64 uint8_t *neigh, struct batman_if *if_incoming)
66 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
67 struct neigh_node *neigh_node;
69 bat_dbg(DBG_BATMAN, bat_priv,
70 "Creating new last-hop neighbor of originator\n");
72 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
76 INIT_LIST_HEAD(&neigh_node->list);
78 memcpy(neigh_node->addr, neigh, ETH_ALEN);
79 neigh_node->orig_node = orig_neigh_node;
80 neigh_node->if_incoming = if_incoming;
82 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
86 static void free_orig_node(void *data, void *arg)
88 struct list_head *list_pos, *list_pos_tmp;
89 struct neigh_node *neigh_node;
90 struct orig_node *orig_node = (struct orig_node *)data;
91 struct bat_priv *bat_priv = (struct bat_priv *)arg;
93 /* for all neighbors towards this originator ... */
94 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
95 neigh_node = list_entry(list_pos, struct neigh_node, list);
101 frag_list_free(&orig_node->frag_list);
102 hna_global_del_orig(bat_priv, orig_node, "originator timed out");
104 kfree(orig_node->bcast_own);
105 kfree(orig_node->bcast_own_sum);
109 void originator_free(struct bat_priv *bat_priv)
113 if (!bat_priv->orig_hash)
116 cancel_delayed_work_sync(&bat_priv->orig_work);
118 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
119 hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv);
120 bat_priv->orig_hash = NULL;
121 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
124 /* this function finds or creates an originator entry for the given
125 * address if it does not exits */
126 struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
128 struct orig_node *orig_node;
129 struct hashtable_t *swaphash;
132 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash, addr));
137 bat_dbg(DBG_BATMAN, bat_priv,
138 "Creating new originator: %pM\n", addr);
140 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
144 INIT_LIST_HEAD(&orig_node->neigh_list);
146 memcpy(orig_node->orig, addr, ETH_ALEN);
147 orig_node->router = NULL;
148 orig_node->hna_buff = NULL;
149 orig_node->bcast_seqno_reset = jiffies - 1
150 - msecs_to_jiffies(RESET_PROTECTION_MS);
151 orig_node->batman_seqno_reset = jiffies - 1
152 - msecs_to_jiffies(RESET_PROTECTION_MS);
154 size = bat_priv->num_ifaces * sizeof(TYPE_OF_WORD) * NUM_WORDS;
156 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
157 if (!orig_node->bcast_own)
160 size = bat_priv->num_ifaces * sizeof(uint8_t);
161 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
163 INIT_LIST_HEAD(&orig_node->frag_list);
164 orig_node->last_frag_packet = 0;
166 if (!orig_node->bcast_own_sum)
169 if (hash_add(bat_priv->orig_hash, orig_node) < 0)
170 goto free_bcast_own_sum;
172 if (bat_priv->orig_hash->elements * 4 > bat_priv->orig_hash->size) {
173 swaphash = hash_resize(bat_priv->orig_hash,
174 bat_priv->orig_hash->size * 2);
177 bat_dbg(DBG_BATMAN, bat_priv,
178 "Couldn't resize orig hash table\n");
180 bat_priv->orig_hash = swaphash;
185 kfree(orig_node->bcast_own_sum);
187 kfree(orig_node->bcast_own);
193 static bool purge_orig_neighbors(struct bat_priv *bat_priv,
194 struct orig_node *orig_node,
195 struct neigh_node **best_neigh_node)
197 struct list_head *list_pos, *list_pos_tmp;
198 struct neigh_node *neigh_node;
199 bool neigh_purged = false;
201 *best_neigh_node = NULL;
203 /* for all neighbors towards this originator ... */
204 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
205 neigh_node = list_entry(list_pos, struct neigh_node, list);
207 if ((time_after(jiffies,
208 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
209 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
210 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
212 if (neigh_node->if_incoming->if_status ==
214 bat_dbg(DBG_BATMAN, bat_priv,
215 "neighbor purge: originator %pM, "
216 "neighbor: %pM, iface: %s\n",
217 orig_node->orig, neigh_node->addr,
218 neigh_node->if_incoming->net_dev->name);
220 bat_dbg(DBG_BATMAN, bat_priv,
221 "neighbor timeout: originator %pM, "
222 "neighbor: %pM, last_valid: %lu\n",
223 orig_node->orig, neigh_node->addr,
224 (neigh_node->last_valid / HZ));
230 if ((*best_neigh_node == NULL) ||
231 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
232 *best_neigh_node = neigh_node;
238 static bool purge_orig_node(struct bat_priv *bat_priv,
239 struct orig_node *orig_node)
241 struct neigh_node *best_neigh_node;
243 if (time_after(jiffies,
244 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
246 bat_dbg(DBG_BATMAN, bat_priv,
247 "Originator timeout: originator %pM, last_valid %lu\n",
248 orig_node->orig, (orig_node->last_valid / HZ));
251 if (purge_orig_neighbors(bat_priv, orig_node,
253 update_routes(bat_priv, orig_node,
256 orig_node->hna_buff_len);
257 /* update bonding candidates, we could have lost
258 * some candidates. */
259 update_bonding_candidates(bat_priv, orig_node);
266 static void _purge_orig(struct bat_priv *bat_priv)
269 struct orig_node *orig_node;
272 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
274 /* for all origins... */
275 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
276 orig_node = hashit.bucket->data;
278 if (purge_orig_node(bat_priv, orig_node)) {
279 hash_remove_bucket(bat_priv->orig_hash, &hashit);
280 free_orig_node(orig_node, bat_priv);
283 if (time_after(jiffies, (orig_node->last_frag_packet +
284 msecs_to_jiffies(FRAG_TIMEOUT))))
285 frag_list_free(&orig_node->frag_list);
288 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
290 softif_neigh_purge(bat_priv);
293 static void purge_orig(struct work_struct *work)
295 struct delayed_work *delayed_work =
296 container_of(work, struct delayed_work, work);
297 struct bat_priv *bat_priv =
298 container_of(delayed_work, struct bat_priv, orig_work);
300 _purge_orig(bat_priv);
301 start_purge_timer(bat_priv);
304 void purge_orig_ref(struct bat_priv *bat_priv)
306 _purge_orig(bat_priv);
309 int orig_seq_print_text(struct seq_file *seq, void *offset)
312 struct net_device *net_dev = (struct net_device *)seq->private;
313 struct bat_priv *bat_priv = netdev_priv(net_dev);
314 struct orig_node *orig_node;
315 struct neigh_node *neigh_node;
316 int batman_count = 0;
321 if ((!bat_priv->primary_if) ||
322 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
323 if (!bat_priv->primary_if)
324 return seq_printf(seq, "BATMAN mesh %s disabled - "
325 "please specify interfaces to enable it\n",
328 return seq_printf(seq, "BATMAN mesh %s "
329 "disabled - primary interface not active\n",
333 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
334 SOURCE_VERSION, REVISION_VERSION_STR,
335 bat_priv->primary_if->net_dev->name,
336 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
337 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
338 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
339 "outgoingIF", "Potential nexthops");
341 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
343 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
345 orig_node = hashit.bucket->data;
347 if (!orig_node->router)
350 if (orig_node->router->tq_avg == 0)
353 last_seen_secs = jiffies_to_msecs(jiffies -
354 orig_node->last_valid) / 1000;
355 last_seen_msecs = jiffies_to_msecs(jiffies -
356 orig_node->last_valid) % 1000;
358 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
359 orig_node->orig, last_seen_secs, last_seen_msecs,
360 orig_node->router->tq_avg, orig_node->router->addr,
361 orig_node->router->if_incoming->net_dev->name);
363 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
364 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
368 seq_printf(seq, "\n");
372 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
374 if ((batman_count == 0))
375 seq_printf(seq, "No batman nodes in range ...\n");
380 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
384 data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
387 pr_err("Can't resize orig: out of memory\n");
391 memcpy(data_ptr, orig_node->bcast_own,
392 (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
393 kfree(orig_node->bcast_own);
394 orig_node->bcast_own = data_ptr;
396 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
398 pr_err("Can't resize orig: out of memory\n");
402 memcpy(data_ptr, orig_node->bcast_own_sum,
403 (max_if_num - 1) * sizeof(uint8_t));
404 kfree(orig_node->bcast_own_sum);
405 orig_node->bcast_own_sum = data_ptr;
410 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
412 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
413 struct orig_node *orig_node;
417 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
419 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
421 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
422 orig_node = hashit.bucket->data;
424 if (orig_node_add_if(orig_node, max_if_num) == -1)
428 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
432 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
436 static int orig_node_del_if(struct orig_node *orig_node,
437 int max_if_num, int del_if_num)
439 void *data_ptr = NULL;
442 /* last interface was removed */
446 chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
447 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
449 pr_err("Can't resize orig: out of memory\n");
453 /* copy first part */
454 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
456 /* copy second part */
457 memcpy(data_ptr + del_if_num * chunk_size,
458 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
459 (max_if_num - del_if_num) * chunk_size);
462 kfree(orig_node->bcast_own);
463 orig_node->bcast_own = data_ptr;
468 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
470 pr_err("Can't resize orig: out of memory\n");
474 memcpy(data_ptr, orig_node->bcast_own_sum,
475 del_if_num * sizeof(uint8_t));
477 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
478 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
479 (max_if_num - del_if_num) * sizeof(uint8_t));
482 kfree(orig_node->bcast_own_sum);
483 orig_node->bcast_own_sum = data_ptr;
488 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
490 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
491 struct batman_if *batman_if_tmp;
492 struct orig_node *orig_node;
497 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
499 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
501 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
502 orig_node = hashit.bucket->data;
504 ret = orig_node_del_if(orig_node, max_if_num,
511 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
513 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
514 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
517 if (batman_if == batman_if_tmp)
520 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
523 if (batman_if_tmp->if_num > batman_if->if_num)
524 batman_if_tmp->if_num--;
528 batman_if->if_num = -1;
529 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
533 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);