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
54enum batadv_bootpop {
55	BATADV_BOOTREPLY	= 2,
56};
57
58enum batadv_boothtype {
59	BATADV_HTYPE_ETHERNET	= 1,
60};
61
62enum batadv_dhcpoptioncode {
63	BATADV_DHCP_OPT_PAD		= 0,
64	BATADV_DHCP_OPT_MSG_TYPE	= 53,
65	BATADV_DHCP_OPT_END		= 255,
66};
67
68enum batadv_dhcptype {
69	BATADV_DHCPACK		= 5,
70};
71
72/* { 99, 130, 83, 99 } */
73#define BATADV_DHCP_MAGIC 1669485411
74
75struct 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
97static 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 */
103static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
104{
105	queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
106			   msecs_to_jiffies(10000));
107}
108
109/**
110 * batadv_dat_entry_release() - release dat_entry from lists and queue for free
111 *  after rcu grace period
112 * @ref: kref pointer of the dat_entry
113 */
114static void batadv_dat_entry_release(struct kref *ref)
115{
116	struct batadv_dat_entry *dat_entry;
117
118	dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
119
120	kfree_rcu(dat_entry, rcu);
121}
122
123/**
124 * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
125 *  release it
126 * @dat_entry: dat_entry to be free'd
127 */
128static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
129{
130	if (!dat_entry)
131		return;
132
133	kref_put(&dat_entry->refcount, batadv_dat_entry_release);
134}
135
136/**
137 * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
138 * @dat_entry: the entry to check
139 *
140 * Return: true if the entry has to be purged now, false otherwise.
141 */
142static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
143{
144	return batadv_has_timed_out(dat_entry->last_update,
145				    BATADV_DAT_ENTRY_TIMEOUT);
146}
147
148/**
149 * __batadv_dat_purge() - delete entries from the DAT local storage
150 * @bat_priv: the bat priv with all the soft interface information
151 * @to_purge: function in charge to decide whether an entry has to be purged or
152 *	      not. This function takes the dat_entry as argument and has to
153 *	      returns a boolean value: true is the entry has to be deleted,
154 *	      false otherwise
155 *
156 * Loops over each entry in the DAT local storage and deletes it if and only if
157 * the to_purge function passed as argument returns true.
158 */
159static void __batadv_dat_purge(struct batadv_priv *bat_priv,
160			       bool (*to_purge)(struct batadv_dat_entry *))
161{
162	spinlock_t *list_lock; /* protects write access to the hash lists */
163	struct batadv_dat_entry *dat_entry;
164	struct hlist_node *node_tmp;
165	struct hlist_head *head;
166	u32 i;
167
168	if (!bat_priv->dat.hash)
169		return;
170
171	for (i = 0; i < bat_priv->dat.hash->size; i++) {
172		head = &bat_priv->dat.hash->table[i];
173		list_lock = &bat_priv->dat.hash->list_locks[i];
174
175		spin_lock_bh(list_lock);
176		hlist_for_each_entry_safe(dat_entry, node_tmp, head,
177					  hash_entry) {
178			/* if a helper function has been passed as parameter,
179			 * ask it if the entry has to be purged or not
180			 */
181			if (to_purge && !to_purge(dat_entry))
182				continue;
183
184			hlist_del_rcu(&dat_entry->hash_entry);
185			batadv_dat_entry_put(dat_entry);
186		}
187		spin_unlock_bh(list_lock);
188	}
189}
190
191/**
192 * batadv_dat_purge() - periodic task that deletes old entries from the local
193 *  DAT hash table
194 * @work: kernel work struct
195 */
196static void batadv_dat_purge(struct work_struct *work)
197{
198	struct delayed_work *delayed_work;
199	struct batadv_priv_dat *priv_dat;
200	struct batadv_priv *bat_priv;
201
202	delayed_work = to_delayed_work(work);
203	priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
204	bat_priv = container_of(priv_dat, struct batadv_priv, dat);
205
206	__batadv_dat_purge(bat_priv, batadv_dat_to_purge);
207	batadv_dat_start_timer(bat_priv);
208}
209
210/**
211 * batadv_compare_dat() - comparing function used in the local DAT hash table
212 * @node: node in the local table
213 * @data2: second object to compare the node to
214 *
215 * Return: true if the two entries are the same, false otherwise.
216 */
217static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
218{
219	const void *data1 = container_of(node, struct batadv_dat_entry,
220					 hash_entry);
221
222	return memcmp(data1, data2, sizeof(__be32)) == 0;
223}
224
225/**
226 * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
227 * @skb: ARP packet
228 * @hdr_size: size of the possible header before the ARP packet
229 *
230 * Return: the value of the hw_src field in the ARP packet.
231 */
232static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
233{
234	u8 *addr;
235
236	addr = (u8 *)(skb->data + hdr_size);
237	addr += ETH_HLEN + sizeof(struct arphdr);
238
239	return addr;
240}
241
242/**
243 * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
244 * @skb: ARP packet
245 * @hdr_size: size of the possible header before the ARP packet
246 *
247 * Return: the value of the ip_src field in the ARP packet.
248 */
249static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
250{
251	return *(__force __be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
252}
253
254/**
255 * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
256 * @skb: ARP packet
257 * @hdr_size: size of the possible header before the ARP packet
258 *
259 * Return: the value of the hw_dst field in the ARP packet.
260 */
261static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
262{
263	return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
264}
265
266/**
267 * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
268 * @skb: ARP packet
269 * @hdr_size: size of the possible header before the ARP packet
270 *
271 * Return: the value of the ip_dst field in the ARP packet.
272 */
273static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
274{
275	u8 *dst = batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4;
276
277	return *(__force __be32 *)dst;
278}
279
280/**
281 * batadv_hash_dat() - compute the hash value for an IP address
282 * @data: data to hash
283 * @size: size of the hash table
284 *
285 * Return: the selected index in the hash table for the given data.
286 */
287static u32 batadv_hash_dat(const void *data, u32 size)
288{
289	u32 hash = 0;
290	const struct batadv_dat_entry *dat = data;
291	const unsigned char *key;
292	__be16 vid;
293	u32 i;
294
295	key = (__force const unsigned char *)&dat->ip;
296	for (i = 0; i < sizeof(dat->ip); i++) {
297		hash += key[i];
298		hash += (hash << 10);
299		hash ^= (hash >> 6);
300	}
301
302	vid = htons(dat->vid);
303	key = (__force const unsigned char *)&vid;
304	for (i = 0; i < sizeof(dat->vid); i++) {
305		hash += key[i];
306		hash += (hash << 10);
307		hash ^= (hash >> 6);
308	}
309
310	hash += (hash << 3);
311	hash ^= (hash >> 11);
312	hash += (hash << 15);
313
314	return hash % size;
315}
316
317/**
318 * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
319 * table
320 * @bat_priv: the bat priv with all the soft interface information
321 * @ip: search key
322 * @vid: VLAN identifier
323 *
324 * Return: the dat_entry if found, NULL otherwise.
325 */
326static struct batadv_dat_entry *
327batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
328			   unsigned short vid)
329{
330	struct hlist_head *head;
331	struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
332	struct batadv_hashtable *hash = bat_priv->dat.hash;
333	u32 index;
334
335	if (!hash)
336		return NULL;
337
338	to_find.ip = ip;
339	to_find.vid = vid;
340
341	index = batadv_hash_dat(&to_find, hash->size);
342	head = &hash->table[index];
343
344	rcu_read_lock();
345	hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
346		if (dat_entry->ip != ip)
347			continue;
348
349		if (!kref_get_unless_zero(&dat_entry->refcount))
350			continue;
351
352		dat_entry_tmp = dat_entry;
353		break;
354	}
355	rcu_read_unlock();
356
357	return dat_entry_tmp;
358}
359
360/**
361 * batadv_dat_entry_add() - add a new dat entry or update it if already exists
362 * @bat_priv: the bat priv with all the soft interface information
363 * @ip: ipv4 to add/edit
364 * @mac_addr: mac address to assign to the given ipv4
365 * @vid: VLAN identifier
366 */
367static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
368				 u8 *mac_addr, unsigned short vid)
369{
370	struct batadv_dat_entry *dat_entry;
371	int hash_added;
372
373	dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
374	/* if this entry is already known, just update it */
375	if (dat_entry) {
376		if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
377			ether_addr_copy(dat_entry->mac_addr, mac_addr);
378		dat_entry->last_update = jiffies;
379		batadv_dbg(BATADV_DBG_DAT, bat_priv,
380			   "Entry updated: %pI4 %pM (vid: %d)\n",
381			   &dat_entry->ip, dat_entry->mac_addr,
382			   batadv_print_vid(vid));
383		goto out;
384	}
385
386	dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
387	if (!dat_entry)
388		goto out;
389
390	dat_entry->ip = ip;
391	dat_entry->vid = vid;
392	ether_addr_copy(dat_entry->mac_addr, mac_addr);
393	dat_entry->last_update = jiffies;
394	kref_init(&dat_entry->refcount);
395
396	kref_get(&dat_entry->refcount);
397	hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
398				     batadv_hash_dat, dat_entry,
399				     &dat_entry->hash_entry);
400
401	if (unlikely(hash_added != 0)) {
402		/* remove the reference for the hash */
403		batadv_dat_entry_put(dat_entry);
404		goto out;
405	}
406
407	batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
408		   &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
409
410out:
411	if (dat_entry)
412		batadv_dat_entry_put(dat_entry);
413}
414
415#ifdef CONFIG_BATMAN_ADV_DEBUG
416
417/**
418 * batadv_dbg_arp() - print a debug message containing all the ARP packet
419 *  details
420 * @bat_priv: the bat priv with all the soft interface information
421 * @skb: ARP packet
422 * @hdr_size: size of the possible header before the ARP packet
423 * @msg: message to print together with the debugging information
424 */
425static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
426			   int hdr_size, char *msg)
427{
428	struct batadv_unicast_4addr_packet *unicast_4addr_packet;
429	struct batadv_bcast_packet *bcast_pkt;
430	u8 *orig_addr;
431	__be32 ip_src, ip_dst;
432
433	if (msg)
434		batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
435
436	ip_src = batadv_arp_ip_src(skb, hdr_size);
437	ip_dst = batadv_arp_ip_dst(skb, hdr_size);
438	batadv_dbg(BATADV_DBG_DAT, bat_priv,
439		   "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
440		   batadv_arp_hw_src(skb, hdr_size), &ip_src,
441		   batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
442
443	if (hdr_size < sizeof(struct batadv_unicast_packet))
444		return;
445
446	unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
447
448	switch (unicast_4addr_packet->u.packet_type) {
449	case BATADV_UNICAST:
450		batadv_dbg(BATADV_DBG_DAT, bat_priv,
451			   "* encapsulated within a UNICAST packet\n");
452		break;
453	case BATADV_UNICAST_4ADDR:
454		batadv_dbg(BATADV_DBG_DAT, bat_priv,
455			   "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
456			   unicast_4addr_packet->src);
457		switch (unicast_4addr_packet->subtype) {
458		case BATADV_P_DAT_DHT_PUT:
459			batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
460			break;
461		case BATADV_P_DAT_DHT_GET:
462			batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
463			break;
464		case BATADV_P_DAT_CACHE_REPLY:
465			batadv_dbg(BATADV_DBG_DAT, bat_priv,
466				   "* type: DAT_CACHE_REPLY\n");
467			break;
468		case BATADV_P_DATA:
469			batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
470			break;
471		default:
472			batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
473				   unicast_4addr_packet->u.packet_type);
474		}
475		break;
476	case BATADV_BCAST:
477		bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
478		orig_addr = bcast_pkt->orig;
479		batadv_dbg(BATADV_DBG_DAT, bat_priv,
480			   "* encapsulated within a BCAST packet (src: %pM)\n",
481			   orig_addr);
482		break;
483	default:
484		batadv_dbg(BATADV_DBG_DAT, bat_priv,
485			   "* encapsulated within an unknown packet type (0x%x)\n",
486			   unicast_4addr_packet->u.packet_type);
487	}
488}
489
490#else
491
492static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
493			   int hdr_size, char *msg)
494{
495}
496
497#endif /* CONFIG_BATMAN_ADV_DEBUG */
498
499/**
500 * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
501 * @res: the array with the already selected candidates
502 * @select: number of already selected candidates
503 * @tmp_max: address of the currently evaluated node
504 * @max: current round max address
505 * @last_max: address of the last selected candidate
506 * @candidate: orig_node under evaluation
507 * @max_orig_node: last selected candidate
508 *
509 * Return: true if the node has been elected as next candidate or false
510 * otherwise.
511 */
512static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
513					 int select, batadv_dat_addr_t tmp_max,
514					 batadv_dat_addr_t max,
515					 batadv_dat_addr_t last_max,
516					 struct batadv_orig_node *candidate,
517					 struct batadv_orig_node *max_orig_node)
518{
519	bool ret = false;
520	int j;
521
522	/* check if orig node candidate is running DAT */
523	if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
524		goto out;
525
526	/* Check if this node has already been selected... */
527	for (j = 0; j < select; j++)
528		if (res[j].orig_node == candidate)
529			break;
530	/* ..and possibly skip it */
531	if (j < select)
532		goto out;
533	/* sanity check: has it already been selected? This should not happen */
534	if (tmp_max > last_max)
535		goto out;
536	/* check if during this iteration an originator with a closer dht
537	 * address has already been found
538	 */
539	if (tmp_max < max)
540		goto out;
541	/* this is an hash collision with the temporary selected node. Choose
542	 * the one with the lowest address
543	 */
544	if (tmp_max == max && max_orig_node &&
545	    batadv_compare_eth(candidate->orig, max_orig_node->orig))
546		goto out;
547
548	ret = true;
549out:
550	return ret;
551}
552
553/**
554 * batadv_choose_next_candidate() - select the next DHT candidate
555 * @bat_priv: the bat priv with all the soft interface information
556 * @cands: candidates array
557 * @select: number of candidates already present in the array
558 * @ip_key: key to look up in the DHT
559 * @last_max: pointer where the address of the selected candidate will be saved
560 */
561static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
562					 struct batadv_dat_candidate *cands,
563					 int select, batadv_dat_addr_t ip_key,
564					 batadv_dat_addr_t *last_max)
565{
566	batadv_dat_addr_t max = 0;
567	batadv_dat_addr_t tmp_max = 0;
568	struct batadv_orig_node *orig_node, *max_orig_node = NULL;
569	struct batadv_hashtable *hash = bat_priv->orig_hash;
570	struct hlist_head *head;
571	int i;
572
573	/* if no node is eligible as candidate, leave the candidate type as
574	 * NOT_FOUND
575	 */
576	cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
577
578	/* iterate over the originator list and find the node with the closest
579	 * dat_address which has not been selected yet
580	 */
581	for (i = 0; i < hash->size; i++) {
582		head = &hash->table[i];
583
584		rcu_read_lock();
585		hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
586			/* the dht space is a ring using unsigned addresses */
587			tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
588				  ip_key;
589
590			if (!batadv_is_orig_node_eligible(cands, select,
591							  tmp_max, max,
592							  *last_max, orig_node,
593							  max_orig_node))
594				continue;
595
596			if (!kref_get_unless_zero(&orig_node->refcount))
597				continue;
598
599			max = tmp_max;
600			if (max_orig_node)
601				batadv_orig_node_put(max_orig_node);
602			max_orig_node = orig_node;
603		}
604		rcu_read_unlock();
605	}
606	if (max_orig_node) {
607		cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
608		cands[select].orig_node = max_orig_node;
609		batadv_dbg(BATADV_DBG_DAT, bat_priv,
610			   "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
611			   select, max_orig_node->orig, max_orig_node->dat_addr,
612			   max);
613	}
614	*last_max = max;
615}
616
617/**
618 * batadv_dat_select_candidates() - select the nodes which the DHT message has
619 *  to be sent to
620 * @bat_priv: the bat priv with all the soft interface information
621 * @ip_dst: ipv4 to look up in the DHT
622 * @vid: VLAN identifier
623 *
624 * An originator O is selected if and only if its DHT_ID value is one of three
625 * closest values (from the LEFT, with wrap around if needed) then the hash
626 * value of the key. ip_dst is the key.
627 *
628 * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
629 */
630static struct batadv_dat_candidate *
631batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
632			     unsigned short vid)
633{
634	int select;
635	batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
636	struct batadv_dat_candidate *res;
637	struct batadv_dat_entry dat;
638
639	if (!bat_priv->orig_hash)
640		return NULL;
641
642	res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
643			    GFP_ATOMIC);
644	if (!res)
645		return NULL;
646
647	dat.ip = ip_dst;
648	dat.vid = vid;
649	ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
650						    BATADV_DAT_ADDR_MAX);
651
652	batadv_dbg(BATADV_DBG_DAT, bat_priv,
653		   "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
654		   ip_key);
655
656	for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
657		batadv_choose_next_candidate(bat_priv, res, select, ip_key,
658					     &last_max);
659
660	return res;
661}
662
663/**
664 * batadv_dat_forward_data() - copy and send payload to the selected candidates
665 * @bat_priv: the bat priv with all the soft interface information
666 * @skb: payload to send
667 * @ip: the DHT key
668 * @vid: VLAN identifier
669 * @packet_subtype: unicast4addr packet subtype to use
670 *
671 * This function copies the skb with pskb_copy() and is sent as a unicast packet
672 * to each of the selected candidates.
673 *
674 * Return: true if the packet is sent to at least one candidate, false
675 * otherwise.
676 */
677static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
678				    struct sk_buff *skb, __be32 ip,
679				    unsigned short vid, int packet_subtype)
680{
681	int i;
682	bool ret = false;
683	int send_status;
684	struct batadv_neigh_node *neigh_node = NULL;
685	struct sk_buff *tmp_skb;
686	struct batadv_dat_candidate *cand;
687
688	cand = batadv_dat_select_candidates(bat_priv, ip, vid);
689	if (!cand)
690		goto out;
691
692	batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
693
694	for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
695		if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
696			continue;
697
698		neigh_node = batadv_orig_router_get(cand[i].orig_node,
699						    BATADV_IF_DEFAULT);
700		if (!neigh_node)
701			goto free_orig;
702
703		tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
704		if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
705							   cand[i].orig_node,
706							   packet_subtype)) {
707			kfree_skb(tmp_skb);
708			goto free_neigh;
709		}
710
711		send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
712		if (send_status == NET_XMIT_SUCCESS) {
713			/* count the sent packet */
714			switch (packet_subtype) {
715			case BATADV_P_DAT_DHT_GET:
716				batadv_inc_counter(bat_priv,
717						   BATADV_CNT_DAT_GET_TX);
718				break;
719			case BATADV_P_DAT_DHT_PUT:
720				batadv_inc_counter(bat_priv,
721						   BATADV_CNT_DAT_PUT_TX);
722				break;
723			}
724
725			/* packet sent to a candidate: return true */
726			ret = true;
727		}
728free_neigh:
729		batadv_neigh_node_put(neigh_node);
730free_orig:
731		batadv_orig_node_put(cand[i].orig_node);
732	}
733
734out:
735	kfree(cand);
736	return ret;
737}
738
739/**
740 * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
741 *  setting change
742 * @bat_priv: the bat priv with all the soft interface information
743 */
744static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
745{
746	char dat_mode;
747
748	dat_mode = atomic_read(&bat_priv->distributed_arp_table);
749
750	switch (dat_mode) {
751	case 0:
752		batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
753		break;
754	case 1:
755		batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
756					       NULL, 0);
757		break;
758	}
759}
760
761/**
762 * batadv_dat_status_update() - update the dat tvlv container after dat
763 *  setting change
764 * @net_dev: the soft interface net device
765 */
766void batadv_dat_status_update(struct net_device *net_dev)
767{
768	struct batadv_priv *bat_priv = netdev_priv(net_dev);
769
770	batadv_dat_tvlv_container_update(bat_priv);
771}
772
773/**
774 * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
775 * @bat_priv: the bat priv with all the soft interface information
776 * @orig: the orig_node of the ogm
777 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
778 * @tvlv_value: tvlv buffer containing the gateway data
779 * @tvlv_value_len: tvlv buffer length
780 */
781static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
782					   struct batadv_orig_node *orig,
783					   u8 flags,
784					   void *tvlv_value, u16 tvlv_value_len)
785{
786	if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
787		clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
788	else
789		set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
790}
791
792/**
793 * batadv_dat_hash_free() - free the local DAT hash table
794 * @bat_priv: the bat priv with all the soft interface information
795 */
796static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
797{
798	if (!bat_priv->dat.hash)
799		return;
800
801	__batadv_dat_purge(bat_priv, NULL);
802
803	batadv_hash_destroy(bat_priv->dat.hash);
804
805	bat_priv->dat.hash = NULL;
806}
807
808/**
809 * batadv_dat_init() - initialise the DAT internals
810 * @bat_priv: the bat priv with all the soft interface information
811 *
812 * Return: 0 in case of success, a negative error code otherwise
813 */
814int batadv_dat_init(struct batadv_priv *bat_priv)
815{
816	if (bat_priv->dat.hash)
817		return 0;
818
819	bat_priv->dat.hash = batadv_hash_new(1024);
820
821	if (!bat_priv->dat.hash)
822		return -ENOMEM;
823
824	INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
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 */
838void 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 */
856int 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
895out:
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 */
912static int
913batadv_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 */
956static int
957batadv_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
979skip:
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 */
994int 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
1041out:
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 */
1059static 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);
1121out:
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 */
1134static 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 */
1165static struct sk_buff *
1166batadv_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 */
1196bool 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	}
1278out:
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 */
1293bool 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	}
1346out:
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 */
1359void 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 */
1406bool 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);
1478out:
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 */
1498static bool
1499batadv_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 */
1538static int
1539batadv_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 */
1586static 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 */
1634static 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 */
1661static 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 */
1694static 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 */
1741static bool
1742batadv_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 */
1779void 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 */
1807void 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 */
1852bool 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
1890out:
1891	if (dat_entry)
1892		batadv_dat_entry_put(dat_entry);
1893	return ret;
1894}
1895