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