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