Skip to content

Commit e1a5382

Browse files
T-Xecsv
authored andcommitted
batman-adv: Make orig_node->router an rcu protected pointer
The rcu protected macros rcu_dereference() and rcu_assign_pointer() for the orig_node->router need to be used, as well as spin/rcu locking. Otherwise we might end up using a router pointer pointing to already freed memory. Therefore this commit introduces the safe getter method orig_node_get_router(). Signed-off-by: Linus Lüssing <linus.luessing@web.de> Signed-off-by: Marek Lindner <lindner_marek@yahoo.de> Signed-off-by: Sven Eckelmann <sven@narfation.org>
1 parent 57f0c07 commit e1a5382

File tree

8 files changed

+232
-212
lines changed

8 files changed

+232
-212
lines changed

net/batman-adv/gateway_client.c

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void gw_election(struct bat_priv *bat_priv)
9898
{
9999
struct hlist_node *node;
100100
struct gw_node *gw_node, *curr_gw, *curr_gw_tmp = NULL;
101+
struct neigh_node *router;
101102
uint8_t max_tq = 0;
102103
uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
103104
int down, up;
@@ -133,26 +134,26 @@ void gw_election(struct bat_priv *bat_priv)
133134
}
134135

135136
hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
136-
if (!gw_node->orig_node->router)
137+
if (gw_node->deleted)
137138
continue;
138139

139-
if (gw_node->deleted)
140+
router = orig_node_get_router(gw_node->orig_node);
141+
if (!router)
140142
continue;
141143

142144
switch (atomic_read(&bat_priv->gw_sel_class)) {
143145
case 1: /* fast connection */
144146
gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags,
145147
&down, &up);
146148

147-
tmp_gw_factor = (gw_node->orig_node->router->tq_avg *
148-
gw_node->orig_node->router->tq_avg *
149+
tmp_gw_factor = (router->tq_avg * router->tq_avg *
149150
down * 100 * 100) /
150151
(TQ_LOCAL_WINDOW_SIZE *
151152
TQ_LOCAL_WINDOW_SIZE * 64);
152153

153154
if ((tmp_gw_factor > max_gw_factor) ||
154155
((tmp_gw_factor == max_gw_factor) &&
155-
(gw_node->orig_node->router->tq_avg > max_tq)))
156+
(router->tq_avg > max_tq)))
156157
curr_gw_tmp = gw_node;
157158
break;
158159

@@ -164,19 +165,25 @@ void gw_election(struct bat_priv *bat_priv)
164165
* soon as a better gateway appears which has
165166
* $routing_class more tq points)
166167
**/
167-
if (gw_node->orig_node->router->tq_avg > max_tq)
168+
if (router->tq_avg > max_tq)
168169
curr_gw_tmp = gw_node;
169170
break;
170171
}
171172

172-
if (gw_node->orig_node->router->tq_avg > max_tq)
173-
max_tq = gw_node->orig_node->router->tq_avg;
173+
if (router->tq_avg > max_tq)
174+
max_tq = router->tq_avg;
174175

175176
if (tmp_gw_factor > max_gw_factor)
176177
max_gw_factor = tmp_gw_factor;
178+
179+
neigh_node_free_ref(router);
177180
}
178181

179182
if (curr_gw != curr_gw_tmp) {
183+
router = orig_node_get_router(curr_gw_tmp->orig_node);
184+
if (!router)
185+
goto out;
186+
180187
if ((curr_gw) && (!curr_gw_tmp))
181188
bat_dbg(DBG_BATMAN, bat_priv,
182189
"Removing selected gateway - "
@@ -187,45 +194,47 @@ void gw_election(struct bat_priv *bat_priv)
187194
"(gw_flags: %i, tq: %i)\n",
188195
curr_gw_tmp->orig_node->orig,
189196
curr_gw_tmp->orig_node->gw_flags,
190-
curr_gw_tmp->orig_node->router->tq_avg);
197+
router->tq_avg);
191198
else
192199
bat_dbg(DBG_BATMAN, bat_priv,
193200
"Changing route to gateway %pM "
194201
"(gw_flags: %i, tq: %i)\n",
195202
curr_gw_tmp->orig_node->orig,
196203
curr_gw_tmp->orig_node->gw_flags,
197-
curr_gw_tmp->orig_node->router->tq_avg);
204+
router->tq_avg);
198205

206+
neigh_node_free_ref(router);
199207
gw_select(bat_priv, curr_gw_tmp);
200208
}
201209

