distributed-arp-table.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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. * @type: ARP type
  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. u16 type, 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 < sizeof(struct batadv_unicast_packet))
  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. u16 type, 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. "dat_select_candidates(): IP=%pI4 hash(IP)=%u\n", &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 %14pM %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_snoop_outgoing_arp_request - snoop the ARP request and try to
  826. * answer using DAT
  827. * @bat_priv: the bat priv with all the soft interface information
  828. * @skb: packet to check
  829. *
  830. * Return: true if the message has been sent to the dht candidates, false
  831. * otherwise. In case of a positive return value the message has to be enqueued
  832. * to permit the fallback.
  833. */
  834. bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
  835. struct sk_buff *skb)
  836. {
  837. u16 type = 0;
  838. __be32 ip_dst, ip_src;
  839. u8 *hw_src;
  840. bool ret = false;
  841. struct batadv_dat_entry *dat_entry = NULL;
  842. struct sk_buff *skb_new;
  843. int hdr_size = 0;
  844. unsigned short vid;
  845. if (!atomic_read(&bat_priv->distributed_arp_table))
  846. goto out;
  847. vid = batadv_dat_get_vid(skb, &hdr_size);
  848. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  849. /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
  850. * message to the selected DHT candidates
  851. */
  852. if (type != ARPOP_REQUEST)
  853. goto out;
  854. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  855. "Parsing outgoing ARP REQUEST");
  856. ip_src = batadv_arp_ip_src(skb, hdr_size);
  857. hw_src = batadv_arp_hw_src(skb, hdr_size);
  858. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  859. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  860. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  861. if (dat_entry) {
  862. /* If the ARP request is destined for a local client the local
  863. * client will answer itself. DAT would only generate a
  864. * duplicate packet.
  865. *
  866. * Moreover, if the soft-interface is enslaved into a bridge, an
  867. * additional DAT answer may trigger kernel warnings about
  868. * a packet coming from the wrong port.
  869. */
  870. if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
  871. ret = true;
  872. goto out;
  873. }
  874. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  875. bat_priv->soft_iface, ip_dst, hw_src,
  876. dat_entry->mac_addr, hw_src);
  877. if (!skb_new)
  878. goto out;
  879. if (vid & BATADV_VLAN_HAS_TAG) {
  880. skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
  881. vid & VLAN_VID_MASK);
  882. if (!skb_new)
  883. goto out;
  884. }
  885. skb_reset_mac_header(skb_new);
  886. skb_new->protocol = eth_type_trans(skb_new,
  887. bat_priv->soft_iface);
  888. bat_priv->stats.rx_packets++;
  889. bat_priv->stats.rx_bytes += skb->len + ETH_HLEN + hdr_size;
  890. bat_priv->soft_iface->last_rx = jiffies;
  891. netif_rx(skb_new);
  892. batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
  893. ret = true;
  894. } else {
  895. /* Send the request to the DHT */
  896. ret = batadv_dat_send_data(bat_priv, skb, ip_dst, vid,
  897. BATADV_P_DAT_DHT_GET);
  898. }
  899. out:
  900. if (dat_entry)
  901. batadv_dat_entry_put(dat_entry);
  902. return ret;
  903. }
  904. /**
  905. * batadv_dat_snoop_incoming_arp_request - snoop the ARP request and try to
  906. * answer using the local DAT storage
  907. * @bat_priv: the bat priv with all the soft interface information
  908. * @skb: packet to check
  909. * @hdr_size: size of the encapsulation header
  910. *
  911. * Return: true if the request has been answered, false otherwise.
  912. */
  913. bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
  914. struct sk_buff *skb, int hdr_size)
  915. {
  916. u16 type;
  917. __be32 ip_src, ip_dst;
  918. u8 *hw_src;
  919. struct sk_buff *skb_new;
  920. struct batadv_dat_entry *dat_entry = NULL;
  921. bool ret = false;
  922. unsigned short vid;
  923. int err;
  924. if (!atomic_read(&bat_priv->distributed_arp_table))
  925. goto out;
  926. vid = batadv_dat_get_vid(skb, &hdr_size);
  927. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  928. if (type != ARPOP_REQUEST)
  929. goto out;
  930. hw_src = batadv_arp_hw_src(skb, hdr_size);
  931. ip_src = batadv_arp_ip_src(skb, hdr_size);
  932. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  933. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  934. "Parsing incoming ARP REQUEST");
  935. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  936. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  937. if (!dat_entry)
  938. goto out;
  939. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  940. bat_priv->soft_iface, ip_dst, hw_src,
  941. dat_entry->mac_addr, hw_src);
  942. if (!skb_new)
  943. goto out;
  944. /* the rest of the TX path assumes that the mac_header offset pointing
  945. * to the inner Ethernet header has been set, therefore reset it now.
  946. */
  947. skb_reset_mac_header(skb_new);
  948. if (vid & BATADV_VLAN_HAS_TAG) {
  949. skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
  950. vid & VLAN_VID_MASK);
  951. if (!skb_new)
  952. goto out;
  953. }
  954. /* To preserve backwards compatibility, the node has choose the outgoing
  955. * format based on the incoming request packet type. The assumption is
  956. * that a node not using the 4addr packet format doesn't support it.
  957. */
  958. if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
  959. err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
  960. BATADV_P_DAT_CACHE_REPLY,
  961. NULL, vid);
  962. else
  963. err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
  964. if (err != NET_XMIT_DROP) {
  965. batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
  966. ret = true;
  967. }
  968. out:
  969. if (dat_entry)
  970. batadv_dat_entry_put(dat_entry);
  971. if (ret)
  972. kfree_skb(skb);
  973. return ret;
  974. }
  975. /**
  976. * batadv_dat_snoop_outgoing_arp_reply - snoop the ARP reply and fill the DHT
  977. * @bat_priv: the bat priv with all the soft interface information
  978. * @skb: packet to check
  979. */
  980. void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
  981. struct sk_buff *skb)
  982. {
  983. u16 type;
  984. __be32 ip_src, ip_dst;
  985. u8 *hw_src, *hw_dst;
  986. int hdr_size = 0;
  987. unsigned short vid;
  988. if (!atomic_read(&bat_priv->distributed_arp_table))
  989. return;
  990. vid = batadv_dat_get_vid(skb, &hdr_size);
  991. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  992. if (type != ARPOP_REPLY)
  993. return;
  994. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  995. "Parsing outgoing ARP REPLY");
  996. hw_src = batadv_arp_hw_src(skb, hdr_size);
  997. ip_src = batadv_arp_ip_src(skb, hdr_size);
  998. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  999. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1000. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1001. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  1002. /* Send the ARP reply to the candidates for both the IP addresses that
  1003. * the node obtained from the ARP reply
  1004. */
  1005. batadv_dat_send_data(bat_priv, skb, ip_src, vid, BATADV_P_DAT_DHT_PUT);
  1006. batadv_dat_send_data(bat_priv, skb, ip_dst, vid, BATADV_P_DAT_DHT_PUT);
  1007. }
  1008. /**
  1009. * batadv_dat_snoop_incoming_arp_reply - snoop the ARP reply and fill the local
  1010. * DAT storage only
  1011. * @bat_priv: the bat priv with all the soft interface information
  1012. * @skb: packet to check
  1013. * @hdr_size: size of the encapsulation header
  1014. *
  1015. * Return: true if the packet was snooped and consumed by DAT. False if the
  1016. * packet has to be delivered to the interface
  1017. */
  1018. bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
  1019. struct sk_buff *skb, int hdr_size)
  1020. {
  1021. u16 type;
  1022. __be32 ip_src, ip_dst;
  1023. u8 *hw_src, *hw_dst;
  1024. bool dropped = false;
  1025. unsigned short vid;
  1026. if (!atomic_read(&bat_priv->distributed_arp_table))
  1027. goto out;
  1028. vid = batadv_dat_get_vid(skb, &hdr_size);
  1029. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  1030. if (type != ARPOP_REPLY)
  1031. goto out;
  1032. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  1033. "Parsing incoming ARP REPLY");
  1034. hw_src = batadv_arp_hw_src(skb, hdr_size);
  1035. ip_src = batadv_arp_ip_src(skb, hdr_size);
  1036. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  1037. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1038. /* Update our internal cache with both the IP addresses the node got
  1039. * within the ARP reply
  1040. */
  1041. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1042. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  1043. /* if this REPLY is directed to a client of mine, let's deliver the
  1044. * packet to the interface
  1045. */
  1046. dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
  1047. /* if this REPLY is sent on behalf of a client of mine, let's drop the
  1048. * packet because the client will reply by itself
  1049. */
  1050. dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
  1051. out:
  1052. if (dropped)
  1053. kfree_skb(skb);
  1054. /* if dropped == false -> deliver to the interface */
  1055. return dropped;
  1056. }
  1057. /**
  1058. * batadv_dat_drop_broadcast_packet - check if an ARP request has to be dropped
  1059. * (because the node has already obtained the reply via DAT) or not
  1060. * @bat_priv: the bat priv with all the soft interface information
  1061. * @forw_packet: the broadcast packet
  1062. *
  1063. * Return: true if the node can drop the packet, false otherwise.
  1064. */
  1065. bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
  1066. struct batadv_forw_packet *forw_packet)
  1067. {
  1068. u16 type;
  1069. __be32 ip_dst;
  1070. struct batadv_dat_entry *dat_entry = NULL;
  1071. bool ret = false;
  1072. int hdr_size = sizeof(struct batadv_bcast_packet);
  1073. unsigned short vid;
  1074. if (!atomic_read(&bat_priv->distributed_arp_table))
  1075. goto out;
  1076. /* If this packet is an ARP_REQUEST and the node already has the
  1077. * information that it is going to ask, then the packet can be dropped
  1078. */
  1079. if (forw_packet->num_packets)
  1080. goto out;
  1081. vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
  1082. type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
  1083. if (type != ARPOP_REQUEST)
  1084. goto out;
  1085. ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
  1086. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  1087. /* check if the node already got this entry */
  1088. if (!dat_entry) {
  1089. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1090. "ARP Request for %pI4: fallback\n", &ip_dst);
  1091. goto out;
  1092. }
  1093. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1094. "ARP Request for %pI4: fallback prevented\n", &ip_dst);
  1095. ret = true;
  1096. out:
  1097. if (dat_entry)
  1098. batadv_dat_entry_put(dat_entry);
  1099. return ret;
  1100. }