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 bat_priv
->orig_hash
= hash_new(1024);
49 if (!bat_priv
->orig_hash
)
52 start_purge_timer(bat_priv
);
59 void neigh_node_free_ref(struct neigh_node
*neigh_node
)
61 if (atomic_dec_and_test(&neigh_node
->refcount
))
62 kfree_rcu(neigh_node
, rcu
);
65 struct neigh_node
*create_neighbor(struct orig_node
*orig_node
,
66 struct orig_node
*orig_neigh_node
,
68 struct hard_iface
*if_incoming
)
70 struct bat_priv
*bat_priv
= netdev_priv(if_incoming
->soft_iface
);
71 struct neigh_node
*neigh_node
;
73 bat_dbg(DBG_BATMAN
, bat_priv
,
74 "Creating new last-hop neighbor of originator\n");
76 neigh_node
= kzalloc(sizeof(struct neigh_node
), GFP_ATOMIC
);
80 INIT_HLIST_NODE(&neigh_node
->list
);
81 INIT_LIST_HEAD(&neigh_node
->bonding_list
);
83 memcpy(neigh_node
->addr
, neigh
, ETH_ALEN
);
84 neigh_node
->orig_node
= orig_neigh_node
;
85 neigh_node
->if_incoming
= if_incoming
;
87 /* extra reference for return */
88 atomic_set(&neigh_node
->refcount
, 2);
90 spin_lock_bh(&orig_node
->neigh_list_lock
);
91 hlist_add_head_rcu(&neigh_node
->list
, &orig_node
->neigh_list
);
92 spin_unlock_bh(&orig_node
->neigh_list_lock
);
96 static void orig_node_free_rcu(struct rcu_head
*rcu
)
98 struct hlist_node
*node
, *node_tmp
;
99 struct neigh_node
*neigh_node
, *tmp_neigh_node
;
100 struct orig_node
*orig_node
;
102 orig_node
= container_of(rcu
, struct orig_node
, rcu
);
104 spin_lock_bh(&orig_node
->neigh_list_lock
);
106 /* for all bonding members ... */
107 list_for_each_entry_safe(neigh_node
, tmp_neigh_node
,
108 &orig_node
->bond_list
, bonding_list
) {
109 list_del_rcu(&neigh_node
->bonding_list
);
110 neigh_node_free_ref(neigh_node
);
113 /* for all neighbors towards this originator ... */
114 hlist_for_each_entry_safe(neigh_node
, node
, node_tmp
,
115 &orig_node
->neigh_list
, list
) {
116 hlist_del_rcu(&neigh_node
->list
);
117 neigh_node_free_ref(neigh_node
);
120 spin_unlock_bh(&orig_node
->neigh_list_lock
);
122 frag_list_free(&orig_node
->frag_list
);
123 hna_global_del_orig(orig_node
->bat_priv
, orig_node
,
124 "originator timed out");
126 kfree(orig_node
->bcast_own
);
127 kfree(orig_node
->bcast_own_sum
);
131 void orig_node_free_ref(struct orig_node
*orig_node
)
133 if (atomic_dec_and_test(&orig_node
->refcount
))
134 call_rcu(&orig_node
->rcu
, orig_node_free_rcu
);
137 void originator_free(struct bat_priv
*bat_priv
)
139 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
140 struct hlist_node
*node
, *node_tmp
;
141 struct hlist_head
*head
;
142 spinlock_t
*list_lock
; /* spinlock to protect write access */
143 struct orig_node
*orig_node
;
149 cancel_delayed_work_sync(&bat_priv
->orig_work
);
151 bat_priv
->orig_hash
= NULL
;
153 for (i
= 0; i
< hash
->size
; i
++) {
154 head
= &hash
->table
[i
];
155 list_lock
= &hash
->list_locks
[i
];
157 spin_lock_bh(list_lock
);
158 hlist_for_each_entry_safe(orig_node
, node
, node_tmp
,
162 orig_node_free_ref(orig_node
);
164 spin_unlock_bh(list_lock
);
170 /* this function finds or creates an originator entry for the given
171 * address if it does not exits */
172 struct orig_node
*get_orig_node(struct bat_priv
*bat_priv
, uint8_t *addr
)
174 struct orig_node
*orig_node
;
178 orig_node
= orig_hash_find(bat_priv
, addr
);
182 bat_dbg(DBG_BATMAN
, bat_priv
,
183 "Creating new originator: %pM\n", addr
);
185 orig_node
= kzalloc(sizeof(struct orig_node
), GFP_ATOMIC
);
189 INIT_HLIST_HEAD(&orig_node
->neigh_list
);
190 INIT_LIST_HEAD(&orig_node
->bond_list
);
191 spin_lock_init(&orig_node
->ogm_cnt_lock
);
192 spin_lock_init(&orig_node
->bcast_seqno_lock
);
193 spin_lock_init(&orig_node
->neigh_list_lock
);
195 /* extra reference for return */
196 atomic_set(&orig_node
->refcount
, 2);
198 orig_node
->bat_priv
= bat_priv
;
199 memcpy(orig_node
->orig
, addr
, ETH_ALEN
);
200 orig_node
->router
= NULL
;
201 orig_node
->hna_buff
= NULL
;
202 orig_node
->bcast_seqno_reset
= jiffies
- 1
203 - msecs_to_jiffies(RESET_PROTECTION_MS
);
204 orig_node
->batman_seqno_reset
= jiffies
- 1
205 - msecs_to_jiffies(RESET_PROTECTION_MS
);
207 atomic_set(&orig_node
->bond_candidates
, 0);
209 size
= bat_priv
->num_ifaces
* sizeof(unsigned long) * NUM_WORDS
;
211 orig_node
->bcast_own
= kzalloc(size
, GFP_ATOMIC
);
212 if (!orig_node
->bcast_own
)
215 size
= bat_priv
->num_ifaces
* sizeof(uint8_t);
216 orig_node
->bcast_own_sum
= kzalloc(size
, GFP_ATOMIC
);
218 INIT_LIST_HEAD(&orig_node
->frag_list
);
219 orig_node
->last_frag_packet
= 0;
221 if (!orig_node
->bcast_own_sum
)
224 hash_added
= hash_add(bat_priv
->orig_hash
, compare_orig
,
225 choose_orig
, orig_node
, &orig_node
->hash_entry
);
227 goto free_bcast_own_sum
;
231 kfree(orig_node
->bcast_own_sum
);
233 kfree(orig_node
->bcast_own
);
239 static bool purge_orig_neighbors(struct bat_priv
*bat_priv
,
240 struct orig_node
*orig_node
,
241 struct neigh_node
**best_neigh_node
)
243 struct hlist_node
*node
, *node_tmp
;
244 struct neigh_node
*neigh_node
;
245 bool neigh_purged
= false;
247 *best_neigh_node
= NULL
;
249 spin_lock_bh(&orig_node
->neigh_list_lock
);
251 /* for all neighbors towards this originator ... */
252 hlist_for_each_entry_safe(neigh_node
, node
, node_tmp
,
253 &orig_node
->neigh_list
, list
) {
255 if ((time_after(jiffies
,
256 neigh_node
->last_valid
+ PURGE_TIMEOUT
* HZ
)) ||
257 (neigh_node
->if_incoming
->if_status
== IF_INACTIVE
) ||
258 (neigh_node
->if_incoming
->if_status
== IF_NOT_IN_USE
) ||
259 (neigh_node
->if_incoming
->if_status
== IF_TO_BE_REMOVED
)) {
261 if ((neigh_node
->if_incoming
->if_status
==
263 (neigh_node
->if_incoming
->if_status
==
265 (neigh_node
->if_incoming
->if_status
==
267 bat_dbg(DBG_BATMAN
, bat_priv
,
268 "neighbor purge: originator %pM, "
269 "neighbor: %pM, iface: %s\n",
270 orig_node
->orig
, neigh_node
->addr
,
271 neigh_node
->if_incoming
->net_dev
->name
);
273 bat_dbg(DBG_BATMAN
, bat_priv
,
274 "neighbor timeout: originator %pM, "
275 "neighbor: %pM, last_valid: %lu\n",
276 orig_node
->orig
, neigh_node
->addr
,
277 (neigh_node
->last_valid
/ HZ
));
281 hlist_del_rcu(&neigh_node
->list
);
282 bonding_candidate_del(orig_node
, neigh_node
);
283 neigh_node_free_ref(neigh_node
);
285 if ((!*best_neigh_node
) ||
286 (neigh_node
->tq_avg
> (*best_neigh_node
)->tq_avg
))
287 *best_neigh_node
= neigh_node
;
291 spin_unlock_bh(&orig_node
->neigh_list_lock
);
295 static bool purge_orig_node(struct bat_priv
*bat_priv
,
296 struct orig_node
*orig_node
)
298 struct neigh_node
*best_neigh_node
;
300 if (time_after(jiffies
,
301 orig_node
->last_valid
+ 2 * PURGE_TIMEOUT
* HZ
)) {
303 bat_dbg(DBG_BATMAN
, bat_priv
,
304 "Originator timeout: originator %pM, last_valid %lu\n",
305 orig_node
->orig
, (orig_node
->last_valid
/ HZ
));
308 if (purge_orig_neighbors(bat_priv
, orig_node
,
310 update_routes(bat_priv
, orig_node
,
313 orig_node
->hna_buff_len
);
320 static void _purge_orig(struct bat_priv
*bat_priv
)
322 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
323 struct hlist_node
*node
, *node_tmp
;
324 struct hlist_head
*head
;
325 spinlock_t
*list_lock
; /* spinlock to protect write access */
326 struct orig_node
*orig_node
;
332 /* for all origins... */
333 for (i
= 0; i
< hash
->size
; i
++) {
334 head
= &hash
->table
[i
];
335 list_lock
= &hash
->list_locks
[i
];
337 spin_lock_bh(list_lock
);
338 hlist_for_each_entry_safe(orig_node
, node
, node_tmp
,
340 if (purge_orig_node(bat_priv
, orig_node
)) {
341 if (orig_node
->gw_flags
)
342 gw_node_delete(bat_priv
, orig_node
);
344 orig_node_free_ref(orig_node
);
348 if (time_after(jiffies
, orig_node
->last_frag_packet
+
349 msecs_to_jiffies(FRAG_TIMEOUT
)))
350 frag_list_free(&orig_node
->frag_list
);
352 spin_unlock_bh(list_lock
);
355 gw_node_purge(bat_priv
);
356 gw_election(bat_priv
);
358 softif_neigh_purge(bat_priv
);
361 static void purge_orig(struct work_struct
*work
)
363 struct delayed_work
*delayed_work
=
364 container_of(work
, struct delayed_work
, work
);
365 struct bat_priv
*bat_priv
=
366 container_of(delayed_work
, struct bat_priv
, orig_work
);
368 _purge_orig(bat_priv
);
369 start_purge_timer(bat_priv
);
372 void purge_orig_ref(struct bat_priv
*bat_priv
)
374 _purge_orig(bat_priv
);
377 int orig_seq_print_text(struct seq_file
*seq
, void *offset
)
379 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
380 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
381 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
382 struct hlist_node
*node
, *node_tmp
;
383 struct hlist_head
*head
;
384 struct orig_node
*orig_node
;
385 struct neigh_node
*neigh_node
;
386 int batman_count
= 0;
391 if ((!bat_priv
->primary_if
) ||
392 (bat_priv
->primary_if
->if_status
!= IF_ACTIVE
)) {
393 if (!bat_priv
->primary_if
)
394 return seq_printf(seq
, "BATMAN mesh %s disabled - "
395 "please specify interfaces to enable it\n",
398 return seq_printf(seq
, "BATMAN mesh %s "
399 "disabled - primary interface not active\n",
403 seq_printf(seq
, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
404 SOURCE_VERSION
, REVISION_VERSION_STR
,
405 bat_priv
->primary_if
->net_dev
->name
,
406 bat_priv
->primary_if
->net_dev
->dev_addr
, net_dev
->name
);
407 seq_printf(seq
, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
408 "Originator", "last-seen", "#", TQ_MAX_VALUE
, "Nexthop",
409 "outgoingIF", "Potential nexthops");
411 for (i
= 0; i
< hash
->size
; i
++) {
412 head
= &hash
->table
[i
];
415 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
416 if (!orig_node
->router
)
419 if (orig_node
->router
->tq_avg
== 0)
422 last_seen_secs
= jiffies_to_msecs(jiffies
-
423 orig_node
->last_valid
) / 1000;
424 last_seen_msecs
= jiffies_to_msecs(jiffies
-
425 orig_node
->last_valid
) % 1000;
427 neigh_node
= orig_node
->router
;
428 seq_printf(seq
, "%pM %4i.%03is (%3i) %pM [%10s]:",
429 orig_node
->orig
, last_seen_secs
,
430 last_seen_msecs
, neigh_node
->tq_avg
,
432 neigh_node
->if_incoming
->net_dev
->name
);
434 hlist_for_each_entry_rcu(neigh_node
, node_tmp
,
435 &orig_node
->neigh_list
, list
) {
436 seq_printf(seq
, " %pM (%3i)", neigh_node
->addr
,
440 seq_printf(seq
, "\n");
446 if ((batman_count
== 0))
447 seq_printf(seq
, "No batman nodes in range ...\n");
452 static int orig_node_add_if(struct orig_node
*orig_node
, int max_if_num
)
456 data_ptr
= kmalloc(max_if_num
* sizeof(unsigned long) * NUM_WORDS
,
459 pr_err("Can't resize orig: out of memory\n");
463 memcpy(data_ptr
, orig_node
->bcast_own
,
464 (max_if_num
- 1) * sizeof(unsigned long) * NUM_WORDS
);
465 kfree(orig_node
->bcast_own
);
466 orig_node
->bcast_own
= data_ptr
;
468 data_ptr
= kmalloc(max_if_num
* sizeof(uint8_t), GFP_ATOMIC
);
470 pr_err("Can't resize orig: out of memory\n");
474 memcpy(data_ptr
, orig_node
->bcast_own_sum
,
475 (max_if_num
- 1) * sizeof(uint8_t));
476 kfree(orig_node
->bcast_own_sum
);
477 orig_node
->bcast_own_sum
= data_ptr
;
482 int orig_hash_add_if(struct hard_iface
*hard_iface
, int max_if_num
)
484 struct bat_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
485 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
486 struct hlist_node
*node
;
487 struct hlist_head
*head
;
488 struct orig_node
*orig_node
;
491 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
493 for (i
= 0; i
< hash
->size
; i
++) {
494 head
= &hash
->table
[i
];
497 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
498 spin_lock_bh(&orig_node
->ogm_cnt_lock
);
499 ret
= orig_node_add_if(orig_node
, max_if_num
);
500 spin_unlock_bh(&orig_node
->ogm_cnt_lock
);
515 static int orig_node_del_if(struct orig_node
*orig_node
,
516 int max_if_num
, int del_if_num
)
518 void *data_ptr
= NULL
;
521 /* last interface was removed */
525 chunk_size
= sizeof(unsigned long) * NUM_WORDS
;
526 data_ptr
= kmalloc(max_if_num
* chunk_size
, GFP_ATOMIC
);
528 pr_err("Can't resize orig: out of memory\n");
532 /* copy first part */
533 memcpy(data_ptr
, orig_node
->bcast_own
, del_if_num
* chunk_size
);
535 /* copy second part */
536 memcpy(data_ptr
+ del_if_num
* chunk_size
,
537 orig_node
->bcast_own
+ ((del_if_num
+ 1) * chunk_size
),
538 (max_if_num
- del_if_num
) * chunk_size
);
541 kfree(orig_node
->bcast_own
);
542 orig_node
->bcast_own
= data_ptr
;
547 data_ptr
= kmalloc(max_if_num
* sizeof(uint8_t), GFP_ATOMIC
);
549 pr_err("Can't resize orig: out of memory\n");
553 memcpy(data_ptr
, orig_node
->bcast_own_sum
,
554 del_if_num
* sizeof(uint8_t));
556 memcpy(data_ptr
+ del_if_num
* sizeof(uint8_t),
557 orig_node
->bcast_own_sum
+ ((del_if_num
+ 1) * sizeof(uint8_t)),
558 (max_if_num
- del_if_num
) * sizeof(uint8_t));
561 kfree(orig_node
->bcast_own_sum
);
562 orig_node
->bcast_own_sum
= data_ptr
;
567 int orig_hash_del_if(struct hard_iface
*hard_iface
, int max_if_num
)
569 struct bat_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
570 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
571 struct hlist_node
*node
;
572 struct hlist_head
*head
;
573 struct hard_iface
*hard_iface_tmp
;
574 struct orig_node
*orig_node
;
577 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
579 for (i
= 0; i
< hash
->size
; i
++) {
580 head
= &hash
->table
[i
];
583 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
584 spin_lock_bh(&orig_node
->ogm_cnt_lock
);
585 ret
= orig_node_del_if(orig_node
, max_if_num
,
587 spin_unlock_bh(&orig_node
->ogm_cnt_lock
);
595 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
597 list_for_each_entry_rcu(hard_iface_tmp
, &hardif_list
, list
) {
598 if (hard_iface_tmp
->if_status
== IF_NOT_IN_USE
)
601 if (hard_iface
== hard_iface_tmp
)
604 if (hard_iface
->soft_iface
!= hard_iface_tmp
->soft_iface
)
607 if (hard_iface_tmp
->if_num
> hard_iface
->if_num
)
608 hard_iface_tmp
->if_num
--;
612 hard_iface
->if_num
= -1;