distributed-arp-table.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /* Copyright (C) 2011-2012 B.A.T.M.A.N. contributors:
  2. *
  3. * Antonio Quartulli
  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 <linux/if_ether.h>
  20. #include <linux/if_arp.h>
  21. #include "main.h"
  22. #include "distributed-arp-table.h"
  23. #include "hard-interface.h"
  24. #include "originator.h"
  25. #include "send.h"
  26. #include "types.h"
  27. #include "unicast.h"
  28. static void batadv_dat_purge(struct work_struct *work);
  29. /**
  30. * batadv_dat_start_timer - initialise the DAT periodic worker
  31. * @bat_priv: the bat priv with all the soft interface information
  32. */
  33. static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
  34. {
  35. INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
  36. queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
  37. msecs_to_jiffies(10000));
  38. }
  39. /**
  40. * batadv_dat_entry_free_ref - decrements the dat_entry refcounter and possibly
  41. * free it
  42. * @dat_entry: the oentry to free
  43. */
  44. static void batadv_dat_entry_free_ref(struct batadv_dat_entry *dat_entry)
  45. {
  46. if (atomic_dec_and_test(&dat_entry->refcount))
  47. kfree_rcu(dat_entry, rcu);
  48. }
  49. /**
  50. * batadv_dat_to_purge - checks whether a dat_entry has to be purged or not
  51. * @dat_entry: the entry to check
  52. *
  53. * Returns true if the entry has to be purged now, false otherwise
  54. */
  55. static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
  56. {
  57. return batadv_has_timed_out(dat_entry->last_update,
  58. BATADV_DAT_ENTRY_TIMEOUT);
  59. }
  60. /**
  61. * __batadv_dat_purge - delete entries from the DAT local storage
  62. * @bat_priv: the bat priv with all the soft interface information
  63. * @to_purge: function in charge to decide whether an entry has to be purged or
  64. * not. This function takes the dat_entry as argument and has to
  65. * returns a boolean value: true is the entry has to be deleted,
  66. * false otherwise
  67. *
  68. * Loops over each entry in the DAT local storage and delete it if and only if
  69. * the to_purge function passed as argument returns true
  70. */
  71. static void __batadv_dat_purge(struct batadv_priv *bat_priv,
  72. bool (*to_purge)(struct batadv_dat_entry *))
  73. {
  74. spinlock_t *list_lock; /* protects write access to the hash lists */
  75. struct batadv_dat_entry *dat_entry;
  76. struct hlist_node *node, *node_tmp;
  77. struct hlist_head *head;
  78. uint32_t i;
  79. if (!bat_priv->dat.hash)
  80. return;
  81. for (i = 0; i < bat_priv->dat.hash->size; i++) {
  82. head = &bat_priv->dat.hash->table[i];
  83. list_lock = &bat_priv->dat.hash->list_locks[i];
  84. spin_lock_bh(list_lock);
  85. hlist_for_each_entry_safe(dat_entry, node, node_tmp, head,
  86. hash_entry) {
  87. /* if an helper function has been passed as parameter,
  88. * ask it if the entry has to be purged or not
  89. */
  90. if (to_purge && !to_purge(dat_entry))
  91. continue;
  92. hlist_del_rcu(node);
  93. batadv_dat_entry_free_ref(dat_entry);
  94. }
  95. spin_unlock_bh(list_lock);
  96. }
  97. }
  98. /**
  99. * batadv_dat_purge - periodic task that deletes old entries from the local DAT
  100. * hash table
  101. * @work: kernel work struct
  102. */
  103. static void batadv_dat_purge(struct work_struct *work)
  104. {
  105. struct delayed_work *delayed_work;
  106. struct batadv_priv_dat *priv_dat;
  107. struct batadv_priv *bat_priv;
  108. delayed_work = container_of(work, struct delayed_work, work);
  109. priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
  110. bat_priv = container_of(priv_dat, struct batadv_priv, dat);
  111. __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
  112. batadv_dat_start_timer(bat_priv);
  113. }
  114. /**
  115. * batadv_compare_dat - comparing function used in the local DAT hash table
  116. * @node: node in the local table
  117. * @data2: second object to compare the node to
  118. *
  119. * Returns 1 if the two entry are the same, 0 otherwise
  120. */
  121. static int batadv_compare_dat(const struct hlist_node *node, const void *data2)
  122. {
  123. const void *data1 = container_of(node, struct batadv_dat_entry,
  124. hash_entry);
  125. return (memcmp(data1, data2, sizeof(__be32)) == 0 ? 1 : 0);
  126. }
  127. /**
  128. * batadv_hash_dat - compute the hash value for an IP address
  129. * @data: data to hash
  130. * @size: size of the hash table
  131. *
  132. * Returns the selected index in the hash table for the given data
  133. */
  134. static uint32_t batadv_hash_dat(const void *data, uint32_t size)
  135. {
  136. const unsigned char *key = data;
  137. uint32_t hash = 0;
  138. size_t i;
  139. for (i = 0; i < 4; i++) {
  140. hash += key[i];
  141. hash += (hash << 10);
  142. hash ^= (hash >> 6);
  143. }
  144. hash += (hash << 3);
  145. hash ^= (hash >> 11);
  146. hash += (hash << 15);
  147. return hash % size;
  148. }
  149. /**
  150. * batadv_dat_entry_hash_find - looks for a given dat_entry in the local hash
  151. * table
  152. * @bat_priv: the bat priv with all the soft interface information
  153. * @ip: search key
  154. *
  155. * Returns the dat_entry if found, NULL otherwise
  156. */
  157. static struct batadv_dat_entry *
  158. batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip)
  159. {
  160. struct hlist_head *head;
  161. struct hlist_node *node;
  162. struct batadv_dat_entry *dat_entry, *dat_entry_tmp = NULL;
  163. struct batadv_hashtable *hash = bat_priv->dat.hash;
  164. uint32_t index;
  165. if (!hash)
  166. return NULL;
  167. index = batadv_hash_dat(&ip, hash->size);
  168. head = &hash->table[index];
  169. rcu_read_lock();
  170. hlist_for_each_entry_rcu(dat_entry, node, head, hash_entry) {
  171. if (dat_entry->ip != ip)
  172. continue;
  173. if (!atomic_inc_not_zero(&dat_entry->refcount))
  174. continue;
  175. dat_entry_tmp = dat_entry;
  176. break;
  177. }
  178. rcu_read_unlock();
  179. return dat_entry_tmp;
  180. }
  181. /**
  182. * batadv_dat_entry_add - add a new dat entry or update it if already exists
  183. * @bat_priv: the bat priv with all the soft interface information
  184. * @ip: ipv4 to add/edit
  185. * @mac_addr: mac address to assign to the given ipv4
  186. */
  187. static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
  188. uint8_t *mac_addr)
  189. {
  190. struct batadv_dat_entry *dat_entry;
  191. int hash_added;
  192. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip);
  193. /* if this entry is already known, just update it */
  194. if (dat_entry) {
  195. if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
  196. memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
  197. dat_entry->last_update = jiffies;
  198. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  199. "Entry updated: %pI4 %pM\n", &dat_entry->ip,
  200. dat_entry->mac_addr);
  201. goto out;
  202. }
  203. dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
  204. if (!dat_entry)
  205. goto out;
  206. dat_entry->ip = ip;
  207. memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
  208. dat_entry->last_update = jiffies;
  209. atomic_set(&dat_entry->refcount, 2);
  210. hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
  211. batadv_hash_dat, &dat_entry->ip,
  212. &dat_entry->hash_entry);
  213. if (unlikely(hash_added != 0)) {
  214. /* remove the reference for the hash */
  215. batadv_dat_entry_free_ref(dat_entry);
  216. goto out;
  217. }
  218. batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM\n",
  219. &dat_entry->ip, dat_entry->mac_addr);
  220. out:
  221. if (dat_entry)
  222. batadv_dat_entry_free_ref(dat_entry);
  223. }
  224. /**
  225. * batadv_is_orig_node_eligible - check whether a node can be a DHT candidate
  226. * @res: the array with the already selected candidates
  227. * @select: number of already selected candidates
  228. * @tmp_max: address of the currently evaluated node
  229. * @max: current round max address
  230. * @last_max: address of the last selected candidate
  231. * @candidate: orig_node under evaluation
  232. * @max_orig_node: last selected candidate
  233. *
  234. * Returns true if the node has been elected as next candidate or false othrwise
  235. */
  236. static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
  237. int select, batadv_dat_addr_t tmp_max,
  238. batadv_dat_addr_t max,
  239. batadv_dat_addr_t last_max,
  240. struct batadv_orig_node *candidate,
  241. struct batadv_orig_node *max_orig_node)
  242. {
  243. bool ret = false;
  244. int j;
  245. /* Check if this node has already been selected... */
  246. for (j = 0; j < select; j++)
  247. if (res[j].orig_node == candidate)
  248. break;
  249. /* ..and possibly skip it */
  250. if (j < select)
  251. goto out;
  252. /* sanity check: has it already been selected? This should not happen */
  253. if (tmp_max > last_max)
  254. goto out;
  255. /* check if during this iteration an originator with a closer dht
  256. * address has already been found
  257. */
  258. if (tmp_max < max)
  259. goto out;
  260. /* this is an hash collision with the temporary selected node. Choose
  261. * the one with the lowest address
  262. */
  263. if ((tmp_max == max) &&
  264. (batadv_compare_eth(candidate->orig, max_orig_node->orig) > 0))
  265. goto out;
  266. ret = true;
  267. out:
  268. return ret;
  269. }
  270. /**
  271. * batadv_choose_next_candidate - select the next DHT candidate
  272. * @bat_priv: the bat priv with all the soft interface information
  273. * @cands: candidates array
  274. * @select: number of candidates already present in the array
  275. * @ip_key: key to look up in the DHT
  276. * @last_max: pointer where the address of the selected candidate will be saved
  277. */
  278. static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
  279. struct batadv_dat_candidate *cands,
  280. int select, batadv_dat_addr_t ip_key,
  281. batadv_dat_addr_t *last_max)
  282. {
  283. batadv_dat_addr_t max = 0, tmp_max = 0;
  284. struct batadv_orig_node *orig_node, *max_orig_node = NULL;
  285. struct batadv_hashtable *hash = bat_priv->orig_hash;
  286. struct hlist_node *node;
  287. struct hlist_head *head;
  288. int i;
  289. /* if no node is eligible as candidate, leave the candidate type as
  290. * NOT_FOUND
  291. */
  292. cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
  293. /* iterate over the originator list and find the node with closest
  294. * dat_address which has not been selected yet
  295. */
  296. for (i = 0; i < hash->size; i++) {
  297. head = &hash->table[i];
  298. rcu_read_lock();
  299. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  300. /* the dht space is a ring and addresses are unsigned */
  301. tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
  302. ip_key;
  303. if (!batadv_is_orig_node_eligible(cands, select,
  304. tmp_max, max,
  305. *last_max, orig_node,
  306. max_orig_node))
  307. continue;
  308. if (!atomic_inc_not_zero(&orig_node->refcount))
  309. continue;
  310. max = tmp_max;
  311. if (max_orig_node)
  312. batadv_orig_node_free_ref(max_orig_node);
  313. max_orig_node = orig_node;
  314. }
  315. rcu_read_unlock();
  316. }
  317. if (max_orig_node) {
  318. cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
  319. cands[select].orig_node = max_orig_node;
  320. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  321. "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
  322. select, max_orig_node->orig, max_orig_node->dat_addr,
  323. max);
  324. }
  325. *last_max = max;
  326. }
  327. /**
  328. * batadv_dat_select_candidates - selects the nodes which the DHT message has to
  329. * be sent to
  330. * @bat_priv: the bat priv with all the soft interface information
  331. * @ip_dst: ipv4 to look up in the DHT
  332. *
  333. * An originator O is selected if and only if its DHT_ID value is one of three
  334. * closest values (from the LEFT, with wrap around if needed) then the hash
  335. * value of the key. ip_dst is the key.
  336. *
  337. * Returns the candidate array of size BATADV_DAT_CANDIDATE_NUM
  338. */
  339. static struct batadv_dat_candidate *
  340. batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst)
  341. {
  342. int select;
  343. batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
  344. struct batadv_dat_candidate *res;
  345. if (!bat_priv->orig_hash)
  346. return NULL;
  347. res = kmalloc(BATADV_DAT_CANDIDATES_NUM * sizeof(*res), GFP_ATOMIC);
  348. if (!res)
  349. return NULL;
  350. ip_key = (batadv_dat_addr_t)batadv_hash_dat(&ip_dst,
  351. BATADV_DAT_ADDR_MAX);
  352. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  353. "dat_select_candidates(): IP=%pI4 hash(IP)=%u\n", &ip_dst,
  354. ip_key);
  355. for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
  356. batadv_choose_next_candidate(bat_priv, res, select, ip_key,
  357. &last_max);
  358. return res;
  359. }
  360. /**
  361. * batadv_dat_send_data - send a payload to the selected candidates
  362. * @bat_priv: the bat priv with all the soft interface information
  363. * @skb: payload to send
  364. * @ip: the DHT key
  365. * @packet_subtype: unicast4addr packet subtype to use
  366. *
  367. * In this function the skb is copied by means of pskb_copy() and is sent as
  368. * unicast packet to each of the selected candidates
  369. *
  370. * Returns true if the packet is sent to at least one candidate, false otherwise
  371. */
  372. static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
  373. struct sk_buff *skb, __be32 ip,
  374. int packet_subtype)
  375. {
  376. int i;
  377. bool ret = false;
  378. int send_status;
  379. struct batadv_neigh_node *neigh_node = NULL;
  380. struct sk_buff *tmp_skb;
  381. struct batadv_dat_candidate *cand;
  382. cand = batadv_dat_select_candidates(bat_priv, ip);
  383. if (!cand)
  384. goto out;
  385. batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
  386. for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
  387. if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
  388. continue;
  389. neigh_node = batadv_orig_node_get_router(cand[i].orig_node);
  390. if (!neigh_node)
  391. goto free_orig;
  392. tmp_skb = pskb_copy(skb, GFP_ATOMIC);
  393. if (!batadv_unicast_4addr_prepare_skb(bat_priv, tmp_skb,
  394. cand[i].orig_node,
  395. packet_subtype)) {
  396. kfree_skb(tmp_skb);
  397. goto free_neigh;
  398. }
  399. send_status = batadv_send_skb_packet(tmp_skb,
  400. neigh_node->if_incoming,
  401. neigh_node->addr);
  402. if (send_status == NET_XMIT_SUCCESS)
  403. /* packet sent to a candidate: return true */
  404. ret = true;
  405. free_neigh:
  406. batadv_neigh_node_free_ref(neigh_node);
  407. free_orig:
  408. batadv_orig_node_free_ref(cand[i].orig_node);
  409. }
  410. out:
  411. kfree(cand);
  412. return ret;
  413. }
  414. /**
  415. * batadv_dat_hash_free - free the local DAT hash table
  416. * @bat_priv: the bat priv with all the soft interface information
  417. */
  418. static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
  419. {
  420. __batadv_dat_purge(bat_priv, NULL);
  421. batadv_hash_destroy(bat_priv->dat.hash);
  422. bat_priv->dat.hash = NULL;
  423. }
  424. /**
  425. * batadv_dat_init - initialise the DAT internals
  426. * @bat_priv: the bat priv with all the soft interface information
  427. */
  428. int batadv_dat_init(struct batadv_priv *bat_priv)
  429. {
  430. if (bat_priv->dat.hash)
  431. return 0;
  432. bat_priv->dat.hash = batadv_hash_new(1024);
  433. if (!bat_priv->dat.hash)
  434. return -ENOMEM;
  435. batadv_dat_start_timer(bat_priv);
  436. return 0;
  437. }
  438. /**
  439. * batadv_dat_free - free the DAT internals
  440. * @bat_priv: the bat priv with all the soft interface information
  441. */
  442. void batadv_dat_free(struct batadv_priv *bat_priv)
  443. {
  444. cancel_delayed_work_sync(&bat_priv->dat.work);
  445. batadv_dat_hash_free(bat_priv);
  446. }
  447. /**
  448. * batadv_dat_cache_seq_print_text - print the local DAT hash table
  449. * @seq: seq file to print on
  450. * @offset: not used
  451. */
  452. int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
  453. {
  454. struct net_device *net_dev = (struct net_device *)seq->private;
  455. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  456. struct batadv_hashtable *hash = bat_priv->dat.hash;
  457. struct batadv_dat_entry *dat_entry;
  458. struct batadv_hard_iface *primary_if;
  459. struct hlist_node *node;
  460. struct hlist_head *head;
  461. unsigned long last_seen_jiffies;
  462. int last_seen_msecs, last_seen_secs, last_seen_mins;
  463. uint32_t i;
  464. primary_if = batadv_seq_print_text_primary_if_get(seq);
  465. if (!primary_if)
  466. goto out;
  467. seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
  468. seq_printf(seq, " %-7s %-13s %5s\n", "IPv4", "MAC",
  469. "last-seen");
  470. for (i = 0; i < hash->size; i++) {
  471. head = &hash->table[i];
  472. rcu_read_lock();
  473. hlist_for_each_entry_rcu(dat_entry, node, head, hash_entry) {
  474. last_seen_jiffies = jiffies - dat_entry->last_update;
  475. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  476. last_seen_mins = last_seen_msecs / 60000;
  477. last_seen_msecs = last_seen_msecs % 60000;
  478. last_seen_secs = last_seen_msecs / 1000;
  479. seq_printf(seq, " * %15pI4 %14pM %6i:%02i\n",
  480. &dat_entry->ip, dat_entry->mac_addr,
  481. last_seen_mins, last_seen_secs);
  482. }
  483. rcu_read_unlock();
  484. }
  485. out:
  486. if (primary_if)
  487. batadv_hardif_free_ref(primary_if);
  488. return 0;
  489. }