distributed-arp-table.c 52 KB

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