distributed-arp-table.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /* Copyright (C) 2011-2013 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 <net/arp.h>
  22. #include "main.h"
  23. #include "hash.h"
  24. #include "distributed-arp-table.h"
  25. #include "hard-interface.h"
  26. #include "originator.h"
  27. #include "send.h"
  28. #include "types.h"
  29. #include "translation-table.h"
  30. static void batadv_dat_purge(struct work_struct *work);
  31. /**
  32. * batadv_dat_start_timer - initialise the DAT periodic worker
  33. * @bat_priv: the bat priv with all the soft interface information
  34. */
  35. static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
  36. {
  37. INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
  38. queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
  39. msecs_to_jiffies(10000));
  40. }
  41. /**
  42. * batadv_dat_entry_free_ref - decrement the dat_entry refcounter and possibly
  43. * free it
  44. * @dat_entry: the entry to free
  45. */
  46. static void batadv_dat_entry_free_ref(struct batadv_dat_entry *dat_entry)
  47. {
  48. if (atomic_dec_and_test(&dat_entry->refcount))
  49. kfree_rcu(dat_entry, rcu);
  50. }
  51. /**
  52. * batadv_dat_to_purge - check whether a dat_entry has to be purged or not
  53. * @dat_entry: the entry to check
  54. *
  55. * Returns true if the entry has to be purged now, false otherwise.
  56. */
  57. static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
  58. {
  59. return batadv_has_timed_out(dat_entry->last_update,
  60. BATADV_DAT_ENTRY_TIMEOUT);
  61. }
  62. /**
  63. * __batadv_dat_purge - delete entries from the DAT local storage
  64. * @bat_priv: the bat priv with all the soft interface information
  65. * @to_purge: function in charge to decide whether an entry has to be purged or
  66. * not. This function takes the dat_entry as argument and has to
  67. * returns a boolean value: true is the entry has to be deleted,
  68. * false otherwise
  69. *
  70. * Loops over each entry in the DAT local storage and deletes it if and only if
  71. * the to_purge function passed as argument returns true.
  72. */
  73. static void __batadv_dat_purge(struct batadv_priv *bat_priv,
  74. bool (*to_purge)(struct batadv_dat_entry *))
  75. {
  76. spinlock_t *list_lock; /* protects write access to the hash lists */
  77. struct batadv_dat_entry *dat_entry;
  78. struct hlist_node *node_tmp;
  79. struct hlist_head *head;
  80. uint32_t i;
  81. if (!bat_priv->dat.hash)
  82. return;
  83. for (i = 0; i < bat_priv->dat.hash->size; i++) {
  84. head = &bat_priv->dat.hash->table[i];
  85. list_lock = &bat_priv->dat.hash->list_locks[i];
  86. spin_lock_bh(list_lock);
  87. hlist_for_each_entry_safe(dat_entry, node_tmp, head,
  88. hash_entry) {
  89. /* if a helper function has been passed as parameter,
  90. * ask it if the entry has to be purged or not
  91. */
  92. if (to_purge && !to_purge(dat_entry))
  93. continue;
  94. hlist_del_rcu(&dat_entry->hash_entry);
  95. batadv_dat_entry_free_ref(dat_entry);
  96. }
  97. spin_unlock_bh(list_lock);
  98. }
  99. }
  100. /**
  101. * batadv_dat_purge - periodic task that deletes old entries from the local DAT
  102. * hash table
  103. * @work: kernel work struct
  104. */
  105. static void batadv_dat_purge(struct work_struct *work)
  106. {
  107. struct delayed_work *delayed_work;
  108. struct batadv_priv_dat *priv_dat;
  109. struct batadv_priv *bat_priv;
  110. delayed_work = container_of(work, struct delayed_work, work);
  111. priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
  112. bat_priv = container_of(priv_dat, struct batadv_priv, dat);
  113. __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
  114. batadv_dat_start_timer(bat_priv);
  115. }
  116. /**
  117. * batadv_compare_dat - comparing function used in the local DAT hash table
  118. * @node: node in the local table
  119. * @data2: second object to compare the node to
  120. *
  121. * Returns 1 if the two entries are the same, 0 otherwise.
  122. */
  123. static int batadv_compare_dat(const struct hlist_node *node, const void *data2)
  124. {
  125. const void *data1 = container_of(node, struct batadv_dat_entry,
  126. hash_entry);
  127. return (memcmp(data1, data2, sizeof(__be32)) == 0 ? 1 : 0);
  128. }
  129. /**
  130. * batadv_arp_hw_src - extract the hw_src field from an ARP packet
  131. * @skb: ARP packet
  132. * @hdr_size: size of the possible header before the ARP packet
  133. *
  134. * Returns the value of the hw_src field in the ARP packet.
  135. */
  136. static uint8_t *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
  137. {
  138. uint8_t *addr;
  139. addr = (uint8_t *)(skb->data + hdr_size);
  140. addr += ETH_HLEN + sizeof(struct arphdr);
  141. return addr;
  142. }
  143. /**
  144. * batadv_arp_ip_src - extract the ip_src field from an ARP packet
  145. * @skb: ARP packet
  146. * @hdr_size: size of the possible header before the ARP packet
  147. *
  148. * Returns the value of the ip_src field in the ARP packet.
  149. */
  150. static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
  151. {
  152. return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
  153. }
  154. /**
  155. * batadv_arp_hw_dst - extract the hw_dst field from an ARP packet
  156. * @skb: ARP packet
  157. * @hdr_size: size of the possible header before the ARP packet
  158. *
  159. * Returns the value of the hw_dst field in the ARP packet.
  160. */
  161. static uint8_t *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
  162. {
  163. return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
  164. }
  165. /**
  166. * batadv_arp_ip_dst - extract the ip_dst field from an ARP packet
  167. * @skb: ARP packet
  168. * @hdr_size: size of the possible header before the ARP packet
  169. *
  170. * Returns the value of the ip_dst field in the ARP packet.
  171. */
  172. static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
  173. {
  174. return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4);
  175. }
  176. /**
  177. * batadv_hash_dat - compute the hash value for an IP address
  178. * @data: data to hash
  179. * @size: size of the hash table
  180. *
  181. * Returns the selected index in the hash table for the given data.
  182. */
  183. static uint32_t batadv_hash_dat(const void *data, uint32_t size)
  184. {
  185. const unsigned char *key = data;
  186. uint32_t hash = 0;
  187. size_t i;
  188. for (i = 0; i < 4; i++) {
  189. hash += key[i];
  190. hash += (hash << 10);
  191. hash ^= (hash >> 6);
  192. }
  193. hash += (hash << 3);
  194. hash ^= (hash >> 11);
  195. hash += (hash << 15);
  196. return hash % size;
  197. }
  198. /**
  199. * batadv_dat_entry_hash_find - look for a given dat_entry in the local hash
  200. * table
  201. * @bat_priv: the bat priv with all the soft interface information
  202. * @ip: search key
  203. *
  204. * Returns the dat_entry if found, NULL otherwise.
  205. */
  206. static struct batadv_dat_entry *
  207. batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip)
  208. {
  209. struct hlist_head *head;
  210. struct batadv_dat_entry *dat_entry, *dat_entry_tmp = NULL;
  211. struct batadv_hashtable *hash = bat_priv->dat.hash;
  212. uint32_t index;
  213. if (!hash)
  214. return NULL;
  215. index = batadv_hash_dat(&ip, hash->size);
  216. head = &hash->table[index];
  217. rcu_read_lock();
  218. hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
  219. if (dat_entry->ip != ip)
  220. continue;
  221. if (!atomic_inc_not_zero(&dat_entry->refcount))
  222. continue;
  223. dat_entry_tmp = dat_entry;
  224. break;
  225. }
  226. rcu_read_unlock();
  227. return dat_entry_tmp;
  228. }
  229. /**
  230. * batadv_dat_entry_add - add a new dat entry or update it if already exists
  231. * @bat_priv: the bat priv with all the soft interface information
  232. * @ip: ipv4 to add/edit
  233. * @mac_addr: mac address to assign to the given ipv4
  234. */
  235. static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
  236. uint8_t *mac_addr)
  237. {
  238. struct batadv_dat_entry *dat_entry;
  239. int hash_added;
  240. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip);
  241. /* if this entry is already known, just update it */
  242. if (dat_entry) {
  243. if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
  244. memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
  245. dat_entry->last_update = jiffies;
  246. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  247. "Entry updated: %pI4 %pM\n", &dat_entry->ip,
  248. dat_entry->mac_addr);
  249. goto out;
  250. }
  251. dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
  252. if (!dat_entry)
  253. goto out;
  254. dat_entry->ip = ip;
  255. memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
  256. dat_entry->last_update = jiffies;
  257. atomic_set(&dat_entry->refcount, 2);
  258. hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
  259. batadv_hash_dat, &dat_entry->ip,
  260. &dat_entry->hash_entry);
  261. if (unlikely(hash_added != 0)) {
  262. /* remove the reference for the hash */
  263. batadv_dat_entry_free_ref(dat_entry);
  264. goto out;
  265. }
  266. batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM\n",
  267. &dat_entry->ip, dat_entry->mac_addr);
  268. out:
  269. if (dat_entry)
  270. batadv_dat_entry_free_ref(dat_entry);
  271. }
  272. #ifdef CONFIG_BATMAN_ADV_DEBUG
  273. /**
  274. * batadv_dbg_arp - print a debug message containing all the ARP packet details
  275. * @bat_priv: the bat priv with all the soft interface information
  276. * @skb: ARP packet
  277. * @type: ARP type
  278. * @hdr_size: size of the possible header before the ARP packet
  279. * @msg: message to print together with the debugging information
  280. */
  281. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  282. uint16_t type, int hdr_size, char *msg)
  283. {
  284. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  285. struct batadv_bcast_packet *bcast_pkt;
  286. uint8_t *orig_addr;
  287. __be32 ip_src, ip_dst;
  288. if (msg)
  289. batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
  290. ip_src = batadv_arp_ip_src(skb, hdr_size);
  291. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  292. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  293. "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
  294. batadv_arp_hw_src(skb, hdr_size), &ip_src,
  295. batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
  296. if (hdr_size == 0)
  297. return;
  298. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  299. switch (unicast_4addr_packet->u.header.packet_type) {
  300. case BATADV_UNICAST:
  301. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  302. "* encapsulated within a UNICAST packet\n");
  303. break;
  304. case BATADV_UNICAST_4ADDR:
  305. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  306. "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
  307. unicast_4addr_packet->src);
  308. switch (unicast_4addr_packet->subtype) {
  309. case BATADV_P_DAT_DHT_PUT:
  310. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
  311. break;
  312. case BATADV_P_DAT_DHT_GET:
  313. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
  314. break;
  315. case BATADV_P_DAT_CACHE_REPLY:
  316. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  317. "* type: DAT_CACHE_REPLY\n");
  318. break;
  319. case BATADV_P_DATA:
  320. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
  321. break;
  322. default:
  323. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
  324. unicast_4addr_packet->u.header.packet_type);
  325. }
  326. break;
  327. case BATADV_BCAST:
  328. bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
  329. orig_addr = bcast_pkt->orig;
  330. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  331. "* encapsulated within a BCAST packet (src: %pM)\n",
  332. orig_addr);
  333. break;
  334. default:
  335. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  336. "* encapsulated within an unknown packet type (0x%x)\n",
  337. unicast_4addr_packet->u.header.packet_type);
  338. }
  339. }
  340. #else
  341. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  342. uint16_t type, int hdr_size, char *msg)
  343. {
  344. }
  345. #endif /* CONFIG_BATMAN_ADV_DEBUG */
  346. /**
  347. * batadv_is_orig_node_eligible - check whether a node can be a DHT candidate
  348. * @res: the array with the already selected candidates
  349. * @select: number of already selected candidates
  350. * @tmp_max: address of the currently evaluated node
  351. * @max: current round max address
  352. * @last_max: address of the last selected candidate
  353. * @candidate: orig_node under evaluation
  354. * @max_orig_node: last selected candidate
  355. *
  356. * Returns true if the node has been elected as next candidate or false
  357. * otherwise.
  358. */
  359. static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
  360. int select, batadv_dat_addr_t tmp_max,
  361. batadv_dat_addr_t max,
  362. batadv_dat_addr_t last_max,
  363. struct batadv_orig_node *candidate,
  364. struct batadv_orig_node *max_orig_node)
  365. {
  366. bool ret = false;
  367. int j;
  368. /* check if orig node candidate is running DAT */
  369. if (!(candidate->capabilities & BATADV_ORIG_CAPA_HAS_DAT))
  370. goto out;
  371. /* Check if this node has already been selected... */
  372. for (j = 0; j < select; j++)
  373. if (res[j].orig_node == candidate)
  374. break;
  375. /* ..and possibly skip it */
  376. if (j < select)
  377. goto out;
  378. /* sanity check: has it already been selected? This should not happen */
  379. if (tmp_max > last_max)
  380. goto out;
  381. /* check if during this iteration an originator with a closer dht
  382. * address has already been found
  383. */
  384. if (tmp_max < max)
  385. goto out;
  386. /* this is an hash collision with the temporary selected node. Choose
  387. * the one with the lowest address
  388. */
  389. if ((tmp_max == max) && max_orig_node &&
  390. (batadv_compare_eth(candidate->orig, max_orig_node->orig) > 0))
  391. goto out;
  392. ret = true;
  393. out:
  394. return ret;
  395. }
  396. /**
  397. * batadv_choose_next_candidate - select the next DHT candidate
  398. * @bat_priv: the bat priv with all the soft interface information
  399. * @cands: candidates array
  400. * @select: number of candidates already present in the array
  401. * @ip_key: key to look up in the DHT
  402. * @last_max: pointer where the address of the selected candidate will be saved
  403. */
  404. static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
  405. struct batadv_dat_candidate *cands,
  406. int select, batadv_dat_addr_t ip_key,
  407. batadv_dat_addr_t *last_max)
  408. {
  409. batadv_dat_addr_t max = 0, tmp_max = 0;
  410. struct batadv_orig_node *orig_node, *max_orig_node = NULL;
  411. struct batadv_hashtable *hash = bat_priv->orig_hash;
  412. struct hlist_head *head;
  413. int i;
  414. /* if no node is eligible as candidate, leave the candidate type as
  415. * NOT_FOUND
  416. */
  417. cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
  418. /* iterate over the originator list and find the node with the closest
  419. * dat_address which has not been selected yet
  420. */
  421. for (i = 0; i < hash->size; i++) {
  422. head = &hash->table[i];
  423. rcu_read_lock();
  424. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  425. /* the dht space is a ring using unsigned addresses */
  426. tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
  427. ip_key;
  428. if (!batadv_is_orig_node_eligible(cands, select,
  429. tmp_max, max,
  430. *last_max, orig_node,
  431. max_orig_node))
  432. continue;
  433. if (!atomic_inc_not_zero(&orig_node->refcount))
  434. continue;
  435. max = tmp_max;
  436. if (max_orig_node)
  437. batadv_orig_node_free_ref(max_orig_node);
  438. max_orig_node = orig_node;
  439. }
  440. rcu_read_unlock();
  441. }
  442. if (max_orig_node) {
  443. cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
  444. cands[select].orig_node = max_orig_node;
  445. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  446. "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
  447. select, max_orig_node->orig, max_orig_node->dat_addr,
  448. max);
  449. }
  450. *last_max = max;
  451. }
  452. /**
  453. * batadv_dat_select_candidates - select the nodes which the DHT message has to
  454. * be sent to
  455. * @bat_priv: the bat priv with all the soft interface information
  456. * @ip_dst: ipv4 to look up in the DHT
  457. *
  458. * An originator O is selected if and only if its DHT_ID value is one of three
  459. * closest values (from the LEFT, with wrap around if needed) then the hash
  460. * value of the key. ip_dst is the key.
  461. *
  462. * Returns the candidate array of size BATADV_DAT_CANDIDATE_NUM.
  463. */
  464. static struct batadv_dat_candidate *
  465. batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst)
  466. {
  467. int select;
  468. batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
  469. struct batadv_dat_candidate *res;
  470. if (!bat_priv->orig_hash)
  471. return NULL;
  472. res = kmalloc(BATADV_DAT_CANDIDATES_NUM * sizeof(*res), GFP_ATOMIC);
  473. if (!res)
  474. return NULL;
  475. ip_key = (batadv_dat_addr_t)batadv_hash_dat(&ip_dst,
  476. BATADV_DAT_ADDR_MAX);
  477. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  478. "dat_select_candidates(): IP=%pI4 hash(IP)=%u\n", &ip_dst,
  479. ip_key);
  480. for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
  481. batadv_choose_next_candidate(bat_priv, res, select, ip_key,
  482. &last_max);
  483. return res;
  484. }
  485. /**
  486. * batadv_dat_send_data - send a payload to the selected candidates
  487. * @bat_priv: the bat priv with all the soft interface information
  488. * @skb: payload to send
  489. * @ip: the DHT key
  490. * @packet_subtype: unicast4addr packet subtype to use
  491. *
  492. * This function copies the skb with pskb_copy() and is sent as unicast packet
  493. * to each of the selected candidates.
  494. *
  495. * Returns true if the packet is sent to at least one candidate, false
  496. * otherwise.
  497. */
  498. static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
  499. struct sk_buff *skb, __be32 ip,
  500. int packet_subtype)
  501. {
  502. int i;
  503. bool ret = false;
  504. int send_status;
  505. struct batadv_neigh_node *neigh_node = NULL;
  506. struct sk_buff *tmp_skb;
  507. struct batadv_dat_candidate *cand;
  508. cand = batadv_dat_select_candidates(bat_priv, ip);
  509. if (!cand)
  510. goto out;
  511. batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
  512. for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
  513. if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
  514. continue;
  515. neigh_node = batadv_orig_node_get_router(cand[i].orig_node);
  516. if (!neigh_node)
  517. goto free_orig;
  518. tmp_skb = pskb_copy(skb, GFP_ATOMIC);
  519. if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
  520. cand[i].orig_node,
  521. packet_subtype)) {
  522. kfree_skb(tmp_skb);
  523. goto free_neigh;
  524. }
  525. send_status = batadv_send_skb_packet(tmp_skb,
  526. neigh_node->if_incoming,
  527. neigh_node->addr);
  528. if (send_status == NET_XMIT_SUCCESS) {
  529. /* count the sent packet */
  530. switch (packet_subtype) {
  531. case BATADV_P_DAT_DHT_GET:
  532. batadv_inc_counter(bat_priv,
  533. BATADV_CNT_DAT_GET_TX);
  534. break;
  535. case BATADV_P_DAT_DHT_PUT:
  536. batadv_inc_counter(bat_priv,
  537. BATADV_CNT_DAT_PUT_TX);
  538. break;
  539. }
  540. /* packet sent to a candidate: return true */
  541. ret = true;
  542. }
  543. free_neigh:
  544. batadv_neigh_node_free_ref(neigh_node);
  545. free_orig:
  546. batadv_orig_node_free_ref(cand[i].orig_node);
  547. }
  548. out:
  549. kfree(cand);
  550. return ret;
  551. }
  552. /**
  553. * batadv_dat_tvlv_container_update - update the dat tvlv container after dat
  554. * setting change
  555. * @bat_priv: the bat priv with all the soft interface information
  556. */
  557. static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
  558. {
  559. char dat_mode;
  560. dat_mode = atomic_read(&bat_priv->distributed_arp_table);
  561. switch (dat_mode) {
  562. case 0:
  563. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  564. break;
  565. case 1:
  566. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
  567. NULL, 0);
  568. break;
  569. }
  570. }
  571. /**
  572. * batadv_dat_status_update - update the dat tvlv container after dat
  573. * setting change
  574. * @net_dev: the soft interface net device
  575. */
  576. void batadv_dat_status_update(struct net_device *net_dev)
  577. {
  578. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  579. batadv_dat_tvlv_container_update(bat_priv);
  580. }
  581. /**
  582. * batadv_gw_tvlv_ogm_handler_v1 - process incoming dat tvlv container
  583. * @bat_priv: the bat priv with all the soft interface information
  584. * @orig: the orig_node of the ogm
  585. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  586. * @tvlv_value: tvlv buffer containing the gateway data
  587. * @tvlv_value_len: tvlv buffer length
  588. */
  589. static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  590. struct batadv_orig_node *orig,
  591. uint8_t flags,
  592. void *tvlv_value,
  593. uint16_t tvlv_value_len)
  594. {
  595. if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
  596. orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_DAT;
  597. else
  598. orig->capabilities |= BATADV_ORIG_CAPA_HAS_DAT;
  599. }
  600. /**
  601. * batadv_dat_hash_free - free the local DAT hash table
  602. * @bat_priv: the bat priv with all the soft interface information
  603. */
  604. static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
  605. {
  606. if (!bat_priv->dat.hash)
  607. return;
  608. __batadv_dat_purge(bat_priv, NULL);
  609. batadv_hash_destroy(bat_priv->dat.hash);
  610. bat_priv->dat.hash = NULL;
  611. }
  612. /**
  613. * batadv_dat_init - initialise the DAT internals
  614. * @bat_priv: the bat priv with all the soft interface information
  615. */
  616. int batadv_dat_init(struct batadv_priv *bat_priv)
  617. {
  618. if (bat_priv->dat.hash)
  619. return 0;
  620. bat_priv->dat.hash = batadv_hash_new(1024);
  621. if (!bat_priv->dat.hash)
  622. return -ENOMEM;
  623. batadv_dat_start_timer(bat_priv);
  624. batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
  625. NULL, BATADV_TVLV_DAT, 1,
  626. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  627. batadv_dat_tvlv_container_update(bat_priv);
  628. return 0;
  629. }
  630. /**
  631. * batadv_dat_free - free the DAT internals
  632. * @bat_priv: the bat priv with all the soft interface information
  633. */
  634. void batadv_dat_free(struct batadv_priv *bat_priv)
  635. {
  636. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  637. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  638. cancel_delayed_work_sync(&bat_priv->dat.work);
  639. batadv_dat_hash_free(bat_priv);
  640. }
  641. /**
  642. * batadv_dat_cache_seq_print_text - print the local DAT hash table
  643. * @seq: seq file to print on
  644. * @offset: not used
  645. */
  646. int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
  647. {
  648. struct net_device *net_dev = (struct net_device *)seq->private;
  649. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  650. struct batadv_hashtable *hash = bat_priv->dat.hash;
  651. struct batadv_dat_entry *dat_entry;
  652. struct batadv_hard_iface *primary_if;
  653. struct hlist_head *head;
  654. unsigned long last_seen_jiffies;
  655. int last_seen_msecs, last_seen_secs, last_seen_mins;
  656. uint32_t i;
  657. primary_if = batadv_seq_print_text_primary_if_get(seq);
  658. if (!primary_if)
  659. goto out;
  660. seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
  661. seq_printf(seq, " %-7s %-13s %5s\n", "IPv4", "MAC",
  662. "last-seen");
  663. for (i = 0; i < hash->size; i++) {
  664. head = &hash->table[i];
  665. rcu_read_lock();
  666. hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
  667. last_seen_jiffies = jiffies - dat_entry->last_update;
  668. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  669. last_seen_mins = last_seen_msecs / 60000;
  670. last_seen_msecs = last_seen_msecs % 60000;
  671. last_seen_secs = last_seen_msecs / 1000;
  672. seq_printf(seq, " * %15pI4 %14pM %6i:%02i\n",
  673. &dat_entry->ip, dat_entry->mac_addr,
  674. last_seen_mins, last_seen_secs);
  675. }
  676. rcu_read_unlock();
  677. }
  678. out:
  679. if (primary_if)
  680. batadv_hardif_free_ref(primary_if);
  681. return 0;
  682. }
  683. /**
  684. * batadv_arp_get_type - parse an ARP packet and gets the type
  685. * @bat_priv: the bat priv with all the soft interface information
  686. * @skb: packet to analyse
  687. * @hdr_size: size of the possible header before the ARP packet in the skb
  688. *
  689. * Returns the ARP type if the skb contains a valid ARP packet, 0 otherwise.
  690. */
  691. static uint16_t batadv_arp_get_type(struct batadv_priv *bat_priv,
  692. struct sk_buff *skb, int hdr_size)
  693. {
  694. struct arphdr *arphdr;
  695. struct ethhdr *ethhdr;
  696. __be32 ip_src, ip_dst;
  697. uint8_t *hw_src, *hw_dst;
  698. uint16_t type = 0;
  699. /* pull the ethernet header */
  700. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
  701. goto out;
  702. ethhdr = (struct ethhdr *)(skb->data + hdr_size);
  703. if (ethhdr->h_proto != htons(ETH_P_ARP))
  704. goto out;
  705. /* pull the ARP payload */
  706. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
  707. arp_hdr_len(skb->dev))))
  708. goto out;
  709. arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
  710. /* check whether the ARP packet carries a valid IP information */
  711. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  712. goto out;
  713. if (arphdr->ar_pro != htons(ETH_P_IP))
  714. goto out;
  715. if (arphdr->ar_hln != ETH_ALEN)
  716. goto out;
  717. if (arphdr->ar_pln != 4)
  718. goto out;
  719. /* Check for bad reply/request. If the ARP message is not sane, DAT
  720. * will simply ignore it
  721. */
  722. ip_src = batadv_arp_ip_src(skb, hdr_size);
  723. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  724. if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
  725. ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
  726. ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
  727. ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
  728. goto out;
  729. hw_src = batadv_arp_hw_src(skb, hdr_size);
  730. if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
  731. goto out;
  732. /* don't care about the destination MAC address in ARP requests */
  733. if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
  734. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  735. if (is_zero_ether_addr(hw_dst) ||
  736. is_multicast_ether_addr(hw_dst))
  737. goto out;
  738. }
  739. type = ntohs(arphdr->ar_op);
  740. out:
  741. return type;
  742. }
  743. /**
  744. * batadv_dat_snoop_outgoing_arp_request - snoop the ARP request and try to
  745. * answer using DAT
  746. * @bat_priv: the bat priv with all the soft interface information
  747. * @skb: packet to check
  748. *
  749. * Returns true if the message has been sent to the dht candidates, false
  750. * otherwise. In case of a positive return value the message has to be enqueued
  751. * to permit the fallback.
  752. */
  753. bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
  754. struct sk_buff *skb)
  755. {
  756. uint16_t type = 0;
  757. __be32 ip_dst, ip_src;
  758. uint8_t *hw_src;
  759. bool ret = false;
  760. struct batadv_dat_entry *dat_entry = NULL;
  761. struct sk_buff *skb_new;
  762. if (!atomic_read(&bat_priv->distributed_arp_table))
  763. goto out;
  764. type = batadv_arp_get_type(bat_priv, skb, 0);
  765. /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
  766. * message to the selected DHT candidates
  767. */
  768. if (type != ARPOP_REQUEST)
  769. goto out;
  770. batadv_dbg_arp(bat_priv, skb, type, 0, "Parsing outgoing ARP REQUEST");
  771. ip_src = batadv_arp_ip_src(skb, 0);
  772. hw_src = batadv_arp_hw_src(skb, 0);
  773. ip_dst = batadv_arp_ip_dst(skb, 0);
  774. batadv_dat_entry_add(bat_priv, ip_src, hw_src);
  775. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst);
  776. if (dat_entry) {
  777. /* If the ARP request is destined for a local client the local
  778. * client will answer itself. DAT would only generate a
  779. * duplicate packet.
  780. *
  781. * Moreover, if the soft-interface is enslaved into a bridge, an
  782. * additional DAT answer may trigger kernel warnings about
  783. * a packet coming from the wrong port.
  784. */
  785. if (batadv_is_my_client(bat_priv, dat_entry->mac_addr)) {
  786. ret = true;
  787. goto out;
  788. }
  789. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  790. bat_priv->soft_iface, ip_dst, hw_src,
  791. dat_entry->mac_addr, hw_src);
  792. if (!skb_new)
  793. goto out;
  794. skb_reset_mac_header(skb_new);
  795. skb_new->protocol = eth_type_trans(skb_new,
  796. bat_priv->soft_iface);
  797. bat_priv->stats.rx_packets++;
  798. bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
  799. bat_priv->soft_iface->last_rx = jiffies;
  800. netif_rx(skb_new);
  801. batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
  802. ret = true;
  803. } else {
  804. /* Send the request to the DHT */
  805. ret = batadv_dat_send_data(bat_priv, skb, ip_dst,
  806. BATADV_P_DAT_DHT_GET);
  807. }
  808. out:
  809. if (dat_entry)
  810. batadv_dat_entry_free_ref(dat_entry);
  811. return ret;
  812. }
  813. /**
  814. * batadv_dat_snoop_incoming_arp_request - snoop the ARP request and try to
  815. * answer using the local DAT storage
  816. * @bat_priv: the bat priv with all the soft interface information
  817. * @skb: packet to check
  818. * @hdr_size: size of the encapsulation header
  819. *
  820. * Returns true if the request has been answered, false otherwise.
  821. */
  822. bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
  823. struct sk_buff *skb, int hdr_size)
  824. {
  825. uint16_t type;
  826. __be32 ip_src, ip_dst;
  827. uint8_t *hw_src;
  828. struct sk_buff *skb_new;
  829. struct batadv_dat_entry *dat_entry = NULL;
  830. bool ret = false;
  831. int err;
  832. if (!atomic_read(&bat_priv->distributed_arp_table))
  833. goto out;
  834. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  835. if (type != ARPOP_REQUEST)
  836. goto out;
  837. hw_src = batadv_arp_hw_src(skb, hdr_size);
  838. ip_src = batadv_arp_ip_src(skb, hdr_size);
  839. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  840. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  841. "Parsing incoming ARP REQUEST");
  842. batadv_dat_entry_add(bat_priv, ip_src, hw_src);
  843. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst);
  844. if (!dat_entry)
  845. goto out;
  846. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  847. bat_priv->soft_iface, ip_dst, hw_src,
  848. dat_entry->mac_addr, hw_src);
  849. if (!skb_new)
  850. goto out;
  851. /* To preserve backwards compatibility, the node has choose the outgoing
  852. * format based on the incoming request packet type. The assumption is
  853. * that a node not using the 4addr packet format doesn't support it.
  854. */
  855. if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
  856. err = batadv_send_skb_unicast_4addr(bat_priv, skb_new,
  857. BATADV_P_DAT_CACHE_REPLY);
  858. else
  859. err = batadv_send_skb_unicast(bat_priv, skb_new);
  860. if (!err) {
  861. batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
  862. ret = true;
  863. }
  864. out:
  865. if (dat_entry)
  866. batadv_dat_entry_free_ref(dat_entry);
  867. if (ret)
  868. kfree_skb(skb);
  869. return ret;
  870. }
  871. /**
  872. * batadv_dat_snoop_outgoing_arp_reply - snoop the ARP reply and fill the DHT
  873. * @bat_priv: the bat priv with all the soft interface information
  874. * @skb: packet to check
  875. */
  876. void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
  877. struct sk_buff *skb)
  878. {
  879. uint16_t type;
  880. __be32 ip_src, ip_dst;
  881. uint8_t *hw_src, *hw_dst;
  882. if (!atomic_read(&bat_priv->distributed_arp_table))
  883. return;
  884. type = batadv_arp_get_type(bat_priv, skb, 0);
  885. if (type != ARPOP_REPLY)
  886. return;
  887. batadv_dbg_arp(bat_priv, skb, type, 0, "Parsing outgoing ARP REPLY");
  888. hw_src = batadv_arp_hw_src(skb, 0);
  889. ip_src = batadv_arp_ip_src(skb, 0);
  890. hw_dst = batadv_arp_hw_dst(skb, 0);
  891. ip_dst = batadv_arp_ip_dst(skb, 0);
  892. batadv_dat_entry_add(bat_priv, ip_src, hw_src);
  893. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst);
  894. /* Send the ARP reply to the candidates for both the IP addresses that
  895. * the node obtained from the ARP reply
  896. */
  897. batadv_dat_send_data(bat_priv, skb, ip_src, BATADV_P_DAT_DHT_PUT);
  898. batadv_dat_send_data(bat_priv, skb, ip_dst, BATADV_P_DAT_DHT_PUT);
  899. }
  900. /**
  901. * batadv_dat_snoop_incoming_arp_reply - snoop the ARP reply and fill the local
  902. * DAT storage only
  903. * @bat_priv: the bat priv with all the soft interface information
  904. * @skb: packet to check
  905. * @hdr_size: size of the encapsulation header
  906. */
  907. bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
  908. struct sk_buff *skb, int hdr_size)
  909. {
  910. uint16_t type;
  911. __be32 ip_src, ip_dst;
  912. uint8_t *hw_src, *hw_dst;
  913. bool ret = false;
  914. if (!atomic_read(&bat_priv->distributed_arp_table))
  915. goto out;
  916. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  917. if (type != ARPOP_REPLY)
  918. goto out;
  919. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  920. "Parsing incoming ARP REPLY");
  921. hw_src = batadv_arp_hw_src(skb, hdr_size);
  922. ip_src = batadv_arp_ip_src(skb, hdr_size);
  923. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  924. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  925. /* Update our internal cache with both the IP addresses the node got
  926. * within the ARP reply
  927. */
  928. batadv_dat_entry_add(bat_priv, ip_src, hw_src);
  929. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst);
  930. /* if this REPLY is directed to a client of mine, let's deliver the
  931. * packet to the interface
  932. */
  933. ret = !batadv_is_my_client(bat_priv, hw_dst);
  934. out:
  935. if (ret)
  936. kfree_skb(skb);
  937. /* if ret == false -> packet has to be delivered to the interface */
  938. return ret;
  939. }
  940. /**
  941. * batadv_dat_drop_broadcast_packet - check if an ARP request has to be dropped
  942. * (because the node has already obtained the reply via DAT) or not
  943. * @bat_priv: the bat priv with all the soft interface information
  944. * @forw_packet: the broadcast packet
  945. *
  946. * Returns true if the node can drop the packet, false otherwise.
  947. */
  948. bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
  949. struct batadv_forw_packet *forw_packet)
  950. {
  951. uint16_t type;
  952. __be32 ip_dst;
  953. struct batadv_dat_entry *dat_entry = NULL;
  954. bool ret = false;
  955. const size_t bcast_len = sizeof(struct batadv_bcast_packet);
  956. if (!atomic_read(&bat_priv->distributed_arp_table))
  957. goto out;
  958. /* If this packet is an ARP_REQUEST and the node already has the
  959. * information that it is going to ask, then the packet can be dropped
  960. */
  961. if (forw_packet->num_packets)
  962. goto out;
  963. type = batadv_arp_get_type(bat_priv, forw_packet->skb, bcast_len);
  964. if (type != ARPOP_REQUEST)
  965. goto out;
  966. ip_dst = batadv_arp_ip_dst(forw_packet->skb, bcast_len);
  967. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst);
  968. /* check if the node already got this entry */
  969. if (!dat_entry) {
  970. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  971. "ARP Request for %pI4: fallback\n", &ip_dst);
  972. goto out;
  973. }
  974. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  975. "ARP Request for %pI4: fallback prevented\n", &ip_dst);
  976. ret = true;
  977. out:
  978. if (dat_entry)
  979. batadv_dat_entry_free_ref(dat_entry);
  980. return ret;
  981. }