originator.c 17.3 KB
Newer Older
1
/* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Marek Lindner, Simon Wunderlich
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

#include "main.h"
21
#include "distributed-arp-table.h"
22 23 24 25 26 27 28 29
#include "originator.h"
#include "hash.h"
#include "translation-table.h"
#include "routing.h"
#include "gateway_client.h"
#include "hard-interface.h"
#include "unicast.h"
#include "soft-interface.h"
30
#include "bridge_loop_avoidance.h"
31
#include "network-coding.h"
32

33 34 35
/* hash class keys */
static struct lock_class_key batadv_orig_hash_lock_class_key;

36
static void batadv_purge_orig(struct work_struct *work);
37

38
/* returns 1 if they are the same originator */
39
static int batadv_compare_orig(const struct hlist_node *node, const void *data2)
40
{
41 42
	const void *data1 = container_of(node, struct batadv_orig_node,
					 hash_entry);
43 44 45 46

	return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
}

47
int batadv_originator_init(struct batadv_priv *bat_priv)
48 49
{
	if (bat_priv->orig_hash)
50
		return 0;
51

52
	bat_priv->orig_hash = batadv_hash_new(1024);
53 54 55 56

	if (!bat_priv->orig_hash)
		goto err;

57 58 59
	batadv_hash_set_lock_class(bat_priv->orig_hash,
				   &batadv_orig_hash_lock_class_key);

60 61 62 63 64
	INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
	queue_delayed_work(batadv_event_workqueue,
			   &bat_priv->orig_work,
			   msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));

65
	return 0;
66 67

err:
68
	return -ENOMEM;
69 70
}

71
void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
72
{
73
	if (atomic_dec_and_test(&neigh_node->refcount))
74
		kfree_rcu(neigh_node, rcu);
75 76
}

77
/* increases the refcounter of a found router */
78 79
struct batadv_neigh_node *
batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
80
{
81
	struct batadv_neigh_node *router;
82 83 84 85 86 87 88 89 90 91 92

	rcu_read_lock();
	router = rcu_dereference(orig_node->router);

	if (router && !atomic_inc_not_zero(&router->refcount))
		router = NULL;

	rcu_read_unlock();
	return router;
}

93 94 95
struct batadv_neigh_node *
batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
		      const uint8_t *neigh_addr, uint32_t seqno)
96
{
97 98
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
	struct batadv_neigh_node *neigh_node;
99

100
	neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
101
	if (!neigh_node)
102
		goto out;
103

104
	INIT_HLIST_NODE(&neigh_node->list);
105

106
	memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
107
	spin_lock_init(&neigh_node->lq_update_lock);
108 109 110

	/* extra reference for return */
	atomic_set(&neigh_node->refcount, 2);
111

112
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
113 114
		   "Creating new neighbor %pM, initial seqno %d\n",
		   neigh_addr, seqno);
115 116

out:
117 118 119
	return neigh_node;
}

120
static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
121
{
122
	struct hlist_node *node_tmp;
123 124
	struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
	struct batadv_orig_node *orig_node;
125

126
	orig_node = container_of(rcu, struct batadv_orig_node, rcu);
127

128 129
	spin_lock_bh(&orig_node->neigh_list_lock);

130 131 132 133
	/* for all bonding members ... */
	list_for_each_entry_safe(neigh_node, tmp_neigh_node,
				 &orig_node->bond_list, bonding_list) {
		list_del_rcu(&neigh_node->bonding_list);
134
		batadv_neigh_node_free_ref(neigh_node);
135 136
	}

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);
142 143
	}

144 145
	spin_unlock_bh(&orig_node->neigh_list_lock);

146 147 148
	/* Free nc_nodes */
	batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);

149
	batadv_frag_list_free(&orig_node->frag_list);
150 151
	batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
				  "originator timed out");
152

153
	kfree(orig_node->tt_buff);
