distributed-arp-table.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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 == 0)
  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. *
  491. * An originator O is selected if and only if its DHT_ID value is one of three
  492. * closest values (from the LEFT, with wrap around if needed) then the hash
  493. * value of the key. ip_dst is the key.
  494. *
  495. * Returns the candidate array of size BATADV_DAT_CANDIDATE_NUM.
  496. */
  497. static struct batadv_dat_candidate *
  498. batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst)
  499. {
  500. int select;
  501. batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
  502. struct batadv_dat_candidate *res;
  503. if (!bat_priv->orig_hash)
  504. return NULL;
  505. res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
  506. GFP_ATOMIC);
  507. if (!res)
  508. return NULL;
  509. ip_key = (batadv_dat_addr_t)batadv_hash_dat(&ip_dst,
  510. BATADV_DAT_ADDR_MAX);
  511. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  512. "dat_select_candidates(): IP=%pI4 hash(IP)=%u\n", &ip_dst,
  513. ip_key);
  514. for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
  515. batadv_choose_next_candidate(bat_priv, res, select, ip_key,
  516. &last_max);
  517. return res;
  518. }
  519. /**
  520. * batadv_dat_send_data - send a payload to the selected candidates
  521. * @bat_priv: the bat priv with all the soft interface information
  522. * @skb: payload to send
  523. * @ip: the DHT key
  524. * @packet_subtype: unicast4addr packet subtype to use
  525. *
  526. * This function copies the skb with pskb_copy() and is sent as unicast packet
  527. * to each of the selected candidates.
  528. *
  529. * Returns true if the packet is sent to at least one candidate, false
  530. * otherwise.
  531. */
  532. static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
  533. struct sk_buff *skb, __be32 ip,
  534. int packet_subtype)
  535. {
  536. int i;
  537. bool ret = false;
  538. int send_status;
  539. struct batadv_neigh_node *neigh_node = NULL;
  540. struct sk_buff *tmp_skb;
  541. struct batadv_dat_candidate *cand;
  542. cand = batadv_dat_select_candidates(bat_priv, ip);
  543. if (!cand)
  544. goto out;
  545. batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
  546. for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
  547. if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
  548. continue;
  549. neigh_node = batadv_orig_router_get(cand[i].orig_node,
  550. BATADV_IF_DEFAULT);
  551. if (!neigh_node)
  552. goto free_orig;
  553. tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
  554. if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
  555. cand[i].orig_node,
  556. packet_subtype)) {
  557. kfree_skb(tmp_skb);
  558. goto free_neigh;
  559. }
  560. send_status = batadv_send_skb_packet(tmp_skb,
  561. neigh_node->if_incoming,
  562. neigh_node->addr);
  563. if (send_status == NET_XMIT_SUCCESS) {
  564. /* count the sent packet */
  565. switch (packet_subtype) {
  566. case BATADV_P_DAT_DHT_GET:
  567. batadv_inc_counter(bat_priv,
  568. BATADV_CNT_DAT_GET_TX);
  569. break;
  570. case BATADV_P_DAT_DHT_PUT:
  571. batadv_inc_counter(bat_priv,
  572. BATADV_CNT_DAT_PUT_TX);
  573. break;
  574. }
  575. /* packet sent to a candidate: return true */
  576. ret = true;
  577. }
  578. free_neigh:
  579. batadv_neigh_node_free_ref(neigh_node);
  580. free_orig:
  581. batadv_orig_node_free_ref(cand[i].orig_node);
  582. }
  583. out:
  584. kfree(cand);
  585. return ret;
  586. }
  587. /**
  588. * batadv_dat_tvlv_container_update - update the dat tvlv container after dat
  589. * setting change
  590. * @bat_priv: the bat priv with all the soft interface information
  591. */
  592. static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
  593. {
  594. char dat_mode;
  595. dat_mode = atomic_read(&bat_priv->distributed_arp_table);
  596. switch (dat_mode) {
  597. case 0:
  598. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  599. break;
  600. case 1:
  601. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
  602. NULL, 0);
  603. break;
  604. }
  605. }
  606. /**
  607. * batadv_dat_status_update - update the dat tvlv container after dat
  608. * setting change
  609. * @net_dev: the soft interface net device
  610. */
  611. void batadv_dat_status_update(struct net_device *net_dev)
  612. {
  613. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  614. batadv_dat_tvlv_container_update(bat_priv);
  615. }
  616. /**
  617. * batadv_gw_tvlv_ogm_handler_v1 - process incoming dat tvlv container
  618. * @bat_priv: the bat priv with all the soft interface information
  619. * @orig: the orig_node of the ogm
  620. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  621. * @tvlv_value: tvlv buffer containing the gateway data
  622. * @tvlv_value_len: tvlv buffer length
  623. */
  624. static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  625. struct batadv_orig_node *orig,
  626. u8 flags,
  627. void *tvlv_value, u16 tvlv_value_len)
  628. {
  629. if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
  630. clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
  631. else
  632. set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
  633. }
  634. /**
  635. * batadv_dat_hash_free - free the local DAT hash table
  636. * @bat_priv: the bat priv with all the soft interface information
  637. */
  638. static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
  639. {
  640. if (!bat_priv->dat.hash)
  641. return;
  642. __batadv_dat_purge(bat_priv, NULL);
  643. batadv_hash_destroy(bat_priv->dat.hash);
  644. bat_priv->dat.hash = NULL;
  645. }
  646. /**
  647. * batadv_dat_init - initialise the DAT internals
  648. * @bat_priv: the bat priv with all the soft interface information
  649. */
  650. int batadv_dat_init(struct batadv_priv *bat_priv)
  651. {
  652. if (bat_priv->dat.hash)
  653. return 0;
  654. bat_priv->dat.hash = batadv_hash_new(1024);
  655. if (!bat_priv->dat.hash)
  656. return -ENOMEM;
  657. batadv_dat_start_timer(bat_priv);
  658. batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
  659. NULL, BATADV_TVLV_DAT, 1,
  660. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  661. batadv_dat_tvlv_container_update(bat_priv);
  662. return 0;
  663. }
  664. /**
  665. * batadv_dat_free - free the DAT internals
  666. * @bat_priv: the bat priv with all the soft interface information
  667. */
  668. void batadv_dat_free(struct batadv_priv *bat_priv)
  669. {
  670. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  671. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  672. cancel_delayed_work_sync(&bat_priv->dat.work);
  673. batadv_dat_hash_free(bat_priv);
  674. }
  675. /**
  676. * batadv_dat_cache_seq_print_text - print the local DAT hash table
  677. * @seq: seq file to print on
  678. * @offset: not used
  679. */
  680. int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
  681. {
  682. struct net_device *net_dev = (struct net_device *)seq->private;
  683. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  684. struct batadv_hashtable *hash = bat_priv->dat.hash;
  685. struct batadv_dat_entry *dat_entry;
  686. struct batadv_hard_iface *primary_if;
  687. struct hlist_head *head;
  688. unsigned long last_seen_jiffies;
  689. int last_seen_msecs, last_seen_secs, last_seen_mins;
  690. u32 i;
  691. primary_if = batadv_seq_print_text_primary_if_get(seq);
  692. if (!primary_if)
  693. goto out;
  694. seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
  695. seq_printf(seq, " %-7s %-9s %4s %11s\n", "IPv4",
  696. "MAC", "VID", "last-seen");
  697. for (i = 0; i < hash->size; i++) {
  698. head = &hash->table[i];
  699. rcu_read_lock();
  700. hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
  701. last_seen_jiffies = jiffies - dat_entry->last_update;
  702. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  703. last_seen_mins = last_seen_msecs / 60000;
  704. last_seen_msecs = last_seen_msecs % 60000;
  705. last_seen_secs = last_seen_msecs / 1000;
  706. seq_printf(seq, " * %15pI4 %14pM %4i %6i:%02i\n",
  707. &dat_entry->ip, dat_entry->mac_addr,
  708. BATADV_PRINT_VID(dat_entry->vid),
  709. last_seen_mins, last_seen_secs);
  710. }
  711. rcu_read_unlock();
  712. }
  713. out:
  714. if (primary_if)
  715. batadv_hardif_free_ref(primary_if);
  716. return 0;
  717. }
  718. /**
  719. * batadv_arp_get_type - parse an ARP packet and gets the type
  720. * @bat_priv: the bat priv with all the soft interface information
  721. * @skb: packet to analyse
  722. * @hdr_size: size of the possible header before the ARP packet in the skb
  723. *
  724. * Returns the ARP type if the skb contains a valid ARP packet, 0 otherwise.
  725. */
  726. static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
  727. struct sk_buff *skb, int hdr_size)
  728. {
  729. struct arphdr *arphdr;
  730. struct ethhdr *ethhdr;
  731. __be32 ip_src, ip_dst;
  732. u8 *hw_src, *hw_dst;
  733. u16 type = 0;
  734. /* pull the ethernet header */
  735. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
  736. goto out;
  737. ethhdr = (struct ethhdr *)(skb->data + hdr_size);
  738. if (ethhdr->h_proto != htons(ETH_P_ARP))
  739. goto out;
  740. /* pull the ARP payload */
  741. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
  742. arp_hdr_len(skb->dev))))
  743. goto out;
  744. arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
  745. /* check whether the ARP packet carries a valid IP information */
  746. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  747. goto out;
  748. if (arphdr->ar_pro != htons(ETH_P_IP))
  749. goto out;
  750. if (arphdr->ar_hln != ETH_ALEN)
  751. goto out;
  752. if (arphdr->ar_pln != 4)
  753. goto out;
  754. /* Check for bad reply/request. If the ARP message is not sane, DAT
  755. * will simply ignore it
  756. */
  757. ip_src = batadv_arp_ip_src(skb, hdr_size);
  758. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  759. if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
  760. ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
  761. ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
  762. ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
  763. goto out;
  764. hw_src = batadv_arp_hw_src(skb, hdr_size);
  765. if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
  766. goto out;
  767. /* don't care about the destination MAC address in ARP requests */
  768. if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
  769. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  770. if (is_zero_ether_addr(hw_dst) ||
  771. is_multicast_ether_addr(hw_dst))
  772. goto out;
  773. }
  774. type = ntohs(arphdr->ar_op);
  775. out:
  776. return type;
  777. }
  778. /**
  779. * batadv_dat_get_vid - extract the VLAN identifier from skb if any
  780. * @skb: the buffer containing the packet to extract the VID from
  781. * @hdr_size: the size of the batman-adv header encapsulating the packet
  782. *
  783. * If the packet embedded in the skb is vlan tagged this function returns the
  784. * VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS is returned.
  785. */
  786. static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
  787. {
  788. unsigned short vid;
  789. vid = batadv_get_vid(skb, *hdr_size);
  790. /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
  791. * If the header contained in the packet is a VLAN one (which is longer)
  792. * hdr_size is updated so that the functions will still skip the
  793. * correct amount of bytes.
  794. */
  795. if (vid & BATADV_VLAN_HAS_TAG)
  796. *hdr_size += VLAN_HLEN;
  797. return vid;
  798. }
  799. /**
  800. * batadv_dat_snoop_outgoing_arp_request - snoop the ARP request and try to
  801. * answer using DAT
  802. * @bat_priv: the bat priv with all the soft interface information
  803. * @skb: packet to check
  804. *
  805. * Returns true if the message has been sent to the dht candidates, false
  806. * otherwise. In case of a positive return value the message has to be enqueued
  807. * to permit the fallback.
  808. */
  809. bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
  810. struct sk_buff *skb)
  811. {
  812. u16 type = 0;
  813. __be32 ip_dst, ip_src;
  814. u8 *hw_src;
  815. bool ret = false;
  816. struct batadv_dat_entry *dat_entry = NULL;
  817. struct sk_buff *skb_new;
  818. int hdr_size = 0;
  819. unsigned short vid;
  820. if (!atomic_read(&bat_priv->distributed_arp_table))
  821. goto out;
  822. vid = batadv_dat_get_vid(skb, &hdr_size);
  823. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  824. /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
  825. * message to the selected DHT candidates
  826. */
  827. if (type != ARPOP_REQUEST)
  828. goto out;
  829. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  830. "Parsing outgoing ARP REQUEST");
  831. ip_src = batadv_arp_ip_src(skb, hdr_size);
  832. hw_src = batadv_arp_hw_src(skb, hdr_size);
  833. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  834. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  835. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  836. if (dat_entry) {
  837. /* If the ARP request is destined for a local client the local
  838. * client will answer itself. DAT would only generate a
  839. * duplicate packet.
  840. *
  841. * Moreover, if the soft-interface is enslaved into a bridge, an
  842. * additional DAT answer may trigger kernel warnings about
  843. * a packet coming from the wrong port.
  844. */
  845. if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
  846. ret = true;
  847. goto out;
  848. }
  849. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  850. bat_priv->soft_iface, ip_dst, hw_src,
  851. dat_entry->mac_addr, hw_src);
  852. if (!skb_new)
  853. goto out;
  854. if (vid & BATADV_VLAN_HAS_TAG)
  855. skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
  856. vid & VLAN_VID_MASK);
  857. skb_reset_mac_header(skb_new);
  858. skb_new->protocol = eth_type_trans(skb_new,
  859. bat_priv->soft_iface);
  860. bat_priv->stats.rx_packets++;
  861. bat_priv->stats.rx_bytes += skb->len + ETH_HLEN + hdr_size;
  862. bat_priv->soft_iface->last_rx = jiffies;
  863. netif_rx(skb_new);
  864. batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
  865. ret = true;
  866. } else {
  867. /* Send the request to the DHT */
  868. ret = batadv_dat_send_data(bat_priv, skb, ip_dst,
  869. BATADV_P_DAT_DHT_GET);
  870. }
  871. out:
  872. if (dat_entry)
  873. batadv_dat_entry_free_ref(dat_entry);
  874. return ret;
  875. }
  876. /**
  877. * batadv_dat_snoop_incoming_arp_request - snoop the ARP request and try to
  878. * answer using the local DAT storage
  879. * @bat_priv: the bat priv with all the soft interface information
  880. * @skb: packet to check
  881. * @hdr_size: size of the encapsulation header
  882. *
  883. * Returns true if the request has been answered, false otherwise.
  884. */
  885. bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
  886. struct sk_buff *skb, int hdr_size)
  887. {
  888. u16 type;
  889. __be32 ip_src, ip_dst;
  890. u8 *hw_src;
  891. struct sk_buff *skb_new;
  892. struct batadv_dat_entry *dat_entry = NULL;
  893. bool ret = false;
  894. unsigned short vid;
  895. int err;
  896. if (!atomic_read(&bat_priv->distributed_arp_table))
  897. goto out;
  898. vid = batadv_dat_get_vid(skb, &hdr_size);
  899. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  900. if (type != ARPOP_REQUEST)
  901. goto out;
  902. hw_src = batadv_arp_hw_src(skb, hdr_size);
  903. ip_src = batadv_arp_ip_src(skb, hdr_size);
  904. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  905. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  906. "Parsing incoming ARP REQUEST");
  907. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  908. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  909. if (!dat_entry)
  910. goto out;
  911. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  912. bat_priv->soft_iface, ip_dst, hw_src,
  913. dat_entry->mac_addr, hw_src);
  914. if (!skb_new)
  915. goto out;
  916. /* the rest of the TX path assumes that the mac_header offset pointing
  917. * to the inner Ethernet header has been set, therefore reset it now.
  918. */
  919. skb_reset_mac_header(skb_new);
  920. if (vid & BATADV_VLAN_HAS_TAG)
  921. skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
  922. vid & VLAN_VID_MASK);
  923. /* To preserve backwards compatibility, the node has choose the outgoing
  924. * format based on the incoming request packet type. The assumption is
  925. * that a node not using the 4addr packet format doesn't support it.
  926. */
  927. if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
  928. err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
  929. BATADV_P_DAT_CACHE_REPLY,
  930. NULL, vid);
  931. else
  932. err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
  933. if (err != NET_XMIT_DROP) {
  934. batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
  935. ret = true;
  936. }
  937. out:
  938. if (dat_entry)
  939. batadv_dat_entry_free_ref(dat_entry);
  940. if (ret)
  941. kfree_skb(skb);
  942. return ret;
  943. }
  944. /**
  945. * batadv_dat_snoop_outgoing_arp_reply - snoop the ARP reply and fill the DHT
  946. * @bat_priv: the bat priv with all the soft interface information
  947. * @skb: packet to check
  948. */
  949. void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
  950. struct sk_buff *skb)
  951. {
  952. u16 type;
  953. __be32 ip_src, ip_dst;
  954. u8 *hw_src, *hw_dst;
  955. int hdr_size = 0;
  956. unsigned short vid;
  957. if (!atomic_read(&bat_priv->distributed_arp_table))
  958. return;
  959. vid = batadv_dat_get_vid(skb, &hdr_size);
  960. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  961. if (type != ARPOP_REPLY)
  962. return;
  963. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  964. "Parsing outgoing ARP REPLY");
  965. hw_src = batadv_arp_hw_src(skb, hdr_size);
  966. ip_src = batadv_arp_ip_src(skb, hdr_size);
  967. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  968. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  969. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  970. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  971. /* Send the ARP reply to the candidates for both the IP addresses that
  972. * the node obtained from the ARP reply
  973. */
  974. batadv_dat_send_data(bat_priv, skb, ip_src, BATADV_P_DAT_DHT_PUT);
  975. batadv_dat_send_data(bat_priv, skb, ip_dst, BATADV_P_DAT_DHT_PUT);
  976. }
  977. /**
  978. * batadv_dat_snoop_incoming_arp_reply - snoop the ARP reply and fill the local
  979. * DAT storage only
  980. * @bat_priv: the bat priv with all the soft interface information
  981. * @skb: packet to check
  982. * @hdr_size: size of the encapsulation header
  983. *
  984. * Returns true if the packet was snooped and consumed by DAT. False if the
  985. * packet has to be delivered to the interface
  986. */
  987. bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
  988. struct sk_buff *skb, int hdr_size)
  989. {
  990. u16 type;
  991. __be32 ip_src, ip_dst;
  992. u8 *hw_src, *hw_dst;
  993. bool dropped = false;
  994. unsigned short vid;
  995. if (!atomic_read(&bat_priv->distributed_arp_table))
  996. goto out;
  997. vid = batadv_dat_get_vid(skb, &hdr_size);
  998. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  999. if (type != ARPOP_REPLY)
  1000. goto out;
  1001. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  1002. "Parsing incoming ARP REPLY");
  1003. hw_src = batadv_arp_hw_src(skb, hdr_size);
  1004. ip_src = batadv_arp_ip_src(skb, hdr_size);
  1005. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  1006. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1007. /* Update our internal cache with both the IP addresses the node got
  1008. * within the ARP reply
  1009. */
  1010. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1011. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  1012. /* if this REPLY is directed to a client of mine, let's deliver the
  1013. * packet to the interface
  1014. */
  1015. dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
  1016. /* if this REPLY is sent on behalf of a client of mine, let's drop the
  1017. * packet because the client will reply by itself
  1018. */
  1019. dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
  1020. out:
  1021. if (dropped)
  1022. kfree_skb(skb);
  1023. /* if dropped == false -> deliver to the interface */
  1024. return dropped;
  1025. }
  1026. /**
  1027. * batadv_dat_drop_broadcast_packet - check if an ARP request has to be dropped
  1028. * (because the node has already obtained the reply via DAT) or not
  1029. * @bat_priv: the bat priv with all the soft interface information
  1030. * @forw_packet: the broadcast packet
  1031. *
  1032. * Returns true if the node can drop the packet, false otherwise.
  1033. */
  1034. bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
  1035. struct batadv_forw_packet *forw_packet)
  1036. {
  1037. u16 type;
  1038. __be32 ip_dst;
  1039. struct batadv_dat_entry *dat_entry = NULL;
  1040. bool ret = false;
  1041. int hdr_size = sizeof(struct batadv_bcast_packet);
  1042. unsigned short vid;
  1043. if (!atomic_read(&bat_priv->distributed_arp_table))
  1044. goto out;
  1045. /* If this packet is an ARP_REQUEST and the node already has the
  1046. * information that it is going to ask, then the packet can be dropped
  1047. */
  1048. if (forw_packet->num_packets)
  1049. goto out;
  1050. vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
  1051. type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
  1052. if (type != ARPOP_REQUEST)
  1053. goto out;
  1054. ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
  1055. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  1056. /* check if the node already got this entry */
  1057. if (!dat_entry) {
  1058. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1059. "ARP Request for %pI4: fallback\n", &ip_dst);
  1060. goto out;
  1061. }
  1062. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1063. "ARP Request for %pI4: fallback prevented\n", &ip_dst);
  1064. ret = true;
  1065. out:
  1066. if (dat_entry)
  1067. batadv_dat_entry_free_ref(dat_entry);
  1068. return ret;
  1069. }