210+
out:
202211
rcu_read_unlock();
203212
}
204213

205214
void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
206215
{
207216
struct orig_node *curr_gw_orig;
217+
struct neigh_node *router_gw = NULL, *router_orig = NULL;
208218
uint8_t gw_tq_avg, orig_tq_avg;
209219

210220
curr_gw_orig = gw_get_selected(bat_priv);
211221
if (!curr_gw_orig)
212222
goto deselect;
213223

214-
rcu_read_lock();
215-
if (!curr_gw_orig->router)
216-
goto deselect_rcu;
224+
router_gw = orig_node_get_router(curr_gw_orig);
225+
if (!router_gw)
226+
goto deselect;
217227

218228
/* this node already is the gateway */
219229
if (curr_gw_orig == orig_node)
220-
goto out_rcu;
221-
222-
if (!orig_node->router)
223-
goto out_rcu;
230+
goto out;
224231

225-
gw_tq_avg = curr_gw_orig->router->tq_avg;
226-
rcu_read_unlock();
232+
router_orig = orig_node_get_router(orig_node);
233+
if (!router_orig)
234+
goto out;
227235

228-
orig_tq_avg = orig_node->router->tq_avg;
236+
gw_tq_avg = router_gw->tq_avg;
237+
orig_tq_avg = router_orig->tq_avg;
229238

230239
/* the TQ value has to be better */
231240
if (orig_tq_avg < gw_tq_avg)
@@ -243,18 +252,16 @@ void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
243252
"Restarting gateway selection: better gateway found (tq curr: "
244253
"%i, tq new: %i)\n",
245254
gw_tq_avg, orig_tq_avg);
246-
goto deselect;
247255

248-
out_rcu:
249-
rcu_read_unlock();
250-
goto out;
251-
deselect_rcu:
252-
rcu_read_unlock();
253256
deselect:
254257
gw_deselect(bat_priv);
255258
out:
256259
if (curr_gw_orig)
257260
orig_node_free_ref(curr_gw_orig);
261+
if (router_gw)
262+
neigh_node_free_ref(router_gw);
263+
if (router_orig)
264+
neigh_node_free_ref(router_orig);
258265

259266
return;
260267
}
@@ -362,30 +369,39 @@ void gw_node_purge(struct bat_priv *bat_priv)
362369
spin_unlock_bh(&bat_priv->gw_list_lock);
363370
}
364371

372+
/**
373+
* fails if orig_node has no router
374+
*/
365375
static int _write_buffer_text(struct bat_priv *bat_priv,
366376
struct seq_file *seq, struct gw_node *gw_node)
367377
{
368378
struct gw_node *curr_gw;
369-
int down, up, ret;
379+
struct neigh_node *router;
380+
int down, up, ret = -1;
370381

371382
gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
372383

384+
router = orig_node_get_router(gw_node->orig_node);
385+
if (!router)
386+
goto out;
387+
373388
rcu_read_lock();
374389
curr_gw = rcu_dereference(bat_priv->curr_gw);
375390

376391
ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
377392
(curr_gw == gw_node ? "=>" : " "),
378393
gw_node->orig_node->orig,
379-
gw_node->orig_node->router->tq_avg,
380-
gw_node->orig_node->router->addr,
381-
gw_node->orig_node->router->if_incoming->net_dev->name,
394+
router->tq_avg, router->addr,
395+
router->if_incoming->net_dev->name,
382396
gw_node->orig_node->gw_flags,
383397
(down > 2048 ? down / 1024 : down),
384398
(down > 2048 ? "MBit" : "KBit"),
385399
(up > 2048 ? up / 1024 : up),
386400
(up > 2048 ? "MBit" : "KBit"));
387401

388402
rcu_read_unlock();
403+
neigh_node_free_ref(router);
404+
out:
389405
return ret;
390406
}
391407

@@ -423,10 +439,10 @@ int gw_client_seq_print_text(struct seq_file *seq, void *offset)
423439
if (gw_node->deleted)
424440
continue;
425441

426-
if (!gw_node->orig_node->router)
442+
/* fails if orig_node has no router */
443+
if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
427444
continue;
428445

429-
_write_buffer_text(bat_priv, seq, gw_node);
430446
gw_count++;
431447
}
432448
rcu_read_unlock();

net/batman-adv/icmp_socket.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,23 +218,13 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,
218218
if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
219219
goto dst_unreach;
220220

221-
rcu_read_lock();
222221
orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
223-
224222
if (!orig_node)
225-
goto unlock;
226-
227-
neigh_node = orig_node->router;
223+
goto dst_unreach;
228224

