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