distributed-arp-table.c 41 KB

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