225+
neigh_node = orig_node_get_router(orig_node);
229226
if (!neigh_node)
230-
goto unlock;
231-
232-
if (!atomic_inc_not_zero(&neigh_node->refcount)) {
233-
neigh_node = NULL;
234-
goto unlock;
235-
}
236-
237-
rcu_read_unlock();
227+
goto dst_unreach;
238228

239229
if (!neigh_node->if_incoming)
240230
goto dst_unreach;
@@ -252,8 +242,6 @@ static ssize_t bat_socket_write(struct file *file, const char __user *buff,
252242
send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
253243
goto out;
254244

255-
unlock:
256-
rcu_read_unlock();
257245
dst_unreach:
258246
icmp_packet->msg_type = DESTINATION_UNREACHABLE;
259247
bat_socket_add_packet(socket_client, icmp_packet, packet_len);

net/batman-adv/originator.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ void neigh_node_free_ref(struct neigh_node *neigh_node)
7070
call_rcu(&neigh_node->rcu, neigh_node_free_rcu);
7171
}
7272

73+
/* increases the refcounter of a found router */
74+
struct neigh_node *orig_node_get_router(struct orig_node *orig_node)
75+
{
76+
struct neigh_node *router;
77+
78+
rcu_read_lock();
79+
router = rcu_dereference(orig_node->router);
80+
81+
if (router && !atomic_inc_not_zero(&router->refcount))
82+
router = NULL;
83+
84+
rcu_read_unlock();
85+
return router;
86+
}
87+
7388
struct neigh_node *create_neighbor(struct orig_node *orig_node,
7489
struct orig_node *orig_neigh_node,
7590
uint8_t *neigh,
@@ -390,7 +405,7 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
390405
struct hlist_node *node, *node_tmp;
391406
struct hlist_head *head;
392407
struct orig_node *orig_node;
393-
struct neigh_node *neigh_node;
408+
struct neigh_node *neigh_node, *neigh_node_tmp;
394409
int batman_count = 0;
395410
int last_seen_secs;
396411
int last_seen_msecs;
@@ -421,37 +436,41 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
421436

422437
rcu_read_lock();
423438
hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
424-
if (!orig_node->router)
439+
neigh_node = orig_node_get_router(orig_node);
440+
if (!neigh_node)
425441
continue;
426442

427-
if (orig_node->router->tq_avg == 0)
428-
continue;
443+
if (neigh_node->tq_avg == 0)
444+
goto next;
429445

430446
last_seen_secs = jiffies_to_msecs(jiffies -
431447
orig_node->last_valid) / 1000;
432448
last_seen_msecs = jiffies_to_msecs(jiffies -
433449
orig_node->last_valid) % 1000;
434450

435-
neigh_node = orig_node->router;
436451
seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
437452
orig_node->orig, last_seen_secs,
438453
last_seen_msecs, neigh_node->tq_avg,
439454
neigh_node->addr,
440455
neigh_node->if_incoming->net_dev->name);
441456

442-
hlist_for_each_entry_rcu(neigh_node, node_tmp,
457+
hlist_for_each_entry_rcu(neigh_node_tmp, node_tmp,
443458
&orig_node->neigh_list, list) {
444-
seq_printf(seq, " %pM (%3i)", neigh_node->addr,
445-
neigh_node->tq_avg);
459+
seq_printf(seq, " %pM (%3i)",
460+
neigh_node_tmp->addr,
461+
neigh_node_tmp->tq_avg);
446462
}
447463

448464
seq_printf(seq, "\n");
449465
batman_count++;
466+
467+
next:
468+
neigh_node_free_ref(neigh_node);
450469
}
451470
rcu_read_unlock();
452471
}
453472

454-
if ((batman_count == 0))
473+
if (batman_count == 0)
455474
seq_printf(seq, "No batman nodes in range ...\n");
456475

457476
return 0;

net/batman-adv/originator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,
3434
uint8_t *neigh,
3535
struct hard_iface *if_incoming);
3636
void neigh_node_free_ref(struct neigh_node *neigh_node);
37+
struct neigh_node *orig_node_get_router(struct orig_node *orig_node);
3738
int orig_seq_print_text(struct seq_file *seq, void *offset);
3839
int orig_hash_add_if(struct hard_iface *hard_iface, int max_if_num);
3940
int orig_hash_del_if(struct hard_iface *hard_iface, int max_if_num);

0 commit comments

Comments
 (0)