originator.c 20 KB

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