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