distributed-arp-table.c 35 KB

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