Merge tag 'batman-adv-for-davem' of git://git.open-mesh.org/linux-merge
[cascardo/linux.git] / net / batman-adv / originator.c
1 /* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors:
2  *
3  * Marek Lindner, Simon Wunderlich
4  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA
18  */
19
20 #include "main.h"
21 #include "distributed-arp-table.h"
22 #include "originator.h"
23 #include "hash.h"
24 #include "translation-table.h"
25 #include "routing.h"
26 #include "gateway_client.h"
27 #include "hard-interface.h"
28 #include "unicast.h"
29 #include "soft-interface.h"
30 #include "bridge_loop_avoidance.h"
31 #include "network-coding.h"
32
33 /* hash class keys */
34 static struct lock_class_key batadv_orig_hash_lock_class_key;
35
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         const void *data1 = container_of(node, struct batadv_orig_node,
42                                          hash_entry);
43
44         return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45 }
46
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         if (!bat_priv->orig_hash)
55                 goto err;
56
57         batadv_hash_set_lock_class(bat_priv->orig_hash,
58                                    &batadv_orig_hash_lock_class_key);
59
60         INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
61         queue_delayed_work(batadv_event_workqueue,
62                            &bat_priv->orig_work,
63                            msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
64
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 struct batadv_neigh_node *
79 batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
80 {
81         struct batadv_neigh_node *router;
82
83         rcu_read_lock();
84         router = rcu_dereference(orig_node->router);
85
86         if (router && !atomic_inc_not_zero(&router->refcount))
87                 router = NULL;
88
89         rcu_read_unlock();
90         return router;
91 }
92
93 struct batadv_neigh_node *
94 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
95                       const uint8_t *neigh_addr)
96 {
97         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
98         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         /* extra reference for return */
110         atomic_set(&neigh_node->refcount, 2);
111
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);
115
116 out:
117         return neigh_node;
118 }
119
120 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
121 {
122         struct hlist_node *node_tmp;
123         struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
124         struct batadv_orig_node *orig_node;
125
126         orig_node = container_of(rcu, struct batadv_orig_node, rcu);
127
128         spin_lock_bh(&orig_node->neigh_list_lock);
129
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);
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         spin_unlock_bh(&orig_node->neigh_list_lock);
145
146         /* Free nc_nodes */
147         batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
148
149         batadv_frag_list_free(&orig_node->frag_list);
150         batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
151                                   "originator timed out");
152
153         kfree(orig_node->tt_buff);
154         kfree(orig_node->bcast_own);
155         kfree(orig_node->bcast_own_sum);
156         kfree(orig_node);
157 }
158
159 /**
160  * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
161  * schedule an rcu callback for freeing it
162  * @orig_node: the orig node to free
163  */
164 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
165 {
166         if (atomic_dec_and_test(&orig_node->refcount))
167                 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
168 }
169
170 /**
171  * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
172  * possibly free it (without rcu callback)
173  * @orig_node: the orig node to free
174  */
175 void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
176 {
177         if (atomic_dec_and_test(&orig_node->refcount))
178                 batadv_orig_node_free_rcu(&orig_node->rcu);
179 }
180
181 void batadv_originator_free(struct batadv_priv *bat_priv)
182 {
183         struct batadv_hashtable *hash = bat_priv->orig_hash;
184         struct hlist_node *node_tmp;
185         struct hlist_head *head;
186         spinlock_t *list_lock; /* spinlock to protect write access */
187         struct batadv_orig_node *orig_node;
188         uint32_t i;
189
190         if (!hash)
191                 return;
192
193         cancel_delayed_work_sync(&bat_priv->orig_work);
194
195         bat_priv->orig_hash = NULL;
196
197         for (i = 0; i < hash->size; i++) {
198                 head = &hash->table[i];
199                 list_lock = &hash->list_locks[i];
200
201                 spin_lock_bh(list_lock);
202                 hlist_for_each_entry_safe(orig_node, node_tmp,
203                                           head, hash_entry) {
204                         hlist_del_rcu(&orig_node->hash_entry);
205                         batadv_orig_node_free_ref(orig_node);
206                 }
207                 spin_unlock_bh(list_lock);
208         }
209
210         batadv_hash_destroy(hash);
211 }
212
213 /* this function finds or creates an originator entry for the given
214  * address if it does not exits
215  */
216 struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
217                                               const uint8_t *addr)
218 {
219         struct batadv_orig_node *orig_node;
220         int size;
221         int hash_added;
222         unsigned long reset_time;
223
224         orig_node = batadv_orig_hash_find(bat_priv, addr);
225         if (orig_node)
226                 return orig_node;
227
228         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
229                    "Creating new originator: %pM\n", addr);
230
231         orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
232         if (!orig_node)
233                 return NULL;
234
235         INIT_HLIST_HEAD(&orig_node->neigh_list);
236         INIT_LIST_HEAD(&orig_node->bond_list);
237         spin_lock_init(&orig_node->ogm_cnt_lock);
238         spin_lock_init(&orig_node->bcast_seqno_lock);
239         spin_lock_init(&orig_node->neigh_list_lock);
240         spin_lock_init(&orig_node->tt_buff_lock);
241
242         batadv_nc_init_orig(orig_node);
243
244         /* extra reference for return */
245         atomic_set(&orig_node->refcount, 2);
246
247         orig_node->tt_initialised = false;
248         orig_node->bat_priv = bat_priv;
249         memcpy(orig_node->orig, addr, ETH_ALEN);
250         batadv_dat_init_orig_node_addr(orig_node);
251         orig_node->router = NULL;
252         orig_node->tt_crc = 0;
253         atomic_set(&orig_node->last_ttvn, 0);
254         orig_node->tt_buff = NULL;
255         orig_node->tt_buff_len = 0;
256         atomic_set(&orig_node->tt_size, 0);
257         reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
258         orig_node->bcast_seqno_reset = reset_time;
259         orig_node->batman_seqno_reset = reset_time;
260
261         atomic_set(&orig_node->bond_candidates, 0);
262
263         size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
264
265         orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
266         if (!orig_node->bcast_own)
267                 goto free_orig_node;
268
269         size = bat_priv->num_ifaces * sizeof(uint8_t);
270         orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
271
272         INIT_LIST_HEAD(&orig_node->frag_list);
273         orig_node->last_frag_packet = 0;
274
275         if (!orig_node->bcast_own_sum)
276                 goto free_bcast_own;
277
278         hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
279                                      batadv_choose_orig, orig_node,
280                                      &orig_node->hash_entry);
281         if (hash_added != 0)
282                 goto free_bcast_own_sum;
283
284         return orig_node;
285 free_bcast_own_sum:
286         kfree(orig_node->bcast_own_sum);
287 free_bcast_own:
288         kfree(orig_node->bcast_own);
289 free_orig_node:
290         kfree(orig_node);
291         return NULL;
292 }
293
294 static bool
295 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
296                             struct batadv_orig_node *orig_node,
297                             struct batadv_neigh_node **best_neigh_node)
298 {
299         struct hlist_node *node_tmp;
300         struct batadv_neigh_node *neigh_node;
301         bool neigh_purged = false;
302         unsigned long last_seen;
303         struct batadv_hard_iface *if_incoming;
304
305         *best_neigh_node = NULL;
306
307         spin_lock_bh(&orig_node->neigh_list_lock);
308
309         /* for all neighbors towards this originator ... */
310         hlist_for_each_entry_safe(neigh_node, node_tmp,
311                                   &orig_node->neigh_list, list) {
312                 last_seen = neigh_node->last_seen;
313                 if_incoming = neigh_node->if_incoming;
314
315                 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
316                     (if_incoming->if_status == BATADV_IF_INACTIVE) ||
317                     (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
318                     (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
319                         if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
320                             (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
321                             (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
322                                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
323                                            "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
324                                            orig_node->orig, neigh_node->addr,
325                                            if_incoming->net_dev->name);
326                         else
327                                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
328                                            "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
329                                            orig_node->orig, neigh_node->addr,
330                                            jiffies_to_msecs(last_seen));
331
332                         neigh_purged = true;
333
334                         hlist_del_rcu(&neigh_node->list);
335                         batadv_bonding_candidate_del(orig_node, neigh_node);
336                         batadv_neigh_node_free_ref(neigh_node);
337                 } else {
338                         if ((!*best_neigh_node) ||
339                             (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
340                                 *best_neigh_node = neigh_node;
341                 }
342         }
343
344         spin_unlock_bh(&orig_node->neigh_list_lock);
345         return neigh_purged;
346 }
347
348 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
349                                    struct batadv_orig_node *orig_node)
350 {
351         struct batadv_neigh_node *best_neigh_node;
352
353         if (batadv_has_timed_out(orig_node->last_seen,
354                                  2 * BATADV_PURGE_TIMEOUT)) {
355                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
356                            "Originator timeout: originator %pM, last_seen %u\n",
357                            orig_node->orig,
358                            jiffies_to_msecs(orig_node->last_seen));
359                 return true;
360         } else {
361                 if (batadv_purge_orig_neighbors(bat_priv, orig_node,
362                                                 &best_neigh_node))
363                         batadv_update_route(bat_priv, orig_node,
364                                             best_neigh_node);
365         }
366
367         return false;
368 }
369
370 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
371 {
372         struct batadv_hashtable *hash = bat_priv->orig_hash;
373         struct hlist_node *node_tmp;
374         struct hlist_head *head;
375         spinlock_t *list_lock; /* spinlock to protect write access */
376         struct batadv_orig_node *orig_node;
377         uint32_t i;
378
379         if (!hash)
380                 return;
381
382         /* for all origins... */
383         for (i = 0; i < hash->size; i++) {
384                 head = &hash->table[i];
385                 list_lock = &hash->list_locks[i];
386
387                 spin_lock_bh(list_lock);
388                 hlist_for_each_entry_safe(orig_node, node_tmp,
389                                           head, hash_entry) {
390                         if (batadv_purge_orig_node(bat_priv, orig_node)) {
391                                 batadv_gw_node_delete(bat_priv, orig_node);
392                                 hlist_del_rcu(&orig_node->hash_entry);
393                                 batadv_orig_node_free_ref(orig_node);
394                                 continue;
395                         }
396
397                         if (batadv_has_timed_out(orig_node->last_frag_packet,
398                                                  BATADV_FRAG_TIMEOUT))
399                                 batadv_frag_list_free(&orig_node->frag_list);
400                 }
401                 spin_unlock_bh(list_lock);
402         }
403
404         batadv_gw_node_purge(bat_priv);
405         batadv_gw_election(bat_priv);
406 }
407
408 static void batadv_purge_orig(struct work_struct *work)
409 {
410         struct delayed_work *delayed_work;
411         struct batadv_priv *bat_priv;
412
413         delayed_work = container_of(work, struct delayed_work, work);
414         bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
415         _batadv_purge_orig(bat_priv);
416         queue_delayed_work(batadv_event_workqueue,
417                            &bat_priv->orig_work,
418                            msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
419 }
420
421 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
422 {
423         _batadv_purge_orig(bat_priv);
424 }
425
426 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
427 {
428         struct net_device *net_dev = (struct net_device *)seq->private;
429         struct batadv_priv *bat_priv = netdev_priv(net_dev);
430         struct batadv_hashtable *hash = bat_priv->orig_hash;
431         struct hlist_head *head;
432         struct batadv_hard_iface *primary_if;
433         struct batadv_orig_node *orig_node;
434         struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
435         int batman_count = 0;
436         int last_seen_secs;
437         int last_seen_msecs;
438         unsigned long last_seen_jiffies;
439         uint32_t i;
440
441         primary_if = batadv_seq_print_text_primary_if_get(seq);
442         if (!primary_if)
443                 goto out;
444
445         seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
446                    BATADV_SOURCE_VERSION, primary_if->net_dev->name,
447                    primary_if->net_dev->dev_addr, net_dev->name);
448         seq_printf(seq, "  %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
449                    "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
450                    "Nexthop", "outgoingIF", "Potential nexthops");
451
452         for (i = 0; i < hash->size; i++) {
453                 head = &hash->table[i];
454
455                 rcu_read_lock();
456                 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
457                         neigh_node = batadv_orig_node_get_router(orig_node);
458                         if (!neigh_node)
459                                 continue;
460
461                         if (neigh_node->tq_avg == 0)
462                                 goto next;
463
464                         last_seen_jiffies = jiffies - orig_node->last_seen;
465                         last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
466                         last_seen_secs = last_seen_msecs / 1000;
467                         last_seen_msecs = last_seen_msecs % 1000;
468
469                         seq_printf(seq, "%pM %4i.%03is   (%3i) %pM [%10s]:",
470                                    orig_node->orig, last_seen_secs,
471                                    last_seen_msecs, neigh_node->tq_avg,
472                                    neigh_node->addr,
473                                    neigh_node->if_incoming->net_dev->name);
474
475                         hlist_for_each_entry_rcu(neigh_node_tmp,
476                                                  &orig_node->neigh_list, list) {
477                                 seq_printf(seq, " %pM (%3i)",
478                                            neigh_node_tmp->addr,
479                                            neigh_node_tmp->tq_avg);
480                         }
481
482                         seq_puts(seq, "\n");
483                         batman_count++;
484
485 next:
486                         batadv_neigh_node_free_ref(neigh_node);
487                 }
488                 rcu_read_unlock();
489         }
490
491         if (batman_count == 0)
492                 seq_puts(seq, "No batman nodes in range ...\n");
493
494 out:
495         if (primary_if)
496                 batadv_hardif_free_ref(primary_if);
497         return 0;
498 }
499
500 static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
501                                    int max_if_num)
502 {
503         void *data_ptr;
504         size_t data_size, old_size;
505
506         data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
507         old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
508         data_ptr = kmalloc(data_size, GFP_ATOMIC);
509         if (!data_ptr)
510                 return -ENOMEM;
511
512         memcpy(data_ptr, orig_node->bcast_own, old_size);
513         kfree(orig_node->bcast_own);
514         orig_node->bcast_own = data_ptr;
515
516         data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
517         if (!data_ptr)
518                 return -ENOMEM;
519
520         memcpy(data_ptr, orig_node->bcast_own_sum,
521                (max_if_num - 1) * sizeof(uint8_t));
522         kfree(orig_node->bcast_own_sum);
523         orig_node->bcast_own_sum = data_ptr;
524
525         return 0;
526 }
527
528 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
529                             int max_if_num)
530 {
531         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
532         struct batadv_hashtable *hash = bat_priv->orig_hash;
533         struct hlist_head *head;
534         struct batadv_orig_node *orig_node;
535         uint32_t i;
536         int ret;
537
538         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
539          * if_num
540          */
541         for (i = 0; i < hash->size; i++) {
542                 head = &hash->table[i];
543
544                 rcu_read_lock();
545                 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
546                         spin_lock_bh(&orig_node->ogm_cnt_lock);
547                         ret = batadv_orig_node_add_if(orig_node, max_if_num);
548                         spin_unlock_bh(&orig_node->ogm_cnt_lock);
549
550                         if (ret == -ENOMEM)
551                                 goto err;
552                 }
553                 rcu_read_unlock();
554         }
555
556         return 0;
557
558 err:
559         rcu_read_unlock();
560         return -ENOMEM;
561 }
562
563 static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
564                                    int max_if_num, int del_if_num)
565 {
566         void *data_ptr = NULL;
567         int chunk_size;
568
569         /* last interface was removed */
570         if (max_if_num == 0)
571                 goto free_bcast_own;
572
573         chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
574         data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
575         if (!data_ptr)
576                 return -ENOMEM;
577
578         /* copy first part */
579         memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
580
581         /* copy second part */
582         memcpy((char *)data_ptr + del_if_num * chunk_size,
583                orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
584                (max_if_num - del_if_num) * chunk_size);
585
586 free_bcast_own:
587         kfree(orig_node->bcast_own);
588         orig_node->bcast_own = data_ptr;
589
590         if (max_if_num == 0)
591                 goto free_own_sum;
592
593         data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
594         if (!data_ptr)
595                 return -ENOMEM;
596
597         memcpy(data_ptr, orig_node->bcast_own_sum,
598                del_if_num * sizeof(uint8_t));
599
600         memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
601                orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
602                (max_if_num - del_if_num) * sizeof(uint8_t));
603
604 free_own_sum:
605         kfree(orig_node->bcast_own_sum);
606         orig_node->bcast_own_sum = data_ptr;
607
608         return 0;
609 }
610
611 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
612                             int max_if_num)
613 {
614         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
615         struct batadv_hashtable *hash = bat_priv->orig_hash;
616         struct hlist_head *head;
617         struct batadv_hard_iface *hard_iface_tmp;
618         struct batadv_orig_node *orig_node;
619         uint32_t i;
620         int ret;
621
622         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
623          * if_num
624          */
625         for (i = 0; i < hash->size; i++) {
626                 head = &hash->table[i];
627
628                 rcu_read_lock();
629                 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
630                         spin_lock_bh(&orig_node->ogm_cnt_lock);
631                         ret = batadv_orig_node_del_if(orig_node, max_if_num,
632                                                       hard_iface->if_num);
633                         spin_unlock_bh(&orig_node->ogm_cnt_lock);
634
635                         if (ret == -ENOMEM)
636                                 goto err;
637                 }
638                 rcu_read_unlock();
639         }
640
641         /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
642         rcu_read_lock();
643         list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
644                 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
645                         continue;
646
647                 if (hard_iface == hard_iface_tmp)
648                         continue;
649
650                 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
651                         continue;
652
653                 if (hard_iface_tmp->if_num > hard_iface->if_num)
654                         hard_iface_tmp->if_num--;
655         }
656         rcu_read_unlock();
657
658         hard_iface->if_num = -1;
659         return 0;
660
661 err:
662         rcu_read_unlock();
663         return -ENOMEM;
664 }