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 struct neigh_node *create_neighbor(struct orig_node *orig_node,
71 struct orig_node *orig_neigh_node,
73 struct batman_if *if_incoming)
75 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
76 struct neigh_node *neigh_node;
78 bat_dbg(DBG_BATMAN, bat_priv,
79 "Creating new last-hop neighbor of originator\n");
81 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
85 INIT_LIST_HEAD(&neigh_node->list);
87 memcpy(neigh_node->addr, neigh, ETH_ALEN);
88 neigh_node->orig_node = orig_neigh_node;
89 neigh_node->if_incoming = if_incoming;
90 kref_init(&neigh_node->refcount);
92 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
96 static void free_orig_node(void *data, void *arg)
98 struct list_head *list_pos, *list_pos_tmp;
99 struct neigh_node *neigh_node;
100 struct orig_node *orig_node = (struct orig_node *)data;
101 struct bat_priv *bat_priv = (struct bat_priv *)arg;
103 /* for all neighbors towards this originator ... */
104 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
105 neigh_node = list_entry(list_pos, struct neigh_node, list);
108 kref_put(&neigh_node->refcount, neigh_node_free_ref);
111 frag_list_free(&orig_node->frag_list);
112 hna_global_del_orig(bat_priv, orig_node, "originator timed out");
114 kfree(orig_node->bcast_own);
115 kfree(orig_node->bcast_own_sum);
119 void originator_free(struct bat_priv *bat_priv)
121 if (!bat_priv->orig_hash)
124 cancel_delayed_work_sync(&bat_priv->orig_work);
126 spin_lock_bh(&bat_priv->orig_hash_lock);
127 hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv);
128 bat_priv->orig_hash = NULL;
129 spin_unlock_bh(&bat_priv->orig_hash_lock);
132 /* this function finds or creates an originator entry for the given
133 * address if it does not exits */
134 struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
136 struct orig_node *orig_node;
140 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
141 compare_orig, choose_orig,
147 bat_dbg(DBG_BATMAN, bat_priv,
148 "Creating new originator: %pM\n", addr);
150 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
154 INIT_LIST_HEAD(&orig_node->neigh_list);
156 memcpy(orig_node->orig, addr, ETH_ALEN);
157 orig_node->router = NULL;
158 orig_node->hna_buff = NULL;
159 orig_node->bcast_seqno_reset = jiffies - 1
160 - msecs_to_jiffies(RESET_PROTECTION_MS);
161 orig_node->batman_seqno_reset = jiffies - 1
162 - msecs_to_jiffies(RESET_PROTECTION_MS);
164 size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
166 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
167 if (!orig_node->bcast_own)
170 size = bat_priv->num_ifaces * sizeof(uint8_t);
171 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
173 INIT_LIST_HEAD(&orig_node->frag_list);
174 orig_node->last_frag_packet = 0;
176 if (!orig_node->bcast_own_sum)
179 hash_added = hash_add(bat_priv->orig_hash, compare_orig, choose_orig,
182 goto free_bcast_own_sum;
186 kfree(orig_node->bcast_own_sum);
188 kfree(orig_node->bcast_own);
194 static bool purge_orig_neighbors(struct bat_priv *bat_priv,
195 struct orig_node *orig_node,
196 struct neigh_node **best_neigh_node)
198 struct list_head *list_pos, *list_pos_tmp;
199 struct neigh_node *neigh_node;
200 bool neigh_purged = false;
202 *best_neigh_node = NULL;
204 /* for all neighbors towards this originator ... */
205 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
206 neigh_node = list_entry(list_pos, struct neigh_node, list);
208 if ((time_after(jiffies,
209 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
210 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
211 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
213 if (neigh_node->if_incoming->if_status ==
215 bat_dbg(DBG_BATMAN, bat_priv,
216 "neighbor purge: originator %pM, "
217 "neighbor: %pM, iface: %s\n",
218 orig_node->orig, neigh_node->addr,
219 neigh_node->if_incoming->net_dev->name);
221 bat_dbg(DBG_BATMAN, bat_priv,
222 "neighbor timeout: originator %pM, "
223 "neighbor: %pM, last_valid: %lu\n",
224 orig_node->orig, neigh_node->addr,
225 (neigh_node->last_valid / HZ));
229 kref_put(&neigh_node->refcount, neigh_node_free_ref);
231 if ((!*best_neigh_node) ||
232 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
233 *best_neigh_node = neigh_node;
239 static bool purge_orig_node(struct bat_priv *bat_priv,
240 struct orig_node *orig_node)
242 struct neigh_node *best_neigh_node;
244 if (time_after(jiffies,
245 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
247 bat_dbg(DBG_BATMAN, bat_priv,
248 "Originator timeout: originator %pM, last_valid %lu\n",
249 orig_node->orig, (orig_node->last_valid / HZ));
252 if (purge_orig_neighbors(bat_priv, orig_node,
254 update_routes(bat_priv, orig_node,
257 orig_node->hna_buff_len);
258 /* update bonding candidates, we could have lost
259 * some candidates. */
260 update_bonding_candidates(orig_node);
267 static void _purge_orig(struct bat_priv *bat_priv)
269 struct hashtable_t *hash = bat_priv->orig_hash;
270 struct hlist_node *walk, *safe;
271 struct hlist_head *head;
272 struct element_t *bucket;
273 struct orig_node *orig_node;
279 spin_lock_bh(&bat_priv->orig_hash_lock);
281 /* for all origins... */
282 for (i = 0; i < hash->size; i++) {
283 head = &hash->table[i];
285 hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
286 orig_node = bucket->data;
288 if (purge_orig_node(bat_priv, orig_node)) {
289 if (orig_node->gw_flags)
290 gw_node_delete(bat_priv, orig_node);
293 free_orig_node(orig_node, bat_priv);
296 if (time_after(jiffies, orig_node->last_frag_packet +
297 msecs_to_jiffies(FRAG_TIMEOUT)))
298 frag_list_free(&orig_node->frag_list);
302 spin_unlock_bh(&bat_priv->orig_hash_lock);
304 gw_node_purge(bat_priv);
305 gw_election(bat_priv);
307 softif_neigh_purge(bat_priv);
310 static void purge_orig(struct work_struct *work)
312 struct delayed_work *delayed_work =
313 container_of(work, struct delayed_work, work);
314 struct bat_priv *bat_priv =
315 container_of(delayed_work, struct bat_priv, orig_work);
317 _purge_orig(bat_priv);
318 start_purge_timer(bat_priv);
321 void purge_orig_ref(struct bat_priv *bat_priv)
323 _purge_orig(bat_priv);
326 int orig_seq_print_text(struct seq_file *seq, void *offset)
328 struct net_device *net_dev = (struct net_device *)seq->private;
329 struct bat_priv *bat_priv = netdev_priv(net_dev);
330 struct hashtable_t *hash = bat_priv->orig_hash;
331 struct hlist_node *walk;
332 struct hlist_head *head;
333 struct element_t *bucket;
334 struct orig_node *orig_node;
335 struct neigh_node *neigh_node;
336 int batman_count = 0;
341 if ((!bat_priv->primary_if) ||
342 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
343 if (!bat_priv->primary_if)
344 return seq_printf(seq, "BATMAN mesh %s disabled - "
345 "please specify interfaces to enable it\n",
348 return seq_printf(seq, "BATMAN mesh %s "
349 "disabled - primary interface not active\n",
353 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
354 SOURCE_VERSION, REVISION_VERSION_STR,
355 bat_priv->primary_if->net_dev->name,
356 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
357 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
358 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
359 "outgoingIF", "Potential nexthops");
361 spin_lock_bh(&bat_priv->orig_hash_lock);
363 for (i = 0; i < hash->size; i++) {
364 head = &hash->table[i];
366 hlist_for_each_entry(bucket, walk, head, hlist) {
367 orig_node = bucket->data;
369 if (!orig_node->router)
372 if (orig_node->router->tq_avg == 0)
375 last_seen_secs = jiffies_to_msecs(jiffies -
376 orig_node->last_valid) / 1000;
377 last_seen_msecs = jiffies_to_msecs(jiffies -
378 orig_node->last_valid) % 1000;
380 neigh_node = orig_node->router;
381 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
382 orig_node->orig, last_seen_secs,
383 last_seen_msecs, neigh_node->tq_avg,
385 neigh_node->if_incoming->net_dev->name);
387 list_for_each_entry(neigh_node, &orig_node->neigh_list,
389 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
393 seq_printf(seq, "\n");
398 spin_unlock_bh(&bat_priv->orig_hash_lock);
400 if ((batman_count == 0))
401 seq_printf(seq, "No batman nodes in range ...\n");
406 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
410 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
413 pr_err("Can't resize orig: out of memory\n");
417 memcpy(data_ptr, orig_node->bcast_own,
418 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
419 kfree(orig_node->bcast_own);
420 orig_node->bcast_own = data_ptr;
422 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
424 pr_err("Can't resize orig: out of memory\n");
428 memcpy(data_ptr, orig_node->bcast_own_sum,
429 (max_if_num - 1) * sizeof(uint8_t));
430 kfree(orig_node->bcast_own_sum);
431 orig_node->bcast_own_sum = data_ptr;
436 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
438 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
439 struct hashtable_t *hash = bat_priv->orig_hash;
440 struct hlist_node *walk;
441 struct hlist_head *head;
442 struct element_t *bucket;
443 struct orig_node *orig_node;
446 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
448 spin_lock_bh(&bat_priv->orig_hash_lock);
450 for (i = 0; i < hash->size; i++) {
451 head = &hash->table[i];
453 hlist_for_each_entry(bucket, walk, head, hlist) {
454 orig_node = bucket->data;
456 if (orig_node_add_if(orig_node, max_if_num) == -1)
461 spin_unlock_bh(&bat_priv->orig_hash_lock);
465 spin_unlock_bh(&bat_priv->orig_hash_lock);
469 static int orig_node_del_if(struct orig_node *orig_node,
470 int max_if_num, int del_if_num)
472 void *data_ptr = NULL;
475 /* last interface was removed */
479 chunk_size = sizeof(unsigned long) * NUM_WORDS;
480 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
482 pr_err("Can't resize orig: out of memory\n");
486 /* copy first part */
487 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
489 /* copy second part */
490 memcpy(data_ptr + del_if_num * chunk_size,
491 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
492 (max_if_num - del_if_num) * chunk_size);
495 kfree(orig_node->bcast_own);
496 orig_node->bcast_own = data_ptr;
501 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
503 pr_err("Can't resize orig: out of memory\n");
507 memcpy(data_ptr, orig_node->bcast_own_sum,
508 del_if_num * sizeof(uint8_t));
510 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
511 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
512 (max_if_num - del_if_num) * sizeof(uint8_t));
515 kfree(orig_node->bcast_own_sum);
516 orig_node->bcast_own_sum = data_ptr;
521 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
523 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
524 struct hashtable_t *hash = bat_priv->orig_hash;
525 struct hlist_node *walk;
526 struct hlist_head *head;
527 struct element_t *bucket;
528 struct batman_if *batman_if_tmp;
529 struct orig_node *orig_node;
532 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
534 spin_lock_bh(&bat_priv->orig_hash_lock);
536 for (i = 0; i < hash->size; i++) {
537 head = &hash->table[i];
539 hlist_for_each_entry(bucket, walk, head, hlist) {
540 orig_node = bucket->data;
542 ret = orig_node_del_if(orig_node, max_if_num,
550 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
552 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
553 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
556 if (batman_if == batman_if_tmp)
559 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
562 if (batman_if_tmp->if_num > batman_if->if_num)
563 batman_if_tmp->if_num--;
567 batman_if->if_num = -1;
568 spin_unlock_bh(&bat_priv->orig_hash_lock);
572 spin_unlock_bh(&bat_priv->orig_hash_lock);