1 /* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "distributed-arp-table.h"
22 #include "originator.h"
24 #include "translation-table.h"
26 #include "gateway_client.h"
27 #include "hard-interface.h"
28 #include "soft-interface.h"
29 #include "bridge_loop_avoidance.h"
30 #include "network-coding.h"
33 static struct lock_class_key batadv_orig_hash_lock_class_key;
35 static void batadv_purge_orig(struct work_struct *work);
37 /* returns 1 if they are the same originator */
38 static int batadv_compare_orig(const struct hlist_node *node, const void *data2)
40 const void *data1 = container_of(node, struct batadv_orig_node,
43 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
46 int batadv_originator_init(struct batadv_priv *bat_priv)
48 if (bat_priv->orig_hash)
51 bat_priv->orig_hash = batadv_hash_new(1024);
53 if (!bat_priv->orig_hash)
56 batadv_hash_set_lock_class(bat_priv->orig_hash,
57 &batadv_orig_hash_lock_class_key);
59 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
60 queue_delayed_work(batadv_event_workqueue,
62 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
70 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
72 if (atomic_dec_and_test(&neigh_node->refcount))
73 kfree_rcu(neigh_node, rcu);
76 /* increases the refcounter of a found router */
77 struct batadv_neigh_node *
78 batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
80 struct batadv_neigh_node *router;
83 router = rcu_dereference(orig_node->router);
85 if (router && !atomic_inc_not_zero(&router->refcount))
92 struct batadv_neigh_node *
93 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
94 const uint8_t *neigh_addr)
96 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
97 struct batadv_neigh_node *neigh_node;
99 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
103 INIT_HLIST_NODE(&neigh_node->list);
105 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
106 spin_lock_init(&neigh_node->lq_update_lock);
108 /* extra reference for return */
109 atomic_set(&neigh_node->refcount, 2);
111 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
112 "Creating new neighbor %pM on interface %s\n", neigh_addr,
113 hard_iface->net_dev->name);
119 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
121 struct hlist_node *node_tmp;
122 struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
123 struct batadv_orig_node *orig_node;
125 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
127 spin_lock_bh(&orig_node->neigh_list_lock);
129 /* for all bonding members ... */
130 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
131 &orig_node->bond_list, bonding_list) {
132 list_del_rcu(&neigh_node->bonding_list);
133 batadv_neigh_node_free_ref(neigh_node);
136 /* for all neighbors towards this originator ... */
137 hlist_for_each_entry_safe(neigh_node, node_tmp,
138 &orig_node->neigh_list, list) {
139 hlist_del_rcu(&neigh_node->list);
140 batadv_neigh_node_free_ref(neigh_node);
143 spin_unlock_bh(&orig_node->neigh_list_lock);
146 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
148 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
149 "originator timed out");
151 kfree(orig_node->tt_buff);
152 kfree(orig_node->bcast_own);
153 kfree(orig_node->bcast_own_sum);
158 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
159 * schedule an rcu callback for freeing it
160 * @orig_node: the orig node to free
162 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
164 if (atomic_dec_and_test(&orig_node->refcount))
165 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
169 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
170 * possibly free it (without rcu callback)
171 * @orig_node: the orig node to free
173 void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
175 if (atomic_dec_and_test(&orig_node->refcount))
176 batadv_orig_node_free_rcu(&orig_node->rcu);
179 void batadv_originator_free(struct batadv_priv *bat_priv)
181 struct batadv_hashtable *hash = bat_priv->orig_hash;
182 struct hlist_node *node_tmp;
183 struct hlist_head *head;
184 spinlock_t *list_lock; /* spinlock to protect write access */
185 struct batadv_orig_node *orig_node;
191 cancel_delayed_work_sync(&bat_priv->orig_work);
193 bat_priv->orig_hash = NULL;
195 for (i = 0; i < hash->size; i++) {
196 head = &hash->table[i];
197 list_lock = &hash->list_locks[i];
199 spin_lock_bh(list_lock);
200 hlist_for_each_entry_safe(orig_node, node_tmp,
202 hlist_del_rcu(&orig_node->hash_entry);
203 batadv_orig_node_free_ref(orig_node);
205 spin_unlock_bh(list_lock);
208 batadv_hash_destroy(hash);
211 /* this function finds or creates an originator entry for the given
212 * address if it does not exits
214 struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
217 struct batadv_orig_node *orig_node;
220 unsigned long reset_time;
222 orig_node = batadv_orig_hash_find(bat_priv, addr);
226 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
227 "Creating new originator: %pM\n", addr);
229 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
233 INIT_HLIST_HEAD(&orig_node->neigh_list);
234 INIT_LIST_HEAD(&orig_node->bond_list);
235 spin_lock_init(&orig_node->ogm_cnt_lock);
236 spin_lock_init(&orig_node->bcast_seqno_lock);
237 spin_lock_init(&orig_node->neigh_list_lock);
238 spin_lock_init(&orig_node->tt_buff_lock);
240 batadv_nc_init_orig(orig_node);
242 /* extra reference for return */
243 atomic_set(&orig_node->refcount, 2);
245 orig_node->tt_initialised = false;
246 orig_node->bat_priv = bat_priv;
247 memcpy(orig_node->orig, addr, ETH_ALEN);
248 batadv_dat_init_orig_node_addr(orig_node);
249 orig_node->router = NULL;
250 orig_node->tt_crc = 0;
251 atomic_set(&orig_node->last_ttvn, 0);
252 orig_node->tt_buff = NULL;
253 orig_node->tt_buff_len = 0;
254 atomic_set(&orig_node->tt_size, 0);
255 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
256 orig_node->bcast_seqno_reset = reset_time;
257 orig_node->batman_seqno_reset = reset_time;
259 atomic_set(&orig_node->bond_candidates, 0);
261 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
263 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
264 if (!orig_node->bcast_own)
267 size = bat_priv->num_ifaces * sizeof(uint8_t);
268 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
270 if (!orig_node->bcast_own_sum)
273 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
274 batadv_choose_orig, orig_node,
275 &orig_node->hash_entry);
277 goto free_bcast_own_sum;
281 kfree(orig_node->bcast_own_sum);
283 kfree(orig_node->bcast_own);
290 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
291 struct batadv_orig_node *orig_node,
292 struct batadv_neigh_node **best_neigh_node)
294 struct hlist_node *node_tmp;
295 struct batadv_neigh_node *neigh_node;
296 bool neigh_purged = false;
297 unsigned long last_seen;
298 struct batadv_hard_iface *if_incoming;
300 *best_neigh_node = NULL;
302 spin_lock_bh(&orig_node->neigh_list_lock);
304 /* for all neighbors towards this originator ... */
305 hlist_for_each_entry_safe(neigh_node, node_tmp,
306 &orig_node->neigh_list, list) {
307 last_seen = neigh_node->last_seen;
308 if_incoming = neigh_node->if_incoming;
310 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
311 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
312 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
313 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
314 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
315 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
316 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
317 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
318 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
319 orig_node->orig, neigh_node->addr,
320 if_incoming->net_dev->name);
322 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
323 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
324 orig_node->orig, neigh_node->addr,
325 jiffies_to_msecs(last_seen));
329 hlist_del_rcu(&neigh_node->list);
330 batadv_bonding_candidate_del(orig_node, neigh_node);
331 batadv_neigh_node_free_ref(neigh_node);
333 if ((!*best_neigh_node) ||
334 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
335 *best_neigh_node = neigh_node;
339 spin_unlock_bh(&orig_node->neigh_list_lock);
343 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
344 struct batadv_orig_node *orig_node)
346 struct batadv_neigh_node *best_neigh_node;
348 if (batadv_has_timed_out(orig_node->last_seen,
349 2 * BATADV_PURGE_TIMEOUT)) {
350 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
351 "Originator timeout: originator %pM, last_seen %u\n",
353 jiffies_to_msecs(orig_node->last_seen));
356 if (batadv_purge_orig_neighbors(bat_priv, orig_node,
358 batadv_update_route(bat_priv, orig_node,
365 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
367 struct batadv_hashtable *hash = bat_priv->orig_hash;
368 struct hlist_node *node_tmp;
369 struct hlist_head *head;
370 spinlock_t *list_lock; /* spinlock to protect write access */
371 struct batadv_orig_node *orig_node;
377 /* for all origins... */
378 for (i = 0; i < hash->size; i++) {
379 head = &hash->table[i];
380 list_lock = &hash->list_locks[i];
382 spin_lock_bh(list_lock);
383 hlist_for_each_entry_safe(orig_node, node_tmp,
385 if (batadv_purge_orig_node(bat_priv, orig_node)) {
386 batadv_gw_node_delete(bat_priv, orig_node);
387 hlist_del_rcu(&orig_node->hash_entry);
388 batadv_orig_node_free_ref(orig_node);
392 spin_unlock_bh(list_lock);
395 batadv_gw_node_purge(bat_priv);
396 batadv_gw_election(bat_priv);
399 static void batadv_purge_orig(struct work_struct *work)
401 struct delayed_work *delayed_work;
402 struct batadv_priv *bat_priv;
404 delayed_work = container_of(work, struct delayed_work, work);
405 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
406 _batadv_purge_orig(bat_priv);
407 queue_delayed_work(batadv_event_workqueue,
408 &bat_priv->orig_work,
409 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
412 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
414 _batadv_purge_orig(bat_priv);
417 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
419 struct net_device *net_dev = (struct net_device *)seq->private;
420 struct batadv_priv *bat_priv = netdev_priv(net_dev);
421 struct batadv_hashtable *hash = bat_priv->orig_hash;
422 struct hlist_head *head;
423 struct batadv_hard_iface *primary_if;
424 struct batadv_orig_node *orig_node;
425 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
426 int batman_count = 0;
429 unsigned long last_seen_jiffies;
432 primary_if = batadv_seq_print_text_primary_if_get(seq);
436 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
437 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
438 primary_if->net_dev->dev_addr, net_dev->name);
439 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
440 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
441 "Nexthop", "outgoingIF", "Potential nexthops");
443 for (i = 0; i < hash->size; i++) {
444 head = &hash->table[i];
447 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
448 neigh_node = batadv_orig_node_get_router(orig_node);
452 if (neigh_node->tq_avg == 0)
455 last_seen_jiffies = jiffies - orig_node->last_seen;
456 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
457 last_seen_secs = last_seen_msecs / 1000;
458 last_seen_msecs = last_seen_msecs % 1000;
460 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
461 orig_node->orig, last_seen_secs,
462 last_seen_msecs, neigh_node->tq_avg,
464 neigh_node->if_incoming->net_dev->name);
466 hlist_for_each_entry_rcu(neigh_node_tmp,
467 &orig_node->neigh_list, list) {
468 seq_printf(seq, " %pM (%3i)",
469 neigh_node_tmp->addr,
470 neigh_node_tmp->tq_avg);
477 batadv_neigh_node_free_ref(neigh_node);
482 if (batman_count == 0)
483 seq_puts(seq, "No batman nodes in range ...\n");
487 batadv_hardif_free_ref(primary_if);
491 static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
495 size_t data_size, old_size;
497 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
498 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
499 data_ptr = kmalloc(data_size, GFP_ATOMIC);
503 memcpy(data_ptr, orig_node->bcast_own, old_size);
504 kfree(orig_node->bcast_own);
505 orig_node->bcast_own = data_ptr;
507 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
511 memcpy(data_ptr, orig_node->bcast_own_sum,
512 (max_if_num - 1) * sizeof(uint8_t));
513 kfree(orig_node->bcast_own_sum);
514 orig_node->bcast_own_sum = data_ptr;
519 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
522 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
523 struct batadv_hashtable *hash = bat_priv->orig_hash;
524 struct hlist_head *head;
525 struct batadv_orig_node *orig_node;
529 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
532 for (i = 0; i < hash->size; i++) {
533 head = &hash->table[i];
536 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
537 spin_lock_bh(&orig_node->ogm_cnt_lock);
538 ret = batadv_orig_node_add_if(orig_node, max_if_num);
539 spin_unlock_bh(&orig_node->ogm_cnt_lock);
554 static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
555 int max_if_num, int del_if_num)
557 void *data_ptr = NULL;
560 /* last interface was removed */
564 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
565 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
569 /* copy first part */
570 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
572 /* copy second part */
573 memcpy((char *)data_ptr + del_if_num * chunk_size,
574 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
575 (max_if_num - del_if_num) * chunk_size);
578 kfree(orig_node->bcast_own);
579 orig_node->bcast_own = data_ptr;
584 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
588 memcpy(data_ptr, orig_node->bcast_own_sum,
589 del_if_num * sizeof(uint8_t));
591 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
592 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
593 (max_if_num - del_if_num) * sizeof(uint8_t));
596 kfree(orig_node->bcast_own_sum);
597 orig_node->bcast_own_sum = data_ptr;
602 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
605 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
606 struct batadv_hashtable *hash = bat_priv->orig_hash;
607 struct hlist_head *head;
608 struct batadv_hard_iface *hard_iface_tmp;
609 struct batadv_orig_node *orig_node;
613 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
616 for (i = 0; i < hash->size; i++) {
617 head = &hash->table[i];
620 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
621 spin_lock_bh(&orig_node->ogm_cnt_lock);
622 ret = batadv_orig_node_del_if(orig_node, max_if_num,
624 spin_unlock_bh(&orig_node->ogm_cnt_lock);
632 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
634 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
635 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
638 if (hard_iface == hard_iface_tmp)
641 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
644 if (hard_iface_tmp->if_num > hard_iface->if_num)
645 hard_iface_tmp->if_num--;
649 hard_iface->if_num = -1;