distributed-arp-table.c 34 KB

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