originator.c 18 KB

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