originator.c 17 KB

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