originator.c 15 KB

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