distributed-arp-table.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /* Copyright (C) 2011-2014 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/if_ether.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/if_vlan.h>
  20. #include <net/arp.h>
  21. #include "main.h"
  22. #include "hash.h"
  23. #include "distributed-arp-table.h"
  24. #include "hard-interface.h"
  25. #include "originator.h"
  26. #include "send.h"
  27. #include "types.h"
  28. #include "translation-table.h"
  29. static void batadv_dat_purge(struct work_struct *work);
  30. /**
  31. * batadv_dat_start_timer - initialise the DAT periodic worker
  32. * @bat_priv: the bat priv with all the soft interface information
  33. */
  34. static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
  35. {
  36. INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
  37. queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
  38. msecs_to_jiffies(10000));
  39. }
  40. /**
  41. * batadv_dat_entry_free_ref - decrement the dat_entry refcounter and possibly
  42. * free it
  43. * @dat_entry: the entry to free
  44. */
  45. static void batadv_dat_entry_free_ref(struct batadv_dat_entry *dat_entry)
  46. {
  47. if (atomic_dec_and_test(&dat_entry->refcount))
  48. kfree_rcu(dat_entry, rcu);
  49. }
  50. /**
  51. * batadv_dat_to_purge - check whether a dat_entry has to be purged or not
  52. * @dat_entry: the entry to check
  53. *
  54. * Returns true if the entry has to be purged now, false otherwise.
  55. */
  56. static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
  57. {
  58. return batadv_has_timed_out(dat_entry->last_update,
  59. BATADV_DAT_ENTRY_TIMEOUT);
  60. }
  61. /**
  62. * __batadv_dat_purge - delete entries from the DAT local storage
  63. * @bat_priv: the bat priv with all the soft interface information
  64. * @to_purge: function in charge to decide whether an entry has to be purged or
  65. * not. This function takes the dat_entry as argument and has to
  66. * returns a boolean value: true is the entry has to be deleted,
  67. * false otherwise
  68. *
  69. * Loops over each entry in the DAT local storage and deletes it if and only if
  70. * the to_purge function passed as argument returns true.
  71. */
  72. static void __batadv_dat_purge(struct batadv_priv *bat_priv,
  73. bool (*to_purge)(struct batadv_dat_entry *))
  74. {
  75. spinlock_t *list_lock; /* protects write access to the hash lists */
  76. struct batadv_dat_entry *dat_entry;
  77. struct hlist_node *node_tmp;
  78. struct hlist_head *head;
  79. uint32_t i;
  80. if (!bat_priv->dat.hash)
  81. return;
  82. for (i = 0; i < bat_priv->dat.hash->size; i++) {
  83. head = &bat_priv->dat.hash->table[i];
  84. list_lock = &bat_priv->dat.hash->list_locks[i];
  85. spin_lock_bh(list_lock);
  86. hlist_for_each_entry_safe(dat_entry, node_tmp, head,
  87. hash_entry) {
  88. /* if a helper function has been passed as parameter,
  89. * ask it if the entry has to be purged or not
  90. */
  91. if (to_purge && !to_purge(dat_entry))
  92. continue;
  93. hlist_del_rcu(&dat_entry->hash_entry);
  94. batadv_dat_entry_free_ref(dat_entry);
  95. }
  96. spin_unlock_bh(list_lock);
  97. }
  98. }
  99. /**
  100. * batadv_dat_purge - periodic task that deletes old entries from the local DAT
  101. * hash table
  102. * @work: kernel work struct
  103. */
  104. static void batadv_dat_purge(struct work_struct *work)
  105. {
  106. struct delayed_work *delayed_work;
  107. struct batadv_priv_dat *priv_dat;
  108. struct batadv_priv *bat_priv;
  109. delayed_work = container_of(work, struct delayed_work, work);
  110. priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
  111. bat_priv = container_of(priv_dat, struct batadv_priv, dat);
  112. __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
  113. batadv_dat_start_timer(bat_priv);
  114. }
  115. /**
  116. * batadv_compare_dat - comparing function used in the local DAT hash table
  117. * @node: node in the local table
  118. * @data2: second object to compare the node to
  119. *
  120. * Returns 1 if the two entries are the same, 0 otherwise.
  121. */
  122. static int batadv_compare_dat(const struct hlist_node *node, const void *data2)
  123. {
  124. const void *data1 = container_of(node, struct batadv_dat_entry,
  125. hash_entry);
  126. return memcmp(data1, data2, sizeof(__be32)) == 0 ? 1 : 0;
  127. }
  128. /**
  129. * batadv_arp_hw_src - extract the hw_src field from an ARP packet
  130. * @skb: ARP packet
  131. * @hdr_size: size of the possible header before the ARP packet
  132. *
  133. * Returns the value of the hw_src field in the ARP packet.
  134. */
  135. static uint8_t *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
  136. {
  137. uint8_t *addr;
  138. addr = (uint8_t *)(skb->data + hdr_size);
  139. addr += ETH_HLEN + sizeof(struct arphdr);
  140. return addr;
  141. }
  142. /**
  143. * batadv_arp_ip_src - extract the ip_src field from an ARP packet
  144. * @skb: ARP packet
  145. * @hdr_size: size of the possible header before the ARP packet
  146. *
  147. * Returns the value of the ip_src field in the ARP packet.
  148. */
  149. static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
  150. {
  151. return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
  152. }
  153. /**
  154. * batadv_arp_hw_dst - extract the hw_dst field from an ARP packet
  155. * @skb: ARP packet
  156. * @hdr_size: size of the possible header before the ARP packet
  157. *
  158. * Returns the value of the hw_dst field in the ARP packet.
  159. */
  160. static uint8_t *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
  161. {
  162. return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
  163. }
  164. /**
  165. * batadv_arp_ip_dst - extract the ip_dst field from an ARP packet
  166. * @skb: ARP packet
  167. * @hdr_size: size of the possible header before the ARP packet
  168. *
  169. * Returns the value of the ip_dst field in the ARP packet.
  170. */
  171. static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
  172. {
  173. return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4);
  174. }
  175. /**
  176. * batadv_hash_dat - compute the hash value for an IP address
  177. * @data: data to hash
  178. * @size: size of the hash table
  179. *
  180. * Returns the selected index in the hash table for the given data.
  181. */
  182. static uint32_t batadv_hash_dat(const void *data, uint32_t size)
  183. {
  184. uint32_t hash = 0;
  185. const struct batadv_dat_entry *dat = data;
  186. hash = batadv_hash_bytes(hash, &dat->ip, sizeof(dat->ip));
  187. hash = batadv_hash_bytes(hash, &dat->vid, sizeof(dat->vid));
  188. hash += (hash << 3);
  189. hash ^= (hash >> 11);
  190. hash += (hash << 15);
  191. return hash % size;
  192. }
  193. /**
  194. * batadv_dat_entry_hash_find - look for a given dat_entry in the local hash
  195. * table
  196. * @bat_priv: the bat priv with all the soft interface information
  197. * @ip: search key
  198. * @vid: VLAN identifier
  199. *
  200. * Returns the dat_entry if found, NULL otherwise.
  201. */
  202. static struct batadv_dat_entry *
  203. batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
  204. unsigned short vid)
  205. {
  206. struct hlist_head *head;
  207. struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
  208. struct batadv_hashtable *hash = bat_priv->dat.hash;
  209. uint32_t index;
  210. if (!hash)
  211. return NULL;
  212. to_find.ip = ip;
  213. to_find.vid = vid;
  214. index = batadv_hash_dat(&to_find, hash->size);
  215. head = &hash->table[index];
  216. rcu_read_lock();
  217. hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
  218. if (dat_entry->ip != ip)
  219. continue;
  220. if (!atomic_inc_not_zero(&dat_entry->refcount))
  221. continue;
  222. dat_entry_tmp = dat_entry;
  223. break;
  224. }
  225. rcu_read_unlock();
  226. return dat_entry_tmp;
  227. }
  228. /**
  229. * batadv_dat_entry_add - add a new dat entry or update it if already exists
  230. * @bat_priv: the bat priv with all the soft interface information
  231. * @ip: ipv4 to add/edit
  232. * @mac_addr: mac address to assign to the given ipv4
  233. * @vid: VLAN identifier
  234. */
  235. static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
  236. uint8_t *mac_addr, unsigned short vid)
  237. {
  238. struct batadv_dat_entry *dat_entry;
  239. int hash_added;
  240. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
  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. ether_addr_copy(dat_entry->mac_addr, mac_addr);
  245. dat_entry->last_update = jiffies;
  246. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  247. "Entry updated: %pI4 %pM (vid: %d)\n",
  248. &dat_entry->ip, dat_entry->mac_addr,
  249. BATADV_PRINT_VID(vid));
  250. goto out;
  251. }
  252. dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
  253. if (!dat_entry)
  254. goto out;
  255. dat_entry->ip = ip;
  256. dat_entry->vid = vid;
  257. ether_addr_copy(dat_entry->mac_addr, mac_addr);
  258. dat_entry->last_update = jiffies;
  259. atomic_set(&dat_entry->refcount, 2);
  260. hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
  261. batadv_hash_dat, dat_entry,
  262. &dat_entry->hash_entry);
  263. if (unlikely(hash_added != 0)) {
  264. /* remove the reference for the hash */
  265. batadv_dat_entry_free_ref(dat_entry);
  266. goto out;
  267. }
  268. batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
  269. &dat_entry->ip, dat_entry->mac_addr, BATADV_PRINT_VID(vid));
  270. out:
  271. if (dat_entry)
  272. batadv_dat_entry_free_ref(dat_entry);
  273. }
  274. #ifdef CONFIG_BATMAN_ADV_DEBUG
  275. /**
  276. * batadv_dbg_arp - print a debug message containing all the ARP packet details
  277. * @bat_priv: the bat priv with all the soft interface information
  278. * @skb: ARP packet
  279. * @type: ARP type
  280. * @hdr_size: size of the possible header before the ARP packet
  281. * @msg: message to print together with the debugging information
  282. */
  283. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  284. uint16_t type, int hdr_size, char *msg)
  285. {
  286. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  287. struct batadv_bcast_packet *bcast_pkt;
  288. uint8_t *orig_addr;
  289. __be32 ip_src, ip_dst;
  290. if (msg)
  291. batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
  292. ip_src = batadv_arp_ip_src(skb, hdr_size);
  293. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  294. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  295. "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
  296. batadv_arp_hw_src(skb, hdr_size), &ip_src,
  297. batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
  298. if (hdr_size == 0)
  299. return;
  300. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  301. switch (unicast_4addr_packet->u.packet_type) {
  302. case BATADV_UNICAST:
  303. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  304. "* encapsulated within a UNICAST packet\n");
  305. break;
  306. case BATADV_UNICAST_4ADDR:
  307. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  308. "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
  309. unicast_4addr_packet->src);
  310. switch (unicast_4addr_packet->subtype) {
  311. case BATADV_P_DAT_DHT_PUT:
  312. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
  313. break;
  314. case BATADV_P_DAT_DHT_GET:
  315. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
  316. break;
  317. case BATADV_P_DAT_CACHE_REPLY:
  318. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  319. "* type: DAT_CACHE_REPLY\n");
  320. break;
  321. case BATADV_P_DATA:
  322. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
  323. break;
  324. default:
  325. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
  326. unicast_4addr_packet->u.packet_type);
  327. }
  328. break;
  329. case BATADV_BCAST:
  330. bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
  331. orig_addr = bcast_pkt->orig;
  332. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  333. "* encapsulated within a BCAST packet (src: %pM)\n",
  334. orig_addr);
  335. break;
  336. default:
  337. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  338. "* encapsulated within an unknown packet type (0x%x)\n",
  339. unicast_4addr_packet->u.packet_type);
  340. }
  341. }
  342. #else
  343. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  344. uint16_t type, int hdr_size, char *msg)
  345. {
  346. }
  347. #endif /* CONFIG_BATMAN_ADV_DEBUG */
  348. /**
  349. * batadv_is_orig_node_eligible - check whether a node can be a DHT candidate
  350. * @res: the array with the already selected candidates
  351. * @select: number of already selected candidates
  352. * @tmp_max: address of the currently evaluated node
  353. * @max: current round max address
  354. * @last_max: address of the last selected candidate
  355. * @candidate: orig_node under evaluation
  356. * @max_orig_node: last selected candidate
  357. *
  358. * Returns true if the node has been elected as next candidate or false
  359. * otherwise.
  360. */
  361. static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
  362. int select, batadv_dat_addr_t tmp_max,
  363. batadv_dat_addr_t max,
  364. batadv_dat_addr_t last_max,
  365. struct batadv_orig_node *candidate,
  366. struct batadv_orig_node *max_orig_node)
  367. {
  368. bool ret = false;
  369. int j;
  370. /* check if orig node candidate is running DAT */
  371. if (!(candidate->capabilities & BATADV_ORIG_CAPA_HAS_DAT))
  372. goto out;
  373. /* Check if this node has already been selected... */
  374. for (j = 0; j < select; j++)
  375. if (res[j].orig_node == candidate)
  376. break;
  377. /* ..and possibly skip it */
  378. if (j < select)
  379. goto out;
  380. /* sanity check: has it already been selected? This should not happen */
  381. if (tmp_max > last_max)
  382. goto out;
  383. /* check if during this iteration an originator with a closer dht
  384. * address has already been found
  385. */
  386. if (tmp_max < max)
  387. goto out;
  388. /* this is an hash collision with the temporary selected node. Choose
  389. * the one with the lowest address
  390. */
  391. if ((tmp_max == max) && max_orig_node &&
  392. (batadv_compare_eth(candidate->orig, max_orig_node->orig) > 0))
  393. goto out;
  394. ret = true;
  395. out:
  396. return ret;
  397. }
  398. /**
  399. * batadv_choose_next_candidate - select the next DHT candidate
  400. * @bat_priv: the bat priv with all the soft interface information
  401. * @cands: candidates array
  402. * @select: number of candidates already present in the array
  403. * @ip_key: key to look up in the DHT
  404. * @last_max: pointer where the address of the selected candidate will be saved
  405. */
  406. static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
  407. struct batadv_dat_candidate *cands,
  408. int select, batadv_dat_addr_t ip_key,
  409. batadv_dat_addr_t *last_max)
  410. {
  411. batadv_dat_addr_t max = 0, tmp_max = 0;
  412. struct batadv_orig_node *orig_node, *max_orig_node = NULL;
  413. struct batadv_hashtable *hash = bat_priv->orig_hash;
  414. struct hlist_head *head;
  415. int i;
  416. /* if no node is eligible as candidate, leave the candidate type as
  417. * NOT_FOUND
  418. */
  419. cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
  420. /* iterate over the originator list and find the node with the closest
  421. * dat_address which has not been selected yet
  422. */
  423. for (i = 0; i < hash->size; i++) {
  424. head = &hash->table[i];
  425. rcu_read_lock();
  426. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  427. /* the dht space is a ring using unsigned addresses */
  428. tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
  429. ip_key;
  430. if (!batadv_is_orig_node_eligible(cands, select,
  431. tmp_max, max,
  432. *last_max, orig_node,
  433. max_orig_node))
  434. continue;
  435. if (!atomic_inc_not_zero(&orig_node->refcount))
  436. continue;
  437. max = tmp_max;
  438. if (max_orig_node)
  439. batadv_orig_node_free_ref(max_orig_node);
  440. max_orig_node = orig_node;
  441. }
  442. rcu_read_unlock();
  443. }
  444. if (max_orig_node) {
  445. cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
  446. cands[select].orig_node = max_orig_node;
  447. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  448. "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
  449. select, max_orig_node->orig, max_orig_node->dat_addr,
  450. max);
  451. }
  452. *last_max = max;
  453. }
  454. /**
  455. * batadv_dat_select_candidates - select the nodes which the DHT message has to
  456. * be sent to
  457. * @bat_priv: the bat priv with all the soft interface information
  458. * @ip_dst: ipv4 to look up in the DHT
  459. *
  460. * An originator O is selected if and only if its DHT_ID value is one of three
  461. * closest values (from the LEFT, with wrap around if needed) then the hash
  462. * value of the key. ip_dst is the key.
  463. *
  464. * Returns the candidate array of size BATADV_DAT_CANDIDATE_NUM.
  465. */
  466. static struct batadv_dat_candidate *
  467. batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst)
  468. {
  469. int select;
  470. batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
  471. struct batadv_dat_candidate *res;
  472. if (!bat_priv->orig_hash)
  473. return NULL;
  474. res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
  475. GFP_ATOMIC);
  476. if (!res)
  477. return NULL;
  478. ip_key = (batadv_dat_addr_t)batadv_hash_dat(&ip_dst,
  479. BATADV_DAT_ADDR_MAX);
  480. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  481. "dat_select_candidates(): IP=%pI4 hash(IP)=%u\n", &ip_dst,
  482. ip_key);
  483. for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
  484. batadv_choose_next_candidate(bat_priv, res, select, ip_key,
  485. &last_max);
  486. return res;
  487. }
  488. /**
  489. * batadv_dat_send_data - send a payload to the selected candidates
  490. * @bat_priv: the bat priv with all the soft interface information
  491. * @skb: payload to send
  492. * @ip: the DHT key
  493. * @packet_subtype: unicast4addr packet subtype to use
  494. *
  495. * This function copies the skb with pskb_copy() and is sent as unicast packet
  496. * to each of the selected candidates.
  497. *
  498. * Returns true if the packet is sent to at least one candidate, false
  499. * otherwise.
  500. */
  501. static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
  502. struct sk_buff *skb, __be32 ip,
  503. int packet_subtype)
  504. {
  505. int i;
  506. bool ret = false;
  507. int send_status;
  508. struct batadv_neigh_node *neigh_node = NULL;
  509. struct sk_buff *tmp_skb;
  510. struct batadv_dat_candidate *cand;
  511. cand = batadv_dat_select_candidates(bat_priv, ip);
  512. if (!cand)
  513. goto out;
  514. batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
  515. for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
  516. if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
  517. continue;
  518. neigh_node = batadv_orig_router_get(cand[i].orig_node,
  519. BATADV_IF_DEFAULT);
  520. if (!neigh_node)
  521. goto free_orig;
  522. tmp_skb = pskb_copy_for_clone(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, vid)) {
  816. ret = true;
  817. goto out;
  818. }
  819. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  820. bat_priv->soft_iface, ip_dst, hw_src,
  821. dat_entry->mac_addr, hw_src);
  822. if (!skb_new)
  823. goto out;
  824. if (vid & BATADV_VLAN_HAS_TAG)
  825. skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
  826. vid & VLAN_VID_MASK);
  827. skb_reset_mac_header(skb_new);
  828. skb_new->protocol = eth_type_trans(skb_new,
  829. bat_priv->soft_iface);
  830. bat_priv->stats.rx_packets++;
  831. bat_priv->stats.rx_bytes += skb->len + ETH_HLEN + hdr_size;
  832. bat_priv->soft_iface->last_rx = jiffies;
  833. netif_rx(skb_new);
  834. batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
  835. ret = true;
  836. } else {
  837. /* Send the request to the DHT */
  838. ret = batadv_dat_send_data(bat_priv, skb, ip_dst,
  839. BATADV_P_DAT_DHT_GET);
  840. }
  841. out:
  842. if (dat_entry)
  843. batadv_dat_entry_free_ref(dat_entry);
  844. return ret;
  845. }
  846. /**
  847. * batadv_dat_snoop_incoming_arp_request - snoop the ARP request and try to
  848. * answer using the local DAT storage
  849. * @bat_priv: the bat priv with all the soft interface information
  850. * @skb: packet to check
  851. * @hdr_size: size of the encapsulation header
  852. *
  853. * Returns true if the request has been answered, false otherwise.
  854. */
  855. bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
  856. struct sk_buff *skb, int hdr_size)
  857. {
  858. uint16_t type;
  859. __be32 ip_src, ip_dst;
  860. uint8_t *hw_src;
  861. struct sk_buff *skb_new;
  862. struct batadv_dat_entry *dat_entry = NULL;
  863. bool ret = false;
  864. unsigned short vid;
  865. int err;
  866. if (!atomic_read(&bat_priv->distributed_arp_table))
  867. goto out;
  868. vid = batadv_dat_get_vid(skb, &hdr_size);
  869. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  870. if (type != ARPOP_REQUEST)
  871. goto out;
  872. hw_src = batadv_arp_hw_src(skb, hdr_size);
  873. ip_src = batadv_arp_ip_src(skb, hdr_size);
  874. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  875. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  876. "Parsing incoming ARP REQUEST");
  877. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  878. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  879. if (!dat_entry)
  880. goto out;
  881. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  882. bat_priv->soft_iface, ip_dst, hw_src,
  883. dat_entry->mac_addr, hw_src);
  884. if (!skb_new)
  885. goto out;
  886. /* the rest of the TX path assumes that the mac_header offset pointing
  887. * to the inner Ethernet header has been set, therefore reset it now.
  888. */
  889. skb_reset_mac_header(skb_new);
  890. if (vid & BATADV_VLAN_HAS_TAG)
  891. skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
  892. vid & VLAN_VID_MASK);
  893. /* To preserve backwards compatibility, the node has choose the outgoing
  894. * format based on the incoming request packet type. The assumption is
  895. * that a node not using the 4addr packet format doesn't support it.
  896. */
  897. if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
  898. err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
  899. BATADV_P_DAT_CACHE_REPLY,
  900. NULL, vid);
  901. else
  902. err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
  903. if (err != NET_XMIT_DROP) {
  904. batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
  905. ret = true;
  906. }
  907. out:
  908. if (dat_entry)
  909. batadv_dat_entry_free_ref(dat_entry);
  910. if (ret)
  911. kfree_skb(skb);
  912. return ret;
  913. }
  914. /**
  915. * batadv_dat_snoop_outgoing_arp_reply - snoop the ARP reply and fill the DHT
  916. * @bat_priv: the bat priv with all the soft interface information
  917. * @skb: packet to check
  918. */
  919. void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
  920. struct sk_buff *skb)
  921. {
  922. uint16_t type;
  923. __be32 ip_src, ip_dst;
  924. uint8_t *hw_src, *hw_dst;
  925. int hdr_size = 0;
  926. unsigned short vid;
  927. if (!atomic_read(&bat_priv->distributed_arp_table))
  928. return;
  929. vid = batadv_dat_get_vid(skb, &hdr_size);
  930. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  931. if (type != ARPOP_REPLY)
  932. return;
  933. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  934. "Parsing outgoing ARP REPLY");
  935. hw_src = batadv_arp_hw_src(skb, hdr_size);
  936. ip_src = batadv_arp_ip_src(skb, hdr_size);
  937. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  938. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  939. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  940. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  941. /* Send the ARP reply to the candidates for both the IP addresses that
  942. * the node obtained from the ARP reply
  943. */
  944. batadv_dat_send_data(bat_priv, skb, ip_src, BATADV_P_DAT_DHT_PUT);
  945. batadv_dat_send_data(bat_priv, skb, ip_dst, BATADV_P_DAT_DHT_PUT);
  946. }
  947. /**
  948. * batadv_dat_snoop_incoming_arp_reply - snoop the ARP reply and fill the local
  949. * DAT storage only
  950. * @bat_priv: the bat priv with all the soft interface information
  951. * @skb: packet to check
  952. * @hdr_size: size of the encapsulation header
  953. */
  954. bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
  955. struct sk_buff *skb, int hdr_size)
  956. {
  957. uint16_t type;
  958. __be32 ip_src, ip_dst;
  959. uint8_t *hw_src, *hw_dst;
  960. bool ret = false;
  961. unsigned short vid;
  962. if (!atomic_read(&bat_priv->distributed_arp_table))
  963. goto out;
  964. vid = batadv_dat_get_vid(skb, &hdr_size);
  965. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  966. if (type != ARPOP_REPLY)
  967. goto out;
  968. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  969. "Parsing incoming ARP REPLY");
  970. hw_src = batadv_arp_hw_src(skb, hdr_size);
  971. ip_src = batadv_arp_ip_src(skb, hdr_size);
  972. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  973. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  974. /* Update our internal cache with both the IP addresses the node got
  975. * within the ARP reply
  976. */
  977. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  978. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  979. /* if this REPLY is directed to a client of mine, let's deliver the
  980. * packet to the interface
  981. */
  982. ret = !batadv_is_my_client(bat_priv, hw_dst, vid);
  983. out:
  984. if (ret)
  985. kfree_skb(skb);
  986. /* if ret == false -> packet has to be delivered to the interface */
  987. return ret;
  988. }
  989. /**
  990. * batadv_dat_drop_broadcast_packet - check if an ARP request has to be dropped
  991. * (because the node has already obtained the reply via DAT) or not
  992. * @bat_priv: the bat priv with all the soft interface information
  993. * @forw_packet: the broadcast packet
  994. *
  995. * Returns true if the node can drop the packet, false otherwise.
  996. */
  997. bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
  998. struct batadv_forw_packet *forw_packet)
  999. {
  1000. uint16_t type;
  1001. __be32 ip_dst;
  1002. struct batadv_dat_entry *dat_entry = NULL;
  1003. bool ret = false;
  1004. int hdr_size = sizeof(struct batadv_bcast_packet);
  1005. unsigned short vid;
  1006. if (!atomic_read(&bat_priv->distributed_arp_table))
  1007. goto out;
  1008. /* If this packet is an ARP_REQUEST and the node already has the
  1009. * information that it is going to ask, then the packet can be dropped
  1010. */
  1011. if (forw_packet->num_packets)
  1012. goto out;
  1013. vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
  1014. type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
  1015. if (type != ARPOP_REQUEST)
  1016. goto out;
  1017. ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
  1018. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  1019. /* check if the node already got this entry */
  1020. if (!dat_entry) {
  1021. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1022. "ARP Request for %pI4: fallback\n", &ip_dst);
  1023. goto out;
  1024. }
  1025. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1026. "ARP Request for %pI4: fallback prevented\n", &ip_dst);
  1027. ret = true;
  1028. out:
  1029. if (dat_entry)
  1030. batadv_dat_entry_free_ref(dat_entry);
  1031. return ret;
  1032. }