distributed-arp-table.c 36 KB

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