Skip to content

Commit 2ae2daf

Browse files
author
Marek Lindner
committed
batman-adv: protect ogm counter arrays with spinlock
Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
1 parent 16b1aba commit 2ae2daf

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎net/batman-adv/originator.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
207207
return NULL;
208208

209209
INIT_HLIST_HEAD(&orig_node->neigh_list);
210+
spin_lock_init(&orig_node->ogm_cnt_lock);
210211
spin_lock_init(&orig_node->neigh_list_lock);
211212
kref_init(&orig_node->refcount);
212213

@@ -517,7 +518,7 @@ int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
517518
struct hlist_head *head;
518519
struct element_t *bucket;
519520
struct orig_node *orig_node;
520-
int i;
521+
int i, ret;
521522

522523
/* resize all orig nodes because orig_node->bcast_own(_sum) depend on
523524
* if_num */
@@ -530,7 +531,11 @@ int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
530531
hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
531532
orig_node = bucket->data;
532533

533-
if (orig_node_add_if(orig_node, max_if_num) == -1)
534+
spin_lock_bh(&orig_node->ogm_cnt_lock);
535+
ret = orig_node_add_if(orig_node, max_if_num);
536+
spin_unlock_bh(&orig_node->ogm_cnt_lock);
537+
538+
if (ret == -1)
534539
goto err;
535540
}
536541
rcu_read_unlock();
@@ -619,8 +624,10 @@ int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
619624
hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
620625
orig_node = bucket->data;
621626

627+
spin_lock_bh(&orig_node->ogm_cnt_lock);
622628
ret = orig_node_del_if(orig_node, max_if_num,
623629
batman_if->if_num);
630+
spin_unlock_bh(&orig_node->ogm_cnt_lock);
624631

625632
if (ret == -1)
626633
goto err;

‎net/batman-adv/routing.c‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ void slide_own_bcast_window(struct batman_if *batman_if)
5555
rcu_read_lock();
5656
hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
5757
orig_node = bucket->data;
58+
spin_lock_bh(&orig_node->ogm_cnt_lock);
5859
word_index = batman_if->if_num * NUM_WORDS;
5960
word = &(orig_node->bcast_own[word_index]);
6061

6162
bit_get_packet(bat_priv, word, 1, 0);
6263
orig_node->bcast_own_sum[batman_if->if_num] =
6364
bit_packet_count(word);
65+
spin_unlock_bh(&orig_node->ogm_cnt_lock);
6466
}
6567
rcu_read_unlock();
6668
}
@@ -278,8 +280,10 @@ static void update_orig(struct bat_priv *bat_priv,
278280
char is_duplicate)
279281
{
280282
struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
283+
struct orig_node *orig_node_tmp;
281284
struct hlist_node *node;
282285
int tmp_hna_buff_len;
286+
uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
283287

284288
bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
285289
"Searching and updating originator entry of received packet\n");
@@ -351,10 +355,22 @@ static void update_orig(struct bat_priv *bat_priv,
351355
/* if the TQ is the same and the link not more symetric we
352356
* won't consider it either */
353357
if ((orig_node->router) &&
354-
((neigh_node->tq_avg == orig_node->router->tq_avg) &&
355-
(orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
356-
>= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
357-
goto update_hna;
358+
(neigh_node->tq_avg == orig_node->router->tq_avg)) {
359+
orig_node_tmp = orig_node->router->orig_node;
360+
spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
361+
bcast_own_sum_orig =
362+
orig_node_tmp->bcast_own_sum[if_incoming->if_num];
363+
spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
364+
365+
orig_node_tmp = neigh_node->orig_node;
366+
spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
367+
bcast_own_sum_neigh =
368+
orig_node_tmp->bcast_own_sum[if_incoming->if_num];
369+
spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
370+
371+
if (bcast_own_sum_orig >= bcast_own_sum_neigh)
372+
goto update_hna;
373+
}
358374

359375
update_routes(bat_priv, orig_node, neigh_node,
360376
hna_buff, tmp_hna_buff_len);
@@ -705,10 +721,13 @@ void receive_bat_packet(struct ethhdr *ethhdr,
705721
batman_packet->orig) &&
706722
(batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
707723
offset = if_incoming->if_num * NUM_WORDS;
724+
725+
spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
708726
word = &(orig_neigh_node->bcast_own[offset]);
709727
bit_mark(word, 0);
710728
orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
711729
bit_packet_count(word);
730+
spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
712731
}
713732

714733
bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "

‎net/batman-adv/types.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct orig_node {
8989
struct kref refcount;
9090
struct bat_priv *bat_priv;
9191
unsigned long last_frag_packet;
92+
spinlock_t ogm_cnt_lock; /* protects ogm counter */
9293
struct {
9394
uint8_t candidates;
9495
struct neigh_node *selected;

0 commit comments

Comments
 (0)