originator.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. #include "main.h"
  20. #include "distributed-arp-table.h"
  21. #include "originator.h"
  22. #include "hash.h"
  23. #include "translation-table.h"
  24. #include "routing.h"
  25. #include "gateway_client.h"
  26. #include "hard-interface.h"
  27. #include "unicast.h"
  28. #include "soft-interface.h"
  29. #include "bridge_loop_avoidance.h"
  30. #include "network-coding.h"
  31. /* hash class keys */
  32. static struct lock_class_key batadv_orig_hash_lock_class_key;
  33. static void batadv_purge_orig(struct work_struct *work);
  34. /* returns 1 if they are the same originator */
  35. static int batadv_compare_orig(const struct hlist_node *node, const void *data2)
  36. {
  37. const void *data1 = container_of(node, struct batadv_orig_node,
  38. hash_entry);
  39. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  40. }
  41. int batadv_originator_init(struct batadv_priv *bat_priv)
  42. {
  43. if (bat_priv->orig_hash)
  44. return 0;
  45. bat_priv->orig_hash = batadv_hash_new(1024);
  46. if (!bat_priv->orig_hash)
  47. goto err;
  48. batadv_hash_set_lock_class(bat_priv->orig_hash,
  49. &batadv_orig_hash_lock_class_key);
  50. INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
  51. queue_delayed_work(batadv_event_workqueue,
  52. &bat_priv->orig_work,
  53. msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
  54. return 0;
  55. err:
  56. return -ENOMEM;
  57. }
  58. void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
  59. {
  60. if (atomic_dec_and_test(&neigh_node->refcount))
  61. kfree_rcu(neigh_node, rcu);
  62. }
  63. /* increases the refcounter of a found router */
  64. struct batadv_neigh_node *
  65. batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
  66. {
  67. struct batadv_neigh_node *router;
  68. rcu_read_lock();
  69. router = rcu_dereference(orig_node->router);
  70. if (router && !atomic_inc_not_zero(&router->refcount))
  71. router = NULL;
  72. rcu_read_unlock();
  73. return router;
  74. }
  75. struct batadv_neigh_node *
  76. batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
  77. const uint8_t *neigh_addr, uint32_t seqno)
  78. {
  79. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  80. struct batadv_neigh_node *neigh_node;
  81. neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
  82. if (!neigh_node)
  83. goto out;
  84. INIT_HLIST_NODE(&neigh_node->list);
  85. memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
  86. spin_lock_init(&neigh_node->lq_update_lock);
  87. /* extra reference for return */
  88. atomic_set(&neigh_node->refcount, 2);
  89. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  90. "Creating new neighbor %pM, initial seqno %d\n",
  91. neigh_addr, seqno);
  92. out:
  93. return neigh_node;
  94. }
  95. static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
  96. {
  97. struct hlist_node *node_tmp;
  98. struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
  99. struct batadv_orig_node *orig_node;
  100. orig_node = container_of(rcu, struct batadv_orig_node, rcu);
  101. spin_lock_bh(&orig_node->neigh_list_lock);
  102. /* for all bonding members ... */
  103. list_for_each_entry_safe(neigh_node, tmp_neigh_node,
  104. &orig_node->bond_list, bonding_list) {
  105. list_del_rcu(&neigh_node->bonding_list);
  106. batadv_neigh_node_free_ref(neigh_node);
  107. }
  108. /* for all neighbors towards this originator ... */
  109. hlist_for_each_entry_safe(neigh_node, node_tmp,
  110. &orig_node->neigh_list, list) {
  111. hlist_del_rcu(&neigh_node->list);
  112. batadv_neigh_node_free_ref(neigh_node);
  113. }
  114. spin_unlock_bh(&orig_node->neigh_list_lock);
  115. /* Free nc_nodes */
  116. batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
  117. batadv_frag_list_free(&orig_node->frag_list);
  118. batadv_tt_global_del_orig(orig_node->bat_priv, orig_node,
  119. "originator timed out");
  120. kfree(orig_node->tt_buff);
  121. kfree(orig_node->bcast_own);
  122. kfree(orig_node->bcast_own_sum);
  123. kfree(orig_node);
  124. }
  125. void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
  126. {
  127. if (atomic_dec_and_test(&orig_node->refcount))
  128. call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
  129. }
  130. void batadv_originator_free(struct batadv_priv *bat_priv)
  131. {
  132. struct batadv_hashtable *hash = bat_priv->orig_hash;
  133. struct hlist_node *node_tmp;
  134. struct hlist_head *head;
  135. spinlock_t *list_lock; /* spinlock to protect write access */
  136. struct batadv_orig_node *orig_node;
  137. uint32_t i;
  138. if (!hash)
  139. return;
  140. cancel_delayed_work_sync(&bat_priv->orig_work);
  141. bat_priv->orig_hash = NULL;
  142. for (i = 0; i < hash->size; i++) {
  143. head = &hash->table[i];
  144. list_lock = &hash->list_locks[i];
  145. spin_lock_bh(list_lock);
  146. hlist_for_each_entry_safe(orig_node, node_tmp,
  147. head, hash_entry) {
  148. hlist_del_rcu(&orig_node->hash_entry);
  149. batadv_orig_node_free_ref(orig_node);
  150. }
  151. spin_unlock_bh(list_lock);
  152. }
  153. batadv_hash_destroy(hash);
  154. }
  155. /* this function finds or creates an originator entry for the given
  156. * address if it does not exits
  157. */
  158. struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
  159. const uint8_t *addr)
  160. {
  161. struct batadv_orig_node *orig_node;
  162. int size;
  163. int hash_added;
  164. unsigned long reset_time;
  165. orig_node = batadv_orig_hash_find(bat_priv, addr);
  166. if (orig_node)
  167. return orig_node;
  168. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  169. "Creating new originator: %pM\n", addr);
  170. orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
  171. if (!orig_node)
  172. return NULL;
  173. INIT_HLIST_HEAD(&orig_node->neigh_list);
  174. INIT_LIST_HEAD(&orig_node->bond_list);
  175. spin_lock_init(&orig_node->ogm_cnt_lock);
  176. spin_lock_init(&orig_node->bcast_seqno_lock);
  177. spin_lock_init(&orig_node->neigh_list_lock);
  178. spin_lock_init(&orig_node->tt_buff_lock);
  179. batadv_nc_init_orig(orig_node);
  180. /* extra reference for return */
  181. atomic_set(&orig_node->refcount, 2);
  182. orig_node->tt_initialised = false;
  183. orig_node->bat_priv = bat_priv;
  184. memcpy(orig_node->orig, addr, ETH_ALEN);
  185. batadv_dat_init_orig_node_addr(orig_node);
  186. orig_node->router = NULL;
  187. orig_node->tt_crc = 0;
  188. atomic_set(&orig_node->last_ttvn, 0);
  189. orig_node->tt_buff = NULL;
  190. orig_node->tt_buff_len = 0;
  191. atomic_set(&orig_node->tt_size, 0);
  192. reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
  193. orig_node->bcast_seqno_reset = reset_time;
  194. orig_node->batman_seqno_reset = reset_time;
  195. atomic_set(&orig_node->bond_candidates, 0);
  196. size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
  197. orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
  198. if (!orig_node->bcast_own)
  199. goto free_orig_node;
  200. size = bat_priv->num_ifaces * sizeof(uint8_t);
  201. orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
  202. INIT_LIST_HEAD(&orig_node->frag_list);
  203. orig_node->last_frag_packet = 0;
  204. if (!orig_node->bcast_own_sum)
  205. goto free_bcast_own;
  206. hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
  207. batadv_choose_orig, orig_node,
  208. &orig_node->hash_entry);
  209. if (hash_added != 0)
  210. goto free_bcast_own_sum;
  211. return orig_node;
  212. free_bcast_own_sum:
  213. kfree(orig_node->bcast_own_sum);
  214. free_bcast_own:
  215. kfree(orig_node->bcast_own);
  216. free_orig_node:
  217. kfree(orig_node);
  218. return NULL;
  219. }
  220. static bool
  221. batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
  222. struct batadv_orig_node *orig_node,
  223. struct batadv_neigh_node **best_neigh_node)
  224. {
  225. struct hlist_node *node_tmp;
  226. struct batadv_neigh_node *neigh_node;
  227. bool neigh_purged = false;
  228. unsigned long last_seen;
  229. struct batadv_hard_iface *if_incoming;
  230. *best_neigh_node = NULL;
  231. spin_lock_bh(&orig_node->neigh_list_lock);
  232. /* for all neighbors towards this originator ... */
  233. hlist_for_each_entry_safe(neigh_node, node_tmp,
  234. &orig_node->neigh_list, list) {
  235. last_seen = neigh_node->last_seen;
  236. if_incoming = neigh_node->if_incoming;
  237. if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
  238. (if_incoming->if_status == BATADV_IF_INACTIVE) ||
  239. (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
  240. (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
  241. if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
  242. (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
  243. (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
  244. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  245. "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
  246. orig_node->orig, neigh_node->addr,
  247. if_incoming->net_dev->name);
  248. else
  249. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  250. "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
  251. orig_node->orig, neigh_node->addr,
  252. jiffies_to_msecs(last_seen));
  253. neigh_purged = true;
  254. hlist_del_rcu(&neigh_node->list);
  255. batadv_bonding_candidate_del(orig_node, neigh_node);
  256. batadv_neigh_node_free_ref(neigh_node);
  257. } else {
  258. if ((!*best_neigh_node) ||
  259. (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
  260. *best_neigh_node = neigh_node;
  261. }
  262. }
  263. spin_unlock_bh(&orig_node->neigh_list_lock);
  264. return neigh_purged;
  265. }
  266. static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
  267. struct batadv_orig_node *orig_node)
  268. {
  269. struct batadv_neigh_node *best_neigh_node;
  270. if (batadv_has_timed_out(orig_node->last_seen,
  271. 2 * BATADV_PURGE_TIMEOUT)) {
  272. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  273. "Originator timeout: originator %pM, last_seen %u\n",
  274. orig_node->orig,
  275. jiffies_to_msecs(orig_node->last_seen));
  276. return true;
  277. } else {
  278. if (batadv_purge_orig_neighbors(bat_priv, orig_node,
  279. &best_neigh_node))
  280. batadv_update_route(bat_priv, orig_node,
  281. best_neigh_node);
  282. }
  283. return false;
  284. }
  285. static void _batadv_purge_orig(struct batadv_priv *bat_priv)
  286. {
  287. struct batadv_hashtable *hash = bat_priv->orig_hash;
  288. struct hlist_node *node_tmp;
  289. struct hlist_head *head;
  290. spinlock_t *list_lock; /* spinlock to protect write access */
  291. struct batadv_orig_node *orig_node;
  292. uint32_t i;
  293. if (!hash)
  294. return;
  295. /* for all origins... */
  296. for (i = 0; i < hash->size; i++) {
  297. head = &hash->table[i];
  298. list_lock = &hash->list_locks[i];
  299. spin_lock_bh(list_lock);
  300. hlist_for_each_entry_safe(orig_node, node_tmp,
  301. head, hash_entry) {
  302. if (batadv_purge_orig_node(bat_priv, orig_node)) {
  303. if (orig_node->gw_flags)
  304. batadv_gw_node_delete(bat_priv,
  305. orig_node);
  306. hlist_del_rcu(&orig_node->hash_entry);
  307. batadv_orig_node_free_ref(orig_node);
  308. continue;
  309. }
  310. if (batadv_has_timed_out(orig_node->last_frag_packet,
  311. BATADV_FRAG_TIMEOUT))
  312. batadv_frag_list_free(&orig_node->frag_list);
  313. }
  314. spin_unlock_bh(list_lock);
  315. }
  316. batadv_gw_node_purge(bat_priv);
  317. batadv_gw_election(bat_priv);
  318. }
  319. static void batadv_purge_orig(struct work_struct *work)
  320. {
  321. struct delayed_work *delayed_work;
  322. struct batadv_priv *bat_priv;
  323. delayed_work = container_of(work, struct delayed_work, work);
  324. bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
  325. _batadv_purge_orig(bat_priv);
  326. queue_delayed_work(batadv_event_workqueue,
  327. &bat_priv->orig_work,
  328. msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
  329. }
  330. void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
  331. {
  332. _batadv_purge_orig(bat_priv);
  333. }
  334. int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
  335. {
  336. struct net_device *net_dev = (struct net_device *)seq->private;
  337. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  338. struct batadv_hashtable *hash = bat_priv->orig_hash;
  339. struct hlist_head *head;
  340. struct batadv_hard_iface *primary_if;
  341. struct batadv_orig_node *orig_node;
  342. struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
  343. int batman_count = 0;
  344. int last_seen_secs;
  345. int last_seen_msecs;
  346. unsigned long last_seen_jiffies;
  347. uint32_t i;
  348. primary_if = batadv_seq_print_text_primary_if_get(seq);
  349. if (!primary_if)
  350. goto out;
  351. seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
  352. BATADV_SOURCE_VERSION, primary_if->net_dev->name,
  353. primary_if->net_dev->dev_addr, net_dev->name);
  354. seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
  355. "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
  356. "Nexthop", "outgoingIF", "Potential nexthops");
  357. for (i = 0; i < hash->size; i++) {
  358. head = &hash->table[i];
  359. rcu_read_lock();
  360. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  361. neigh_node = batadv_orig_node_get_router(orig_node);
  362. if (!neigh_node)
  363. continue;
  364. if (neigh_node->tq_avg == 0)
  365. goto next;
  366. last_seen_jiffies = jiffies - orig_node->last_seen;
  367. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  368. last_seen_secs = last_seen_msecs / 1000;
  369. last_seen_msecs = last_seen_msecs % 1000;
  370. seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
  371. orig_node->orig, last_seen_secs,
  372. last_seen_msecs, neigh_node->tq_avg,
  373. neigh_node->addr,
  374. neigh_node->if_incoming->net_dev->name);
  375. hlist_for_each_entry_rcu(neigh_node_tmp,
  376. &orig_node->neigh_list, list) {
  377. seq_printf(seq, " %pM (%3i)",
  378. neigh_node_tmp->addr,
  379. neigh_node_tmp->tq_avg);
  380. }
  381. seq_puts(seq, "\n");
  382. batman_count++;
  383. next:
  384. batadv_neigh_node_free_ref(neigh_node);
  385. }
  386. rcu_read_unlock();
  387. }
  388. if (batman_count == 0)
  389. seq_puts(seq, "No batman nodes in range ...\n");
  390. out:
  391. if (primary_if)
  392. batadv_hardif_free_ref(primary_if);
  393. return 0;
  394. }
  395. static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
  396. int max_if_num)
  397. {
  398. void *data_ptr;
  399. size_t data_size, old_size;
  400. data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
  401. old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
  402. data_ptr = kmalloc(data_size, GFP_ATOMIC);
  403. if (!data_ptr)
  404. return -ENOMEM;
  405. memcpy(data_ptr, orig_node->bcast_own, old_size);
  406. kfree(orig_node->bcast_own);
  407. orig_node->bcast_own = data_ptr;
  408. data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
  409. if (!data_ptr)
  410. return -ENOMEM;
  411. memcpy(data_ptr, orig_node->bcast_own_sum,
  412. (max_if_num - 1) * sizeof(uint8_t));
  413. kfree(orig_node->bcast_own_sum);
  414. orig_node->bcast_own_sum = data_ptr;
  415. return 0;
  416. }
  417. int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
  418. int max_if_num)
  419. {
  420. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  421. struct batadv_hashtable *hash = bat_priv->orig_hash;
  422. struct hlist_head *head;
  423. struct batadv_orig_node *orig_node;
  424. uint32_t i;
  425. int ret;
  426. /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
  427. * if_num
  428. */
  429. for (i = 0; i < hash->size; i++) {
  430. head = &hash->table[i];
  431. rcu_read_lock();
  432. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  433. spin_lock_bh(&orig_node->ogm_cnt_lock);
  434. ret = batadv_orig_node_add_if(orig_node, max_if_num);
  435. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  436. if (ret == -ENOMEM)
  437. goto err;
  438. }
  439. rcu_read_unlock();
  440. }
  441. return 0;
  442. err:
  443. rcu_read_unlock();
  444. return -ENOMEM;
  445. }
  446. static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
  447. int max_if_num, int del_if_num)
  448. {
  449. void *data_ptr = NULL;
  450. int chunk_size;
  451. /* last interface was removed */
  452. if (max_if_num == 0)
  453. goto free_bcast_own;
  454. chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
  455. data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
  456. if (!data_ptr)
  457. return -ENOMEM;
  458. /* copy first part */
  459. memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
  460. /* copy second part */
  461. memcpy((char *)data_ptr + del_if_num * chunk_size,
  462. orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
  463. (max_if_num - del_if_num) * chunk_size);
  464. free_bcast_own:
  465. kfree(orig_node->bcast_own);
  466. orig_node->bcast_own = data_ptr;
  467. if (max_if_num == 0)
  468. goto free_own_sum;
  469. data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
  470. if (!data_ptr)
  471. return -ENOMEM;
  472. memcpy(data_ptr, orig_node->bcast_own_sum,
  473. del_if_num * sizeof(uint8_t));
  474. memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
  475. orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
  476. (max_if_num - del_if_num) * sizeof(uint8_t));
  477. free_own_sum:
  478. kfree(orig_node->bcast_own_sum);
  479. orig_node->bcast_own_sum = data_ptr;
  480. return 0;
  481. }
  482. int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
  483. int max_if_num)
  484. {
  485. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  486. struct batadv_hashtable *hash = bat_priv->orig_hash;
  487. struct hlist_head *head;
  488. struct batadv_hard_iface *hard_iface_tmp;
  489. struct batadv_orig_node *orig_node;
  490. uint32_t i;
  491. int ret;
  492. /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
  493. * if_num
  494. */
  495. for (i = 0; i < hash->size; i++) {
  496. head = &hash->table[i];
  497. rcu_read_lock();
  498. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  499. spin_lock_bh(&orig_node->ogm_cnt_lock);
  500. ret = batadv_orig_node_del_if(orig_node, max_if_num,
  501. hard_iface->if_num);
  502. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  503. if (ret == -ENOMEM)
  504. goto err;
  505. }
  506. rcu_read_unlock();
  507. }
  508. /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
  509. rcu_read_lock();
  510. list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
  511. if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
  512. continue;
  513. if (hard_iface == hard_iface_tmp)
  514. continue;
  515. if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
  516. continue;
  517. if (hard_iface_tmp->if_num > hard_iface->if_num)
  518. hard_iface_tmp->if_num--;
  519. }
  520. rcu_read_unlock();
  521. hard_iface->if_num = -1;
  522. return 0;
  523. err:
  524. rcu_read_unlock();
  525. return -ENOMEM;
  526. }