xref: /linux/net/batman-adv/distributed-arp-table.c (revision d1807b20d548171d40c5d21a7cf22830ea8f348e)
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 
657 	/* iterate over the originator list and find the node with the closest
658 	 * dat_address which has not been selected yet
659 	 */
660 	for (i = 0; i < hash->size; i++) {
661 		head = &hash->table[i];
662 
663 		rcu_read_lock();
664 		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
665 			/* the dht space is a ring using unsigned addresses */
666 			tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
667 				  ip_key;
668 
669 			if (!batadv_is_orig_node_eligible(cands, select,
670 							  tmp_max, max,
671 							  *last_max, orig_node,
672 							  max_orig_node))
673 				continue;
674 
675 			if (!kref_get_unless_zero(&orig_node->refcount))
676 				continue;
677 
678 			max = tmp_max;
679 			batadv_orig_node_put(max_orig_node);
680 			max_orig_node = orig_node;
681 		}
682 		rcu_read_unlock();
683 	}
684 	if (max_orig_node) {
685 		cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
686 		cands[select].orig_node = max_orig_node;
687 		batadv_dbg(BATADV_DBG_DAT, bat_priv,
688 			   "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
689 			   select, max_orig_node->orig, max_orig_node->dat_addr,
690 			   max);
691 	}
692 	*last_max = max;
693 }
694 
695 /**
696  * batadv_dat_select_candidates() - select the nodes which the DHT message has
697  *  to be sent to
698  * @bat_priv: the bat priv with all the mesh interface information
699  * @ip_dst: ipv4 to look up in the DHT
700  * @vid: VLAN identifier
701  *
702  * An originator O is selected if and only if its DHT_ID value is one of three
703  * closest values (from the LEFT, with wrap around if needed) then the hash
704  * value of the key. ip_dst is the key.
705  *
706  * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
707  */
708 static struct batadv_dat_candidate *
709 batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
710 			     unsigned short vid)
711 {
712 	batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX;
713 	struct batadv_dat_candidate *res;
714 	struct batadv_dat_entry dat;
715 	batadv_dat_addr_t ip_key;
716 	int select;
717 
718 	if (!bat_priv->orig_hash)
719 		return NULL;
720 
721 	res = kmalloc_objs(*res, BATADV_DAT_CANDIDATES_NUM, GFP_ATOMIC);
722 	if (!res)
723 		return NULL;
724 
725 	dat.ip = ip_dst;
726 	dat.vid = vid;
727 	ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
728 						    BATADV_DAT_ADDR_MAX);
729 
730 	batadv_dbg(BATADV_DBG_DAT, bat_priv,
731 		   "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
732 		   ip_key);
733 
734 	for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
735 		batadv_choose_next_candidate(bat_priv, res, select, ip_key,
736 					     &last_max);
737 
738 	return res;
739 }
740 
741 /**
742  * batadv_dat_forward_data() - copy and send payload to the selected candidates
743  * @bat_priv: the bat priv with all the mesh interface information
744  * @skb: payload to send
745  * @ip: the DHT key
746  * @vid: VLAN identifier
747  * @packet_subtype: unicast4addr packet subtype to use
748  *
749  * This function copies the skb with pskb_copy() and is sent as a unicast packet
750  * to each of the selected candidates.
751  *
752  * Return: true if the packet is sent to at least one candidate, false
753  * otherwise.
754  */
755 static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
756 				    struct sk_buff *skb, __be32 ip,
757 				    unsigned short vid, int packet_subtype)
758 {
759 	struct batadv_neigh_node *neigh_node = NULL;
760 	struct batadv_dat_candidate *cand;
761 	struct sk_buff *tmp_skb;
762 	bool ret = false;
763 	int send_status;
764 	int i;
765 
766 	cand = batadv_dat_select_candidates(bat_priv, ip, vid);
767 	if (!cand)
768 		return ret;
769 
770 	batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
771 
772 	for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
773 		if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
774 			continue;
775 
776 		neigh_node = batadv_orig_router_get(cand[i].orig_node,
777 						    BATADV_IF_DEFAULT);
778 		if (!neigh_node)
779 			goto free_orig;
780 
781 		tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
782 		if (!tmp_skb)
783 			goto free_neigh;
784 
785 		if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
786 							   cand[i].orig_node,
787 							   packet_subtype)) {
788 			kfree_skb(tmp_skb);
789 			goto free_neigh;
790 		}
791 
792 		send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
793 		if (send_status == NET_XMIT_SUCCESS) {
794 			/* count the sent packet */
795 			switch (packet_subtype) {
796 			case BATADV_P_DAT_DHT_GET:
797 				batadv_inc_counter(bat_priv,
798 						   BATADV_CNT_DAT_GET_TX);
799 				break;
800 			case BATADV_P_DAT_DHT_PUT:
801 				batadv_inc_counter(bat_priv,
802 						   BATADV_CNT_DAT_PUT_TX);
803 				break;
804 			}
805 
806 			/* packet sent to a candidate: return true */
807 			ret = true;
808 		}
809 free_neigh:
810 		batadv_neigh_node_put(neigh_node);
811 free_orig:
812 		batadv_orig_node_put(cand[i].orig_node);
813 	}
814 
815 	kfree(cand);
816 	return ret;
817 }
818 
819 /**
820  * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
821  *  setting change
822  * @bat_priv: the bat priv with all the mesh interface information
823  */
824 static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
825 {
826 	char dat_mode;
827 
828 	dat_mode = READ_ONCE(bat_priv->distributed_arp_table);
829 
830 	switch (dat_mode) {
831 	case 0:
832 		batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
833 		break;
834 	case 1:
835 		batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
836 					       NULL, 0);
837 		break;
838 	}
839 }
840 
841 /**
842  * batadv_dat_status_update() - update the dat tvlv container after dat
843  *  setting change
844  * @net_dev: the mesh interface net device
845  */
846 void batadv_dat_status_update(struct net_device *net_dev)
847 {
848 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
849 
850 	batadv_dat_tvlv_container_update(bat_priv);
851 }
852 
853 /**
854  * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
855  * @bat_priv: the bat priv with all the mesh interface information
856  * @orig: the orig_node of the ogm
857  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
858  * @tvlv_value: tvlv buffer containing the gateway data
859  * @tvlv_value_len: tvlv buffer length
860  */
861 static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
862 					   struct batadv_orig_node *orig,
863 					   u8 flags,
864 					   void *tvlv_value, u16 tvlv_value_len)
865 {
866 	assign_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities,
867 		   !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND));
868 }
869 
870 /**
871  * batadv_dat_hash_free() - free the local DAT hash table
872  * @bat_priv: the bat priv with all the mesh interface information
873  */
874 static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
875 {
876 	if (!bat_priv->dat.hash)
877 		return;
878 
879 	__batadv_dat_purge(bat_priv, NULL);
880 
881 	batadv_hash_destroy(bat_priv->dat.hash);
882 
883 	bat_priv->dat.hash = NULL;
884 }
885 
886 /**
887  * batadv_dat_init() - initialise the DAT internals
888  * @bat_priv: the bat priv with all the mesh interface information
889  *
890  * Return: 0 in case of success, a negative error code otherwise
891  */
892 int batadv_dat_init(struct batadv_priv *bat_priv)
893 {
894 	if (bat_priv->dat.hash)
895 		return 0;
896 
897 	bat_priv->dat.hash = batadv_hash_new(1024);
898 
899 	if (!bat_priv->dat.hash)
900 		return -ENOMEM;
901 
902 	INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
903 	batadv_dat_start_timer(bat_priv);
904 
905 	batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
906 				     NULL, NULL, BATADV_TVLV_DAT, 1,
907 				     BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
908 	batadv_dat_tvlv_container_update(bat_priv);
909 	return 0;
910 }
911 
912 /**
913  * batadv_dat_free() - free the DAT internals
914  * @bat_priv: the bat priv with all the mesh interface information
915  */
916 void batadv_dat_free(struct batadv_priv *bat_priv)
917 {
918 	batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
919 	batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
920 
921 	disable_delayed_work_sync(&bat_priv->dat.work);
922 
923 	batadv_dat_hash_free(bat_priv);
924 }
925 
926 /**
927  * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
928  *  netlink socket
929  * @msg: buffer for the message
930  * @portid: netlink port
931  * @cb: Control block containing additional options
932  * @dat_entry: entry to dump
933  *
934  * Return: 0 or error code.
935  */
936 static int
937 batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
938 			    struct netlink_callback *cb,
939 			    struct batadv_dat_entry *dat_entry)
940 {
941 	u8 mac[ETH_ALEN] __aligned(2);
942 	u64 u64_mac;
943 	int msecs;
944 	void *hdr;
945 
946 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
947 			  &batadv_netlink_family, NLM_F_MULTI,
948 			  BATADV_CMD_GET_DAT_CACHE);
949 	if (!hdr)
950 		return -ENOBUFS;
951 
952 	genl_dump_check_consistent(cb, hdr);
953 
954 	msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
955 	u64_mac = atomic64_read(&dat_entry->mac_addr);
956 	u64_to_ether_addr(u64_mac, mac);
957 
958 	if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
959 			    dat_entry->ip) ||
960 	    nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN, mac) ||
961 	    nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
962 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
963 		genlmsg_cancel(msg, hdr);
964 		return -EMSGSIZE;
965 	}
966 
967 	genlmsg_end(msg, hdr);
968 	return 0;
969 }
970 
971 /**
972  * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
973  *  a netlink socket
974  * @msg: buffer for the message
975  * @portid: netlink port
976  * @cb: Control block containing additional options
977  * @hash: hash to dump
978  * @bucket: bucket index to dump
979  * @idx_skip: How many entries to skip
980  *
981  * Return: 0 or error code.
982  */
983 static int
984 batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
985 			     struct netlink_callback *cb,
986 			     struct batadv_hashtable *hash, unsigned int bucket,
987 			     int *idx_skip)
988 {
989 	struct batadv_dat_entry *dat_entry;
990 	int idx = 0;
991 
992 	spin_lock_bh(&hash->list_locks[bucket]);
993 	cb->seq = atomic_read(&hash->generation) << 1 | 1;
994 
995 	hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
996 		if (idx < *idx_skip)
997 			goto skip;
998 
999 		if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
1000 			spin_unlock_bh(&hash->list_locks[bucket]);
1001 			*idx_skip = idx;
1002 
1003 			return -EMSGSIZE;
1004 		}
1005 
1006 skip:
1007 		idx++;
1008 	}
1009 	spin_unlock_bh(&hash->list_locks[bucket]);
1010 
1011 	return 0;
1012 }
1013 
1014 /**
1015  * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
1016  * @msg: buffer for the message
1017  * @cb: callback structure containing arguments
1018  *
1019  * Return: message length.
1020  */
1021 int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
1022 {
1023 	struct batadv_hard_iface *primary_if = NULL;
1024 	int portid = NETLINK_CB(cb->skb).portid;
1025 	struct net_device *mesh_iface;
1026 	struct batadv_hashtable *hash;
1027 	struct batadv_priv *bat_priv;
1028 	int bucket = cb->args[0];
1029 	int idx = cb->args[1];
1030 	int ret = 0;
1031 
1032 	mesh_iface = batadv_netlink_get_meshif(cb);
1033 	if (IS_ERR(mesh_iface))
1034 		return PTR_ERR(mesh_iface);
1035 
1036 	bat_priv = netdev_priv(mesh_iface);
1037 	hash = bat_priv->dat.hash;
1038 
1039 	primary_if = batadv_primary_if_get_selected(bat_priv);
1040 	if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1041 		ret = -ENOENT;
1042 		goto out;
1043 	}
1044 
1045 	while (bucket < hash->size) {
1046 		if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
1047 						 &idx))
1048 			break;
1049 
1050 		bucket++;
1051 		idx = 0;
1052 	}
1053 
1054 	cb->args[0] = bucket;
1055 	cb->args[1] = idx;
1056 
1057 	ret = msg->len;
1058 
1059 out:
1060 	batadv_hardif_put(primary_if);
1061 
1062 	dev_put(mesh_iface);
1063 
1064 	return ret;
1065 }
1066 
1067 /**
1068  * batadv_arp_get_type() - parse an ARP packet and gets the type
1069  * @bat_priv: the bat priv with all the mesh interface information
1070  * @skb: packet to analyse
1071  * @hdr_size: size of the possible header before the ARP packet in the skb
1072  *
1073  * Warning: This function may reallocate the skb data buffer via
1074  * pskb_may_pull()/... Any pointer into the skb data (e.g. obtained from skb->data
1075  * or eth_hdr()) before this call must be considered invalid afterwards and has
1076  * to be reacquired.
1077  *
1078  * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
1079  */
1080 static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
1081 			       struct sk_buff *skb, int hdr_size)
1082 {
1083 	struct arphdr *arphdr;
1084 	struct ethhdr *ethhdr;
1085 	__be32 ip_src;
1086 	__be32 ip_dst;
1087 	u16 type = 0;
1088 	u8 *hw_src;
1089 	u8 *hw_dst;
1090 
1091 	/* pull the ethernet header */
1092 	if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1093 		goto out;
1094 
1095 	ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1096 
1097 	if (ethhdr->h_proto != htons(ETH_P_ARP))
1098 		goto out;
1099 
1100 	/* pull the ARP payload */
1101 	if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
1102 				    arp_hdr_len(skb->dev))))
1103 		goto out;
1104 
1105 	arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
1106 
1107 	/* check whether the ARP packet carries a valid IP information */
1108 	if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1109 		goto out;
1110 
1111 	if (arphdr->ar_pro != htons(ETH_P_IP))
1112 		goto out;
1113 
1114 	if (arphdr->ar_hln != ETH_ALEN)
1115 		goto out;
1116 
1117 	if (arphdr->ar_pln != 4)
1118 		goto out;
1119 
1120 	/* Check for bad reply/request. If the ARP message is not sane, DAT
1121 	 * will simply ignore it
1122 	 */
1123 	ip_src = batadv_arp_ip_src(skb, hdr_size);
1124 	ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1125 	if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
1126 	    ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
1127 	    ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
1128 	    ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
1129 		goto out;
1130 
1131 	hw_src = batadv_arp_hw_src(skb, hdr_size);
1132 	if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
1133 		goto out;
1134 
1135 	/* don't care about the destination MAC address in ARP requests */
1136 	if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
1137 		hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1138 		if (is_zero_ether_addr(hw_dst) ||
1139 		    is_multicast_ether_addr(hw_dst))
1140 			goto out;
1141 	}
1142 
1143 	type = ntohs(arphdr->ar_op);
1144 out:
1145 	return type;
1146 }
1147 
1148 /**
1149  * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
1150  * @skb: the buffer containing the packet to extract the VID from
1151  * @hdr_size: the size of the batman-adv header encapsulating the packet
1152  *
1153  * The caller must ensure that at least @hdr_size + ETH_HLEN bytes are
1154  * accessible after skb->data.
1155  *
1156  * Warning: This function calls batadv_get_vid() and may therefore reallocate
1157  * the skb data buffer. Any pointer into the skb data (e.g. obtained from
1158  * skb->data or eth_hdr()) before this call must be considered invalid
1159  * afterwards and has to be reacquired.
1160  *
1161  * Return: If the packet embedded in the skb is vlan tagged this function
1162  * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
1163  * is returned.
1164  */
1165 static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
1166 {
1167 	unsigned short vid;
1168 
1169 	vid = batadv_get_vid(skb, *hdr_size);
1170 
1171 	/* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
1172 	 * If the header contained in the packet is a VLAN one (which is longer)
1173 	 * hdr_size is updated so that the functions will still skip the
1174 	 * correct amount of bytes.
1175 	 */
1176 	if (vid & BATADV_VLAN_HAS_TAG)
1177 		*hdr_size += VLAN_HLEN;
1178 
1179 	return vid;
1180 }
1181 
1182 /**
1183  * batadv_dat_arp_create_reply() - create an ARP Reply
1184  * @bat_priv: the bat priv with all the mesh interface information
1185  * @ip_src: ARP sender IP
1186  * @ip_dst: ARP target IP
1187  * @hw_src: Ethernet source and ARP sender MAC
1188  * @hw_dst: Ethernet destination and ARP target MAC
1189  * @vid: VLAN identifier (optional, set to zero otherwise)
1190  *
1191  * Creates an ARP Reply from the given values, optionally encapsulated in a
1192  * VLAN header.
1193  *
1194  * Return: An skb containing an ARP Reply.
1195  */
1196 static struct sk_buff *
1197 batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
1198 			    __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
1199 			    unsigned short vid)
1200 {
1201 	struct sk_buff *skb;
1202 
1203 	skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->mesh_iface,
1204 			 ip_src, hw_dst, hw_src, hw_dst);
1205 	if (!skb)
1206 		return NULL;
1207 
1208 	skb_reset_mac_header(skb);
1209 
1210 	if (vid & BATADV_VLAN_HAS_TAG)
1211 		skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
1212 				      vid & VLAN_VID_MASK);
1213 
1214 	return skb;
1215 }
1216 
1217 /**
1218  * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
1219  * answer using DAT
1220  * @bat_priv: the bat priv with all the mesh interface information
1221  * @skb: packet to check
1222  *
1223  * Warning: This function may reallocate the skb data buffer via
1224  * batadv_dat_get_vid()/.... Any pointer into the skb data (e.g. obtained
1225  * from skb->data or eth_hdr()) before this call must be considered
1226  * invalid afterwards and has to be reacquired.
1227  *
1228  * Return: true if the message has been sent to the dht candidates, false
1229  * otherwise. In case of a positive return value the message has to be enqueued
1230  * to permit the fallback.
1231  */
1232 bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
1233 					   struct sk_buff *skb)
1234 {
1235 	struct net_device *mesh_iface = bat_priv->mesh_iface;
1236 	struct batadv_dat_entry *dat_entry = NULL;
1237 	u8 mac[ETH_ALEN] __aligned(2);
1238 	struct sk_buff *skb_new;
1239 	unsigned short vid;
1240 	bool ret = false;
1241 	int hdr_size = 0;
1242 	__be32 ip_dst;
1243 	__be32 ip_src;
1244 	u16 type = 0;
1245 	u64 u64_mac;
1246 	u8 *hw_src;
1247 
1248 	if (!READ_ONCE(bat_priv->distributed_arp_table))
1249 		goto out;
1250 
1251 	/* first, find out the vid. */
1252 	if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1253 		goto out;
1254 
1255 	vid = batadv_dat_get_vid(skb, &hdr_size);
1256 
1257 	type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1258 	/* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
1259 	 * message to the selected DHT candidates
1260 	 */
1261 	if (type != ARPOP_REQUEST)
1262 		goto out;
1263 
1264 	batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
1265 
1266 	ip_src = batadv_arp_ip_src(skb, hdr_size);
1267 	hw_src = batadv_arp_hw_src(skb, hdr_size);
1268 	ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1269 
1270 	batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1271 
1272 	dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1273 	if (dat_entry) {
1274 		u64_mac = atomic64_read(&dat_entry->mac_addr);
1275 		u64_to_ether_addr(u64_mac, mac);
1276 
1277 		/* If the ARP request is destined for a local client the local
1278 		 * client will answer itself. DAT would only generate a
1279 		 * duplicate packet.
1280 		 *
1281 		 * Moreover, if the mesh-interface is enslaved into a bridge, an
1282 		 * additional DAT answer may trigger kernel warnings about
1283 		 * a packet coming from the wrong port.
1284 		 */
1285 		if (batadv_is_my_client(bat_priv, mac, vid)) {
1286 			ret = true;
1287 			goto out;
1288 		}
1289 
1290 		/* If BLA is enabled, only send ARP replies if we have claimed
1291 		 * the destination for the ARP request or if no one else of
1292 		 * the backbone gws belonging to our backbone has claimed the
1293 		 * destination.
1294 		 */
1295 		if (!batadv_bla_check_claim(bat_priv, mac, vid)) {
1296 			batadv_dbg(BATADV_DBG_DAT, bat_priv,
1297 				   "Device %pM claimed by another backbone gw. Don't send ARP reply!",
1298 				   mac);
1299 			ret = true;
1300 			goto out;
1301 		}
1302 
1303 		skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1304 						      mac, hw_src, vid);
1305 		if (!skb_new)
1306 			goto out;
1307 
1308 		skb_new->protocol = eth_type_trans(skb_new, mesh_iface);
1309 
1310 		batadv_inc_counter(bat_priv, BATADV_CNT_RX);
1311 		batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
1312 				   skb->len + ETH_HLEN + hdr_size);
1313 
1314 		netif_rx(skb_new);
1315 		batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
1316 		ret = true;
1317 	} else {
1318 		/* Send the request to the DHT */
1319 		ret = batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1320 					      BATADV_P_DAT_DHT_GET);
1321 	}
1322 out:
1323 	batadv_dat_entry_put(dat_entry);
1324 	return ret;
1325 }
1326 
1327 /**
1328  * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
1329  * answer using the local DAT storage
1330  * @bat_priv: the bat priv with all the mesh interface information
1331  * @skb: packet to check
1332  * @hdr_size: size of the encapsulation header
1333  *
1334  * Warning: This function may reallocate the skb data buffer via
1335  * batadv_dat_get_vid()/... Any pointer into the skb data (e.g. obtained
1336  * from skb->data or eth_hdr()) before this call must be considered
1337  * invalid afterwards and has to be reacquired.
1338  *
1339  * Return: true if the request has been answered, false otherwise.
1340  */
1341 bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
1342 					   struct sk_buff *skb, int hdr_size)
1343 {
1344 	struct batadv_dat_entry *dat_entry = NULL;
1345 	u8 mac[ETH_ALEN] __aligned(2);
1346 	struct sk_buff *skb_new;
1347 	unsigned short vid;
1348 	bool ret = false;
1349 	__be32 ip_src;
1350 	__be32 ip_dst;
1351 	u64 u64_mac;
1352 	u8 *hw_src;
1353 	u16 type;
1354 	int err;
1355 
1356 	if (!READ_ONCE(bat_priv->distributed_arp_table))
1357 		goto out;
1358 
1359 	/* first, find out the vid. */
1360 	if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1361 		goto out;
1362 
1363 	vid = batadv_dat_get_vid(skb, &hdr_size);
1364 
1365 	type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1366 	if (type != ARPOP_REQUEST)
1367 		goto out;
1368 
1369 	hw_src = batadv_arp_hw_src(skb, hdr_size);
1370 	ip_src = batadv_arp_ip_src(skb, hdr_size);
1371 	ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1372 
1373 	batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
1374 
1375 	batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1376 
1377 	dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1378 	if (!dat_entry)
1379 		goto out;
1380 
1381 	u64_mac = atomic64_read(&dat_entry->mac_addr);
1382 	u64_to_ether_addr(u64_mac, mac);
1383 
1384 	skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
1385 					      mac, hw_src, vid);
1386 	if (!skb_new)
1387 		goto out;
1388 
1389 	err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
1390 					   BATADV_P_DAT_CACHE_REPLY,
1391 					   NULL, vid);
1392 	if (err != NET_XMIT_DROP) {
1393 		batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
1394 		ret = true;
1395 	}
1396 out:
1397 	batadv_dat_entry_put(dat_entry);
1398 	if (ret)
1399 		kfree_skb(skb);
1400 	return ret;
1401 }
1402 
1403 /**
1404  * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
1405  * @bat_priv: the bat priv with all the mesh interface information
1406  * @skb: packet to check
1407  *
1408  * Warning: This function may reallocate the skb data buffer via
1409  * batadv_dat_get_vid()/... Any pointer into the skb data (e.g. obtained
1410  * from skb->data or eth_hdr()) before this call must be considered
1411  * invalid afterwards and has to be reacquired.
1412  */
1413 void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
1414 					 struct sk_buff *skb)
1415 {
1416 	unsigned short vid;
1417 	int hdr_size = 0;
1418 	__be32 ip_src;
1419 	__be32 ip_dst;
1420 	u8 *hw_src;
1421 	u8 *hw_dst;
1422 	u16 type;
1423 
1424 	if (!READ_ONCE(bat_priv->distributed_arp_table))
1425 		return;
1426 
1427 	/* first, find out the vid. */
1428 	if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1429 		return;
1430 
1431 	vid = batadv_dat_get_vid(skb, &hdr_size);
1432 
1433 	type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1434 	if (type != ARPOP_REPLY)
1435 		return;
1436 
1437 	batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
1438 
1439 	hw_src = batadv_arp_hw_src(skb, hdr_size);
1440 	ip_src = batadv_arp_ip_src(skb, hdr_size);
1441 	hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1442 	ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1443 
1444 	batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1445 	batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1446 
1447 	/* Send the ARP reply to the candidates for both the IP addresses that
1448 	 * the node obtained from the ARP reply
1449 	 */
1450 	batadv_dat_forward_data(bat_priv, skb, ip_src, vid,
1451 				BATADV_P_DAT_DHT_PUT);
1452 	batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1453 				BATADV_P_DAT_DHT_PUT);
1454 }
1455 
1456 /**
1457  * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
1458  *  local DAT storage only
1459  * @bat_priv: the bat priv with all the mesh interface information
1460  * @skb: packet to check
1461  * @hdr_size: size of the encapsulation header
1462  *
1463  * Warning: This function may reallocate the skb data buffer via
1464  * batadv_dat_get_vid()/... Any pointer into the skb data (e.g. obtained
1465  * from skb->data or eth_hdr()) before this call must be considered
1466  * invalid afterwards and has to be reacquired.
1467  *
1468  * Return: true if the packet was snooped and consumed by DAT. False if the
1469  * packet has to be delivered to the interface
1470  */
1471 bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1472 					 struct sk_buff *skb, int hdr_size)
1473 {
1474 	struct batadv_dat_entry *dat_entry = NULL;
1475 	u8 mac[ETH_ALEN] __aligned(2);
1476 	bool dropped = false;
1477 	unsigned short vid;
1478 	__be32 ip_src;
1479 	__be32 ip_dst;
1480 	u64 u64_mac;
1481 	u8 *hw_src;
1482 	u8 *hw_dst;
1483 	u16 type;
1484 
1485 	if (!READ_ONCE(bat_priv->distributed_arp_table))
1486 		goto out;
1487 
1488 	/* first, find out the vid. */
1489 	if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1490 		goto out;
1491 
1492 	vid = batadv_dat_get_vid(skb, &hdr_size);
1493 
1494 	type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1495 	if (type != ARPOP_REPLY)
1496 		goto out;
1497 
1498 	batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
1499 
1500 	hw_src = batadv_arp_hw_src(skb, hdr_size);
1501 	ip_src = batadv_arp_ip_src(skb, hdr_size);
1502 	hw_dst = batadv_arp_hw_dst(skb, hdr_size);
1503 	ip_dst = batadv_arp_ip_dst(skb, hdr_size);
1504 
1505 	/* If ip_dst is already in cache and has the right mac address,
1506 	 * drop this frame if this ARP reply is destined for us because it's
1507 	 * most probably an ARP reply generated by another node of the DHT.
1508 	 * We have most probably received already a reply earlier. Delivering
1509 	 * this frame would lead to doubled receive of an ARP reply.
1510 	 */
1511 	dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
1512 	if (dat_entry) {
1513 		u64_mac = atomic64_read(&dat_entry->mac_addr);
1514 		u64_to_ether_addr(u64_mac, mac);
1515 
1516 		if (batadv_compare_eth(hw_src, mac)) {
1517 			batadv_dbg(BATADV_DBG_DAT, bat_priv,
1518 				   "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
1519 				   hw_src, &ip_src, hw_dst, &ip_dst,
1520 				   mac, &dat_entry->ip);
1521 			dropped = true;
1522 		}
1523 	}
1524 
1525 	/* Update our internal cache with both the IP addresses the node got
1526 	 * within the ARP reply
1527 	 */
1528 	batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1529 	batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1530 
1531 	if (dropped)
1532 		goto out;
1533 
1534 	/* If BLA is enabled, only forward ARP replies if we have claimed the
1535 	 * source of the ARP reply or if no one else of the same backbone has
1536 	 * already claimed that client. This prevents that different gateways
1537 	 * to the same backbone all forward the ARP reply leading to multiple
1538 	 * replies in the backbone.
1539 	 */
1540 	if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
1541 		batadv_dbg(BATADV_DBG_DAT, bat_priv,
1542 			   "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
1543 			   hw_src);
1544 		dropped = true;
1545 		goto out;
1546 	}
1547 
1548 	/* if this REPLY is directed to a client of mine, let's deliver the
1549 	 * packet to the interface
1550 	 */
1551 	dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
1552 
1553 	/* if this REPLY is sent on behalf of a client of mine, let's drop the
1554 	 * packet because the client will reply by itself
1555 	 */
1556 	dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
1557 out:
1558 	if (dropped)
1559 		kfree_skb(skb);
1560 	batadv_dat_entry_put(dat_entry);
1561 	/* if dropped == false -> deliver to the interface */
1562 	return dropped;
1563 }
1564 
1565 /**
1566  * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
1567  * @skb: the packet to check
1568  * @ip_src: a buffer to store the IPv4 source address in
1569  *
1570  * Checks whether the given skb has an IP and UDP header valid for a DHCP
1571  * message from a DHCP server. And if so, stores the IPv4 source address in
1572  * the provided buffer.
1573  *
1574  * Return: True if valid, false otherwise.
1575  */
1576 static bool
1577 batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
1578 {
1579 	unsigned int offset = skb_network_offset(skb);
1580 	struct udphdr *udphdr;
1581 	struct udphdr _udphdr;
1582 	struct iphdr *iphdr;
1583 	struct iphdr _iphdr;
1584 
1585 	iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
1586 	if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
1587 		return false;
1588 
1589 	if (iphdr->protocol != IPPROTO_UDP)
1590 		return false;
1591 
1592 	offset += iphdr->ihl * 4;
1593 	skb_set_transport_header(skb, offset);
1594 
1595 	udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
1596 	if (!udphdr || udphdr->source != htons(67))
1597 		return false;
1598 
1599 	*ip_src = get_unaligned(&iphdr->saddr);
1600 
1601 	return true;
1602 }
1603 
1604 /**
1605  * batadv_dat_check_dhcp() - examine packet for valid DHCP message
1606  * @skb: the packet to check
1607  * @proto: ethernet protocol hint (behind a potential vlan)
1608  * @ip_src: a buffer to store the IPv4 source address in
1609  *
1610  * Checks whether the given skb is a valid DHCP packet. And if so, stores the
1611  * IPv4 source address in the provided buffer.
1612  *
1613  * Caller needs to ensure that the skb network header is set correctly.
1614  *
1615  * Return: If skb is a valid DHCP packet, then returns its op code
1616  * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
1617  */
1618 static int
1619 batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
1620 {
1621 	struct batadv_dhcp_header *dhcp_h;
1622 	struct batadv_dhcp_header _dhcp_h;
1623 	unsigned int offset;
1624 	__be32 *magic;
1625 	__be32 _magic;
1626 
1627 	if (proto != htons(ETH_P_IP))
1628 		return -EINVAL;
1629 
1630 	if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
1631 		return -EINVAL;
1632 
1633 	offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1634 	if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
1635 		return -EINVAL;
1636 
1637 	dhcp_h = skb_header_pointer(skb, offset, sizeof(_dhcp_h), &_dhcp_h);
1638 	if (!dhcp_h || dhcp_h->htype != BATADV_HTYPE_ETHERNET ||
1639 	    dhcp_h->hlen != ETH_ALEN)
1640 		return -EINVAL;
1641 
1642 	offset += offsetof(struct batadv_dhcp_packet, magic);
1643 
1644 	magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
1645 	if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
1646 		return -EINVAL;
1647 
1648 	return dhcp_h->op;
1649 }
1650 
1651 /**
1652  * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
1653  * @skb: the DHCP packet to parse
1654  *
1655  * Iterates over the DHCP options of the given DHCP packet to find a
1656  * DHCP Message Type option and parse it.
1657  *
1658  * Caller needs to ensure that the given skb is a valid DHCP packet and
1659  * that the skb transport header is set correctly.
1660  *
1661  * Return: The found DHCP message type value, if found. -EINVAL otherwise.
1662  */
1663 static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
1664 {
1665 	unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1666 	struct batadv_dhcp_option_header *tl;
1667 	struct batadv_dhcp_option_header _tl;
1668 	u8 *type;
1669 	u8 _type;
1670 
1671 	offset += sizeof(struct batadv_dhcp_packet);
1672 
1673 	while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
1674 		if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
1675 			break;
1676 
1677 		if (tl->type == BATADV_DHCP_OPT_END)
1678 			break;
1679 
1680 		if (tl->type == BATADV_DHCP_OPT_PAD)
1681 			offset++;
1682 		else
1683 			offset += tl->len + sizeof(_tl);
1684 	}
1685 
1686 	/* Option Overload Code not supported */
1687 	if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
1688 	    tl->len != sizeof(_type))
1689 		return -EINVAL;
1690 
1691 	offset += sizeof(_tl);
1692 
1693 	type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
1694 	if (!type)
1695 		return -EINVAL;
1696 
1697 	return *type;
1698 }
1699 
1700 /**
1701  * batadv_dat_dhcp_get_yiaddr() - get yiaddr from a DHCP packet
1702  * @skb: the DHCP packet to parse
1703  * @buf: a buffer to store the yiaddr in
1704  *
1705  * Caller needs to ensure that the given skb is a valid DHCP packet and
1706  * that the skb transport header is set correctly.
1707  *
1708  * Return: True on success, false otherwise.
1709  */
1710 static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
1711 {
1712 	unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1713 	__be32 *yiaddr;
1714 
1715 	offset += offsetof(struct batadv_dhcp_packet, yiaddr);
1716 	yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
1717 
1718 	if (!yiaddr)
1719 		return false;
1720 
1721 	if (yiaddr != buf)
1722 		*buf = get_unaligned(yiaddr);
1723 
1724 	return true;
1725 }
1726 
1727 /**
1728  * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
1729  * @skb: the DHCP packet to parse
1730  * @buf: a buffer to store the chaddr in
1731  *
1732  * Caller needs to ensure that the given skb is a valid DHCP packet and
1733  * that the skb transport header is set correctly.
1734  *
1735  * Return: True on success, false otherwise
1736  */
1737 static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
1738 {
1739 	unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
1740 	u8 *chaddr;
1741 
1742 	offset += offsetof(struct batadv_dhcp_packet, chaddr);
1743 	chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
1744 
1745 	if (!chaddr)
1746 		return false;
1747 
1748 	if (chaddr != buf)
1749 		memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
1750 
1751 	return true;
1752 }
1753 
1754 /**
1755  * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
1756  *  DAT cache
1757  * @bat_priv: the bat priv with all the mesh interface information
1758  * @chaddr: the DHCP client MAC address
1759  * @yiaddr: the DHCP client IP address
1760  * @hw_dst: the DHCP server MAC address
1761  * @ip_dst: the DHCP server IP address
1762  * @vid: VLAN identifier
1763  *
1764  * Adds given MAC/IP pairs to the local DAT cache and propagates them further
1765  * into the DHT.
1766  *
1767  * For the DHT propagation, client MAC + IP will appear as the ARP Reply
1768  * transmitter (and hw_dst/ip_dst as the target).
1769  */
1770 static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
1771 				__be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
1772 				unsigned short vid)
1773 {
1774 	struct sk_buff *skb;
1775 
1776 	skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
1777 					  hw_dst, vid);
1778 	if (!skb)
1779 		return;
1780 
1781 	skb_set_network_header(skb, ETH_HLEN);
1782 
1783 	batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1784 	batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1785 
1786 	batadv_dat_forward_data(bat_priv, skb, yiaddr, vid,
1787 				BATADV_P_DAT_DHT_PUT);
1788 	batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
1789 				BATADV_P_DAT_DHT_PUT);
1790 
1791 	consume_skb(skb);
1792 
1793 	batadv_dbg(BATADV_DBG_DAT, bat_priv,
1794 		   "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1795 		   &ip_dst, hw_dst, batadv_print_vid(vid));
1796 	batadv_dbg(BATADV_DBG_DAT, bat_priv,
1797 		   "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1798 		   &yiaddr, chaddr, batadv_print_vid(vid));
1799 }
1800 
1801 /**
1802  * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
1803  * @skb: the packet to check
1804  * @proto: ethernet protocol hint (behind a potential vlan)
1805  * @ip_src: a buffer to store the IPv4 source address in
1806  * @chaddr: a buffer to store the DHCP Client Hardware Address in
1807  * @yiaddr: a buffer to store the DHCP Your IP Address in
1808  *
1809  * Checks whether the given skb is a valid DHCPACK. And if so, stores the
1810  * IPv4 server source address (ip_src), client MAC address (chaddr) and client
1811  * IPv4 address (yiaddr) in the provided buffers.
1812  *
1813  * Caller needs to ensure that the skb network header is set correctly.
1814  *
1815  * Return: True if the skb is a valid DHCPACK. False otherwise.
1816  */
1817 static bool
1818 batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
1819 			  u8 *chaddr, __be32 *yiaddr)
1820 {
1821 	int type;
1822 
1823 	type = batadv_dat_check_dhcp(skb, proto, ip_src);
1824 	if (type != BATADV_BOOTREPLY)
1825 		return false;
1826 
1827 	type = batadv_dat_get_dhcp_message_type(skb);
1828 	if (type != BATADV_DHCPACK)
1829 		return false;
1830 
1831 	if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
1832 		return false;
1833 
1834 	if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
1835 		return false;
1836 
1837 	return true;
1838 }
1839 
1840 /**
1841  * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
1842  * @bat_priv: the bat priv with all the mesh interface information
1843  * @skb: the packet to snoop
1844  * @proto: ethernet protocol hint (behind a potential vlan)
1845  * @vid: VLAN identifier
1846  *
1847  * This function first checks whether the given skb is a valid DHCPACK. If
1848  * so then its source MAC and IP as well as its DHCP Client Hardware Address
1849  * field and DHCP Your IP Address field are added to the local DAT cache and
1850  * propagated into the DHT.
1851  *
1852  * Caller needs to ensure that the skb mac and network headers are set
1853  * correctly.
1854  */
1855 void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
1856 					struct sk_buff *skb,
1857 					__be16 proto,
1858 					unsigned short vid)
1859 {
1860 	u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1861 	__be32 ip_src;
1862 	__be32 yiaddr;
1863 
1864 	if (!READ_ONCE(bat_priv->distributed_arp_table))
1865 		return;
1866 
1867 	if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1868 		return;
1869 
1870 	batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
1871 			    ip_src, vid);
1872 }
1873 
1874 /**
1875  * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
1876  * @bat_priv: the bat priv with all the mesh interface information
1877  * @skb: the packet to snoop
1878  * @hdr_size: header size, up to the tail of the batman-adv header
1879  *
1880  * This function first checks whether the given skb is a valid DHCPACK. If
1881  * so then its source MAC and IP as well as its DHCP Client Hardware Address
1882  * field and DHCP Your IP Address field are added to the local DAT cache.
1883  *
1884  * Warning: This function may reallocate the skb data buffer via
1885  * pskb_may_pull()/batadv_dat_get_vid()/... Any pointer into the skb data
1886  * (e.g.obtained from skb->data or eth_hdr()) before this call must be
1887  * considered invalid afterwards and has to be reacquired.
1888  */
1889 void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
1890 					struct sk_buff *skb, int hdr_size)
1891 {
1892 	u8 chaddr[BATADV_DHCP_CHADDR_LEN];
1893 	struct ethhdr *ethhdr;
1894 	unsigned short vid;
1895 	int hdr_size_tmp;
1896 	__be32 ip_src;
1897 	__be32 yiaddr;
1898 	__be16 proto;
1899 	u8 *hw_src;
1900 
1901 	if (!READ_ONCE(bat_priv->distributed_arp_table))
1902 		return;
1903 
1904 	if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
1905 		return;
1906 
1907 	ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1908 	skb_set_network_header(skb, hdr_size + ETH_HLEN);
1909 	proto = ethhdr->h_proto;
1910 
1911 	if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
1912 		return;
1913 
1914 	hdr_size_tmp = hdr_size;
1915 	vid = batadv_dat_get_vid(skb, &hdr_size_tmp);
1916 	ethhdr = (struct ethhdr *)(skb->data + hdr_size);
1917 	hw_src = ethhdr->h_source;
1918 
1919 	batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
1920 	batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1921 
1922 	batadv_dbg(BATADV_DBG_DAT, bat_priv,
1923 		   "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
1924 		   &ip_src, hw_src, batadv_print_vid(vid));
1925 	batadv_dbg(BATADV_DBG_DAT, bat_priv,
1926 		   "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
1927 		   &yiaddr, chaddr, batadv_print_vid(vid));
1928 }
1929 
1930 /**
1931  * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
1932  *  dropped (because the node has already obtained the reply via DAT) or not
1933  * @bat_priv: the bat priv with all the mesh interface information
1934  * @forw_packet: the broadcast packet
1935  *
1936  * Warning: This function may reallocate the skb data buffer via
1937  * batadv_dat_get_vid()/... Any pointer into the skb data (e.g. obtained
1938  * from skb->data or eth_hdr()) before this call must be considered
1939  * invalid afterwards and has to be reacquired.
1940  *
1941  * Return: true if the node can drop the packet, false otherwise.
1942  */
1943 bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1944 				      struct batadv_forw_packet *forw_packet)
1945 {
1946 	int hdr_size = sizeof(struct batadv_bcast_packet);
1947 	struct batadv_dat_entry *dat_entry = NULL;
1948 	unsigned short vid;
1949 	bool ret = false;
1950 	__be32 ip_dst;
1951 	u16 type;
1952 
1953 	if (!READ_ONCE(bat_priv->distributed_arp_table))
1954 		goto out;
1955 
1956 	/* If this packet is an ARP_REQUEST and the node already has the
1957 	 * information that it is going to ask, then the packet can be dropped
1958 	 */
1959 	if (batadv_forw_packet_is_rebroadcast(forw_packet))
1960 		goto out;
1961 
1962 	/* first, find out the vid. */
1963 	if (!pskb_may_pull(forw_packet->skb, hdr_size + ETH_HLEN))
1964 		goto out;
1965 
1966 	vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1967 
1968 	type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
1969 	if (type != ARPOP_REQUEST)
1970 		goto out;
1971 
1972 	ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1973 	dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1974 	/* check if the node already got this entry */
1975 	if (!dat_entry) {
1976 		batadv_dbg(BATADV_DBG_DAT, bat_priv,
1977 			   "ARP Request for %pI4: fallback\n", &ip_dst);
1978 		goto out;
1979 	}
1980 
1981 	batadv_dbg(BATADV_DBG_DAT, bat_priv,
1982 		   "ARP Request for %pI4: fallback prevented\n", &ip_dst);
1983 	ret = true;
1984 
1985 out:
1986 	batadv_dat_entry_put(dat_entry);
1987 	return ret;
1988 }
1989