154 155 156 157 158
	kfree(orig_node->bcast_own);
	kfree(orig_node->bcast_own_sum);
	kfree(orig_node);
}

159
void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
160 161
{
	if (atomic_dec_and_test(&orig_node->refcount))
162
		call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
163 164
}

165
void batadv_originator_free(struct batadv_priv *bat_priv)
166
{
167
	struct batadv_hashtable *hash = bat_priv->orig_hash;
168
	struct hlist_node *node_tmp;
169 170
	struct hlist_head *head;
	spinlock_t *list_lock; /* spinlock to protect write access */
171
	struct batadv_orig_node *orig_node;
172
	uint32_t i;
173 174

	if (!hash)
175 176 177 178 179
		return;

	cancel_delayed_work_sync(&bat_priv->orig_work);

	bat_priv->orig_hash = NULL;
180 181 182 183 184 185

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
		list_lock = &hash->list_locks[i];

		spin_lock_bh(list_lock);
186
		hlist_for_each_entry_safe(orig_node, node_tmp,
187
					  head, hash_entry) {
188
			hlist_del_rcu(&orig_node->hash_entry);
189
			batadv_orig_node_free_ref(orig_node);
190 191 192 193
		}
		spin_unlock_bh(list_lock);
	}

194
	batadv_hash_destroy(hash);
195 196 197
}

/* this function finds or creates an originator entry for the given
198 199
 * address if it does not exits
 */
200 201
struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
					      const uint8_t *addr)
202
{
203
	struct batadv_orig_node *orig_node;
204 205
	int size;
	int hash_added;
206
	unsigned long reset_time;
207

208
	orig_node = batadv_orig_hash_find(bat_priv, addr);
209
	if (orig_node)
210 211
		return orig_node;

212 213
	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
		   "Creating new originator: %pM\n", addr);
214

215
	orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
216 217 218
	if (!orig_node)
		return NULL;

219
	INIT_HLIST_HEAD(&orig_node->neigh_list);
220
	INIT_LIST_HEAD(&orig_node->bond_list);
221
	spin_lock_init(&orig_node->ogm_cnt_lock);
222
	spin_lock_init(&orig_node->bcast_seqno_lock);
223
	spin_lock_init(&orig_node->neigh_list_lock);
224
	spin_lock_init(&orig_node->tt_buff_lock);
225

226 227
	batadv_nc_init_orig(orig_node);

228 229
	/* extra reference for return */
	atomic_set(&orig_node->refcount, 2);
230

231
	orig_node->tt_initialised = false;
232
	orig_node->bat_priv = bat_priv;
233
	memcpy(orig_node->orig, addr, ETH_ALEN);
234
	batadv_dat_init_orig_node_addr(orig_node);
235
	orig_node->router = NULL;
236 237
	orig_node->tt_crc = 0;
	atomic_set(&orig_node->last_ttvn, 0);
238
	orig_node->tt_buff = NULL;
239 240
	orig_node->tt_buff_len = 0;
	atomic_set(&orig_node->tt_size, 0);
241 242 243
	reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
	orig_node->bcast_seqno_reset = reset_time;
	orig_node->batman_seqno_reset = reset_time;
244

245 246
	atomic_set(&orig_node->bond_candidates, 0);

247
	size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
248 249 250 251 252 253 254 255 256 257 258 259 260 261

	orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
	if (!orig_node->bcast_own)
		goto free_orig_node;

	size = bat_priv->num_ifaces * sizeof(uint8_t);
	orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);

	INIT_LIST_HEAD(&orig_node->frag_list);
	orig_node->last_frag_packet = 0;

	if (!orig_node->bcast_own_sum)
		goto free_bcast_own;

262
	hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
263
				     batadv_choose_orig, orig_node,
264
				     &orig_node->hash_entry);
265
	if (hash_added != 0)
266 267 268 269 270 271 272 273 274 275 276 277
		goto free_bcast_own_sum;

	return orig_node;
free_bcast_own_sum:
	kfree(orig_node->bcast_own_sum);
