distributed-arp-table.c 38 KB

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