2 * Copyright (C) 2009-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
22 /* increase the reference counter for this originator */
25 #include "originator.h"
27 #include "translation-table.h"
29 #include "gateway_client.h"
30 #include "hard-interface.h"
32 #include "soft-interface.h"
34 static void purge_orig(struct work_struct *work);
36 static void start_purge_timer(struct bat_priv *bat_priv)
38 INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
39 queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
42 int originator_init(struct bat_priv *bat_priv)
44 if (bat_priv->orig_hash)
47 spin_lock_bh(&bat_priv->orig_hash_lock);
48 bat_priv->orig_hash = hash_new(1024);
50 if (!bat_priv->orig_hash)
53 spin_unlock_bh(&bat_priv->orig_hash_lock);
54 start_purge_timer(bat_priv);
58 spin_unlock_bh(&bat_priv->orig_hash_lock);
62 static void neigh_node_free_rcu(struct rcu_head *rcu)
64 struct neigh_node *neigh_node;
66 neigh_node = container_of(rcu, struct neigh_node, rcu);
70 void neigh_node_free_ref(struct neigh_node *neigh_node)
72 if (atomic_dec_and_test(&neigh_node->refcount))
73 call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
76 struct neigh_node *create_neighbor(struct orig_node *orig_node,
77 struct orig_node *orig_neigh_node,
79 struct batman_if *if_incoming)
81 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
82 struct neigh_node *neigh_node;
84 bat_dbg(DBG_BATMAN, bat_priv,
85 "Creating new last-hop neighbor of originator\n");
87 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
91 INIT_HLIST_NODE(&neigh_node->list);
92 INIT_LIST_HEAD(&neigh_node->bonding_list);
94 memcpy(neigh_node->addr, neigh, ETH_ALEN);
95 neigh_node->orig_node = orig_neigh_node;
96 neigh_node->if_incoming = if_incoming;
97 atomic_set(&neigh_node->refcount, 1);
99 spin_lock_bh(&orig_node->neigh_list_lock);
100 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
101 spin_unlock_bh(&orig_node->neigh_list_lock);
105 static void orig_node_free_rcu(struct rcu_head *rcu)
107 struct hlist_node *node, *node_tmp;
108 struct neigh_node *neigh_node, *tmp_neigh_node;
109 struct orig_node *orig_node;
111 orig_node = container_of(rcu, struct orig_node, rcu);
113 spin_lock_bh(&orig_node->neigh_list_lock);
115 /* for all bonding members ... */
116 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
117 &orig_node->bond_list, bonding_list) {
118 list_del_rcu(&neigh_node->bonding_list);
119 neigh_node_free_ref(neigh_node);
122 /* for all neighbors towards this originator ... */
123 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
124 &orig_node->neigh_list, list) {
125 hlist_del_rcu(&neigh_node->list);
126 neigh_node_free_ref(neigh_node);
129 spin_unlock_bh(&orig_node->neigh_list_lock);
131 frag_list_free(&orig_node->frag_list);
132 hna_global_del_orig(orig_node->bat_priv, orig_node,
133 "originator timed out");
135 kfree(orig_node->bcast_own);
136 kfree(orig_node->bcast_own_sum);
140 void orig_node_free_ref(struct orig_node *orig_node)
142 if (atomic_dec_and_test(&orig_node->refcount))
143 call_rcu(&orig_node->rcu, orig_node_free_rcu);
146 void originator_free(struct bat_priv *bat_priv)
148 struct hashtable_t *hash = bat_priv->orig_hash;
149 struct hlist_node *node, *node_tmp;
150 struct hlist_head *head;
151 spinlock_t *list_lock; /* spinlock to protect write access */
152 struct orig_node *orig_node;
158 cancel_delayed_work_sync(&bat_priv->orig_work);
160 spin_lock_bh(&bat_priv->orig_hash_lock);
161 bat_priv->orig_hash = NULL;
163 for (i = 0; i < hash->size; i++) {
164 head = &hash->table[i];
165 list_lock = &hash->list_locks[i];
167 spin_lock_bh(list_lock);
168 hlist_for_each_entry_safe(orig_node, node, node_tmp,
172 orig_node_free_ref(orig_node);
174 spin_unlock_bh(list_lock);
178 spin_unlock_bh(&bat_priv->orig_hash_lock);
181 /* this function finds or creates an originator entry for the given
182 * address if it does not exits */
183 struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
185 struct orig_node *orig_node;
189 orig_node = orig_hash_find(bat_priv, addr);
193 bat_dbg(DBG_BATMAN, bat_priv,
194 "Creating new originator: %pM\n", addr);
196 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
200 INIT_HLIST_HEAD(&orig_node->neigh_list);
201 INIT_LIST_HEAD(&orig_node->bond_list);
202 spin_lock_init(&orig_node->ogm_cnt_lock);
203 spin_lock_init(&orig_node->bcast_seqno_lock);
204 spin_lock_init(&orig_node->neigh_list_lock);
206 /* extra reference for return */
207 atomic_set(&orig_node->refcount, 2);
209 orig_node->bat_priv = bat_priv;
210 memcpy(orig_node->orig, addr, ETH_ALEN);
211 orig_node->router = NULL;
212 orig_node->hna_buff = NULL;
213 orig_node->bcast_seqno_reset = jiffies - 1
214 - msecs_to_jiffies(RESET_PROTECTION_MS);
215 orig_node->batman_seqno_reset = jiffies - 1
216 - msecs_to_jiffies(RESET_PROTECTION_MS);
218 atomic_set(&orig_node->bond_candidates, 0);
220 size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
222 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
223 if (!orig_node->bcast_own)
226 size = bat_priv->num_ifaces * sizeof(uint8_t);
227 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
229 INIT_LIST_HEAD(&orig_node->frag_list);
230 orig_node->last_frag_packet = 0;
232 if (!orig_node->bcast_own_sum)
235 hash_added = hash_add(bat_priv->orig_hash, compare_orig,
236 choose_orig, orig_node, &orig_node->hash_entry);
238 goto free_bcast_own_sum;
242 kfree(orig_node->bcast_own_sum);
244 kfree(orig_node->bcast_own);
250 static bool purge_orig_neighbors(struct bat_priv *bat_priv,
251 struct orig_node *orig_node,
252 struct neigh_node **best_neigh_node)
254 struct hlist_node *node, *node_tmp;
255 struct neigh_node *neigh_node;
256 bool neigh_purged = false;
258 *best_neigh_node = NULL;
260 spin_lock_bh(&orig_node->neigh_list_lock);
262 /* for all neighbors towards this originator ... */
263 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
264 &orig_node->neigh_list, list) {
266 if ((time_after(jiffies,
267 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
268 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
269 (neigh_node->if_incoming->if_status == IF_NOT_IN_USE) ||
270 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
272 if ((neigh_node->if_incoming->if_status ==
274 (neigh_node->if_incoming->if_status ==
276 (neigh_node->if_incoming->if_status ==
278 bat_dbg(DBG_BATMAN, bat_priv,
279 "neighbor purge: originator %pM, "
280 "neighbor: %pM, iface: %s\n",
281 orig_node->orig, neigh_node->addr,
282 neigh_node->if_incoming->net_dev->name);
284 bat_dbg(DBG_BATMAN, bat_priv,
285 "neighbor timeout: originator %pM, "
286 "neighbor: %pM, last_valid: %lu\n",
287 orig_node->orig, neigh_node->addr,
288 (neigh_node->last_valid / HZ));
292 hlist_del_rcu(&neigh_node->list);
293 bonding_candidate_del(orig_node, neigh_node);
294 neigh_node_free_ref(neigh_node);
296 if ((!*best_neigh_node) ||
297 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
298 *best_neigh_node = neigh_node;
302 spin_unlock_bh(&orig_node->neigh_list_lock);
306 static bool purge_orig_node(struct bat_priv *bat_priv,
307 struct orig_node *orig_node)
309 struct neigh_node *best_neigh_node;
311 if (time_after(jiffies,
312 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
314 bat_dbg(DBG_BATMAN, bat_priv,
315 "Originator timeout: originator %pM, last_valid %lu\n",
316 orig_node->orig, (orig_node->last_valid / HZ));
319 if (purge_orig_neighbors(bat_priv, orig_node,
321 update_routes(bat_priv, orig_node,
324 orig_node->hna_buff_len);
331 static void _purge_orig(struct bat_priv *bat_priv)
333 struct hashtable_t *hash = bat_priv->orig_hash;
334 struct hlist_node *node, *node_tmp;
335 struct hlist_head *head;
336 spinlock_t *list_lock; /* spinlock to protect write access */
337 struct orig_node *orig_node;
343 spin_lock_bh(&bat_priv->orig_hash_lock);
345 /* for all origins... */
346 for (i = 0; i < hash->size; i++) {
347 head = &hash->table[i];
348 list_lock = &hash->list_locks[i];
350 spin_lock_bh(list_lock);
351 hlist_for_each_entry_safe(orig_node, node, node_tmp,
353 if (purge_orig_node(bat_priv, orig_node)) {
354 if (orig_node->gw_flags)
355 gw_node_delete(bat_priv, orig_node);
357 orig_node_free_ref(orig_node);
361 if (time_after(jiffies, orig_node->last_frag_packet +
362 msecs_to_jiffies(FRAG_TIMEOUT)))
363 frag_list_free(&orig_node->frag_list);
365 spin_unlock_bh(list_lock);
368 spin_unlock_bh(&bat_priv->orig_hash_lock);
370 gw_node_purge(bat_priv);
371 gw_election(bat_priv);
373 softif_neigh_purge(bat_priv);
376 static void purge_orig(struct work_struct *work)
378 struct delayed_work *delayed_work =
379 container_of(work, struct delayed_work, work);
380 struct bat_priv *bat_priv =
381 container_of(delayed_work, struct bat_priv, orig_work);
383 _purge_orig(bat_priv);
384 start_purge_timer(bat_priv);
387 void purge_orig_ref(struct bat_priv *bat_priv)
389 _purge_orig(bat_priv);
392 int orig_seq_print_text(struct seq_file *seq, void *offset)
394 struct net_device *net_dev = (struct net_device *)seq->private;
395 struct bat_priv *bat_priv = netdev_priv(net_dev);
396 struct hashtable_t *hash = bat_priv->orig_hash;
397 struct hlist_node *node, *node_tmp;
398 struct hlist_head *head;
399 struct orig_node *orig_node;
400 struct neigh_node *neigh_node;
401 int batman_count = 0;
406 if ((!bat_priv->primary_if) ||
407 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
408 if (!bat_priv->primary_if)
409 return seq_printf(seq, "BATMAN mesh %s disabled - "
410 "please specify interfaces to enable it\n",
413 return seq_printf(seq, "BATMAN mesh %s "
414 "disabled - primary interface not active\n",
418 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
419 SOURCE_VERSION, REVISION_VERSION_STR,
420 bat_priv->primary_if->net_dev->name,
421 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
422 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
423 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
424 "outgoingIF", "Potential nexthops");
426 spin_lock_bh(&bat_priv->orig_hash_lock);
428 for (i = 0; i < hash->size; i++) {
429 head = &hash->table[i];
432 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
433 if (!orig_node->router)
436 if (orig_node->router->tq_avg == 0)
439 last_seen_secs = jiffies_to_msecs(jiffies -
440 orig_node->last_valid) / 1000;
441 last_seen_msecs = jiffies_to_msecs(jiffies -
442 orig_node->last_valid) % 1000;
444 neigh_node = orig_node->router;
445 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
446 orig_node->orig, last_seen_secs,
447 last_seen_msecs, neigh_node->tq_avg,
449 neigh_node->if_incoming->net_dev->name);
451 hlist_for_each_entry_rcu(neigh_node, node_tmp,
452 &orig_node->neigh_list, list) {
453 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
457 seq_printf(seq, "\n");
463 spin_unlock_bh(&bat_priv->orig_hash_lock);
465 if ((batman_count == 0))
466 seq_printf(seq, "No batman nodes in range ...\n");
471 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
475 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
478 pr_err("Can't resize orig: out of memory\n");
482 memcpy(data_ptr, orig_node->bcast_own,
483 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
484 kfree(orig_node->bcast_own);
485 orig_node->bcast_own = data_ptr;
487 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
489 pr_err("Can't resize orig: out of memory\n");
493 memcpy(data_ptr, orig_node->bcast_own_sum,
494 (max_if_num - 1) * sizeof(uint8_t));
495 kfree(orig_node->bcast_own_sum);
496 orig_node->bcast_own_sum = data_ptr;
501 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
503 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
504 struct hashtable_t *hash = bat_priv->orig_hash;
505 struct hlist_node *node;
506 struct hlist_head *head;
507 struct orig_node *orig_node;
510 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
512 spin_lock_bh(&bat_priv->orig_hash_lock);
514 for (i = 0; i < hash->size; i++) {
515 head = &hash->table[i];
518 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
519 spin_lock_bh(&orig_node->ogm_cnt_lock);
520 ret = orig_node_add_if(orig_node, max_if_num);
521 spin_unlock_bh(&orig_node->ogm_cnt_lock);
529 spin_unlock_bh(&bat_priv->orig_hash_lock);
534 spin_unlock_bh(&bat_priv->orig_hash_lock);
538 static int orig_node_del_if(struct orig_node *orig_node,
539 int max_if_num, int del_if_num)
541 void *data_ptr = NULL;
544 /* last interface was removed */
548 chunk_size = sizeof(unsigned long) * NUM_WORDS;
549 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
551 pr_err("Can't resize orig: out of memory\n");
555 /* copy first part */
556 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
558 /* copy second part */
559 memcpy(data_ptr + del_if_num * chunk_size,
560 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
561 (max_if_num - del_if_num) * chunk_size);
564 kfree(orig_node->bcast_own);
565 orig_node->bcast_own = data_ptr;
570 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
572 pr_err("Can't resize orig: out of memory\n");
576 memcpy(data_ptr, orig_node->bcast_own_sum,
577 del_if_num * sizeof(uint8_t));
579 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
580 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
581 (max_if_num - del_if_num) * sizeof(uint8_t));
584 kfree(orig_node->bcast_own_sum);
585 orig_node->bcast_own_sum = data_ptr;
590 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
592 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
593 struct hashtable_t *hash = bat_priv->orig_hash;
594 struct hlist_node *node;
595 struct hlist_head *head;
596 struct batman_if *batman_if_tmp;
597 struct orig_node *orig_node;
600 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
602 spin_lock_bh(&bat_priv->orig_hash_lock);
604 for (i = 0; i < hash->size; i++) {
605 head = &hash->table[i];
608 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
609 spin_lock_bh(&orig_node->ogm_cnt_lock);
610 ret = orig_node_del_if(orig_node, max_if_num,
612 spin_unlock_bh(&orig_node->ogm_cnt_lock);
620 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
622 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
623 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
626 if (batman_if == batman_if_tmp)
629 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
632 if (batman_if_tmp->if_num > batman_if->if_num)
633 batman_if_tmp->if_num--;
637 batman_if->if_num = -1;
638 spin_unlock_bh(&bat_priv->orig_hash_lock);
643 spin_unlock_bh(&bat_priv->orig_hash_lock);