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