originator.c 19 KB

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