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