distributed-arp-table.c 35 KB

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