distributed-arp-table.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Antonio Quartulli
  5. */
  6. #include "distributed-arp-table.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/bitops.h>
  10. #include <linux/byteorder/generic.h>
  11. #include <linux/container_of.h>
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/gfp.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/in.h>
  20. #include <linux/ip.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/kref.h>
  23. #include <linux/list.h>
  24. #include <linux/netlink.h>
  25. #include <linux/rculist.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/stddef.h>
  31. #include <linux/string.h>
  32. #include <linux/udp.h>
  33. #include <linux/unaligned.h>
  34. #include <linux/workqueue.h>
  35. #include <net/arp.h>
  36. #include <net/genetlink.h>
  37. #include <net/netlink.h>
  38. #include <uapi/linux/batman_adv.h>
  39. #include "bridge_loop_avoidance.h"
  40. #include "hard-interface.h"
  41. #include "hash.h"
  42. #include "log.h"
  43. #include "netlink.h"
  44. #include "originator.h"
  45. #include "send.h"
  46. #include "translation-table.h"
  47. #include "tvlv.h"
  48. enum batadv_bootpop {
  49. BATADV_BOOTREPLY = 2,
  50. };
  51. enum batadv_boothtype {
  52. BATADV_HTYPE_ETHERNET = 1,
  53. };
  54. enum batadv_dhcpoptioncode {
  55. BATADV_DHCP_OPT_PAD = 0,
  56. BATADV_DHCP_OPT_MSG_TYPE = 53,
  57. BATADV_DHCP_OPT_END = 255,
  58. };
  59. enum batadv_dhcptype {
  60. BATADV_DHCPACK = 5,
  61. };
  62. /* { 99, 130, 83, 99 } */
  63. #define BATADV_DHCP_MAGIC 1669485411
  64. struct batadv_dhcp_packet {
  65. __u8 op;
  66. __u8 htype;
  67. __u8 hlen;
  68. __u8 hops;
  69. __be32 xid;
  70. __be16 secs;
  71. __be16 flags;
  72. __be32 ciaddr;
  73. __be32 yiaddr;
  74. __be32 siaddr;
  75. __be32 giaddr;
  76. __u8 chaddr[16];
  77. __u8 sname[64];
  78. __u8 file[128];
  79. __be32 magic;
  80. /* __u8 options[]; */
  81. };
  82. #define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
  83. #define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
  84. static void batadv_dat_purge(struct work_struct *work);
  85. /**
  86. * batadv_dat_start_timer() - initialise the DAT periodic worker
  87. * @bat_priv: the bat priv with all the mesh interface information
  88. */
  89. static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
  90. {
  91. queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
  92. msecs_to_jiffies(10000));
  93. }
  94. /**
  95. * batadv_dat_entry_release() - release dat_entry from lists and queue for free
  96. * after rcu grace period
  97. * @ref: kref pointer of the dat_entry
  98. */
  99. static void batadv_dat_entry_release(struct kref *ref)
  100. {
  101. struct batadv_dat_entry *dat_entry;
  102. dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
  103. kfree_rcu(dat_entry, rcu);
  104. }
  105. /**
  106. * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
  107. * release it
  108. * @dat_entry: dat_entry to be free'd
  109. */
  110. static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
  111. {
  112. if (!dat_entry)
  113. return;
  114. kref_put(&dat_entry->refcount, batadv_dat_entry_release);
  115. }
  116. /**
  117. * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
  118. * @dat_entry: the entry to check
  119. *
  120. * Return: true if the entry has to be purged now, false otherwise.
  121. */
  122. static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
  123. {
  124. return batadv_has_timed_out(dat_entry->last_update,
  125. BATADV_DAT_ENTRY_TIMEOUT);
  126. }
  127. /**
  128. * __batadv_dat_purge() - delete entries from the DAT local storage
  129. * @bat_priv: the bat priv with all the mesh interface information
  130. * @to_purge: function in charge to decide whether an entry has to be purged or
  131. * not. This function takes the dat_entry as argument and has to
  132. * returns a boolean value: true is the entry has to be deleted,
  133. * false otherwise
  134. *
  135. * Loops over each entry in the DAT local storage and deletes it if and only if
  136. * the to_purge function passed as argument returns true.
  137. */
  138. static void __batadv_dat_purge(struct batadv_priv *bat_priv,
  139. bool (*to_purge)(struct batadv_dat_entry *))
  140. {
  141. spinlock_t *list_lock; /* protects write access to the hash lists */
  142. struct batadv_dat_entry *dat_entry;
  143. struct hlist_node *node_tmp;
  144. struct hlist_head *head;
  145. u32 i;
  146. if (!bat_priv->dat.hash)
  147. return;
  148. for (i = 0; i < bat_priv->dat.hash->size; i++) {
  149. head = &bat_priv->dat.hash->table[i];
  150. list_lock = &bat_priv->dat.hash->list_locks[i];
  151. spin_lock_bh(list_lock);
  152. hlist_for_each_entry_safe(dat_entry, node_tmp, head,
  153. hash_entry) {
  154. /* if a helper function has been passed as parameter,
  155. * ask it if the entry has to be purged or not
  156. */
  157. if (to_purge && !to_purge(dat_entry))
  158. continue;
  159. hlist_del_rcu(&dat_entry->hash_entry);
  160. batadv_dat_entry_put(dat_entry);
  161. }
  162. spin_unlock_bh(list_lock);
  163. }
  164. }
  165. /**
  166. * batadv_dat_purge() - periodic task that deletes old entries from the local
  167. * DAT hash table
  168. * @work: kernel work struct
  169. */
  170. static void batadv_dat_purge(struct work_struct *work)
  171. {
  172. struct delayed_work *delayed_work;
  173. struct batadv_priv_dat *priv_dat;
  174. struct batadv_priv *bat_priv;
  175. delayed_work = to_delayed_work(work);
  176. priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
  177. bat_priv = container_of(priv_dat, struct batadv_priv, dat);
  178. __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
  179. batadv_dat_start_timer(bat_priv);
  180. }
  181. /**
  182. * batadv_compare_dat() - comparing function used in the local DAT hash table
  183. * @node: node in the local table
  184. * @data2: second object to compare the node to
  185. *
  186. * Return: true if the two entries are the same, false otherwise.
  187. */
  188. static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
  189. {
  190. const void *data1 = container_of(node, struct batadv_dat_entry,
  191. hash_entry);
  192. return memcmp(data1, data2, sizeof(__be32)) == 0;
  193. }
  194. /**
  195. * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
  196. * @skb: ARP packet
  197. * @hdr_size: size of the possible header before the ARP packet
  198. *
  199. * Return: the value of the hw_src field in the ARP packet.
  200. */
  201. static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
  202. {
  203. u8 *addr;
  204. addr = (u8 *)(skb->data + hdr_size);
  205. addr += ETH_HLEN + sizeof(struct arphdr);
  206. return addr;
  207. }
  208. /**
  209. * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
  210. * @skb: ARP packet
  211. * @hdr_size: size of the possible header before the ARP packet
  212. *
  213. * Return: the value of the ip_src field in the ARP packet.
  214. */
  215. static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
  216. {
  217. return *(__force __be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
  218. }
  219. /**
  220. * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
  221. * @skb: ARP packet
  222. * @hdr_size: size of the possible header before the ARP packet
  223. *
  224. * Return: the value of the hw_dst field in the ARP packet.
  225. */
  226. static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
  227. {
  228. return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
  229. }
  230. /**
  231. * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
  232. * @skb: ARP packet
  233. * @hdr_size: size of the possible header before the ARP packet
  234. *
  235. * Return: the value of the ip_dst field in the ARP packet.
  236. */
  237. static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
  238. {
  239. u8 *dst = batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4;
  240. return *(__force __be32 *)dst;
  241. }
  242. /**
  243. * batadv_hash_dat() - compute the hash value for an IP address
  244. * @data: data to hash
  245. * @size: size of the hash table
  246. *
  247. * Return: the selected index in the hash table for the given data.
  248. */
  249. static u32 batadv_hash_dat(const void *data, u32 size)
  250. {
  251. u32 hash = 0;
  252. const struct batadv_dat_entry *dat = data;
  253. const unsigned char *key;
  254. __be16 vid;
  255. u32 i;
  256. key = (__force const unsigned char *)&dat->ip;
  257. for (i = 0; i < sizeof(dat->ip); i++) {
  258. hash += key[i];
  259. hash += (hash << 10);
  260. hash ^= (hash >> 6);
  261. }
  262. vid = htons(dat->vid);
  263. key = (__force const unsigned char *)&vid;
  264. for (i = 0; i < sizeof(dat->vid); i++) {
  265. hash += key[i];
  266. hash += (hash << 10);
  267. hash ^= (hash >> 6);
  268. }
  269. hash += (hash << 3);
  270. hash ^= (hash >> 11);
  271. hash += (hash << 15);
  272. return hash % size;
  273. }
  274. /**
  275. * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
  276. * table
  277. * @bat_priv: the bat priv with all the mesh interface information
  278. * @ip: search key
  279. * @vid: VLAN identifier
  280. *
  281. * Return: the dat_entry if found, NULL otherwise.
  282. */
  283. static struct batadv_dat_entry *
  284. batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
  285. unsigned short vid)
  286. {
  287. struct hlist_head *head;
  288. struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
  289. struct batadv_hashtable *hash = bat_priv->dat.hash;
  290. u32 index;
  291. if (!hash)
  292. return NULL;
  293. to_find.ip = ip;
  294. to_find.vid = vid;
  295. index = batadv_hash_dat(&to_find, hash->size);
  296. head = &hash->table[index];
  297. rcu_read_lock();
  298. hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
  299. if (dat_entry->ip != ip)
  300. continue;
  301. if (!kref_get_unless_zero(&dat_entry->refcount))
  302. continue;
  303. dat_entry_tmp = dat_entry;
  304. break;
  305. }
  306. rcu_read_unlock();
  307. return dat_entry_tmp;
  308. }
  309. /**
  310. * batadv_dat_entry_add() - add a new dat entry or update it if already exists
  311. * @bat_priv: the bat priv with all the mesh interface information
  312. * @ip: ipv4 to add/edit
  313. * @mac_addr: mac address to assign to the given ipv4
  314. * @vid: VLAN identifier
  315. */
  316. static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
  317. u8 *mac_addr, unsigned short vid)
  318. {
  319. struct batadv_dat_entry *dat_entry;
  320. int hash_added;
  321. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
  322. /* if this entry is already known, just update it */
  323. if (dat_entry) {
  324. if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
  325. ether_addr_copy(dat_entry->mac_addr, mac_addr);
  326. dat_entry->last_update = jiffies;
  327. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  328. "Entry updated: %pI4 %pM (vid: %d)\n",
  329. &dat_entry->ip, dat_entry->mac_addr,
  330. batadv_print_vid(vid));
  331. goto out;
  332. }
  333. dat_entry = kmalloc_obj(*dat_entry, GFP_ATOMIC);
  334. if (!dat_entry)
  335. goto out;
  336. dat_entry->ip = ip;
  337. dat_entry->vid = vid;
  338. ether_addr_copy(dat_entry->mac_addr, mac_addr);
  339. dat_entry->last_update = jiffies;
  340. kref_init(&dat_entry->refcount);
  341. kref_get(&dat_entry->refcount);
  342. hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
  343. batadv_hash_dat, dat_entry,
  344. &dat_entry->hash_entry);
  345. if (unlikely(hash_added != 0)) {
  346. /* remove the reference for the hash */
  347. batadv_dat_entry_put(dat_entry);
  348. goto out;
  349. }
  350. batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
  351. &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
  352. out:
  353. batadv_dat_entry_put(dat_entry);
  354. }
  355. #ifdef CONFIG_BATMAN_ADV_DEBUG
  356. /**
  357. * batadv_dbg_arp() - print a debug message containing all the ARP packet
  358. * details
  359. * @bat_priv: the bat priv with all the mesh interface information
  360. * @skb: ARP packet
  361. * @hdr_size: size of the possible header before the ARP packet
  362. * @msg: message to print together with the debugging information
  363. */
  364. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  365. int hdr_size, char *msg)
  366. {
  367. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  368. struct batadv_bcast_packet *bcast_pkt;
  369. u8 *orig_addr;
  370. __be32 ip_src, ip_dst;
  371. if (msg)
  372. batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
  373. ip_src = batadv_arp_ip_src(skb, hdr_size);
  374. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  375. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  376. "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
  377. batadv_arp_hw_src(skb, hdr_size), &ip_src,
  378. batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
  379. if (hdr_size < sizeof(struct batadv_unicast_packet))
  380. return;
  381. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  382. switch (unicast_4addr_packet->u.packet_type) {
  383. case BATADV_UNICAST:
  384. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  385. "* encapsulated within a UNICAST packet\n");
  386. break;
  387. case BATADV_UNICAST_4ADDR:
  388. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  389. "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
  390. unicast_4addr_packet->src);
  391. switch (unicast_4addr_packet->subtype) {
  392. case BATADV_P_DAT_DHT_PUT:
  393. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
  394. break;
  395. case BATADV_P_DAT_DHT_GET:
  396. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
  397. break;
  398. case BATADV_P_DAT_CACHE_REPLY:
  399. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  400. "* type: DAT_CACHE_REPLY\n");
  401. break;
  402. case BATADV_P_DATA:
  403. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
  404. break;
  405. default:
  406. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
  407. unicast_4addr_packet->u.packet_type);
  408. }
  409. break;
  410. case BATADV_BCAST:
  411. bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
  412. orig_addr = bcast_pkt->orig;
  413. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  414. "* encapsulated within a BCAST packet (src: %pM)\n",
  415. orig_addr);
  416. break;
  417. default:
  418. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  419. "* encapsulated within an unknown packet type (0x%x)\n",
  420. unicast_4addr_packet->u.packet_type);
  421. }
  422. }
  423. #else
  424. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  425. int hdr_size, char *msg)
  426. {
  427. }
  428. #endif /* CONFIG_BATMAN_ADV_DEBUG */
  429. /**
  430. * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
  431. * @res: the array with the already selected candidates
  432. * @select: number of already selected candidates
  433. * @tmp_max: address of the currently evaluated node
  434. * @max: current round max address
  435. * @last_max: address of the last selected candidate
  436. * @candidate: orig_node under evaluation
  437. * @max_orig_node: last selected candidate
  438. *
  439. * Return: true if the node has been elected as next candidate or false
  440. * otherwise.
  441. */
  442. static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
  443. int select, batadv_dat_addr_t tmp_max,
  444. batadv_dat_addr_t max,
  445. batadv_dat_addr_t last_max,
  446. struct batadv_orig_node *candidate,
  447. struct batadv_orig_node *max_orig_node)
  448. {
  449. bool ret = false;
  450. int j;
  451. /* check if orig node candidate is running DAT */
  452. if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
  453. goto out;
  454. /* Check if this node has already been selected... */
  455. for (j = 0; j < select; j++)
  456. if (res[j].orig_node == candidate)
  457. break;
  458. /* ..and possibly skip it */
  459. if (j < select)
  460. goto out;
  461. /* sanity check: has it already been selected? This should not happen */
  462. if (tmp_max > last_max)
  463. goto out;
  464. /* check if during this iteration an originator with a closer dht
  465. * address has already been found
  466. */
  467. if (tmp_max < max)
  468. goto out;
  469. /* this is an hash collision with the temporary selected node. Choose
  470. * the one with the lowest address
  471. */
  472. if (tmp_max == max && max_orig_node &&
  473. batadv_compare_eth(candidate->orig, max_orig_node->orig))
  474. goto out;
  475. ret = true;
  476. out:
  477. return ret;
  478. }
  479. /**
  480. * batadv_choose_next_candidate() - select the next DHT candidate
  481. * @bat_priv: the bat priv with all the mesh interface information
  482. * @cands: candidates array
  483. * @select: number of candidates already present in the array
  484. * @ip_key: key to look up in the DHT
  485. * @last_max: pointer where the address of the selected candidate will be saved
  486. */
  487. static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
  488. struct batadv_dat_candidate *cands,
  489. int select, batadv_dat_addr_t ip_key,
  490. batadv_dat_addr_t *last_max)
  491. {
  492. batadv_dat_addr_t max = 0;
  493. batadv_dat_addr_t tmp_max = 0;
  494. struct batadv_orig_node *orig_node, *max_orig_node = NULL;
  495. struct batadv_hashtable *hash = bat_priv->orig_hash;
  496. struct hlist_head *head;
  497. int i;
  498. /* if no node is eligible as candidate, leave the candidate type as
  499. * NOT_FOUND
  500. */
  501. cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
  502. /* iterate over the originator list and find the node with the closest
  503. * dat_address which has not been selected yet
  504. */
  505. for (i = 0; i < hash->size; i++) {
  506. head = &hash->table[i];
  507. rcu_read_lock();
  508. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  509. /* the dht space is a ring using unsigned addresses */
  510. tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
  511. ip_key;
  512. if (!batadv_is_orig_node_eligible(cands, select,
  513. tmp_max, max,
  514. *last_max, orig_node,
  515. max_orig_node))
  516. continue;
  517. if (!kref_get_unless_zero(&orig_node->refcount))
  518. continue;
  519. max = tmp_max;
  520. batadv_orig_node_put(max_orig_node);
  521. max_orig_node = orig_node;
  522. }
  523. rcu_read_unlock();
  524. }
  525. if (max_orig_node) {
  526. cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
  527. cands[select].orig_node = max_orig_node;
  528. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  529. "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
  530. select, max_orig_node->orig, max_orig_node->dat_addr,
  531. max);
  532. }
  533. *last_max = max;
  534. }
  535. /**
  536. * batadv_dat_select_candidates() - select the nodes which the DHT message has
  537. * to be sent to
  538. * @bat_priv: the bat priv with all the mesh interface information
  539. * @ip_dst: ipv4 to look up in the DHT
  540. * @vid: VLAN identifier
  541. *
  542. * An originator O is selected if and only if its DHT_ID value is one of three
  543. * closest values (from the LEFT, with wrap around if needed) then the hash
  544. * value of the key. ip_dst is the key.
  545. *
  546. * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
  547. */
  548. static struct batadv_dat_candidate *
  549. batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
  550. unsigned short vid)
  551. {
  552. int select;
  553. batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
  554. struct batadv_dat_candidate *res;
  555. struct batadv_dat_entry dat;
  556. if (!bat_priv->orig_hash)
  557. return NULL;
  558. res = kmalloc_objs(*res, BATADV_DAT_CANDIDATES_NUM, GFP_ATOMIC);
  559. if (!res)
  560. return NULL;
  561. dat.ip = ip_dst;
  562. dat.vid = vid;
  563. ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
  564. BATADV_DAT_ADDR_MAX);
  565. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  566. "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
  567. ip_key);
  568. for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
  569. batadv_choose_next_candidate(bat_priv, res, select, ip_key,
  570. &last_max);
  571. return res;
  572. }
  573. /**
  574. * batadv_dat_forward_data() - copy and send payload to the selected candidates
  575. * @bat_priv: the bat priv with all the mesh interface information
  576. * @skb: payload to send
  577. * @ip: the DHT key
  578. * @vid: VLAN identifier
  579. * @packet_subtype: unicast4addr packet subtype to use
  580. *
  581. * This function copies the skb with pskb_copy() and is sent as a unicast packet
  582. * to each of the selected candidates.
  583. *
  584. * Return: true if the packet is sent to at least one candidate, false
  585. * otherwise.
  586. */
  587. static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
  588. struct sk_buff *skb, __be32 ip,
  589. unsigned short vid, int packet_subtype)
  590. {
  591. int i;
  592. bool ret = false;
  593. int send_status;
  594. struct batadv_neigh_node *neigh_node = NULL;
  595. struct sk_buff *tmp_skb;
  596. struct batadv_dat_candidate *cand;
  597. cand = batadv_dat_select_candidates(bat_priv, ip, vid);
  598. if (!cand)
  599. return ret;
  600. batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
  601. for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
  602. if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
  603. continue;
  604. neigh_node = batadv_orig_router_get(cand[i].orig_node,
  605. BATADV_IF_DEFAULT);
  606. if (!neigh_node)
  607. goto free_orig;
  608. tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
  609. if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
  610. cand[i].orig_node,
  611. packet_subtype)) {
  612. kfree_skb(tmp_skb);
  613. goto free_neigh;
  614. }
  615. send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
  616. if (send_status == NET_XMIT_SUCCESS) {
  617. /* count the sent packet */
  618. switch (packet_subtype) {
  619. case BATADV_P_DAT_DHT_GET:
  620. batadv_inc_counter(bat_priv,
  621. BATADV_CNT_DAT_GET_TX);
  622. break;
  623. case BATADV_P_DAT_DHT_PUT:
  624. batadv_inc_counter(bat_priv,
  625. BATADV_CNT_DAT_PUT_TX);
  626. break;
  627. }
  628. /* packet sent to a candidate: return true */
  629. ret = true;
  630. }
  631. free_neigh:
  632. batadv_neigh_node_put(neigh_node);
  633. free_orig:
  634. batadv_orig_node_put(cand[i].orig_node);
  635. }
  636. kfree(cand);
  637. return ret;
  638. }
  639. /**
  640. * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
  641. * setting change
  642. * @bat_priv: the bat priv with all the mesh interface information
  643. */
  644. static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
  645. {
  646. char dat_mode;
  647. dat_mode = atomic_read(&bat_priv->distributed_arp_table);
  648. switch (dat_mode) {
  649. case 0:
  650. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  651. break;
  652. case 1:
  653. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
  654. NULL, 0);
  655. break;
  656. }
  657. }
  658. /**
  659. * batadv_dat_status_update() - update the dat tvlv container after dat
  660. * setting change
  661. * @net_dev: the mesh interface net device
  662. */
  663. void batadv_dat_status_update(struct net_device *net_dev)
  664. {
  665. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  666. batadv_dat_tvlv_container_update(bat_priv);
  667. }
  668. /**
  669. * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
  670. * @bat_priv: the bat priv with all the mesh interface information
  671. * @orig: the orig_node of the ogm
  672. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  673. * @tvlv_value: tvlv buffer containing the gateway data
  674. * @tvlv_value_len: tvlv buffer length
  675. */
  676. static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  677. struct batadv_orig_node *orig,
  678. u8 flags,
  679. void *tvlv_value, u16 tvlv_value_len)
  680. {
  681. if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
  682. clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
  683. else
  684. set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
  685. }
  686. /**
  687. * batadv_dat_hash_free() - free the local DAT hash table
  688. * @bat_priv: the bat priv with all the mesh interface information
  689. */
  690. static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
  691. {
  692. if (!bat_priv->dat.hash)
  693. return;
  694. __batadv_dat_purge(bat_priv, NULL);
  695. batadv_hash_destroy(bat_priv->dat.hash);
  696. bat_priv->dat.hash = NULL;
  697. }
  698. /**
  699. * batadv_dat_init() - initialise the DAT internals
  700. * @bat_priv: the bat priv with all the mesh interface information
  701. *
  702. * Return: 0 in case of success, a negative error code otherwise
  703. */
  704. int batadv_dat_init(struct batadv_priv *bat_priv)
  705. {
  706. if (bat_priv->dat.hash)
  707. return 0;
  708. bat_priv->dat.hash = batadv_hash_new(1024);
  709. if (!bat_priv->dat.hash)
  710. return -ENOMEM;
  711. INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
  712. batadv_dat_start_timer(bat_priv);
  713. batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
  714. NULL, NULL, BATADV_TVLV_DAT, 1,
  715. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  716. batadv_dat_tvlv_container_update(bat_priv);
  717. return 0;
  718. }
  719. /**
  720. * batadv_dat_free() - free the DAT internals
  721. * @bat_priv: the bat priv with all the mesh interface information
  722. */
  723. void batadv_dat_free(struct batadv_priv *bat_priv)
  724. {
  725. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  726. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  727. cancel_delayed_work_sync(&bat_priv->dat.work);
  728. batadv_dat_hash_free(bat_priv);
  729. }
  730. /**
  731. * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
  732. * netlink socket
  733. * @msg: buffer for the message
  734. * @portid: netlink port
  735. * @cb: Control block containing additional options
  736. * @dat_entry: entry to dump
  737. *
  738. * Return: 0 or error code.
  739. */
  740. static int
  741. batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
  742. struct netlink_callback *cb,
  743. struct batadv_dat_entry *dat_entry)
  744. {
  745. int msecs;
  746. void *hdr;
  747. hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
  748. &batadv_netlink_family, NLM_F_MULTI,
  749. BATADV_CMD_GET_DAT_CACHE);
  750. if (!hdr)
  751. return -ENOBUFS;
  752. genl_dump_check_consistent(cb, hdr);
  753. msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
  754. if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
  755. dat_entry->ip) ||
  756. nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
  757. dat_entry->mac_addr) ||
  758. nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
  759. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
  760. genlmsg_cancel(msg, hdr);
  761. return -EMSGSIZE;
  762. }
  763. genlmsg_end(msg, hdr);
  764. return 0;
  765. }
  766. /**
  767. * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
  768. * a netlink socket
  769. * @msg: buffer for the message
  770. * @portid: netlink port
  771. * @cb: Control block containing additional options
  772. * @hash: hash to dump
  773. * @bucket: bucket index to dump
  774. * @idx_skip: How many entries to skip
  775. *
  776. * Return: 0 or error code.
  777. */
  778. static int
  779. batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
  780. struct netlink_callback *cb,
  781. struct batadv_hashtable *hash, unsigned int bucket,
  782. int *idx_skip)
  783. {
  784. struct batadv_dat_entry *dat_entry;
  785. int idx = 0;
  786. spin_lock_bh(&hash->list_locks[bucket]);
  787. cb->seq = atomic_read(&hash->generation) << 1 | 1;
  788. hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
  789. if (idx < *idx_skip)
  790. goto skip;
  791. if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
  792. spin_unlock_bh(&hash->list_locks[bucket]);
  793. *idx_skip = idx;
  794. return -EMSGSIZE;
  795. }
  796. skip:
  797. idx++;
  798. }
  799. spin_unlock_bh(&hash->list_locks[bucket]);
  800. return 0;
  801. }
  802. /**
  803. * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
  804. * @msg: buffer for the message
  805. * @cb: callback structure containing arguments
  806. *
  807. * Return: message length.
  808. */
  809. int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
  810. {
  811. struct batadv_hard_iface *primary_if = NULL;
  812. int portid = NETLINK_CB(cb->skb).portid;
  813. struct net_device *mesh_iface;
  814. struct batadv_hashtable *hash;
  815. struct batadv_priv *bat_priv;
  816. int bucket = cb->args[0];
  817. int idx = cb->args[1];
  818. int ret = 0;
  819. mesh_iface = batadv_netlink_get_meshif(cb);
  820. if (IS_ERR(mesh_iface))
  821. return PTR_ERR(mesh_iface);
  822. bat_priv = netdev_priv(mesh_iface);
  823. hash = bat_priv->dat.hash;
  824. primary_if = batadv_primary_if_get_selected(bat_priv);
  825. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  826. ret = -ENOENT;
  827. goto out;
  828. }
  829. while (bucket < hash->size) {
  830. if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
  831. &idx))
  832. break;
  833. bucket++;
  834. idx = 0;
  835. }
  836. cb->args[0] = bucket;
  837. cb->args[1] = idx;
  838. ret = msg->len;
  839. out:
  840. batadv_hardif_put(primary_if);
  841. dev_put(mesh_iface);
  842. return ret;
  843. }
  844. /**
  845. * batadv_arp_get_type() - parse an ARP packet and gets the type
  846. * @bat_priv: the bat priv with all the mesh interface information
  847. * @skb: packet to analyse
  848. * @hdr_size: size of the possible header before the ARP packet in the skb
  849. *
  850. * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
  851. */
  852. static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
  853. struct sk_buff *skb, int hdr_size)
  854. {
  855. struct arphdr *arphdr;
  856. struct ethhdr *ethhdr;
  857. __be32 ip_src, ip_dst;
  858. u8 *hw_src, *hw_dst;
  859. u16 type = 0;
  860. /* pull the ethernet header */
  861. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
  862. goto out;
  863. ethhdr = (struct ethhdr *)(skb->data + hdr_size);
  864. if (ethhdr->h_proto != htons(ETH_P_ARP))
  865. goto out;
  866. /* pull the ARP payload */
  867. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
  868. arp_hdr_len(skb->dev))))
  869. goto out;
  870. arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
  871. /* check whether the ARP packet carries a valid IP information */
  872. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  873. goto out;
  874. if (arphdr->ar_pro != htons(ETH_P_IP))
  875. goto out;
  876. if (arphdr->ar_hln != ETH_ALEN)
  877. goto out;
  878. if (arphdr->ar_pln != 4)
  879. goto out;
  880. /* Check for bad reply/request. If the ARP message is not sane, DAT
  881. * will simply ignore it
  882. */
  883. ip_src = batadv_arp_ip_src(skb, hdr_size);
  884. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  885. if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
  886. ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
  887. ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
  888. ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
  889. goto out;
  890. hw_src = batadv_arp_hw_src(skb, hdr_size);
  891. if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
  892. goto out;
  893. /* don't care about the destination MAC address in ARP requests */
  894. if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
  895. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  896. if (is_zero_ether_addr(hw_dst) ||
  897. is_multicast_ether_addr(hw_dst))
  898. goto out;
  899. }
  900. type = ntohs(arphdr->ar_op);
  901. out:
  902. return type;
  903. }
  904. /**
  905. * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
  906. * @skb: the buffer containing the packet to extract the VID from
  907. * @hdr_size: the size of the batman-adv header encapsulating the packet
  908. *
  909. * Return: If the packet embedded in the skb is vlan tagged this function
  910. * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
  911. * is returned.
  912. */
  913. static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
  914. {
  915. unsigned short vid;
  916. vid = batadv_get_vid(skb, *hdr_size);
  917. /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
  918. * If the header contained in the packet is a VLAN one (which is longer)
  919. * hdr_size is updated so that the functions will still skip the
  920. * correct amount of bytes.
  921. */
  922. if (vid & BATADV_VLAN_HAS_TAG)
  923. *hdr_size += VLAN_HLEN;
  924. return vid;
  925. }
  926. /**
  927. * batadv_dat_arp_create_reply() - create an ARP Reply
  928. * @bat_priv: the bat priv with all the mesh interface information
  929. * @ip_src: ARP sender IP
  930. * @ip_dst: ARP target IP
  931. * @hw_src: Ethernet source and ARP sender MAC
  932. * @hw_dst: Ethernet destination and ARP target MAC
  933. * @vid: VLAN identifier (optional, set to zero otherwise)
  934. *
  935. * Creates an ARP Reply from the given values, optionally encapsulated in a
  936. * VLAN header.
  937. *
  938. * Return: An skb containing an ARP Reply.
  939. */
  940. static struct sk_buff *
  941. batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
  942. __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
  943. unsigned short vid)
  944. {
  945. struct sk_buff *skb;
  946. skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->mesh_iface,
  947. ip_src, hw_dst, hw_src, hw_dst);
  948. if (!skb)
  949. return NULL;
  950. skb_reset_mac_header(skb);
  951. if (vid & BATADV_VLAN_HAS_TAG)
  952. skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
  953. vid & VLAN_VID_MASK);
  954. return skb;
  955. }
  956. /**
  957. * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
  958. * answer using DAT
  959. * @bat_priv: the bat priv with all the mesh interface information
  960. * @skb: packet to check
  961. *
  962. * Return: true if the message has been sent to the dht candidates, false
  963. * otherwise. In case of a positive return value the message has to be enqueued
  964. * to permit the fallback.
  965. */
  966. bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
  967. struct sk_buff *skb)
  968. {
  969. u16 type = 0;
  970. __be32 ip_dst, ip_src;
  971. u8 *hw_src;
  972. bool ret = false;
  973. struct batadv_dat_entry *dat_entry = NULL;
  974. struct sk_buff *skb_new;
  975. struct net_device *mesh_iface = bat_priv->mesh_iface;
  976. int hdr_size = 0;
  977. unsigned short vid;
  978. if (!atomic_read(&bat_priv->distributed_arp_table))
  979. goto out;
  980. vid = batadv_dat_get_vid(skb, &hdr_size);
  981. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  982. /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
  983. * message to the selected DHT candidates
  984. */
  985. if (type != ARPOP_REQUEST)
  986. goto out;
  987. batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
  988. ip_src = batadv_arp_ip_src(skb, hdr_size);
  989. hw_src = batadv_arp_hw_src(skb, hdr_size);
  990. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  991. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  992. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  993. if (dat_entry) {
  994. /* If the ARP request is destined for a local client the local
  995. * client will answer itself. DAT would only generate a
  996. * duplicate packet.
  997. *
  998. * Moreover, if the mesh-interface is enslaved into a bridge, an
  999. * additional DAT answer may trigger kernel warnings about
  1000. * a packet coming from the wrong port.
  1001. */
  1002. if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
  1003. ret = true;
  1004. goto out;
  1005. }
  1006. /* If BLA is enabled, only send ARP replies if we have claimed
  1007. * the destination for the ARP request or if no one else of
  1008. * the backbone gws belonging to our backbone has claimed the
  1009. * destination.
  1010. */
  1011. if (!batadv_bla_check_claim(bat_priv,
  1012. dat_entry->mac_addr, vid)) {
  1013. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1014. "Device %pM claimed by another backbone gw. Don't send ARP reply!",
  1015. dat_entry->mac_addr);
  1016. ret = true;
  1017. goto out;
  1018. }
  1019. skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
  1020. dat_entry->mac_addr,
  1021. hw_src, vid);
  1022. if (!skb_new)
  1023. goto out;
  1024. skb_new->protocol = eth_type_trans(skb_new, mesh_iface);
  1025. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  1026. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  1027. skb->len + ETH_HLEN + hdr_size);
  1028. netif_rx(skb_new);
  1029. batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
  1030. ret = true;
  1031. } else {
  1032. /* Send the request to the DHT */
  1033. ret = batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
  1034. BATADV_P_DAT_DHT_GET);
  1035. }
  1036. out:
  1037. batadv_dat_entry_put(dat_entry);
  1038. return ret;
  1039. }
  1040. /**
  1041. * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
  1042. * answer using the local DAT storage
  1043. * @bat_priv: the bat priv with all the mesh interface information
  1044. * @skb: packet to check
  1045. * @hdr_size: size of the encapsulation header
  1046. *
  1047. * Return: true if the request has been answered, false otherwise.
  1048. */
  1049. bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
  1050. struct sk_buff *skb, int hdr_size)
  1051. {
  1052. u16 type;
  1053. __be32 ip_src, ip_dst;
  1054. u8 *hw_src;
  1055. struct sk_buff *skb_new;
  1056. struct batadv_dat_entry *dat_entry = NULL;
  1057. bool ret = false;
  1058. unsigned short vid;
  1059. int err;
  1060. if (!atomic_read(&bat_priv->distributed_arp_table))
  1061. goto out;
  1062. vid = batadv_dat_get_vid(skb, &hdr_size);
  1063. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  1064. if (type != ARPOP_REQUEST)
  1065. goto out;
  1066. hw_src = batadv_arp_hw_src(skb, hdr_size);
  1067. ip_src = batadv_arp_ip_src(skb, hdr_size);
  1068. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1069. batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
  1070. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1071. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  1072. if (!dat_entry)
  1073. goto out;
  1074. skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
  1075. dat_entry->mac_addr, hw_src, vid);
  1076. if (!skb_new)
  1077. goto out;
  1078. /* To preserve backwards compatibility, the node has choose the outgoing
  1079. * format based on the incoming request packet type. The assumption is
  1080. * that a node not using the 4addr packet format doesn't support it.
  1081. */
  1082. if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
  1083. err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
  1084. BATADV_P_DAT_CACHE_REPLY,
  1085. NULL, vid);
  1086. else
  1087. err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
  1088. if (err != NET_XMIT_DROP) {
  1089. batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
  1090. ret = true;
  1091. }
  1092. out:
  1093. batadv_dat_entry_put(dat_entry);
  1094. if (ret)
  1095. kfree_skb(skb);
  1096. return ret;
  1097. }
  1098. /**
  1099. * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
  1100. * @bat_priv: the bat priv with all the mesh interface information
  1101. * @skb: packet to check
  1102. */
  1103. void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
  1104. struct sk_buff *skb)
  1105. {
  1106. u16 type;
  1107. __be32 ip_src, ip_dst;
  1108. u8 *hw_src, *hw_dst;
  1109. int hdr_size = 0;
  1110. unsigned short vid;
  1111. if (!atomic_read(&bat_priv->distributed_arp_table))
  1112. return;
  1113. vid = batadv_dat_get_vid(skb, &hdr_size);
  1114. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  1115. if (type != ARPOP_REPLY)
  1116. return;
  1117. batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
  1118. hw_src = batadv_arp_hw_src(skb, hdr_size);
  1119. ip_src = batadv_arp_ip_src(skb, hdr_size);
  1120. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  1121. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1122. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1123. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  1124. /* Send the ARP reply to the candidates for both the IP addresses that
  1125. * the node obtained from the ARP reply
  1126. */
  1127. batadv_dat_forward_data(bat_priv, skb, ip_src, vid,
  1128. BATADV_P_DAT_DHT_PUT);
  1129. batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
  1130. BATADV_P_DAT_DHT_PUT);
  1131. }
  1132. /**
  1133. * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
  1134. * local DAT storage only
  1135. * @bat_priv: the bat priv with all the mesh interface information
  1136. * @skb: packet to check
  1137. * @hdr_size: size of the encapsulation header
  1138. *
  1139. * Return: true if the packet was snooped and consumed by DAT. False if the
  1140. * packet has to be delivered to the interface
  1141. */
  1142. bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
  1143. struct sk_buff *skb, int hdr_size)
  1144. {
  1145. struct batadv_dat_entry *dat_entry = NULL;
  1146. u16 type;
  1147. __be32 ip_src, ip_dst;
  1148. u8 *hw_src, *hw_dst;
  1149. bool dropped = false;
  1150. unsigned short vid;
  1151. if (!atomic_read(&bat_priv->distributed_arp_table))
  1152. goto out;
  1153. vid = batadv_dat_get_vid(skb, &hdr_size);
  1154. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  1155. if (type != ARPOP_REPLY)
  1156. goto out;
  1157. batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
  1158. hw_src = batadv_arp_hw_src(skb, hdr_size);
  1159. ip_src = batadv_arp_ip_src(skb, hdr_size);
  1160. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  1161. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1162. /* If ip_dst is already in cache and has the right mac address,
  1163. * drop this frame if this ARP reply is destined for us because it's
  1164. * most probably an ARP reply generated by another node of the DHT.
  1165. * We have most probably received already a reply earlier. Delivering
  1166. * this frame would lead to doubled receive of an ARP reply.
  1167. */
  1168. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
  1169. if (dat_entry && batadv_compare_eth(hw_src, dat_entry->mac_addr)) {
  1170. batadv_dbg(BATADV_DBG_DAT, bat_priv, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
  1171. hw_src, &ip_src, hw_dst, &ip_dst,
  1172. dat_entry->mac_addr, &dat_entry->ip);
  1173. dropped = true;
  1174. }
  1175. /* Update our internal cache with both the IP addresses the node got
  1176. * within the ARP reply
  1177. */
  1178. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1179. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  1180. if (dropped)
  1181. goto out;
  1182. /* If BLA is enabled, only forward ARP replies if we have claimed the
  1183. * source of the ARP reply or if no one else of the same backbone has
  1184. * already claimed that client. This prevents that different gateways
  1185. * to the same backbone all forward the ARP reply leading to multiple
  1186. * replies in the backbone.
  1187. */
  1188. if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
  1189. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1190. "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
  1191. hw_src);
  1192. dropped = true;
  1193. goto out;
  1194. }
  1195. /* if this REPLY is directed to a client of mine, let's deliver the
  1196. * packet to the interface
  1197. */
  1198. dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
  1199. /* if this REPLY is sent on behalf of a client of mine, let's drop the
  1200. * packet because the client will reply by itself
  1201. */
  1202. dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
  1203. out:
  1204. if (dropped)
  1205. kfree_skb(skb);
  1206. batadv_dat_entry_put(dat_entry);
  1207. /* if dropped == false -> deliver to the interface */
  1208. return dropped;
  1209. }
  1210. /**
  1211. * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
  1212. * @skb: the packet to check
  1213. * @ip_src: a buffer to store the IPv4 source address in
  1214. *
  1215. * Checks whether the given skb has an IP and UDP header valid for a DHCP
  1216. * message from a DHCP server. And if so, stores the IPv4 source address in
  1217. * the provided buffer.
  1218. *
  1219. * Return: True if valid, false otherwise.
  1220. */
  1221. static bool
  1222. batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
  1223. {
  1224. unsigned int offset = skb_network_offset(skb);
  1225. struct udphdr *udphdr, _udphdr;
  1226. struct iphdr *iphdr, _iphdr;
  1227. iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
  1228. if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
  1229. return false;
  1230. if (iphdr->protocol != IPPROTO_UDP)
  1231. return false;
  1232. offset += iphdr->ihl * 4;
  1233. skb_set_transport_header(skb, offset);
  1234. udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
  1235. if (!udphdr || udphdr->source != htons(67))
  1236. return false;
  1237. *ip_src = get_unaligned(&iphdr->saddr);
  1238. return true;
  1239. }
  1240. /**
  1241. * batadv_dat_check_dhcp() - examine packet for valid DHCP message
  1242. * @skb: the packet to check
  1243. * @proto: ethernet protocol hint (behind a potential vlan)
  1244. * @ip_src: a buffer to store the IPv4 source address in
  1245. *
  1246. * Checks whether the given skb is a valid DHCP packet. And if so, stores the
  1247. * IPv4 source address in the provided buffer.
  1248. *
  1249. * Caller needs to ensure that the skb network header is set correctly.
  1250. *
  1251. * Return: If skb is a valid DHCP packet, then returns its op code
  1252. * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
  1253. */
  1254. static int
  1255. batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
  1256. {
  1257. __be32 *magic, _magic;
  1258. unsigned int offset;
  1259. struct {
  1260. __u8 op;
  1261. __u8 htype;
  1262. __u8 hlen;
  1263. __u8 hops;
  1264. } *dhcp_h, _dhcp_h;
  1265. if (proto != htons(ETH_P_IP))
  1266. return -EINVAL;
  1267. if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
  1268. return -EINVAL;
  1269. offset = skb_transport_offset(skb) + sizeof(struct udphdr);
  1270. if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
  1271. return -EINVAL;
  1272. dhcp_h = skb_header_pointer(skb, offset, sizeof(_dhcp_h), &_dhcp_h);
  1273. if (!dhcp_h || dhcp_h->htype != BATADV_HTYPE_ETHERNET ||
  1274. dhcp_h->hlen != ETH_ALEN)
  1275. return -EINVAL;
  1276. offset += offsetof(struct batadv_dhcp_packet, magic);
  1277. magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
  1278. if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
  1279. return -EINVAL;
  1280. return dhcp_h->op;
  1281. }
  1282. /**
  1283. * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
  1284. * @skb: the DHCP packet to parse
  1285. *
  1286. * Iterates over the DHCP options of the given DHCP packet to find a
  1287. * DHCP Message Type option and parse it.
  1288. *
  1289. * Caller needs to ensure that the given skb is a valid DHCP packet and
  1290. * that the skb transport header is set correctly.
  1291. *
  1292. * Return: The found DHCP message type value, if found. -EINVAL otherwise.
  1293. */
  1294. static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
  1295. {
  1296. unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
  1297. u8 *type, _type;
  1298. struct {
  1299. u8 type;
  1300. u8 len;
  1301. } *tl, _tl;
  1302. offset += sizeof(struct batadv_dhcp_packet);
  1303. while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
  1304. if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
  1305. break;
  1306. if (tl->type == BATADV_DHCP_OPT_END)
  1307. break;
  1308. if (tl->type == BATADV_DHCP_OPT_PAD)
  1309. offset++;
  1310. else
  1311. offset += tl->len + sizeof(_tl);
  1312. }
  1313. /* Option Overload Code not supported */
  1314. if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
  1315. tl->len != sizeof(_type))
  1316. return -EINVAL;
  1317. offset += sizeof(_tl);
  1318. type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
  1319. if (!type)
  1320. return -EINVAL;
  1321. return *type;
  1322. }
  1323. /**
  1324. * batadv_dat_dhcp_get_yiaddr() - get yiaddr from a DHCP packet
  1325. * @skb: the DHCP packet to parse
  1326. * @buf: a buffer to store the yiaddr in
  1327. *
  1328. * Caller needs to ensure that the given skb is a valid DHCP packet and
  1329. * that the skb transport header is set correctly.
  1330. *
  1331. * Return: True on success, false otherwise.
  1332. */
  1333. static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
  1334. {
  1335. unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
  1336. __be32 *yiaddr;
  1337. offset += offsetof(struct batadv_dhcp_packet, yiaddr);
  1338. yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
  1339. if (!yiaddr)
  1340. return false;
  1341. if (yiaddr != buf)
  1342. *buf = get_unaligned(yiaddr);
  1343. return true;
  1344. }
  1345. /**
  1346. * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
  1347. * @skb: the DHCP packet to parse
  1348. * @buf: a buffer to store the chaddr in
  1349. *
  1350. * Caller needs to ensure that the given skb is a valid DHCP packet and
  1351. * that the skb transport header is set correctly.
  1352. *
  1353. * Return: True on success, false otherwise
  1354. */
  1355. static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
  1356. {
  1357. unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
  1358. u8 *chaddr;
  1359. offset += offsetof(struct batadv_dhcp_packet, chaddr);
  1360. chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
  1361. if (!chaddr)
  1362. return false;
  1363. if (chaddr != buf)
  1364. memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
  1365. return true;
  1366. }
  1367. /**
  1368. * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
  1369. * DAT cache
  1370. * @bat_priv: the bat priv with all the mesh interface information
  1371. * @chaddr: the DHCP client MAC address
  1372. * @yiaddr: the DHCP client IP address
  1373. * @hw_dst: the DHCP server MAC address
  1374. * @ip_dst: the DHCP server IP address
  1375. * @vid: VLAN identifier
  1376. *
  1377. * Adds given MAC/IP pairs to the local DAT cache and propagates them further
  1378. * into the DHT.
  1379. *
  1380. * For the DHT propagation, client MAC + IP will appear as the ARP Reply
  1381. * transmitter (and hw_dst/ip_dst as the target).
  1382. */
  1383. static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
  1384. __be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
  1385. unsigned short vid)
  1386. {
  1387. struct sk_buff *skb;
  1388. skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
  1389. hw_dst, vid);
  1390. if (!skb)
  1391. return;
  1392. skb_set_network_header(skb, ETH_HLEN);
  1393. batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
  1394. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  1395. batadv_dat_forward_data(bat_priv, skb, yiaddr, vid,
  1396. BATADV_P_DAT_DHT_PUT);
  1397. batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
  1398. BATADV_P_DAT_DHT_PUT);
  1399. consume_skb(skb);
  1400. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1401. "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
  1402. &ip_dst, hw_dst, batadv_print_vid(vid));
  1403. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1404. "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
  1405. &yiaddr, chaddr, batadv_print_vid(vid));
  1406. }
  1407. /**
  1408. * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
  1409. * @skb: the packet to check
  1410. * @proto: ethernet protocol hint (behind a potential vlan)
  1411. * @ip_src: a buffer to store the IPv4 source address in
  1412. * @chaddr: a buffer to store the DHCP Client Hardware Address in
  1413. * @yiaddr: a buffer to store the DHCP Your IP Address in
  1414. *
  1415. * Checks whether the given skb is a valid DHCPACK. And if so, stores the
  1416. * IPv4 server source address (ip_src), client MAC address (chaddr) and client
  1417. * IPv4 address (yiaddr) in the provided buffers.
  1418. *
  1419. * Caller needs to ensure that the skb network header is set correctly.
  1420. *
  1421. * Return: True if the skb is a valid DHCPACK. False otherwise.
  1422. */
  1423. static bool
  1424. batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
  1425. u8 *chaddr, __be32 *yiaddr)
  1426. {
  1427. int type;
  1428. type = batadv_dat_check_dhcp(skb, proto, ip_src);
  1429. if (type != BATADV_BOOTREPLY)
  1430. return false;
  1431. type = batadv_dat_get_dhcp_message_type(skb);
  1432. if (type != BATADV_DHCPACK)
  1433. return false;
  1434. if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
  1435. return false;
  1436. if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
  1437. return false;
  1438. return true;
  1439. }
  1440. /**
  1441. * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
  1442. * @bat_priv: the bat priv with all the mesh interface information
  1443. * @skb: the packet to snoop
  1444. * @proto: ethernet protocol hint (behind a potential vlan)
  1445. * @vid: VLAN identifier
  1446. *
  1447. * This function first checks whether the given skb is a valid DHCPACK. If
  1448. * so then its source MAC and IP as well as its DHCP Client Hardware Address
  1449. * field and DHCP Your IP Address field are added to the local DAT cache and
  1450. * propagated into the DHT.
  1451. *
  1452. * Caller needs to ensure that the skb mac and network headers are set
  1453. * correctly.
  1454. */
  1455. void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
  1456. struct sk_buff *skb,
  1457. __be16 proto,
  1458. unsigned short vid)
  1459. {
  1460. u8 chaddr[BATADV_DHCP_CHADDR_LEN];
  1461. __be32 ip_src, yiaddr;
  1462. if (!atomic_read(&bat_priv->distributed_arp_table))
  1463. return;
  1464. if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
  1465. return;
  1466. batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
  1467. ip_src, vid);
  1468. }
  1469. /**
  1470. * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
  1471. * @bat_priv: the bat priv with all the mesh interface information
  1472. * @skb: the packet to snoop
  1473. * @hdr_size: header size, up to the tail of the batman-adv header
  1474. *
  1475. * This function first checks whether the given skb is a valid DHCPACK. If
  1476. * so then its source MAC and IP as well as its DHCP Client Hardware Address
  1477. * field and DHCP Your IP Address field are added to the local DAT cache.
  1478. */
  1479. void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
  1480. struct sk_buff *skb, int hdr_size)
  1481. {
  1482. u8 chaddr[BATADV_DHCP_CHADDR_LEN];
  1483. struct ethhdr *ethhdr;
  1484. __be32 ip_src, yiaddr;
  1485. unsigned short vid;
  1486. __be16 proto;
  1487. u8 *hw_src;
  1488. if (!atomic_read(&bat_priv->distributed_arp_table))
  1489. return;
  1490. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
  1491. return;
  1492. ethhdr = (struct ethhdr *)(skb->data + hdr_size);
  1493. skb_set_network_header(skb, hdr_size + ETH_HLEN);
  1494. proto = ethhdr->h_proto;
  1495. if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
  1496. return;
  1497. hw_src = ethhdr->h_source;
  1498. vid = batadv_dat_get_vid(skb, &hdr_size);
  1499. batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
  1500. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1501. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1502. "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
  1503. &ip_src, hw_src, batadv_print_vid(vid));
  1504. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1505. "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
  1506. &yiaddr, chaddr, batadv_print_vid(vid));
  1507. }
  1508. /**
  1509. * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
  1510. * dropped (because the node has already obtained the reply via DAT) or not
  1511. * @bat_priv: the bat priv with all the mesh interface information
  1512. * @forw_packet: the broadcast packet
  1513. *
  1514. * Return: true if the node can drop the packet, false otherwise.
  1515. */
  1516. bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
  1517. struct batadv_forw_packet *forw_packet)
  1518. {
  1519. u16 type;
  1520. __be32 ip_dst;
  1521. struct batadv_dat_entry *dat_entry = NULL;
  1522. bool ret = false;
  1523. int hdr_size = sizeof(struct batadv_bcast_packet);
  1524. unsigned short vid;
  1525. if (!atomic_read(&bat_priv->distributed_arp_table))
  1526. goto out;
  1527. /* If this packet is an ARP_REQUEST and the node already has the
  1528. * information that it is going to ask, then the packet can be dropped
  1529. */
  1530. if (batadv_forw_packet_is_rebroadcast(forw_packet))
  1531. goto out;
  1532. vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
  1533. type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
  1534. if (type != ARPOP_REQUEST)
  1535. goto out;
  1536. ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
  1537. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  1538. /* check if the node already got this entry */
  1539. if (!dat_entry) {
  1540. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1541. "ARP Request for %pI4: fallback\n", &ip_dst);
  1542. goto out;
  1543. }
  1544. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1545. "ARP Request for %pI4: fallback prevented\n", &ip_dst);
  1546. ret = true;
  1547. out:
  1548. batadv_dat_entry_put(dat_entry);
  1549. return ret;
  1550. }