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