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 "originator.h"
23 #include "translation-table.h"
25 #include "gateway_client.h"
26 #include "hard-interface.h"
28 #include "soft-interface.h"
29 #include "bridge_loop_avoidance.h"
31 static void batadv_purge_orig(struct work_struct *work);
33 static void batadv_start_purge_timer(struct batadv_priv *bat_priv)
35 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
36 queue_delayed_work(batadv_event_workqueue,
37 &bat_priv->orig_work, msecs_to_jiffies(1000));
40 /* returns 1 if they are the same originator */
41 static int batadv_compare_orig(const struct hlist_node *node, const void *data2)
43 const void *data1 = container_of(node, struct batadv_orig_node,
46 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
49 int batadv_originator_init(struct batadv_priv *bat_priv)
51 if (bat_priv->orig_hash)
54 bat_priv->orig_hash = batadv_hash_new(1024);
56 if (!bat_priv->orig_hash)
59 batadv_start_purge_timer(bat_priv);
66 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
68 if (atomic_dec_and_test(&neigh_node->refcount))
69 kfree_rcu(neigh_node, rcu);
72 /* increases the refcounter of a found router */
73 struct batadv_neigh_node *
74 batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
76 struct batadv_neigh_node *router;
79 router = rcu_dereference(orig_node->router);
81 if (router && !atomic_inc_not_zero(&router->refcount))
88 struct batadv_neigh_node *
89 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
90 const uint8_t *neigh_addr, uint32_t seqno)
92 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
93 struct batadv_neigh_node *neigh_node;
95 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
99 INIT_HLIST_NODE(&neigh_node->list);
101 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
102 spin_lock_init(&neigh_node->lq_update_lock);
104 /* extra reference for return */
105 atomic_set(&neigh_node->refcount, 2);
107 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
108 "Creating new neighbor %pM, initial seqno %d\n",
115 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
117 struct hlist_node *node, *node_tmp;
118 struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
119 struct batadv_orig_node *orig_node;
121 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
123 spin_lock_bh(&orig_node->neigh_list_lock);
125 /* for all bonding members ... */
126 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
127 &orig_node->bond_list, bonding_list) {
128 list_del_rcu(&neigh_node->bonding_list);
129 batadv_neigh_node_free_ref(neigh_node);
132 /* for all neighbors towards this originator ... */
133 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
134 &orig_node->neigh_list, list) {
135 hlist_del_rcu(&neigh_node->list);
136 batadv_neigh_node_free_ref(neigh_node);
139 spin_unlock_bh(&orig_node->neigh_list_lock);
141 batadv_frag_list_free(&orig_node->frag_list);
142 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
143 "originator timed out");
145 kfree(orig_node->tt_buff);
146 kfree(orig_node->bcast_own);
147 kfree(orig_node->bcast_own_sum);
151 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
153 if (atomic_dec_and_test(&orig_node->refcount))
154 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
157 void batadv_originator_free(struct batadv_priv *bat_priv)
159 struct batadv_hashtable *hash = bat_priv->orig_hash;
160 struct hlist_node *node, *node_tmp;
161 struct hlist_head *head;
162 spinlock_t *list_lock; /* spinlock to protect write access */
163 struct batadv_orig_node *orig_node;
169 cancel_delayed_work_sync(&bat_priv->orig_work);
171 bat_priv->orig_hash = NULL;
173 for (i = 0; i < hash->size; i++) {
174 head = &hash->table[i];
175 list_lock = &hash->list_locks[i];
177 spin_lock_bh(list_lock);
178 hlist_for_each_entry_safe(orig_node, node, node_tmp,
182 batadv_orig_node_free_ref(orig_node);
184 spin_unlock_bh(list_lock);
187 batadv_hash_destroy(hash);
190 /* this function finds or creates an originator entry for the given
191 * address if it does not exits
193 struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
196 struct batadv_orig_node *orig_node;
199 unsigned long reset_time;
201 orig_node = batadv_orig_hash_find(bat_priv, addr);
205 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
206 "Creating new originator: %pM\n", addr);
208 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
212 INIT_HLIST_HEAD(&orig_node->neigh_list);
213 INIT_LIST_HEAD(&orig_node->bond_list);
214 spin_lock_init(&orig_node->ogm_cnt_lock);
215 spin_lock_init(&orig_node->bcast_seqno_lock);
216 spin_lock_init(&orig_node->neigh_list_lock);
217 spin_lock_init(&orig_node->tt_buff_lock);
219 /* extra reference for return */
220 atomic_set(&orig_node->refcount, 2);
222 orig_node->tt_initialised = false;
223 orig_node->tt_poss_change = false;
224 orig_node->bat_priv = bat_priv;
225 memcpy(orig_node->orig, addr, ETH_ALEN);
226 orig_node->router = NULL;
227 orig_node->tt_crc = 0;
228 atomic_set(&orig_node->last_ttvn, 0);
229 orig_node->tt_buff = NULL;
230 orig_node->tt_buff_len = 0;
231 atomic_set(&orig_node->tt_size, 0);
232 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
233 orig_node->bcast_seqno_reset = reset_time;
234 orig_node->batman_seqno_reset = reset_time;
236 atomic_set(&orig_node->bond_candidates, 0);
238 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
240 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
241 if (!orig_node->bcast_own)
244 size = bat_priv->num_ifaces * sizeof(uint8_t);
245 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
247 INIT_LIST_HEAD(&orig_node->frag_list);
248 orig_node->last_frag_packet = 0;
250 if (!orig_node->bcast_own_sum)
253 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
254 batadv_choose_orig, orig_node,
255 &orig_node->hash_entry);
257 goto free_bcast_own_sum;
261 kfree(orig_node->bcast_own_sum);
263 kfree(orig_node->bcast_own);
270 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
271 struct batadv_orig_node *orig_node,
272 struct batadv_neigh_node **best_neigh_node)
274 struct hlist_node *node, *node_tmp;
275 struct batadv_neigh_node *neigh_node;
276 bool neigh_purged = false;
277 unsigned long last_seen;
278 struct batadv_hard_iface *if_incoming;
280 *best_neigh_node = NULL;
282 spin_lock_bh(&orig_node->neigh_list_lock);
284 /* for all neighbors towards this originator ... */
285 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
286 &orig_node->neigh_list, list) {
288 last_seen = neigh_node->last_seen;
289 if_incoming = neigh_node->if_incoming;
291 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
292 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
293 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
294 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
296 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
297 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
298 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
299 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
300 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
301 orig_node->orig, neigh_node->addr,
302 if_incoming->net_dev->name);
304 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
305 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
306 orig_node->orig, neigh_node->addr,
307 jiffies_to_msecs(last_seen));
311 hlist_del_rcu(&neigh_node->list);
312 batadv_bonding_candidate_del(orig_node, neigh_node);
313 batadv_neigh_node_free_ref(neigh_node);
315 if ((!*best_neigh_node) ||
316 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
317 *best_neigh_node = neigh_node;
321 spin_unlock_bh(&orig_node->neigh_list_lock);
325 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
326 struct batadv_orig_node *orig_node)
328 struct batadv_neigh_node *best_neigh_node;
330 if (batadv_has_timed_out(orig_node->last_seen,
331 2 * BATADV_PURGE_TIMEOUT)) {
332 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
333 "Originator timeout: originator %pM, last_seen %u\n",
335 jiffies_to_msecs(orig_node->last_seen));
338 if (batadv_purge_orig_neighbors(bat_priv, orig_node,
340 batadv_update_route(bat_priv, orig_node,
347 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
349 struct batadv_hashtable *hash = bat_priv->orig_hash;
350 struct hlist_node *node, *node_tmp;
351 struct hlist_head *head;
352 spinlock_t *list_lock; /* spinlock to protect write access */
353 struct batadv_orig_node *orig_node;
359 /* for all origins... */
360 for (i = 0; i < hash->size; i++) {
361 head = &hash->table[i];
362 list_lock = &hash->list_locks[i];
364 spin_lock_bh(list_lock);
365 hlist_for_each_entry_safe(orig_node, node, node_tmp,
367 if (batadv_purge_orig_node(bat_priv, orig_node)) {
368 if (orig_node->gw_flags)
369 batadv_gw_node_delete(bat_priv,
372 batadv_orig_node_free_ref(orig_node);
376 if (batadv_has_timed_out(orig_node->last_frag_packet,
377 BATADV_FRAG_TIMEOUT))
378 batadv_frag_list_free(&orig_node->frag_list);
380 spin_unlock_bh(list_lock);
383 batadv_gw_node_purge(bat_priv);
384 batadv_gw_election(bat_priv);
387 static void batadv_purge_orig(struct work_struct *work)
389 struct delayed_work *delayed_work;
390 struct batadv_priv *bat_priv;
392 delayed_work = container_of(work, struct delayed_work, work);
393 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
394 _batadv_purge_orig(bat_priv);
395 batadv_start_purge_timer(bat_priv);
398 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
400 _batadv_purge_orig(bat_priv);
403 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
405 struct net_device *net_dev = (struct net_device *)seq->private;
406 struct batadv_priv *bat_priv = netdev_priv(net_dev);
407 struct batadv_hashtable *hash = bat_priv->orig_hash;
408 struct hlist_node *node, *node_tmp;
409 struct hlist_head *head;
410 struct batadv_hard_iface *primary_if;
411 struct batadv_orig_node *orig_node;
412 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
413 int batman_count = 0;
416 unsigned long last_seen_jiffies;
420 primary_if = batadv_primary_if_get_selected(bat_priv);
423 ret = seq_printf(seq,
424 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
429 if (primary_if->if_status != BATADV_IF_ACTIVE) {
430 ret = seq_printf(seq,
431 "BATMAN mesh %s disabled - primary interface not active\n",
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, 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, 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);
473 seq_printf(seq, "\n");
477 batadv_neigh_node_free_ref(neigh_node);
482 if (batman_count == 0)
483 seq_printf(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_node *node;
525 struct hlist_head *head;
526 struct batadv_orig_node *orig_node;
530 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
533 for (i = 0; i < hash->size; i++) {
534 head = &hash->table[i];
537 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
538 spin_lock_bh(&orig_node->ogm_cnt_lock);
539 ret = batadv_orig_node_add_if(orig_node, max_if_num);
540 spin_unlock_bh(&orig_node->ogm_cnt_lock);
555 static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
556 int max_if_num, int del_if_num)
558 void *data_ptr = NULL;
561 /* last interface was removed */
565 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
566 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
570 /* copy first part */
571 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
573 /* copy second part */
574 memcpy((char *)data_ptr + del_if_num * chunk_size,
575 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
576 (max_if_num - del_if_num) * chunk_size);
579 kfree(orig_node->bcast_own);
580 orig_node->bcast_own = data_ptr;
585 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
589 memcpy(data_ptr, orig_node->bcast_own_sum,
590 del_if_num * sizeof(uint8_t));
592 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
593 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
594 (max_if_num - del_if_num) * sizeof(uint8_t));
597 kfree(orig_node->bcast_own_sum);
598 orig_node->bcast_own_sum = data_ptr;
603 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
606 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
607 struct batadv_hashtable *hash = bat_priv->orig_hash;
608 struct hlist_node *node;
609 struct hlist_head *head;
610 struct batadv_hard_iface *hard_iface_tmp;
611 struct batadv_orig_node *orig_node;
615 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
618 for (i = 0; i < hash->size; i++) {
619 head = &hash->table[i];
622 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
623 spin_lock_bh(&orig_node->ogm_cnt_lock);
624 ret = batadv_orig_node_del_if(orig_node, max_if_num,
626 spin_unlock_bh(&orig_node->ogm_cnt_lock);
634 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
636 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
637 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
640 if (hard_iface == hard_iface_tmp)
643 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
646 if (hard_iface_tmp->if_num > hard_iface->if_num)
647 hard_iface_tmp->if_num--;
651 hard_iface->if_num = -1;