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