1 /* Copyright (C) 2009-2012 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"
29 #include "soft-interface.h"
30 #include "bridge_loop_avoidance.h"
33 static struct lock_class_key batadv_orig_hash_lock_class_key;
35 static void batadv_purge_orig(struct work_struct *work);
37 static void batadv_start_purge_timer(struct batadv_priv *bat_priv)
39 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
40 queue_delayed_work(batadv_event_workqueue,
41 &bat_priv->orig_work, msecs_to_jiffies(1000));
44 /* returns 1 if they are the same originator */
45 static int batadv_compare_orig(const struct hlist_node *node, const void *data2)
47 const void *data1 = container_of(node, struct batadv_orig_node,
50 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
53 int batadv_originator_init(struct batadv_priv *bat_priv)
55 if (bat_priv->orig_hash)
58 bat_priv->orig_hash = batadv_hash_new(1024);
60 if (!bat_priv->orig_hash)
63 batadv_hash_set_lock_class(bat_priv->orig_hash,
64 &batadv_orig_hash_lock_class_key);
66 batadv_start_purge_timer(bat_priv);
73 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
75 if (atomic_dec_and_test(&neigh_node->refcount))
76 kfree_rcu(neigh_node, rcu);
79 /* increases the refcounter of a found router */
80 struct batadv_neigh_node *
81 batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
83 struct batadv_neigh_node *router;
86 router = rcu_dereference(orig_node->router);
88 if (router && !atomic_inc_not_zero(&router->refcount))
95 struct batadv_neigh_node *
96 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
97 const uint8_t *neigh_addr, uint32_t seqno)
99 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
100 struct batadv_neigh_node *neigh_node;
102 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
106 INIT_HLIST_NODE(&neigh_node->list);
108 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
109 spin_lock_init(&neigh_node->lq_update_lock);
111 /* extra reference for return */
112 atomic_set(&neigh_node->refcount, 2);
114 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
115 "Creating new neighbor %pM, initial seqno %d\n",
122 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
124 struct hlist_node *node, *node_tmp;
125 struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
126 struct batadv_orig_node *orig_node;
128 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
130 spin_lock_bh(&orig_node->neigh_list_lock);
132 /* for all bonding members ... */
133 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
134 &orig_node->bond_list, bonding_list) {
135 list_del_rcu(&neigh_node->bonding_list);
136 batadv_neigh_node_free_ref(neigh_node);
139 /* for all neighbors towards this originator ... */
140 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
141 &orig_node->neigh_list, list) {
142 hlist_del_rcu(&neigh_node->list);
143 batadv_neigh_node_free_ref(neigh_node);
146 spin_unlock_bh(&orig_node->neigh_list_lock);
148 batadv_frag_list_free(&orig_node->frag_list);
149 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
150 "originator timed out");
152 kfree(orig_node->tt_buff);
153 kfree(orig_node->bcast_own);
154 kfree(orig_node->bcast_own_sum);
158 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
160 if (atomic_dec_and_test(&orig_node->refcount))
161 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
164 void batadv_originator_free(struct batadv_priv *bat_priv)
166 struct batadv_hashtable *hash = bat_priv->orig_hash;
167 struct hlist_node *node, *node_tmp;
168 struct hlist_head *head;
169 spinlock_t *list_lock; /* spinlock to protect write access */
170 struct batadv_orig_node *orig_node;
176 cancel_delayed_work_sync(&bat_priv->orig_work);
178 bat_priv->orig_hash = NULL;
180 for (i = 0; i < hash->size; i++) {
181 head = &hash->table[i];
182 list_lock = &hash->list_locks[i];
184 spin_lock_bh(list_lock);
185 hlist_for_each_entry_safe(orig_node, node, node_tmp,
188 batadv_orig_node_free_ref(orig_node);
190 spin_unlock_bh(list_lock);
193 batadv_hash_destroy(hash);
196 /* this function finds or creates an originator entry for the given
197 * address if it does not exits
199 struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
202 struct batadv_orig_node *orig_node;
205 unsigned long reset_time;
207 orig_node = batadv_orig_hash_find(bat_priv, addr);
211 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
212 "Creating new originator: %pM\n", addr);
214 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
218 INIT_HLIST_HEAD(&orig_node->neigh_list);
219 INIT_LIST_HEAD(&orig_node->bond_list);
220 spin_lock_init(&orig_node->ogm_cnt_lock);
221 spin_lock_init(&orig_node->bcast_seqno_lock);
222 spin_lock_init(&orig_node->neigh_list_lock);
223 spin_lock_init(&orig_node->tt_buff_lock);
225 /* extra reference for return */
226 atomic_set(&orig_node->refcount, 2);
228 orig_node->tt_initialised = false;
229 orig_node->bat_priv = bat_priv;
230 memcpy(orig_node->orig, addr, ETH_ALEN);
231 batadv_dat_init_orig_node_addr(orig_node);
232 orig_node->router = NULL;
233 orig_node->tt_crc = 0;
234 atomic_set(&orig_node->last_ttvn, 0);
235 orig_node->tt_buff = NULL;
236 orig_node->tt_buff_len = 0;
237 atomic_set(&orig_node->tt_size, 0);
238 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
239 orig_node->bcast_seqno_reset = reset_time;
240 orig_node->batman_seqno_reset = reset_time;
242 atomic_set(&orig_node->bond_candidates, 0);
244 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
246 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
247 if (!orig_node->bcast_own)
250 size = bat_priv->num_ifaces * sizeof(uint8_t);
251 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
253 INIT_LIST_HEAD(&orig_node->frag_list);
254 orig_node->last_frag_packet = 0;
256 if (!orig_node->bcast_own_sum)
259 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
260 batadv_choose_orig, orig_node,
261 &orig_node->hash_entry);
263 goto free_bcast_own_sum;
267 kfree(orig_node->bcast_own_sum);
269 kfree(orig_node->bcast_own);
276 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
277 struct batadv_orig_node *orig_node,
278 struct batadv_neigh_node **best_neigh_node)
280 struct hlist_node *node, *node_tmp;
281 struct batadv_neigh_node *neigh_node;
282 bool neigh_purged = false;
283 unsigned long last_seen;
284 struct batadv_hard_iface *if_incoming;
286 *best_neigh_node = NULL;
288 spin_lock_bh(&orig_node->neigh_list_lock);
290 /* for all neighbors towards this originator ... */
291 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
292 &orig_node->neigh_list, list) {
293 last_seen = neigh_node->last_seen;
294 if_incoming = neigh_node->if_incoming;
296 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
297 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
298 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
299 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
300 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
301 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
302 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
303 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
304 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
305 orig_node->orig, neigh_node->addr,
306 if_incoming->net_dev->name);
308 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
309 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
310 orig_node->orig, neigh_node->addr,
311 jiffies_to_msecs(last_seen));
315 hlist_del_rcu(&neigh_node->list);
316 batadv_bonding_candidate_del(orig_node, neigh_node);
317 batadv_neigh_node_free_ref(neigh_node);
319 if ((!*best_neigh_node) ||
320 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
321 *best_neigh_node = neigh_node;
325 spin_unlock_bh(&orig_node->neigh_list_lock);
329 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
330 struct batadv_orig_node *orig_node)
332 struct batadv_neigh_node *best_neigh_node;
334 if (batadv_has_timed_out(orig_node->last_seen,
335 2 * BATADV_PURGE_TIMEOUT)) {
336 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
337 "Originator timeout: originator %pM, last_seen %u\n",
339 jiffies_to_msecs(orig_node->last_seen));
342 if (batadv_purge_orig_neighbors(bat_priv, orig_node,
344 batadv_update_route(bat_priv, orig_node,
351 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
353 struct batadv_hashtable *hash = bat_priv->orig_hash;
354 struct hlist_node *node, *node_tmp;
355 struct hlist_head *head;
356 spinlock_t *list_lock; /* spinlock to protect write access */
357 struct batadv_orig_node *orig_node;
363 /* for all origins... */
364 for (i = 0; i < hash->size; i++) {
365 head = &hash->table[i];
366 list_lock = &hash->list_locks[i];
368 spin_lock_bh(list_lock);
369 hlist_for_each_entry_safe(orig_node, node, node_tmp,
371 if (batadv_purge_orig_node(bat_priv, orig_node)) {
372 if (orig_node->gw_flags)
373 batadv_gw_node_delete(bat_priv,
376 batadv_orig_node_free_ref(orig_node);
380 if (batadv_has_timed_out(orig_node->last_frag_packet,
381 BATADV_FRAG_TIMEOUT))
382 batadv_frag_list_free(&orig_node->frag_list);
384 spin_unlock_bh(list_lock);
387 batadv_gw_node_purge(bat_priv);
388 batadv_gw_election(bat_priv);
391 static void batadv_purge_orig(struct work_struct *work)
393 struct delayed_work *delayed_work;
394 struct batadv_priv *bat_priv;
396 delayed_work = container_of(work, struct delayed_work, work);
397 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
398 _batadv_purge_orig(bat_priv);
399 batadv_start_purge_timer(bat_priv);
402 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
404 _batadv_purge_orig(bat_priv);
407 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
409 struct net_device *net_dev = (struct net_device *)seq->private;
410 struct batadv_priv *bat_priv = netdev_priv(net_dev);
411 struct batadv_hashtable *hash = bat_priv->orig_hash;
412 struct hlist_node *node, *node_tmp;
413 struct hlist_head *head;
414 struct batadv_hard_iface *primary_if;
415 struct batadv_orig_node *orig_node;
416 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
417 int batman_count = 0;
420 unsigned long last_seen_jiffies;
423 primary_if = batadv_seq_print_text_primary_if_get(seq);
427 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
428 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
429 primary_if->net_dev->dev_addr, net_dev->name);
430 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
431 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
432 "Nexthop", "outgoingIF", "Potential nexthops");
434 for (i = 0; i < hash->size; i++) {
435 head = &hash->table[i];
438 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
439 neigh_node = batadv_orig_node_get_router(orig_node);
443 if (neigh_node->tq_avg == 0)
446 last_seen_jiffies = jiffies - orig_node->last_seen;
447 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
448 last_seen_secs = last_seen_msecs / 1000;
449 last_seen_msecs = last_seen_msecs % 1000;
451 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
452 orig_node->orig, last_seen_secs,
453 last_seen_msecs, neigh_node->tq_avg,
455 neigh_node->if_incoming->net_dev->name);
457 hlist_for_each_entry_rcu(neigh_node_tmp, node_tmp,
458 &orig_node->neigh_list, list) {
459 seq_printf(seq, " %pM (%3i)",
460 neigh_node_tmp->addr,
461 neigh_node_tmp->tq_avg);
464 seq_printf(seq, "\n");
468 batadv_neigh_node_free_ref(neigh_node);
473 if (batman_count == 0)
474 seq_printf(seq, "No batman nodes in range ...\n");
478 batadv_hardif_free_ref(primary_if);
482 static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
486 size_t data_size, old_size;
488 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
489 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
490 data_ptr = kmalloc(data_size, GFP_ATOMIC);
494 memcpy(data_ptr, orig_node->bcast_own, old_size);
495 kfree(orig_node->bcast_own);
496 orig_node->bcast_own = data_ptr;
498 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
502 memcpy(data_ptr, orig_node->bcast_own_sum,
503 (max_if_num - 1) * sizeof(uint8_t));
504 kfree(orig_node->bcast_own_sum);
505 orig_node->bcast_own_sum = data_ptr;
510 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
513 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
514 struct batadv_hashtable *hash = bat_priv->orig_hash;
515 struct hlist_node *node;
516 struct hlist_head *head;
517 struct batadv_orig_node *orig_node;
521 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
524 for (i = 0; i < hash->size; i++) {
525 head = &hash->table[i];
528 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
529 spin_lock_bh(&orig_node->ogm_cnt_lock);
530 ret = batadv_orig_node_add_if(orig_node, max_if_num);
531 spin_unlock_bh(&orig_node->ogm_cnt_lock);
546 static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
547 int max_if_num, int del_if_num)
549 void *data_ptr = NULL;
552 /* last interface was removed */
556 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
557 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
561 /* copy first part */
562 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
564 /* copy second part */
565 memcpy((char *)data_ptr + del_if_num * chunk_size,
566 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
567 (max_if_num - del_if_num) * chunk_size);
570 kfree(orig_node->bcast_own);
571 orig_node->bcast_own = data_ptr;
576 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
580 memcpy(data_ptr, orig_node->bcast_own_sum,
581 del_if_num * sizeof(uint8_t));
583 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
584 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
585 (max_if_num - del_if_num) * sizeof(uint8_t));
588 kfree(orig_node->bcast_own_sum);
589 orig_node->bcast_own_sum = data_ptr;
594 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
597 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
598 struct batadv_hashtable *hash = bat_priv->orig_hash;
599 struct hlist_node *node;
600 struct hlist_head *head;
601 struct batadv_hard_iface *hard_iface_tmp;
602 struct batadv_orig_node *orig_node;
606 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
609 for (i = 0; i < hash->size; i++) {
610 head = &hash->table[i];
613 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
614 spin_lock_bh(&orig_node->ogm_cnt_lock);
615 ret = batadv_orig_node_del_if(orig_node, max_if_num,
617 spin_unlock_bh(&orig_node->ogm_cnt_lock);
625 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
627 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
628 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
631 if (hard_iface == hard_iface_tmp)
634 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
637 if (hard_iface_tmp->if_num > hard_iface->if_num)
638 hard_iface_tmp->if_num--;
642 hard_iface->if_num = -1;