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