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"
31 #include "fragmentation.h"
34 static struct lock_class_key batadv_orig_hash_lock_class_key;
36 static void batadv_purge_orig(struct work_struct *work);
38 /* returns 1 if they are the same originator */
39 static int batadv_compare_orig(const struct hlist_node *node, const void *data2)
41 const void *data1 = container_of(node, struct batadv_orig_node,
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
47 int batadv_originator_init(struct batadv_priv *bat_priv)
49 if (bat_priv->orig_hash)
52 bat_priv->orig_hash = batadv_hash_new(1024);
54 if (!bat_priv->orig_hash)
57 batadv_hash_set_lock_class(bat_priv->orig_hash,
58 &batadv_orig_hash_lock_class_key);
60 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
61 queue_delayed_work(batadv_event_workqueue,
63 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
71 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
73 if (atomic_dec_and_test(&neigh_node->refcount))
74 kfree_rcu(neigh_node, rcu);
77 /* increases the refcounter of a found router */
78 struct batadv_neigh_node *
79 batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
81 struct batadv_neigh_node *router;
84 router = rcu_dereference(orig_node->router);
86 if (router && !atomic_inc_not_zero(&router->refcount))
93 struct batadv_neigh_node *
94 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
95 const uint8_t *neigh_addr)
97 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
98 struct batadv_neigh_node *neigh_node;
100 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
104 INIT_HLIST_NODE(&neigh_node->list);
106 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
107 spin_lock_init(&neigh_node->lq_update_lock);
109 /* extra reference for return */
110 atomic_set(&neigh_node->refcount, 2);
112 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
113 "Creating new neighbor %pM on interface %s\n", neigh_addr,
114 hard_iface->net_dev->name);
120 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
122 struct hlist_node *node_tmp;
123 struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
124 struct batadv_orig_node *orig_node;
126 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
128 spin_lock_bh(&orig_node->neigh_list_lock);
130 /* for all bonding members ... */
131 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
132 &orig_node->bond_list, bonding_list) {
133 list_del_rcu(&neigh_node->bonding_list);
134 batadv_neigh_node_free_ref(neigh_node);
137 /* for all neighbors towards this originator ... */
138 hlist_for_each_entry_safe(neigh_node, node_tmp,
139 &orig_node->neigh_list, list) {
140 hlist_del_rcu(&neigh_node->list);
141 batadv_neigh_node_free_ref(neigh_node);
144 spin_unlock_bh(&orig_node->neigh_list_lock);
147 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
149 batadv_frag_purge_orig(orig_node, NULL);
151 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
152 "originator timed out");
154 kfree(orig_node->tt_buff);
155 kfree(orig_node->bcast_own);
156 kfree(orig_node->bcast_own_sum);
161 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
162 * schedule an rcu callback for freeing it
163 * @orig_node: the orig node to free
165 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
167 if (atomic_dec_and_test(&orig_node->refcount))
168 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
172 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
173 * possibly free it (without rcu callback)
174 * @orig_node: the orig node to free
176 void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
178 if (atomic_dec_and_test(&orig_node->refcount))
179 batadv_orig_node_free_rcu(&orig_node->rcu);
182 void batadv_originator_free(struct batadv_priv *bat_priv)
184 struct batadv_hashtable *hash = bat_priv->orig_hash;
185 struct hlist_node *node_tmp;
186 struct hlist_head *head;
187 spinlock_t *list_lock; /* spinlock to protect write access */
188 struct batadv_orig_node *orig_node;
194 cancel_delayed_work_sync(&bat_priv->orig_work);
196 bat_priv->orig_hash = NULL;
198 for (i = 0; i < hash->size; i++) {
199 head = &hash->table[i];
200 list_lock = &hash->list_locks[i];
202 spin_lock_bh(list_lock);
203 hlist_for_each_entry_safe(orig_node, node_tmp,
205 hlist_del_rcu(&orig_node->hash_entry);
206 batadv_orig_node_free_ref(orig_node);
208 spin_unlock_bh(list_lock);
211 batadv_hash_destroy(hash);
214 /* this function finds or creates an originator entry for the given
215 * address if it does not exits
217 struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
220 struct batadv_orig_node *orig_node;
223 unsigned long reset_time;
225 orig_node = batadv_orig_hash_find(bat_priv, addr);
229 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
230 "Creating new originator: %pM\n", addr);
232 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
236 INIT_HLIST_HEAD(&orig_node->neigh_list);
237 INIT_LIST_HEAD(&orig_node->bond_list);
238 spin_lock_init(&orig_node->ogm_cnt_lock);
239 spin_lock_init(&orig_node->bcast_seqno_lock);
240 spin_lock_init(&orig_node->neigh_list_lock);
241 spin_lock_init(&orig_node->tt_buff_lock);
243 batadv_nc_init_orig(orig_node);
245 /* extra reference for return */
246 atomic_set(&orig_node->refcount, 2);
248 orig_node->tt_initialised = false;
249 orig_node->bat_priv = bat_priv;
250 memcpy(orig_node->orig, addr, ETH_ALEN);
251 batadv_dat_init_orig_node_addr(orig_node);
252 orig_node->router = NULL;
253 orig_node->tt_crc = 0;
254 atomic_set(&orig_node->last_ttvn, 0);
255 orig_node->tt_buff = NULL;
256 orig_node->tt_buff_len = 0;
257 atomic_set(&orig_node->tt_size, 0);
258 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
259 orig_node->bcast_seqno_reset = reset_time;
260 orig_node->batman_seqno_reset = reset_time;
262 atomic_set(&orig_node->bond_candidates, 0);
264 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
266 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
267 if (!orig_node->bcast_own)
270 size = bat_priv->num_ifaces * sizeof(uint8_t);
271 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
273 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
274 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
275 spin_lock_init(&orig_node->fragments[i].lock);
276 orig_node->fragments[i].size = 0;
279 if (!orig_node->bcast_own_sum)
282 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
283 batadv_choose_orig, orig_node,
284 &orig_node->hash_entry);
286 goto free_bcast_own_sum;
290 kfree(orig_node->bcast_own_sum);
292 kfree(orig_node->bcast_own);
299 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
300 struct batadv_orig_node *orig_node,
301 struct batadv_neigh_node **best_neigh_node)
303 struct hlist_node *node_tmp;
304 struct batadv_neigh_node *neigh_node;
305 bool neigh_purged = false;
306 unsigned long last_seen;
307 struct batadv_hard_iface *if_incoming;
309 *best_neigh_node = NULL;
311 spin_lock_bh(&orig_node->neigh_list_lock);
313 /* for all neighbors towards this originator ... */
314 hlist_for_each_entry_safe(neigh_node, node_tmp,
315 &orig_node->neigh_list, list) {
316 last_seen = neigh_node->last_seen;
317 if_incoming = neigh_node->if_incoming;
319 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
320 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
321 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
322 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
323 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
324 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
325 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
326 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
327 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
328 orig_node->orig, neigh_node->addr,
329 if_incoming->net_dev->name);
331 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
332 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
333 orig_node->orig, neigh_node->addr,
334 jiffies_to_msecs(last_seen));
338 hlist_del_rcu(&neigh_node->list);
339 batadv_bonding_candidate_del(orig_node, neigh_node);
340 batadv_neigh_node_free_ref(neigh_node);
342 if ((!*best_neigh_node) ||
343 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
344 *best_neigh_node = neigh_node;
348 spin_unlock_bh(&orig_node->neigh_list_lock);
352 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
353 struct batadv_orig_node *orig_node)
355 struct batadv_neigh_node *best_neigh_node;
357 if (batadv_has_timed_out(orig_node->last_seen,
358 2 * BATADV_PURGE_TIMEOUT)) {
359 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
360 "Originator timeout: originator %pM, last_seen %u\n",
362 jiffies_to_msecs(orig_node->last_seen));
365 if (batadv_purge_orig_neighbors(bat_priv, orig_node,
367 batadv_update_route(bat_priv, orig_node,
374 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
376 struct batadv_hashtable *hash = bat_priv->orig_hash;
377 struct hlist_node *node_tmp;
378 struct hlist_head *head;
379 spinlock_t *list_lock; /* spinlock to protect write access */
380 struct batadv_orig_node *orig_node;
386 /* for all origins... */
387 for (i = 0; i < hash->size; i++) {
388 head = &hash->table[i];
389 list_lock = &hash->list_locks[i];
391 spin_lock_bh(list_lock);
392 hlist_for_each_entry_safe(orig_node, node_tmp,
394 if (batadv_purge_orig_node(bat_priv, orig_node)) {
395 batadv_gw_node_delete(bat_priv, orig_node);
396 hlist_del_rcu(&orig_node->hash_entry);
397 batadv_orig_node_free_ref(orig_node);
401 batadv_frag_purge_orig(orig_node,
402 batadv_frag_check_entry);
404 spin_unlock_bh(list_lock);
407 batadv_gw_node_purge(bat_priv);
408 batadv_gw_election(bat_priv);
411 static void batadv_purge_orig(struct work_struct *work)
413 struct delayed_work *delayed_work;
414 struct batadv_priv *bat_priv;
416 delayed_work = container_of(work, struct delayed_work, work);
417 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
418 _batadv_purge_orig(bat_priv);
419 queue_delayed_work(batadv_event_workqueue,
420 &bat_priv->orig_work,
421 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
424 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
426 _batadv_purge_orig(bat_priv);
429 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
431 struct net_device *net_dev = (struct net_device *)seq->private;
432 struct batadv_priv *bat_priv = netdev_priv(net_dev);
433 struct batadv_hashtable *hash = bat_priv->orig_hash;
434 struct hlist_head *head;
435 struct batadv_hard_iface *primary_if;
436 struct batadv_orig_node *orig_node;
437 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
438 int batman_count = 0;
441 unsigned long last_seen_jiffies;
444 primary_if = batadv_seq_print_text_primary_if_get(seq);
448 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
449 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
450 primary_if->net_dev->dev_addr, net_dev->name);
451 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
452 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
453 "Nexthop", "outgoingIF", "Potential nexthops");
455 for (i = 0; i < hash->size; i++) {
456 head = &hash->table[i];
459 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
460 neigh_node = batadv_orig_node_get_router(orig_node);
464 if (neigh_node->tq_avg == 0)
467 last_seen_jiffies = jiffies - orig_node->last_seen;
468 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
469 last_seen_secs = last_seen_msecs / 1000;
470 last_seen_msecs = last_seen_msecs % 1000;
472 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
473 orig_node->orig, last_seen_secs,
474 last_seen_msecs, neigh_node->tq_avg,
476 neigh_node->if_incoming->net_dev->name);
478 hlist_for_each_entry_rcu(neigh_node_tmp,
479 &orig_node->neigh_list, list) {
480 seq_printf(seq, " %pM (%3i)",
481 neigh_node_tmp->addr,
482 neigh_node_tmp->tq_avg);
489 batadv_neigh_node_free_ref(neigh_node);
494 if (batman_count == 0)
495 seq_puts(seq, "No batman nodes in range ...\n");
499 batadv_hardif_free_ref(primary_if);
503 static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
507 size_t data_size, old_size;
509 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
510 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
511 data_ptr = kmalloc(data_size, GFP_ATOMIC);
515 memcpy(data_ptr, orig_node->bcast_own, old_size);
516 kfree(orig_node->bcast_own);
517 orig_node->bcast_own = data_ptr;
519 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
523 memcpy(data_ptr, orig_node->bcast_own_sum,
524 (max_if_num - 1) * sizeof(uint8_t));
525 kfree(orig_node->bcast_own_sum);
526 orig_node->bcast_own_sum = data_ptr;
531 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
534 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
535 struct batadv_hashtable *hash = bat_priv->orig_hash;
536 struct hlist_head *head;
537 struct batadv_orig_node *orig_node;
541 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
544 for (i = 0; i < hash->size; i++) {
545 head = &hash->table[i];
548 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
549 spin_lock_bh(&orig_node->ogm_cnt_lock);
550 ret = batadv_orig_node_add_if(orig_node, max_if_num);
551 spin_unlock_bh(&orig_node->ogm_cnt_lock);
566 static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
567 int max_if_num, int del_if_num)
569 void *data_ptr = NULL;
572 /* last interface was removed */
576 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
577 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
581 /* copy first part */
582 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
584 /* copy second part */
585 memcpy((char *)data_ptr + del_if_num * chunk_size,
586 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
587 (max_if_num - del_if_num) * chunk_size);
590 kfree(orig_node->bcast_own);
591 orig_node->bcast_own = data_ptr;
596 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
600 memcpy(data_ptr, orig_node->bcast_own_sum,
601 del_if_num * sizeof(uint8_t));
603 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
604 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
605 (max_if_num - del_if_num) * sizeof(uint8_t));
608 kfree(orig_node->bcast_own_sum);
609 orig_node->bcast_own_sum = data_ptr;
614 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
617 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
618 struct batadv_hashtable *hash = bat_priv->orig_hash;
619 struct hlist_head *head;
620 struct batadv_hard_iface *hard_iface_tmp;
621 struct batadv_orig_node *orig_node;
625 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
628 for (i = 0; i < hash->size; i++) {
629 head = &hash->table[i];
632 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
633 spin_lock_bh(&orig_node->ogm_cnt_lock);
634 ret = batadv_orig_node_del_if(orig_node, max_if_num,
636 spin_unlock_bh(&orig_node->ogm_cnt_lock);
644 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
646 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
647 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
650 if (hard_iface == hard_iface_tmp)
653 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
656 if (hard_iface_tmp->if_num > hard_iface->if_num)
657 hard_iface_tmp->if_num--;
661 hard_iface->if_num = -1;