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