distributed-arp-table.c 36 KB

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