originator.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. /* increase the reference counter for this originator */
  22. #include "main.h"
  23. #include "originator.h"
  24. #include "hash.h"
  25. #include "translation-table.h"
  26. #include "routing.h"
  27. #include "gateway_client.h"
  28. #include "hard-interface.h"
  29. #include "unicast.h"
  30. #include "soft-interface.h"
  31. static void purge_orig(struct work_struct *work);
  32. static void start_purge_timer(struct bat_priv *bat_priv)
  33. {
  34. INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
  35. queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
  36. }
  37. int originator_init(struct bat_priv *bat_priv)
  38. {
  39. if (bat_priv->orig_hash)
  40. return 1;
  41. spin_lock_bh(&bat_priv->orig_hash_lock);
  42. bat_priv->orig_hash = hash_new(1024);
  43. if (!bat_priv->orig_hash)
  44. goto err;
  45. spin_unlock_bh(&bat_priv->orig_hash_lock);
  46. start_purge_timer(bat_priv);
  47. return 1;
  48. err:
  49. spin_unlock_bh(&bat_priv->orig_hash_lock);
  50. return 0;
  51. }
  52. struct neigh_node *
  53. create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
  54. uint8_t *neigh, struct batman_if *if_incoming)
  55. {
  56. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  57. struct neigh_node *neigh_node;
  58. bat_dbg(DBG_BATMAN, bat_priv,
  59. "Creating new last-hop neighbor of originator\n");
  60. neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
  61. if (!neigh_node)
  62. return NULL;
  63. INIT_LIST_HEAD(&neigh_node->list);
  64. memcpy(neigh_node->addr, neigh, ETH_ALEN);
  65. neigh_node->orig_node = orig_neigh_node;
  66. neigh_node->if_incoming = if_incoming;
  67. list_add_tail(&neigh_node->list, &orig_node->neigh_list);
  68. return neigh_node;
  69. }
  70. static void free_orig_node(void *data, void *arg)
  71. {
  72. struct list_head *list_pos, *list_pos_tmp;
  73. struct neigh_node *neigh_node;
  74. struct orig_node *orig_node = (struct orig_node *)data;
  75. struct bat_priv *bat_priv = (struct bat_priv *)arg;
  76. /* for all neighbors towards this originator ... */
  77. list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
  78. neigh_node = list_entry(list_pos, struct neigh_node, list);
  79. list_del(list_pos);
  80. kfree(neigh_node);
  81. }
  82. frag_list_free(&orig_node->frag_list);
  83. hna_global_del_orig(bat_priv, orig_node, "originator timed out");
  84. kfree(orig_node->bcast_own);
  85. kfree(orig_node->bcast_own_sum);
  86. kfree(orig_node);
  87. }
  88. void originator_free(struct bat_priv *bat_priv)
  89. {
  90. if (!bat_priv->orig_hash)
  91. return;
  92. cancel_delayed_work_sync(&bat_priv->orig_work);
  93. spin_lock_bh(&bat_priv->orig_hash_lock);
  94. hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv);
  95. bat_priv->orig_hash = NULL;
  96. spin_unlock_bh(&bat_priv->orig_hash_lock);
  97. }
  98. /* this function finds or creates an originator entry for the given
  99. * address if it does not exits */
  100. struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
  101. {
  102. struct orig_node *orig_node;
  103. int size;
  104. int hash_added;
  105. orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
  106. compare_orig, choose_orig,
  107. addr));
  108. if (orig_node)
  109. return orig_node;
  110. bat_dbg(DBG_BATMAN, bat_priv,
  111. "Creating new originator: %pM\n", addr);
  112. orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
  113. if (!orig_node)
  114. return NULL;
  115. INIT_LIST_HEAD(&orig_node->neigh_list);
  116. memcpy(orig_node->orig, addr, ETH_ALEN);
  117. orig_node->router = NULL;
  118. orig_node->hna_buff = NULL;
  119. orig_node->bcast_seqno_reset = jiffies - 1
  120. - msecs_to_jiffies(RESET_PROTECTION_MS);
  121. orig_node->batman_seqno_reset = jiffies - 1
  122. - msecs_to_jiffies(RESET_PROTECTION_MS);
  123. size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
  124. orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
  125. if (!orig_node->bcast_own)
  126. goto free_orig_node;
  127. size = bat_priv->num_ifaces * sizeof(uint8_t);
  128. orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
  129. INIT_LIST_HEAD(&orig_node->frag_list);
  130. orig_node->last_frag_packet = 0;
  131. if (!orig_node->bcast_own_sum)
  132. goto free_bcast_own;
  133. hash_added = hash_add(bat_priv->orig_hash, compare_orig, choose_orig,
  134. orig_node);
  135. if (hash_added < 0)
  136. goto free_bcast_own_sum;
  137. return orig_node;
  138. free_bcast_own_sum:
  139. kfree(orig_node->bcast_own_sum);
  140. free_bcast_own:
  141. kfree(orig_node->bcast_own);
  142. free_orig_node:
  143. kfree(orig_node);
  144. return NULL;
  145. }
  146. static bool purge_orig_neighbors(struct bat_priv *bat_priv,
  147. struct orig_node *orig_node,
  148. struct neigh_node **best_neigh_node)
  149. {
  150. struct list_head *list_pos, *list_pos_tmp;
  151. struct neigh_node *neigh_node;
  152. bool neigh_purged = false;
  153. *best_neigh_node = NULL;
  154. /* for all neighbors towards this originator ... */
  155. list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
  156. neigh_node = list_entry(list_pos, struct neigh_node, list);
  157. if ((time_after(jiffies,
  158. neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
  159. (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
  160. (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
  161. if (neigh_node->if_incoming->if_status ==
  162. IF_TO_BE_REMOVED)
  163. bat_dbg(DBG_BATMAN, bat_priv,
  164. "neighbor purge: originator %pM, "
  165. "neighbor: %pM, iface: %s\n",
  166. orig_node->orig, neigh_node->addr,
  167. neigh_node->if_incoming->net_dev->name);
  168. else
  169. bat_dbg(DBG_BATMAN, bat_priv,
  170. "neighbor timeout: originator %pM, "
  171. "neighbor: %pM, last_valid: %lu\n",
  172. orig_node->orig, neigh_node->addr,
  173. (neigh_node->last_valid / HZ));
  174. neigh_purged = true;
  175. list_del(list_pos);
  176. kfree(neigh_node);
  177. } else {
  178. if ((!*best_neigh_node) ||
  179. (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
  180. *best_neigh_node = neigh_node;
  181. }
  182. }
  183. return neigh_purged;
  184. }
  185. static bool purge_orig_node(struct bat_priv *bat_priv,
  186. struct orig_node *orig_node)
  187. {
  188. struct neigh_node *best_neigh_node;
  189. if (time_after(jiffies,
  190. orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
  191. bat_dbg(DBG_BATMAN, bat_priv,
  192. "Originator timeout: originator %pM, last_valid %lu\n",
  193. orig_node->orig, (orig_node->last_valid / HZ));
  194. return true;
  195. } else {
  196. if (purge_orig_neighbors(bat_priv, orig_node,
  197. &best_neigh_node)) {
  198. update_routes(bat_priv, orig_node,
  199. best_neigh_node,
  200. orig_node->hna_buff,
  201. orig_node->hna_buff_len);
  202. /* update bonding candidates, we could have lost
  203. * some candidates. */
  204. update_bonding_candidates(bat_priv, orig_node);
  205. }
  206. }
  207. return false;
  208. }
  209. static void _purge_orig(struct bat_priv *bat_priv)
  210. {
  211. struct hashtable_t *hash = bat_priv->orig_hash;
  212. struct hlist_node *walk, *safe;
  213. struct hlist_head *head;
  214. struct element_t *bucket;
  215. struct orig_node *orig_node;
  216. int i;
  217. if (!hash)
  218. return;
  219. spin_lock_bh(&bat_priv->orig_hash_lock);
  220. /* for all origins... */
  221. for (i = 0; i < hash->size; i++) {
  222. head = &hash->table[i];
  223. hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
  224. orig_node = bucket->data;
  225. if (purge_orig_node(bat_priv, orig_node)) {
  226. if (orig_node->gw_flags)
  227. gw_node_delete(bat_priv, orig_node);
  228. hlist_del(walk);
  229. kfree(bucket);
  230. free_orig_node(orig_node, bat_priv);
  231. }
  232. if (time_after(jiffies, orig_node->last_frag_packet +
  233. msecs_to_jiffies(FRAG_TIMEOUT)))
  234. frag_list_free(&orig_node->frag_list);
  235. }
  236. }
  237. spin_unlock_bh(&bat_priv->orig_hash_lock);
  238. gw_node_purge(bat_priv);
  239. gw_election(bat_priv);
  240. softif_neigh_purge(bat_priv);
  241. }
  242. static void purge_orig(struct work_struct *work)
  243. {
  244. struct delayed_work *delayed_work =
  245. container_of(work, struct delayed_work, work);
  246. struct bat_priv *bat_priv =
  247. container_of(delayed_work, struct bat_priv, orig_work);
  248. _purge_orig(bat_priv);
  249. start_purge_timer(bat_priv);
  250. }
  251. void purge_orig_ref(struct bat_priv *bat_priv)
  252. {
  253. _purge_orig(bat_priv);
  254. }
  255. int orig_seq_print_text(struct seq_file *seq, void *offset)
  256. {
  257. struct net_device *net_dev = (struct net_device *)seq->private;
  258. struct bat_priv *bat_priv = netdev_priv(net_dev);
  259. struct hashtable_t *hash = bat_priv->orig_hash;
  260. struct hlist_node *walk;
  261. struct hlist_head *head;
  262. struct element_t *bucket;
  263. struct orig_node *orig_node;
  264. struct neigh_node *neigh_node;
  265. int batman_count = 0;
  266. int last_seen_secs;
  267. int last_seen_msecs;
  268. int i;
  269. if ((!bat_priv->primary_if) ||
  270. (bat_priv->primary_if->if_status != IF_ACTIVE)) {
  271. if (!bat_priv->primary_if)
  272. return seq_printf(seq, "BATMAN mesh %s disabled - "
  273. "please specify interfaces to enable it\n",
  274. net_dev->name);
  275. return seq_printf(seq, "BATMAN mesh %s "
  276. "disabled - primary interface not active\n",
  277. net_dev->name);
  278. }
  279. seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
  280. SOURCE_VERSION, REVISION_VERSION_STR,
  281. bat_priv->primary_if->net_dev->name,
  282. bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
  283. seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
  284. "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
  285. "outgoingIF", "Potential nexthops");
  286. spin_lock_bh(&bat_priv->orig_hash_lock);
  287. for (i = 0; i < hash->size; i++) {
  288. head = &hash->table[i];
  289. hlist_for_each_entry(bucket, walk, head, hlist) {
  290. orig_node = bucket->data;
  291. if (!orig_node->router)
  292. continue;
  293. if (orig_node->router->tq_avg == 0)
  294. continue;
  295. last_seen_secs = jiffies_to_msecs(jiffies -
  296. orig_node->last_valid) / 1000;
  297. last_seen_msecs = jiffies_to_msecs(jiffies -
  298. orig_node->last_valid) % 1000;
  299. neigh_node = orig_node->router;
  300. seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
  301. orig_node->orig, last_seen_secs,
  302. last_seen_msecs, neigh_node->tq_avg,
  303. neigh_node->addr,
  304. neigh_node->if_incoming->net_dev->name);
  305. list_for_each_entry(neigh_node, &orig_node->neigh_list,
  306. list) {
  307. seq_printf(seq, " %pM (%3i)", neigh_node->addr,
  308. neigh_node->tq_avg);
  309. }
  310. seq_printf(seq, "\n");
  311. batman_count++;
  312. }
  313. }
  314. spin_unlock_bh(&bat_priv->orig_hash_lock);
  315. if ((batman_count == 0))
  316. seq_printf(seq, "No batman nodes in range ...\n");
  317. return 0;
  318. }
  319. static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
  320. {
  321. void *data_ptr;
  322. data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
  323. GFP_ATOMIC);
  324. if (!data_ptr) {
  325. pr_err("Can't resize orig: out of memory\n");
  326. return -1;
  327. }
  328. memcpy(data_ptr, orig_node->bcast_own,
  329. (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
  330. kfree(orig_node->bcast_own);
  331. orig_node->bcast_own = data_ptr;
  332. data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
  333. if (!data_ptr) {
  334. pr_err("Can't resize orig: out of memory\n");
  335. return -1;
  336. }
  337. memcpy(data_ptr, orig_node->bcast_own_sum,
  338. (max_if_num - 1) * sizeof(uint8_t));
  339. kfree(orig_node->bcast_own_sum);
  340. orig_node->bcast_own_sum = data_ptr;
  341. return 0;
  342. }
  343. int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
  344. {
  345. struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
  346. struct hashtable_t *hash = bat_priv->orig_hash;
  347. struct hlist_node *walk;
  348. struct hlist_head *head;
  349. struct element_t *bucket;
  350. struct orig_node *orig_node;
  351. int i;
  352. /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
  353. * if_num */
  354. spin_lock_bh(&bat_priv->orig_hash_lock);
  355. for (i = 0; i < hash->size; i++) {
  356. head = &hash->table[i];
  357. hlist_for_each_entry(bucket, walk, head, hlist) {
  358. orig_node = bucket->data;
  359. if (orig_node_add_if(orig_node, max_if_num) == -1)
  360. goto err;
  361. }
  362. }
  363. spin_unlock_bh(&bat_priv->orig_hash_lock);
  364. return 0;
  365. err:
  366. spin_unlock_bh(&bat_priv->orig_hash_lock);
  367. return -ENOMEM;
  368. }
  369. static int orig_node_del_if(struct orig_node *orig_node,
  370. int max_if_num, int del_if_num)
  371. {
  372. void *data_ptr = NULL;
  373. int chunk_size;
  374. /* last interface was removed */
  375. if (max_if_num == 0)
  376. goto free_bcast_own;
  377. chunk_size = sizeof(unsigned long) * NUM_WORDS;
  378. data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
  379. if (!data_ptr) {
  380. pr_err("Can't resize orig: out of memory\n");
  381. return -1;
  382. }
  383. /* copy first part */
  384. memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
  385. /* copy second part */
  386. memcpy(data_ptr + del_if_num * chunk_size,
  387. orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
  388. (max_if_num - del_if_num) * chunk_size);
  389. free_bcast_own:
  390. kfree(orig_node->bcast_own);
  391. orig_node->bcast_own = data_ptr;
  392. if (max_if_num == 0)
  393. goto free_own_sum;
  394. data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
  395. if (!data_ptr) {
  396. pr_err("Can't resize orig: out of memory\n");
  397. return -1;
  398. }
  399. memcpy(data_ptr, orig_node->bcast_own_sum,
  400. del_if_num * sizeof(uint8_t));
  401. memcpy(data_ptr + del_if_num * sizeof(uint8_t),
  402. orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
  403. (max_if_num - del_if_num) * sizeof(uint8_t));
  404. free_own_sum:
  405. kfree(orig_node->bcast_own_sum);
  406. orig_node->bcast_own_sum = data_ptr;
  407. return 0;
  408. }
  409. int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
  410. {
  411. struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
  412. struct hashtable_t *hash = bat_priv->orig_hash;
  413. struct hlist_node *walk;
  414. struct hlist_head *head;
  415. struct element_t *bucket;
  416. struct batman_if *batman_if_tmp;
  417. struct orig_node *orig_node;
  418. int i, ret;
  419. /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
  420. * if_num */
  421. spin_lock_bh(&bat_priv->orig_hash_lock);
  422. for (i = 0; i < hash->size; i++) {
  423. head = &hash->table[i];
  424. hlist_for_each_entry(bucket, walk, head, hlist) {
  425. orig_node = bucket->data;
  426. ret = orig_node_del_if(orig_node, max_if_num,
  427. batman_if->if_num);
  428. if (ret == -1)
  429. goto err;
  430. }
  431. }
  432. /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
  433. rcu_read_lock();
  434. list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
  435. if (batman_if_tmp->if_status == IF_NOT_IN_USE)
  436. continue;
  437. if (batman_if == batman_if_tmp)
  438. continue;
  439. if (batman_if->soft_iface != batman_if_tmp->soft_iface)
  440. continue;
  441. if (batman_if_tmp->if_num > batman_if->if_num)
  442. batman_if_tmp->if_num--;
  443. }
  444. rcu_read_unlock();
  445. batman_if->if_num = -1;
  446. spin_unlock_bh(&bat_priv->orig_hash_lock);
  447. return 0;
  448. err:
  449. spin_unlock_bh(&bat_priv->orig_hash_lock);
  450. return -ENOMEM;
  451. }