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