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