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