free_bcast_own:
	kfree(orig_node->bcast_own);
free_orig_node:
	kfree(orig_node);
	return NULL;
}

278 279 280 281
static bool
batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
			    struct batadv_orig_node *orig_node,
			    struct batadv_neigh_node **best_neigh_node)
282
{
283
	struct hlist_node *node_tmp;
284
	struct batadv_neigh_node *neigh_node;
285
	bool neigh_purged = false;
286
	unsigned long last_seen;
287
	struct batadv_hard_iface *if_incoming;
288 289 290

	*best_neigh_node = NULL;

291 292
	spin_lock_bh(&orig_node->neigh_list_lock);

293
	/* for all neighbors towards this originator ... */
294
	hlist_for_each_entry_safe(neigh_node, node_tmp,
295
				  &orig_node->neigh_list, list) {
296 297 298
		last_seen = neigh_node->last_seen;
		if_incoming = neigh_node->if_incoming;

299
		if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
300 301 302 303 304 305
		    (if_incoming->if_status == BATADV_IF_INACTIVE) ||
		    (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
		    (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
			if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
			    (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
			    (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
306
				batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
307 308 309
					   "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
					   orig_node->orig, neigh_node->addr,
					   if_incoming->net_dev->name);
310
			else
311
				batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
312 313 314
					   "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
					   orig_node->orig, neigh_node->addr,
					   jiffies_to_msecs(last_seen));
315 316

			neigh_purged = true;
317

318
			hlist_del_rcu(&neigh_node->list);
319
			batadv_bonding_candidate_del(orig_node, neigh_node);
320
			batadv_neigh_node_free_ref(neigh_node);
321 322 323 324 325 326
		} else {
			if ((!*best_neigh_node) ||
			    (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
				*best_neigh_node = neigh_node;
		}
	}
327 328

	spin_unlock_bh(&orig_node->neigh_list_lock);
329 330 331
	return neigh_purged;
}

332 333
static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
				   struct batadv_orig_node *orig_node)
334
{
335
	struct batadv_neigh_node *best_neigh_node;
336

337 338
	if (batadv_has_timed_out(orig_node->last_seen,
				 2 * BATADV_PURGE_TIMEOUT)) {
339
		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
340 341 342
			   "Originator timeout: originator %pM, last_seen %u\n",
			   orig_node->orig,
			   jiffies_to_msecs(orig_node->last_seen));
343 344
		return true;
	} else {
345 346
		if (batadv_purge_orig_neighbors(bat_priv, orig_node,
						&best_neigh_node))
347 348
			batadv_update_route(bat_priv, orig_node,
					    best_neigh_node);
349 350 351 352 353
	}

	return false;
}

354
static void _batadv_purge_orig(struct batadv_priv *bat_priv)
355
{
356
	struct batadv_hashtable *hash = bat_priv->orig_hash;
357
	struct hlist_node *node_tmp;
358
	struct hlist_head *head;
359
	spinlock_t *list_lock; /* spinlock to protect write access */
360
	struct batadv_orig_node *orig_node;
361
	uint32_t i;
362 363 364 365 366 367 368

	if (!hash)
		return;

	/* for all origins... */
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
369
		list_lock = &hash->list_locks[i];
370

371
		spin_lock_bh(list_lock);
372
		hlist_for_each_entry_safe(orig_node, node_tmp,
373
					  head, hash_entry) {
374
			if (batadv_purge_orig_node(bat_priv, orig_node)) {
375
				if (orig_node->gw_flags)
376 377
					batadv_gw_node_delete(bat_priv,
							      orig_node);
378
				hlist_del_rcu(&orig_node->hash_entry);
379
				batadv_orig_node_free_ref(orig_node);
380
				continue;
381 382
			}

383
			if (batadv_has_timed_out(orig_node->last_frag_packet,
384
						 BATADV_FRAG_TIMEOUT))
385
				batadv_frag_list_free(&orig_node->frag_list);
386
		}
387
		spin_unlock_bh(list_lock);
388 389
	}

390 391
	batadv_gw_node_purge(bat_priv);
	batadv_gw_election(bat_priv);
392 393
}

