originator.c 17 KB

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