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