394
static void batadv_purge_orig(struct work_struct *work)
395
{
396 397
	struct delayed_work *delayed_work;
	struct batadv_priv *bat_priv;
398

399 400
	delayed_work = container_of(work, struct delayed_work, work);
	bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
401
	_batadv_purge_orig(bat_priv);
402 403 404
	queue_delayed_work(batadv_event_workqueue,
			   &bat_priv->orig_work,
			   msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
405 406
}

407
void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
408
{
409
	_batadv_purge_orig(bat_priv);
410 411
}

412
int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
413 414
{
	struct net_device *net_dev = (struct net_device *)seq->private;
415
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
416
	struct batadv_hashtable *hash = bat_priv->orig_hash;
417
	struct hlist_head *head;
418 419 420
	struct batadv_hard_iface *primary_if;
	struct batadv_orig_node *orig_node;
	struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
421 422 423
	int batman_count = 0;
	int last_seen_secs;
	int last_seen_msecs;
424
	unsigned long last_seen_jiffies;
425
	uint32_t i;
426

427 428
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
429
		goto out;
430

431
	seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
432
		   BATADV_SOURCE_VERSION, primary_if->net_dev->name,
433
		   primary_if->net_dev->dev_addr, net_dev->name);
434
	seq_printf(seq, "  %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
435 436
		   "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
		   "Nexthop", "outgoingIF", "Potential nexthops");
437 438 439 440

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

441
		rcu_read_lock();
442
		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
443
			neigh_node = batadv_orig_node_get_router(orig_node);
444
			if (!neigh_node)
445 446
				continue;

447 448
			if (neigh_node->tq_avg == 0)
				goto next;
449

450 451 452 453
			last_seen_jiffies = jiffies - orig_node->last_seen;
			last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
			last_seen_secs = last_seen_msecs / 1000;
			last_seen_msecs = last_seen_msecs % 1000;
454 455 456 457 458 459 460

			seq_printf(seq, "%pM %4i.%03is   (%3i) %pM [%10s]:",
				   orig_node->orig, last_seen_secs,
				   last_seen_msecs, neigh_node->tq_avg,
				   neigh_node->addr,
				   neigh_node->if_incoming->net_dev->name);

461
			hlist_for_each_entry_rcu(neigh_node_tmp,
462
						 &orig_node->neigh_list, list) {
463 464 465
				seq_printf(seq, " %pM (%3i)",
					   neigh_node_tmp->addr,
					   neigh_node_tmp->tq_avg);
466 467 468 469
			}

			seq_printf(seq, "\n");
			batman_count++;
470 471

next:
472
			batadv_neigh_node_free_ref(neigh_node);
473
		}
474
		rcu_read_unlock();
475 476
	}

477
	if (batman_count == 0)
478 479
		seq_printf(seq, "No batman nodes in range ...\n");

480 481
out:
	if (primary_if)
482
		batadv_hardif_free_ref(primary_if);
483
	return 0;
484 485
}

486 487
static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
				   int max_if_num)
488 489
{
	void *data_ptr;
490
	size_t data_size, old_size;
491

492 493 494
	data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
	old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
	data_ptr = kmalloc(data_size, GFP_ATOMIC);
495
	if (!data_ptr)
496
		return -ENOMEM;
497

498
	memcpy(data_ptr, orig_node->bcast_own, old_size);
499 500 501 502
	kfree(orig_node->bcast_own);
	orig_node->bcast_own = data_ptr;

	data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
503
	if (!data_ptr)
504
		return -ENOMEM;
505 506 507 508 509 510 511 512 513

	memcpy(data_ptr, orig_node->bcast_own_sum,
	       (max_if_num - 1) * sizeof(uint8_t));
	kfree(orig_node->bcast_own_sum);
	orig_node->bcast_own_sum = data_ptr;

	return 0;
}

