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