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 void neigh_node_free_ref(struct kref *refcount)
64 struct neigh_node *neigh_node;
66 neigh_node = container_of(refcount, struct neigh_node, refcount);
70 static void neigh_node_free_rcu(struct rcu_head *rcu)
72 struct neigh_node *neigh_node;
74 neigh_node = container_of(rcu, struct neigh_node, rcu);
75 kref_put(&neigh_node->refcount, neigh_node_free_ref);
78 struct neigh_node *create_neighbor(struct orig_node *orig_node,
79 struct orig_node *orig_neigh_node,
81 struct batman_if *if_incoming)
83 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
84 struct neigh_node *neigh_node;
86 bat_dbg(DBG_BATMAN, bat_priv,
87 "Creating new last-hop neighbor of originator\n");
89 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
93 INIT_HLIST_NODE(&neigh_node->list);
95 memcpy(neigh_node->addr, neigh, ETH_ALEN);
96 neigh_node->orig_node = orig_neigh_node;
97 neigh_node->if_incoming = if_incoming;
98 kref_init(&neigh_node->refcount);
100 spin_lock_bh(&orig_node->neigh_list_lock);
101 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
102 spin_unlock_bh(&orig_node->neigh_list_lock);
106 static void free_orig_node(void *data, void *arg)
108 struct hlist_node *node, *node_tmp;
109 struct neigh_node *neigh_node;
110 struct orig_node *orig_node = (struct orig_node *)data;
111 struct bat_priv *bat_priv = (struct bat_priv *)arg;
113 spin_lock_bh(&orig_node->neigh_list_lock);
115 /* for all neighbors towards this originator ... */
116 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
117 &orig_node->neigh_list, list) {
118 hlist_del_rcu(&neigh_node->list);
119 call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
122 spin_unlock_bh(&orig_node->neigh_list_lock);
124 frag_list_free(&orig_node->frag_list);
125 hna_global_del_orig(bat_priv, orig_node, "originator timed out");
127 kfree(orig_node->bcast_own);
128 kfree(orig_node->bcast_own_sum);
132 void originator_free(struct bat_priv *bat_priv)
134 if (!bat_priv->orig_hash)
137 cancel_delayed_work_sync(&bat_priv->orig_work);
139 spin_lock_bh(&bat_priv->orig_hash_lock);
140 hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv);
141 bat_priv->orig_hash = NULL;
142 spin_unlock_bh(&bat_priv->orig_hash_lock);
145 /* this function finds or creates an originator entry for the given
146 * address if it does not exits */
147 struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
149 struct orig_node *orig_node;
153 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
154 compare_orig, choose_orig,
160 bat_dbg(DBG_BATMAN, bat_priv,
161 "Creating new originator: %pM\n", addr);
163 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
167 INIT_HLIST_HEAD(&orig_node->neigh_list);
168 spin_lock_init(&orig_node->neigh_list_lock);
170 memcpy(orig_node->orig, addr, ETH_ALEN);
171 orig_node->router = NULL;
172 orig_node->hna_buff = NULL;
173 orig_node->bcast_seqno_reset = jiffies - 1
174 - msecs_to_jiffies(RESET_PROTECTION_MS);
175 orig_node->batman_seqno_reset = jiffies - 1
176 - msecs_to_jiffies(RESET_PROTECTION_MS);
178 size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
180 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
181 if (!orig_node->bcast_own)
184 size = bat_priv->num_ifaces * sizeof(uint8_t);
185 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
187 INIT_LIST_HEAD(&orig_node->frag_list);
188 orig_node->last_frag_packet = 0;
190 if (!orig_node->bcast_own_sum)
193 hash_added = hash_add(bat_priv->orig_hash, compare_orig, choose_orig,
196 goto free_bcast_own_sum;
200 kfree(orig_node->bcast_own_sum);
202 kfree(orig_node->bcast_own);
208 static bool purge_orig_neighbors(struct bat_priv *bat_priv,
209 struct orig_node *orig_node,
210 struct neigh_node **best_neigh_node)
212 struct hlist_node *node, *node_tmp;
213 struct neigh_node *neigh_node;
214 bool neigh_purged = false;
216 *best_neigh_node = NULL;
218 spin_lock_bh(&orig_node->neigh_list_lock);
220 /* for all neighbors towards this originator ... */
221 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
222 &orig_node->neigh_list, list) {
224 if ((time_after(jiffies,
225 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
226 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
227 (neigh_node->if_incoming->if_status == IF_NOT_IN_USE) ||
228 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
230 if ((neigh_node->if_incoming->if_status ==
232 (neigh_node->if_incoming->if_status ==
234 (neigh_node->if_incoming->if_status ==
236 bat_dbg(DBG_BATMAN, bat_priv,
237 "neighbor purge: originator %pM, "
238 "neighbor: %pM, iface: %s\n",
239 orig_node->orig, neigh_node->addr,
240 neigh_node->if_incoming->net_dev->name);
242 bat_dbg(DBG_BATMAN, bat_priv,
243 "neighbor timeout: originator %pM, "
244 "neighbor: %pM, last_valid: %lu\n",
245 orig_node->orig, neigh_node->addr,
246 (neigh_node->last_valid / HZ));
250 hlist_del_rcu(&neigh_node->list);
251 call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
253 if ((!*best_neigh_node) ||
254 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
255 *best_neigh_node = neigh_node;
259 spin_unlock_bh(&orig_node->neigh_list_lock);
263 static bool purge_orig_node(struct bat_priv *bat_priv,
264 struct orig_node *orig_node)
266 struct neigh_node *best_neigh_node;
268 if (time_after(jiffies,
269 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
271 bat_dbg(DBG_BATMAN, bat_priv,
272 "Originator timeout: originator %pM, last_valid %lu\n",
273 orig_node->orig, (orig_node->last_valid / HZ));
276 if (purge_orig_neighbors(bat_priv, orig_node,
278 update_routes(bat_priv, orig_node,
281 orig_node->hna_buff_len);
282 /* update bonding candidates, we could have lost
283 * some candidates. */
284 update_bonding_candidates(orig_node);
291 static void _purge_orig(struct bat_priv *bat_priv)
293 struct hashtable_t *hash = bat_priv->orig_hash;
294 struct hlist_node *walk, *safe;
295 struct hlist_head *head;
296 struct element_t *bucket;
297 struct orig_node *orig_node;
303 spin_lock_bh(&bat_priv->orig_hash_lock);
305 /* for all origins... */
306 for (i = 0; i < hash->size; i++) {
307 head = &hash->table[i];
309 hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
310 orig_node = bucket->data;
312 if (purge_orig_node(bat_priv, orig_node)) {
313 if (orig_node->gw_flags)
314 gw_node_delete(bat_priv, orig_node);
317 free_orig_node(orig_node, bat_priv);
320 if (time_after(jiffies, orig_node->last_frag_packet +
321 msecs_to_jiffies(FRAG_TIMEOUT)))
322 frag_list_free(&orig_node->frag_list);
326 spin_unlock_bh(&bat_priv->orig_hash_lock);
328 gw_node_purge(bat_priv);
329 gw_election(bat_priv);
331 softif_neigh_purge(bat_priv);
334 static void purge_orig(struct work_struct *work)
336 struct delayed_work *delayed_work =
337 container_of(work, struct delayed_work, work);
338 struct bat_priv *bat_priv =
339 container_of(delayed_work, struct bat_priv, orig_work);
341 _purge_orig(bat_priv);
342 start_purge_timer(bat_priv);
345 void purge_orig_ref(struct bat_priv *bat_priv)
347 _purge_orig(bat_priv);
350 int orig_seq_print_text(struct seq_file *seq, void *offset)
352 struct net_device *net_dev = (struct net_device *)seq->private;
353 struct bat_priv *bat_priv = netdev_priv(net_dev);
354 struct hashtable_t *hash = bat_priv->orig_hash;
355 struct hlist_node *walk, *node;
356 struct hlist_head *head;
357 struct element_t *bucket;
358 struct orig_node *orig_node;
359 struct neigh_node *neigh_node;
360 int batman_count = 0;
365 if ((!bat_priv->primary_if) ||
366 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
367 if (!bat_priv->primary_if)
368 return seq_printf(seq, "BATMAN mesh %s disabled - "
369 "please specify interfaces to enable it\n",
372 return seq_printf(seq, "BATMAN mesh %s "
373 "disabled - primary interface not active\n",
377 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
378 SOURCE_VERSION, REVISION_VERSION_STR,
379 bat_priv->primary_if->net_dev->name,
380 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
381 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
382 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
383 "outgoingIF", "Potential nexthops");
385 spin_lock_bh(&bat_priv->orig_hash_lock);
387 for (i = 0; i < hash->size; i++) {
388 head = &hash->table[i];
390 hlist_for_each_entry(bucket, walk, head, hlist) {
391 orig_node = bucket->data;
393 if (!orig_node->router)
396 if (orig_node->router->tq_avg == 0)
399 last_seen_secs = jiffies_to_msecs(jiffies -
400 orig_node->last_valid) / 1000;
401 last_seen_msecs = jiffies_to_msecs(jiffies -
402 orig_node->last_valid) % 1000;
404 neigh_node = orig_node->router;
405 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
406 orig_node->orig, last_seen_secs,
407 last_seen_msecs, neigh_node->tq_avg,
409 neigh_node->if_incoming->net_dev->name);
412 hlist_for_each_entry_rcu(neigh_node, node,
413 &orig_node->neigh_list, list) {
414 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
419 seq_printf(seq, "\n");
424 spin_unlock_bh(&bat_priv->orig_hash_lock);
426 if ((batman_count == 0))
427 seq_printf(seq, "No batman nodes in range ...\n");
432 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
436 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
439 pr_err("Can't resize orig: out of memory\n");
443 memcpy(data_ptr, orig_node->bcast_own,
444 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
445 kfree(orig_node->bcast_own);
446 orig_node->bcast_own = data_ptr;
448 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
450 pr_err("Can't resize orig: out of memory\n");
454 memcpy(data_ptr, orig_node->bcast_own_sum,
455 (max_if_num - 1) * sizeof(uint8_t));
456 kfree(orig_node->bcast_own_sum);
457 orig_node->bcast_own_sum = data_ptr;
462 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
464 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
465 struct hashtable_t *hash = bat_priv->orig_hash;
466 struct hlist_node *walk;
467 struct hlist_head *head;
468 struct element_t *bucket;
469 struct orig_node *orig_node;
472 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
474 spin_lock_bh(&bat_priv->orig_hash_lock);
476 for (i = 0; i < hash->size; i++) {
477 head = &hash->table[i];
479 hlist_for_each_entry(bucket, walk, head, hlist) {
480 orig_node = bucket->data;
482 if (orig_node_add_if(orig_node, max_if_num) == -1)
487 spin_unlock_bh(&bat_priv->orig_hash_lock);
491 spin_unlock_bh(&bat_priv->orig_hash_lock);
495 static int orig_node_del_if(struct orig_node *orig_node,
496 int max_if_num, int del_if_num)
498 void *data_ptr = NULL;
501 /* last interface was removed */
505 chunk_size = sizeof(unsigned long) * NUM_WORDS;
506 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
508 pr_err("Can't resize orig: out of memory\n");
512 /* copy first part */
513 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
515 /* copy second part */
516 memcpy(data_ptr + del_if_num * chunk_size,
517 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
518 (max_if_num - del_if_num) * chunk_size);
521 kfree(orig_node->bcast_own);
522 orig_node->bcast_own = data_ptr;
527 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
529 pr_err("Can't resize orig: out of memory\n");
533 memcpy(data_ptr, orig_node->bcast_own_sum,
534 del_if_num * sizeof(uint8_t));
536 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
537 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
538 (max_if_num - del_if_num) * sizeof(uint8_t));
541 kfree(orig_node->bcast_own_sum);
542 orig_node->bcast_own_sum = data_ptr;
547 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
549 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
550 struct hashtable_t *hash = bat_priv->orig_hash;
551 struct hlist_node *walk;
552 struct hlist_head *head;
553 struct element_t *bucket;
554 struct batman_if *batman_if_tmp;
555 struct orig_node *orig_node;
558 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
560 spin_lock_bh(&bat_priv->orig_hash_lock);
562 for (i = 0; i < hash->size; i++) {
563 head = &hash->table[i];
565 hlist_for_each_entry(bucket, walk, head, hlist) {
566 orig_node = bucket->data;
568 ret = orig_node_del_if(orig_node, max_if_num,
576 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
578 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
579 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
582 if (batman_if == batman_if_tmp)
585 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
588 if (batman_if_tmp->if_num > batman_if->if_num)
589 batman_if_tmp->if_num--;
593 batman_if->if_num = -1;
594 spin_unlock_bh(&bat_priv->orig_hash_lock);
598 spin_unlock_bh(&bat_priv->orig_hash_lock);