514 515
int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
			    int max_if_num)
516
{
517
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
518
	struct batadv_hashtable *hash = bat_priv->orig_hash;
519
	struct hlist_head *head;
520
	struct batadv_orig_node *orig_node;
521 522
	uint32_t i;
	int ret;
523 524

	/* resize all orig nodes because orig_node->bcast_own(_sum) depend on
525 526
	 * if_num
	 */
527 528 529
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

530
		rcu_read_lock();
531
		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
532
			spin_lock_bh(&orig_node->ogm_cnt_lock);
533
			ret = batadv_orig_node_add_if(orig_node, max_if_num);
534 535
			spin_unlock_bh(&orig_node->ogm_cnt_lock);

536
			if (ret == -ENOMEM)
537 538
				goto err;
		}
539
		rcu_read_unlock();
540 541 542 543 544
	}

	return 0;

err:
545
	rcu_read_unlock();
546 547 548
	return -ENOMEM;
}

549
static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
550
				   int max_if_num, int del_if_num)
551 552 553 554 555 556 557 558
{
	void *data_ptr = NULL;
	int chunk_size;

	/* last interface was removed */
	if (max_if_num == 0)
		goto free_bcast_own;

559
	chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
560
	data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
561
	if (!data_ptr)
562
		return -ENOMEM;
563 564 565 566 567

	/* copy first part */
	memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);

	/* copy second part */
568
	memcpy((char *)data_ptr + del_if_num * chunk_size,
569 570 571 572 573 574 575 576 577 578 579
	       orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
	       (max_if_num - del_if_num) * chunk_size);

free_bcast_own:
	kfree(orig_node->bcast_own);
	orig_node->bcast_own = data_ptr;

	if (max_if_num == 0)
		goto free_own_sum;

	data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
580
	if (!data_ptr)
581
		return -ENOMEM;
582 583 584 585

	memcpy(data_ptr, orig_node->bcast_own_sum,
	       del_if_num * sizeof(uint8_t));

586
	memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
587 588 589 590 591 592 593 594 595 596
	       orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
	       (max_if_num - del_if_num) * sizeof(uint8_t));

free_own_sum:
	kfree(orig_node->bcast_own_sum);
	orig_node->bcast_own_sum = data_ptr;

	return 0;
}

597 598
int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
			    int max_if_num)
599
{
600
	struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
601
	struct batadv_hashtable *hash = bat_priv->orig_hash;
602
	struct hlist_head *head;
603 604
	struct batadv_hard_iface *hard_iface_tmp;
	struct batadv_orig_node *orig_node;
605 606
	uint32_t i;
	int ret;
607 608

	/* resize all orig nodes because orig_node->bcast_own(_sum) depend on
609 610
	 * if_num
	 */
611 612 613
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

614
		rcu_read_lock();
615
		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
616
			spin_lock_bh(&orig_node->ogm_cnt_lock);
617 618
			ret = batadv_orig_node_del_if(orig_node, max_if_num,
						      hard_iface->if_num);
619
			spin_unlock_bh(&orig_node->ogm_cnt_lock);
620

621
			if (ret == -ENOMEM)
622 623
				goto err;
		}
624
		rcu_read_unlock();
625 626 627 628
	}

	/* renumber remaining batman interfaces _inside_ of orig_hash_lock */
	rcu_read_lock();
629
	list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
630
		if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
631 632
			continue;

633
		if (hard_iface == hard_iface_tmp)
634 635
			continue;

636
		if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
637 638
			continue;

639 640
		if (hard_iface_tmp->if_num > hard_iface->if_num)
			hard_iface_tmp->if_num--;
641 642 643
	}
	rcu_read_unlock();

644
	hard_iface->if_num = -1;
645 646 647
	return 0;

err:
648
	rcu_read_unlock();
649 650
	return -ENOMEM;
}