1/* Copyright (C) 2011-2014 B.A.T.M.A.N. contributors: 2 * 3 * Antonio Quartulli 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of version 2 of the GNU General Public 7 * License as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18#include <linux/bitops.h> 19#include <linux/if_ether.h> 20#include <linux/if_arp.h> 21#include <linux/if_vlan.h> 22#include <net/arp.h> 23 24#include "main.h" 25#include "hash.h" 26#include "distributed-arp-table.h" 27#include "hard-interface.h" 28#include "originator.h" 29#include "send.h" 30#include "types.h" 31#include "translation-table.h" 32 33static void batadv_dat_purge(struct work_struct *work); 34 35/** 36 * batadv_dat_start_timer - initialise the DAT periodic worker 37 * @bat_priv: the bat priv with all the soft interface information 38 */ 39static void batadv_dat_start_timer(struct batadv_priv *bat_priv) 40{ 41 INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge); 42 queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work, 43 msecs_to_jiffies(10000)); 44} 45 46/** 47 * batadv_dat_entry_free_ref - decrement the dat_entry refcounter and possibly 48 * free it 49 * @dat_entry: the entry to free 50 */ 51static void batadv_dat_entry_free_ref(struct batadv_dat_entry *dat_entry) 52{ 53 if (atomic_dec_and_test(&dat_entry->refcount)) 54 kfree_rcu(dat_entry, rcu); 55} 56 57/** 58 * batadv_dat_to_purge - check whether a dat_entry has to be purged or not 59 * @dat_entry: the entry to check 60 * 61 * Returns true if the entry has to be purged now, false otherwise. 62 */ 63static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry) 64{ 65 return batadv_has_timed_out(dat_entry->last_update, 66 BATADV_DAT_ENTRY_TIMEOUT); 67} 68 69/** 70 * __batadv_dat_purge - delete entries from the DAT local storage 71 * @bat_priv: the bat priv with all the soft interface information 72 * @to_purge: function in charge to decide whether an entry has to be purged or 73 * not. This function takes the dat_entry as argument and has to 74 * returns a boolean value: true is the entry has to be deleted, 75 * false otherwise 76 * 77 * Loops over each entry in the DAT local storage and deletes it if and only if 78 * the to_purge function passed as argument returns true. 79 */ 80static void __batadv_dat_purge(struct batadv_priv *bat_priv, 81 bool (*to_purge)(struct batadv_dat_entry *)) 82{ 83 spinlock_t *list_lock; /* protects write access to the hash lists */ 84 struct batadv_dat_entry *dat_entry; 85 struct hlist_node *node_tmp; 86 struct hlist_head *head; 87 uint32_t i; 88 89 if (!bat_priv->dat.hash) 90 return; 91 92 for (i = 0; i < bat_priv->dat.hash->size; i++) { 93 head = &bat_priv->dat.hash->table[i]; 94 list_lock = &bat_priv->dat.hash->list_locks[i]; 95 96 spin_lock_bh(list_lock); 97 hlist_for_each_entry_safe(dat_entry, node_tmp, head, 98 hash_entry) { 99 /* if a helper function has been passed as parameter, 100 * ask it if the entry has to be purged or not 101 */ 102 if (to_purge && !to_purge(dat_entry)) 103 continue; 104 105 hlist_del_rcu(&dat_entry->hash_entry); 106 batadv_dat_entry_free_ref(dat_entry); 107 } 108 spin_unlock_bh(list_lock); 109 } 110} 111 112/** 113 * batadv_dat_purge - periodic task that deletes old entries from the local DAT 114 * hash table 115 * @work: kernel work struct 116 */ 117static void batadv_dat_purge(struct work_struct *work) 118{ 119 struct delayed_work *delayed_work; 120 struct batadv_priv_dat *priv_dat; 121 struct batadv_priv *bat_priv; 122 123 delayed_work = container_of(work, struct delayed_work, work); 124 priv_dat = container_of(delayed_work, struct batadv_priv_dat, work); 125 bat_priv = container_of(priv_dat, struct batadv_priv, dat); 126 127 __batadv_dat_purge(bat_priv, batadv_dat_to_purge); 128 batadv_dat_start_timer(bat_priv); 129} 130 131/** 132 * batadv_compare_dat - comparing function used in the local DAT hash table 133 * @node: node in the local table 134 * @data2: second object to compare the node to 135 * 136 * Returns 1 if the two entries are the same, 0 otherwise. 137 */ 138static int batadv_compare_dat(const struct hlist_node *node, const void *data2) 139{ 140 const void *data1 = container_of(node, struct batadv_dat_entry, 141 hash_entry); 142 143 return memcmp(data1, data2, sizeof(__be32)) == 0 ? 1 : 0; 144} 145 146/** 147 * batadv_arp_hw_src - extract the hw_src field from an ARP packet 148 * @skb: ARP packet 149 * @hdr_size: size of the possible header before the ARP packet 150 * 151 * Returns the value of the hw_src field in the ARP packet. 152 */ 153static uint8_t *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size) 154{ 155 uint8_t *addr; 156 157 addr = (uint8_t *)(skb->data + hdr_size); 158 addr += ETH_HLEN + sizeof(struct arphdr); 159 160 return addr; 161} 162 163/** 164 * batadv_arp_ip_src - extract the ip_src field from an ARP packet 165 * @skb: ARP packet 166 * @hdr_size: size of the possible header before the ARP packet 167 * 168 * Returns the value of the ip_src field in the ARP packet. 169 */ 170static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size) 171{ 172 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN); 173} 174 175/** 176 * batadv_arp_hw_dst - extract the hw_dst field from an ARP packet 177 * @skb: ARP packet 178 * @hdr_size: size of the possible header before the ARP packet 179 * 180 * Returns the value of the hw_dst field in the ARP packet. 181 */ 182static uint8_t *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size) 183{ 184 return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4; 185} 186 187/** 188 * batadv_arp_ip_dst - extract the ip_dst field from an ARP packet 189 * @skb: ARP packet 190 * @hdr_size: size of the possible header before the ARP packet 191 * 192 * Returns the value of the ip_dst field in the ARP packet. 193 */ 194static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size) 195{ 196 return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4); 197} 198 199/** 200 * batadv_hash_dat - compute the hash value for an IP address 201 * @data: data to hash 202 * @size: size of the hash table 203 * 204 * Returns the selected index in the hash table for the given data. 205 */ 206static uint32_t batadv_hash_dat(const void *data, uint32_t size) 207{ 208 uint32_t hash = 0; 209 const struct batadv_dat_entry *dat = data; 210 211 hash = batadv_hash_bytes(hash, &dat->ip, sizeof(dat->ip)); 212 hash = batadv_hash_bytes(hash, &dat->vid, sizeof(dat->vid)); 213 214 hash += (hash << 3); 215 hash ^= (hash >> 11); 216 hash += (hash << 15); 217 218 return hash % size; 219} 220 221/** 222 * batadv_dat_entry_hash_find - look for a given dat_entry in the local hash 223 * table 224 * @bat_priv: the bat priv with all the soft interface information 225 * @ip: search key 226 * @vid: VLAN identifier 227 * 228 * Returns the dat_entry if found, NULL otherwise. 229 */ 230static struct batadv_dat_entry * 231batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip, 232 unsigned short vid) 233{ 234 struct hlist_head *head; 235 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL; 236 struct batadv_hashtable *hash = bat_priv->dat.hash; 237 uint32_t index; 238 239 if (!hash) 240 return NULL; 241 242 to_find.ip = ip; 243 to_find.vid = vid; 244 245 index = batadv_hash_dat(&to_find, hash->size); 246 head = &hash->table[index]; 247 248 rcu_read_lock(); 249 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) { 250 if (dat_entry->ip != ip) 251 continue; 252 253 if (!atomic_inc_not_zero(&dat_entry->refcount)) 254 continue; 255 256 dat_entry_tmp = dat_entry; 257 break; 258 } 259 rcu_read_unlock(); 260 261 return dat_entry_tmp; 262} 263 264/** 265 * batadv_dat_entry_add - add a new dat entry or update it if already exists 266 * @bat_priv: the bat priv with all the soft interface information 267 * @ip: ipv4 to add/edit 268 * @mac_addr: mac address to assign to the given ipv4 269 * @vid: VLAN identifier 270 */ 271static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip, 272 uint8_t *mac_addr, unsigned short vid) 273{ 274 struct batadv_dat_entry *dat_entry; 275 int hash_added; 276 277 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid); 278 /* if this entry is already known, just update it */ 279 if (dat_entry) { 280 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr)) 281 ether_addr_copy(dat_entry->mac_addr, mac_addr); 282 dat_entry->last_update = jiffies; 283 batadv_dbg(BATADV_DBG_DAT, bat_priv, 284 "Entry updated: %pI4 %pM (vid: %d)\n", 285 &dat_entry->ip, dat_entry->mac_addr, 286 BATADV_PRINT_VID(vid)); 287 goto out; 288 } 289 290 dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC); 291 if (!dat_entry) 292 goto out; 293 294 dat_entry->ip = ip; 295 dat_entry->vid = vid; 296 ether_addr_copy(dat_entry->mac_addr, mac_addr); 297 dat_entry->last_update = jiffies; 298 atomic_set(&dat_entry->refcount, 2); 299 300 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat, 301 batadv_hash_dat, dat_entry, 302 &dat_entry->hash_entry); 303 304 if (unlikely(hash_added != 0)) { 305 /* remove the reference for the hash */ 306 batadv_dat_entry_free_ref(dat_entry); 307 goto out; 308 } 309 310 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n", 311 &dat_entry->ip, dat_entry->mac_addr, BATADV_PRINT_VID(vid)); 312 313out: 314 if (dat_entry) 315 batadv_dat_entry_free_ref(dat_entry); 316} 317 318#ifdef CONFIG_BATMAN_ADV_DEBUG 319 320/** 321 * batadv_dbg_arp - print a debug message containing all the ARP packet details 322 * @bat_priv: the bat priv with all the soft interface information 323 * @skb: ARP packet 324 * @type: ARP type 325 * @hdr_size: size of the possible header before the ARP packet 326 * @msg: message to print together with the debugging information 327 */ 328static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb, 329 uint16_t type, int hdr_size, char *msg) 330{ 331 struct batadv_unicast_4addr_packet *unicast_4addr_packet; 332 struct batadv_bcast_packet *bcast_pkt; 333 uint8_t *orig_addr; 334 __be32 ip_src, ip_dst; 335 336 if (msg) 337 batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg); 338 339 ip_src = batadv_arp_ip_src(skb, hdr_size); 340 ip_dst = batadv_arp_ip_dst(skb, hdr_size); 341 batadv_dbg(BATADV_DBG_DAT, bat_priv, 342 "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n", 343 batadv_arp_hw_src(skb, hdr_size), &ip_src, 344 batadv_arp_hw_dst(skb, hdr_size), &ip_dst); 345 346 if (hdr_size == 0) 347 return; 348 349 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data; 350 351 switch (unicast_4addr_packet->u.packet_type) { 352 case BATADV_UNICAST: 353 batadv_dbg(BATADV_DBG_DAT, bat_priv, 354 "* encapsulated within a UNICAST packet\n"); 355 break; 356 case BATADV_UNICAST_4ADDR: 357 batadv_dbg(BATADV_DBG_DAT, bat_priv, 358 "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n", 359 unicast_4addr_packet->src); 360 switch (unicast_4addr_packet->subtype) { 361 case BATADV_P_DAT_DHT_PUT: 362 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n"); 363 break; 364 case BATADV_P_DAT_DHT_GET: 365 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n"); 366 break; 367 case BATADV_P_DAT_CACHE_REPLY: 368 batadv_dbg(BATADV_DBG_DAT, bat_priv, 369 "* type: DAT_CACHE_REPLY\n"); 370 break; 371 case BATADV_P_DATA: 372 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n"); 373 break; 374 default: 375 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n", 376 unicast_4addr_packet->u.packet_type); 377 } 378 break; 379 case BATADV_BCAST: 380 bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet; 381 orig_addr = bcast_pkt->orig; 382 batadv_dbg(BATADV_DBG_DAT, bat_priv, 383 "* encapsulated within a BCAST packet (src: %pM)\n", 384 orig_addr); 385 break; 386 default: 387 batadv_dbg(BATADV_DBG_DAT, bat_priv, 388 "* encapsulated within an unknown packet type (0x%x)\n", 389 unicast_4addr_packet->u.packet_type); 390 } 391} 392 393#else 394 395static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb, 396 uint16_t type, int hdr_size, char *msg) 397{ 398} 399 400#endif /* CONFIG_BATMAN_ADV_DEBUG */ 401 402/** 403 * batadv_is_orig_node_eligible - check whether a node can be a DHT candidate 404 * @res: the array with the already selected candidates 405 * @select: number of already selected candidates 406 * @tmp_max: address of the currently evaluated node 407 * @max: current round max address 408 * @last_max: address of the last selected candidate 409 * @candidate: orig_node under evaluation 410 * @max_orig_node: last selected candidate 411 * 412 * Returns true if the node has been elected as next candidate or false 413 * otherwise. 414 */ 415static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res, 416 int select, batadv_dat_addr_t tmp_max, 417 batadv_dat_addr_t max, 418 batadv_dat_addr_t last_max, 419 struct batadv_orig_node *candidate, 420 struct batadv_orig_node *max_orig_node) 421{ 422 bool ret = false; 423 int j; 424 425 /* check if orig node candidate is running DAT */ 426 if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities)) 427 goto out; 428 429 /* Check if this node has already been selected... */ 430 for (j = 0; j < select; j++) 431 if (res[j].orig_node == candidate) 432 break; 433 /* ..and possibly skip it */ 434 if (j < select) 435 goto out; 436 /* sanity check: has it already been selected? This should not happen */ 437 if (tmp_max > last_max) 438 goto out; 439 /* check if during this iteration an originator with a closer dht 440 * address has already been found 441 */ 442 if (tmp_max < max) 443 goto out; 444 /* this is an hash collision with the temporary selected node. Choose 445 * the one with the lowest address 446 */ 447 if ((tmp_max == max) && max_orig_node && 448 (batadv_compare_eth(candidate->orig, max_orig_node->orig) > 0)) 449 goto out; 450 451 ret = true; 452out: 453 return ret; 454} 455 456/** 457 * batadv_choose_next_candidate - select the next DHT candidate 458 * @bat_priv: the bat priv with all the soft interface information 459 * @cands: candidates array 460 * @select: number of candidates already present in the array 461 * @ip_key: key to look up in the DHT 462 * @last_max: pointer where the address of the selected candidate will be saved 463 */ 464static void batadv_choose_next_candidate(struct batadv_priv *bat_priv, 465 struct batadv_dat_candidate *cands, 466 int select, batadv_dat_addr_t ip_key, 467 batadv_dat_addr_t *last_max) 468{ 469 batadv_dat_addr_t max = 0, tmp_max = 0; 470 struct batadv_orig_node *orig_node, *max_orig_node = NULL; 471 struct batadv_hashtable *hash = bat_priv->orig_hash; 472 struct hlist_head *head; 473 int i; 474 475 /* if no node is eligible as candidate, leave the candidate type as 476 * NOT_FOUND 477 */ 478 cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND; 479 480 /* iterate over the originator list and find the node with the closest 481 * dat_address which has not been selected yet 482 */ 483 for (i = 0; i < hash->size; i++) { 484 head = &hash->table[i]; 485 486 rcu_read_lock(); 487 hlist_for_each_entry_rcu(orig_node, head, hash_entry) { 488 /* the dht space is a ring using unsigned addresses */ 489 tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr + 490 ip_key; 491 492 if (!batadv_is_orig_node_eligible(cands, select, 493 tmp_max, max, 494 *last_max, orig_node, 495 max_orig_node)) 496 continue; 497 498 if (!atomic_inc_not_zero(&orig_node->refcount)) 499 continue; 500 501 max = tmp_max; 502 if (max_orig_node) 503 batadv_orig_node_free_ref(max_orig_node); 504 max_orig_node = orig_node; 505 } 506 rcu_read_unlock(); 507 } 508 if (max_orig_node) { 509 cands[select].type = BATADV_DAT_CANDIDATE_ORIG; 510 cands[select].orig_node = max_orig_node; 511 batadv_dbg(BATADV_DBG_DAT, bat_priv, 512 "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n", 513 select, max_orig_node->orig, max_orig_node->dat_addr, 514 max); 515 } 516 *last_max = max; 517} 518 519/** 520 * batadv_dat_select_candidates - select the nodes which the DHT message has to 521 * be sent to 522 * @bat_priv: the bat priv with all the soft interface information 523 * @ip_dst: ipv4 to look up in the DHT 524 * 525 * An originator O is selected if and only if its DHT_ID value is one of three 526 * closest values (from the LEFT, with wrap around if needed) then the hash 527 * value of the key. ip_dst is the key. 528 * 529 * Returns the candidate array of size BATADV_DAT_CANDIDATE_NUM. 530 */ 531static struct batadv_dat_candidate * 532batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst) 533{ 534 int select; 535 batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key; 536 struct batadv_dat_candidate *res; 537 538 if (!bat_priv->orig_hash) 539 return NULL; 540 541 res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res), 542 GFP_ATOMIC); 543 if (!res) 544 return NULL; 545 546 ip_key = (batadv_dat_addr_t)batadv_hash_dat(&ip_dst, 547 BATADV_DAT_ADDR_MAX); 548 549 batadv_dbg(BATADV_DBG_DAT, bat_priv, 550 "dat_select_candidates(): IP=%pI4 hash(IP)=%u\n", &ip_dst, 551 ip_key); 552 553 for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++) 554 batadv_choose_next_candidate(bat_priv, res, select, ip_key, 555 &last_max); 556 557 return res; 558} 559 560/** 561 * batadv_dat_send_data - send a payload to the selected candidates 562 * @bat_priv: the bat priv with all the soft interface information 563 * @skb: payload to send 564 * @ip: the DHT key 565 * @packet_subtype: unicast4addr packet subtype to use 566 * 567 * This function copies the skb with pskb_copy() and is sent as unicast packet 568 * to each of the selected candidates. 569 * 570 * Returns true if the packet is sent to at least one candidate, false 571 * otherwise. 572 */ 573static bool batadv_dat_send_data(struct batadv_priv *bat_priv, 574 struct sk_buff *skb, __be32 ip, 575 int packet_subtype) 576{ 577 int i; 578 bool ret = false; 579 int send_status; 580 struct batadv_neigh_node *neigh_node = NULL; 581 struct sk_buff *tmp_skb; 582 struct batadv_dat_candidate *cand; 583 584 cand = batadv_dat_select_candidates(bat_priv, ip); 585 if (!cand) 586 goto out; 587 588 batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip); 589 590 for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) { 591 if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND) 592 continue; 593 594 neigh_node = batadv_orig_router_get(cand[i].orig_node, 595 BATADV_IF_DEFAULT); 596 if (!neigh_node) 597 goto free_orig; 598 599 tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC); 600 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb, 601 cand[i].orig_node, 602 packet_subtype)) { 603 kfree_skb(tmp_skb); 604 goto free_neigh; 605 } 606 607 send_status = batadv_send_skb_packet(tmp_skb, 608 neigh_node->if_incoming, 609 neigh_node->addr); 610 if (send_status == NET_XMIT_SUCCESS) { 611 /* count the sent packet */ 612 switch (packet_subtype) { 613 case BATADV_P_DAT_DHT_GET: 614 batadv_inc_counter(bat_priv, 615 BATADV_CNT_DAT_GET_TX); 616 break; 617 case BATADV_P_DAT_DHT_PUT: 618 batadv_inc_counter(bat_priv, 619 BATADV_CNT_DAT_PUT_TX); 620 break; 621 } 622 623 /* packet sent to a candidate: return true */ 624 ret = true; 625 } 626free_neigh: 627 batadv_neigh_node_free_ref(neigh_node); 628free_orig: 629 batadv_orig_node_free_ref(cand[i].orig_node); 630 } 631 632out: 633 kfree(cand); 634 return ret; 635} 636 637/** 638 * batadv_dat_tvlv_container_update - update the dat tvlv container after dat 639 * setting change 640 * @bat_priv: the bat priv with all the soft interface information 641 */ 642static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv) 643{ 644 char dat_mode; 645 646 dat_mode = atomic_read(&bat_priv->distributed_arp_table); 647 648 switch (dat_mode) { 649 case 0: 650 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1); 651 break; 652 case 1: 653 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1, 654 NULL, 0); 655 break; 656 } 657} 658 659/** 660 * batadv_dat_status_update - update the dat tvlv container after dat 661 * setting change 662 * @net_dev: the soft interface net device 663 */ 664void batadv_dat_status_update(struct net_device *net_dev) 665{ 666 struct batadv_priv *bat_priv = netdev_priv(net_dev); 667 668 batadv_dat_tvlv_container_update(bat_priv); 669} 670 671/** 672 * batadv_gw_tvlv_ogm_handler_v1 - process incoming dat tvlv container 673 * @bat_priv: the bat priv with all the soft interface information 674 * @orig: the orig_node of the ogm 675 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags) 676 * @tvlv_value: tvlv buffer containing the gateway data 677 * @tvlv_value_len: tvlv buffer length 678 */ 679static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv, 680 struct batadv_orig_node *orig, 681 uint8_t flags, 682 void *tvlv_value, 683 uint16_t tvlv_value_len) 684{ 685 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) 686 clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities); 687 else 688 set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities); 689} 690 691/** 692 * batadv_dat_hash_free - free the local DAT hash table 693 * @bat_priv: the bat priv with all the soft interface information 694 */ 695static void batadv_dat_hash_free(struct batadv_priv *bat_priv) 696{ 697 if (!bat_priv->dat.hash) 698 return; 699 700 __batadv_dat_purge(bat_priv, NULL); 701 702 batadv_hash_destroy(bat_priv->dat.hash); 703 704 bat_priv->dat.hash = NULL; 705} 706 707/** 708 * batadv_dat_init - initialise the DAT internals 709 * @bat_priv: the bat priv with all the soft interface information 710 */ 711int batadv_dat_init(struct batadv_priv *bat_priv) 712{ 713 if (bat_priv->dat.hash) 714 return 0; 715 716 bat_priv->dat.hash = batadv_hash_new(1024); 717 718 if (!bat_priv->dat.hash) 719 return -ENOMEM; 720 721 batadv_dat_start_timer(bat_priv); 722 723 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1, 724 NULL, BATADV_TVLV_DAT, 1, 725 BATADV_TVLV_HANDLER_OGM_CIFNOTFND); 726 batadv_dat_tvlv_container_update(bat_priv); 727 return 0; 728} 729 730/** 731 * batadv_dat_free - free the DAT internals 732 * @bat_priv: the bat priv with all the soft interface information 733 */ 734void batadv_dat_free(struct batadv_priv *bat_priv) 735{ 736 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1); 737 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1); 738 739 cancel_delayed_work_sync(&bat_priv->dat.work); 740 741 batadv_dat_hash_free(bat_priv); 742} 743 744/** 745 * batadv_dat_cache_seq_print_text - print the local DAT hash table 746 * @seq: seq file to print on 747 * @offset: not used 748 */ 749int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset) 750{ 751 struct net_device *net_dev = (struct net_device *)seq->private; 752 struct batadv_priv *bat_priv = netdev_priv(net_dev); 753 struct batadv_hashtable *hash = bat_priv->dat.hash; 754 struct batadv_dat_entry *dat_entry; 755 struct batadv_hard_iface *primary_if; 756 struct hlist_head *head; 757 unsigned long last_seen_jiffies; 758 int last_seen_msecs, last_seen_secs, last_seen_mins; 759 uint32_t i; 760 761 primary_if = batadv_seq_print_text_primary_if_get(seq); 762 if (!primary_if) 763 goto out; 764 765 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name); 766 seq_printf(seq, " %-7s %-9s %4s %11s\n", "IPv4", 767 "MAC", "VID", "last-seen"); 768 769 for (i = 0; i < hash->size; i++) { 770 head = &hash->table[i]; 771 772 rcu_read_lock(); 773 hlist_for_each_entry_rcu(dat_entry, head, hash_entry) { 774 last_seen_jiffies = jiffies - dat_entry->last_update; 775 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); 776 last_seen_mins = last_seen_msecs / 60000; 777 last_seen_msecs = last_seen_msecs % 60000; 778 last_seen_secs = last_seen_msecs / 1000; 779 780 seq_printf(seq, " * %15pI4 %14pM %4i %6i:%02i\n", 781 &dat_entry->ip, dat_entry->mac_addr, 782 BATADV_PRINT_VID(dat_entry->vid), 783 last_seen_mins, last_seen_secs); 784 } 785 rcu_read_unlock(); 786 } 787 788out: 789 if (primary_if) 790 batadv_hardif_free_ref(primary_if); 791 return 0; 792} 793 794/** 795 * batadv_arp_get_type - parse an ARP packet and gets the type 796 * @bat_priv: the bat priv with all the soft interface information 797 * @skb: packet to analyse 798 * @hdr_size: size of the possible header before the ARP packet in the skb 799 * 800 * Returns the ARP type if the skb contains a valid ARP packet, 0 otherwise. 801 */ 802static uint16_t batadv_arp_get_type(struct batadv_priv *bat_priv, 803 struct sk_buff *skb, int hdr_size) 804{ 805 struct arphdr *arphdr; 806 struct ethhdr *ethhdr; 807 __be32 ip_src, ip_dst; 808 uint8_t *hw_src, *hw_dst; 809 uint16_t type = 0; 810 811 /* pull the ethernet header */ 812 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN))) 813 goto out; 814 815 ethhdr = (struct ethhdr *)(skb->data + hdr_size); 816 817 if (ethhdr->h_proto != htons(ETH_P_ARP)) 818 goto out; 819 820 /* pull the ARP payload */ 821 if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN + 822 arp_hdr_len(skb->dev)))) 823 goto out; 824 825 arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN); 826 827 /* check whether the ARP packet carries a valid IP information */ 828 if (arphdr->ar_hrd != htons(ARPHRD_ETHER)) 829 goto out; 830 831 if (arphdr->ar_pro != htons(ETH_P_IP)) 832 goto out; 833 834 if (arphdr->ar_hln != ETH_ALEN) 835 goto out; 836 837 if (arphdr->ar_pln != 4) 838 goto out; 839 840 /* Check for bad reply/request. If the ARP message is not sane, DAT 841 * will simply ignore it 842 */ 843 ip_src = batadv_arp_ip_src(skb, hdr_size); 844 ip_dst = batadv_arp_ip_dst(skb, hdr_size); 845 if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) || 846 ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) || 847 ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) || 848 ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst)) 849 goto out; 850 851 hw_src = batadv_arp_hw_src(skb, hdr_size); 852 if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src)) 853 goto out; 854 855 /* don't care about the destination MAC address in ARP requests */ 856 if (arphdr->ar_op != htons(ARPOP_REQUEST)) { 857 hw_dst = batadv_arp_hw_dst(skb, hdr_size); 858 if (is_zero_ether_addr(hw_dst) || 859 is_multicast_ether_addr(hw_dst)) 860 goto out; 861 } 862 863 type = ntohs(arphdr->ar_op); 864out: 865 return type; 866} 867 868/** 869 * batadv_dat_get_vid - extract the VLAN identifier from skb if any 870 * @skb: the buffer containing the packet to extract the VID from 871 * @hdr_size: the size of the batman-adv header encapsulating the packet 872 * 873 * If the packet embedded in the skb is vlan tagged this function returns the 874 * VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS is returned. 875 */ 876static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size) 877{ 878 unsigned short vid; 879 880 vid = batadv_get_vid(skb, *hdr_size); 881 882 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN. 883 * If the header contained in the packet is a VLAN one (which is longer) 884 * hdr_size is updated so that the functions will still skip the 885 * correct amount of bytes. 886 */ 887 if (vid & BATADV_VLAN_HAS_TAG) 888 *hdr_size += VLAN_HLEN; 889 890 return vid; 891} 892 893/** 894 * batadv_dat_snoop_outgoing_arp_request - snoop the ARP request and try to 895 * answer using DAT 896 * @bat_priv: the bat priv with all the soft interface information 897 * @skb: packet to check 898 * 899 * Returns true if the message has been sent to the dht candidates, false 900 * otherwise. In case of a positive return value the message has to be enqueued 901 * to permit the fallback. 902 */ 903bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv, 904 struct sk_buff *skb) 905{ 906 uint16_t type = 0; 907 __be32 ip_dst, ip_src; 908 uint8_t *hw_src; 909 bool ret = false; 910 struct batadv_dat_entry *dat_entry = NULL; 911 struct sk_buff *skb_new; 912 int hdr_size = 0; 913 unsigned short vid; 914 915 if (!atomic_read(&bat_priv->distributed_arp_table)) 916 goto out; 917 918 vid = batadv_dat_get_vid(skb, &hdr_size); 919 920 type = batadv_arp_get_type(bat_priv, skb, hdr_size); 921 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast 922 * message to the selected DHT candidates 923 */ 924 if (type != ARPOP_REQUEST) 925 goto out; 926 927 batadv_dbg_arp(bat_priv, skb, type, hdr_size, 928 "Parsing outgoing ARP REQUEST"); 929 930 ip_src = batadv_arp_ip_src(skb, hdr_size); 931 hw_src = batadv_arp_hw_src(skb, hdr_size); 932 ip_dst = batadv_arp_ip_dst(skb, hdr_size); 933 934 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid); 935 936 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid); 937 if (dat_entry) { 938 /* If the ARP request is destined for a local client the local 939 * client will answer itself. DAT would only generate a 940 * duplicate packet. 941 * 942 * Moreover, if the soft-interface is enslaved into a bridge, an 943 * additional DAT answer may trigger kernel warnings about 944 * a packet coming from the wrong port. 945 */ 946 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) { 947 ret = true; 948 goto out; 949 } 950 951 skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src, 952 bat_priv->soft_iface, ip_dst, hw_src, 953 dat_entry->mac_addr, hw_src); 954 if (!skb_new) 955 goto out; 956 957 if (vid & BATADV_VLAN_HAS_TAG) 958 skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q), 959 vid & VLAN_VID_MASK); 960 961 skb_reset_mac_header(skb_new); 962 skb_new->protocol = eth_type_trans(skb_new, 963 bat_priv->soft_iface); 964 bat_priv->stats.rx_packets++; 965 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN + hdr_size; 966 bat_priv->soft_iface->last_rx = jiffies; 967 968 netif_rx(skb_new); 969 batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n"); 970 ret = true; 971 } else { 972 /* Send the request to the DHT */ 973 ret = batadv_dat_send_data(bat_priv, skb, ip_dst, 974 BATADV_P_DAT_DHT_GET); 975 } 976out: 977 if (dat_entry) 978 batadv_dat_entry_free_ref(dat_entry); 979 return ret; 980} 981 982/** 983 * batadv_dat_snoop_incoming_arp_request - snoop the ARP request and try to 984 * answer using the local DAT storage 985 * @bat_priv: the bat priv with all the soft interface information 986 * @skb: packet to check 987 * @hdr_size: size of the encapsulation header 988 * 989 * Returns true if the request has been answered, false otherwise. 990 */ 991bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv, 992 struct sk_buff *skb, int hdr_size) 993{ 994 uint16_t type; 995 __be32 ip_src, ip_dst; 996 uint8_t *hw_src; 997 struct sk_buff *skb_new; 998 struct batadv_dat_entry *dat_entry = NULL; 999 bool ret = false; 1000 unsigned short vid; 1001 int err; 1002 1003 if (!atomic_read(&bat_priv->distributed_arp_table)) 1004 goto out; 1005 1006 vid = batadv_dat_get_vid(skb, &hdr_size); 1007 1008 type = batadv_arp_get_type(bat_priv, skb, hdr_size); 1009 if (type != ARPOP_REQUEST) 1010 goto out; 1011 1012 hw_src = batadv_arp_hw_src(skb, hdr_size); 1013 ip_src = batadv_arp_ip_src(skb, hdr_size); 1014 ip_dst = batadv_arp_ip_dst(skb, hdr_size); 1015 1016 batadv_dbg_arp(bat_priv, skb, type, hdr_size, 1017 "Parsing incoming ARP REQUEST"); 1018 1019 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid); 1020 1021 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid); 1022 if (!dat_entry) 1023 goto out; 1024 1025 skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src, 1026 bat_priv->soft_iface, ip_dst, hw_src, 1027 dat_entry->mac_addr, hw_src); 1028 1029 if (!skb_new) 1030 goto out; 1031 1032 /* the rest of the TX path assumes that the mac_header offset pointing 1033 * to the inner Ethernet header has been set, therefore reset it now. 1034 */ 1035 skb_reset_mac_header(skb_new); 1036 1037 if (vid & BATADV_VLAN_HAS_TAG) 1038 skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q), 1039 vid & VLAN_VID_MASK); 1040 1041 /* To preserve backwards compatibility, the node has choose the outgoing 1042 * format based on the incoming request packet type. The assumption is 1043 * that a node not using the 4addr packet format doesn't support it. 1044 */ 1045 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet)) 1046 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new, 1047 BATADV_P_DAT_CACHE_REPLY, 1048 NULL, vid); 1049 else 1050 err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid); 1051 1052 if (err != NET_XMIT_DROP) { 1053 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX); 1054 ret = true; 1055 } 1056out: 1057 if (dat_entry) 1058 batadv_dat_entry_free_ref(dat_entry); 1059 if (ret) 1060 kfree_skb(skb); 1061 return ret; 1062} 1063 1064/** 1065 * batadv_dat_snoop_outgoing_arp_reply - snoop the ARP reply and fill the DHT 1066 * @bat_priv: the bat priv with all the soft interface information 1067 * @skb: packet to check 1068 */ 1069void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv, 1070 struct sk_buff *skb) 1071{ 1072 uint16_t type; 1073 __be32 ip_src, ip_dst; 1074 uint8_t *hw_src, *hw_dst; 1075 int hdr_size = 0; 1076 unsigned short vid; 1077 1078 if (!atomic_read(&bat_priv->distributed_arp_table)) 1079 return; 1080 1081 vid = batadv_dat_get_vid(skb, &hdr_size); 1082 1083 type = batadv_arp_get_type(bat_priv, skb, hdr_size); 1084 if (type != ARPOP_REPLY) 1085 return; 1086 1087 batadv_dbg_arp(bat_priv, skb, type, hdr_size, 1088 "Parsing outgoing ARP REPLY"); 1089 1090 hw_src = batadv_arp_hw_src(skb, hdr_size); 1091 ip_src = batadv_arp_ip_src(skb, hdr_size); 1092 hw_dst = batadv_arp_hw_dst(skb, hdr_size); 1093 ip_dst = batadv_arp_ip_dst(skb, hdr_size); 1094 1095 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid); 1096 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid); 1097 1098 /* Send the ARP reply to the candidates for both the IP addresses that 1099 * the node obtained from the ARP reply 1100 */ 1101 batadv_dat_send_data(bat_priv, skb, ip_src, BATADV_P_DAT_DHT_PUT); 1102 batadv_dat_send_data(bat_priv, skb, ip_dst, BATADV_P_DAT_DHT_PUT); 1103} 1104 1105/** 1106 * batadv_dat_snoop_incoming_arp_reply - snoop the ARP reply and fill the local 1107 * DAT storage only 1108 * @bat_priv: the bat priv with all the soft interface information 1109 * @skb: packet to check 1110 * @hdr_size: size of the encapsulation header 1111 */ 1112bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv, 1113 struct sk_buff *skb, int hdr_size) 1114{ 1115 uint16_t type; 1116 __be32 ip_src, ip_dst; 1117 uint8_t *hw_src, *hw_dst; 1118 bool ret = false; 1119 unsigned short vid; 1120 1121 if (!atomic_read(&bat_priv->distributed_arp_table)) 1122 goto out; 1123 1124 vid = batadv_dat_get_vid(skb, &hdr_size); 1125 1126 type = batadv_arp_get_type(bat_priv, skb, hdr_size); 1127 if (type != ARPOP_REPLY) 1128 goto out; 1129 1130 batadv_dbg_arp(bat_priv, skb, type, hdr_size, 1131 "Parsing incoming ARP REPLY"); 1132 1133 hw_src = batadv_arp_hw_src(skb, hdr_size); 1134 ip_src = batadv_arp_ip_src(skb, hdr_size); 1135 hw_dst = batadv_arp_hw_dst(skb, hdr_size); 1136 ip_dst = batadv_arp_ip_dst(skb, hdr_size); 1137 1138 /* Update our internal cache with both the IP addresses the node got 1139 * within the ARP reply 1140 */ 1141 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid); 1142 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid); 1143 1144 /* if this REPLY is directed to a client of mine, let's deliver the 1145 * packet to the interface 1146 */ 1147 ret = !batadv_is_my_client(bat_priv, hw_dst, vid); 1148out: 1149 if (ret) 1150 kfree_skb(skb); 1151 /* if ret == false -> packet has to be delivered to the interface */ 1152 return ret; 1153} 1154 1155/** 1156 * batadv_dat_drop_broadcast_packet - check if an ARP request has to be dropped 1157 * (because the node has already obtained the reply via DAT) or not 1158 * @bat_priv: the bat priv with all the soft interface information 1159 * @forw_packet: the broadcast packet 1160 * 1161 * Returns true if the node can drop the packet, false otherwise. 1162 */ 1163bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv, 1164 struct batadv_forw_packet *forw_packet) 1165{ 1166 uint16_t type; 1167 __be32 ip_dst; 1168 struct batadv_dat_entry *dat_entry = NULL; 1169 bool ret = false; 1170 int hdr_size = sizeof(struct batadv_bcast_packet); 1171 unsigned short vid; 1172 1173 if (!atomic_read(&bat_priv->distributed_arp_table)) 1174 goto out; 1175 1176 /* If this packet is an ARP_REQUEST and the node already has the 1177 * information that it is going to ask, then the packet can be dropped 1178 */ 1179 if (forw_packet->num_packets) 1180 goto out; 1181 1182 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size); 1183 1184 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size); 1185 if (type != ARPOP_REQUEST) 1186 goto out; 1187 1188 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size); 1189 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid); 1190 /* check if the node already got this entry */ 1191 if (!dat_entry) { 1192 batadv_dbg(BATADV_DBG_DAT, bat_priv, 1193 "ARP Request for %pI4: fallback\n", &ip_dst); 1194 goto out; 1195 } 1196 1197 batadv_dbg(BATADV_DBG_DAT, bat_priv, 1198 "ARP Request for %pI4: fallback prevented\n", &ip_dst); 1199 ret = true; 1200 1201out: 1202 if (dat_entry) 1203 batadv_dat_entry_free_ref(dat_entry); 1204 return ret; 1205} 1206