1 /* Copyright (C) 2011-2015 B.A.T.M.A.N. contributors: 2 * 3 * Simon Wunderlich 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 "bridge_loop_avoidance.h" 19 #include "main.h" 20 21 #include <linux/atomic.h> 22 #include <linux/byteorder/generic.h> 23 #include <linux/compiler.h> 24 #include <linux/crc16.h> 25 #include <linux/errno.h> 26 #include <linux/etherdevice.h> 27 #include <linux/fs.h> 28 #include <linux/if_arp.h> 29 #include <linux/if_ether.h> 30 #include <linux/if_vlan.h> 31 #include <linux/jhash.h> 32 #include <linux/jiffies.h> 33 #include <linux/kernel.h> 34 #include <linux/list.h> 35 #include <linux/lockdep.h> 36 #include <linux/netdevice.h> 37 #include <linux/rculist.h> 38 #include <linux/rcupdate.h> 39 #include <linux/seq_file.h> 40 #include <linux/skbuff.h> 41 #include <linux/slab.h> 42 #include <linux/spinlock.h> 43 #include <linux/stddef.h> 44 #include <linux/string.h> 45 #include <linux/workqueue.h> 46 #include <net/arp.h> 47 48 #include "hard-interface.h" 49 #include "hash.h" 50 #include "originator.h" 51 #include "packet.h" 52 #include "translation-table.h" 53 54 static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05}; 55 56 static void batadv_bla_periodic_work(struct work_struct *work); 57 static void 58 batadv_bla_send_announce(struct batadv_priv *bat_priv, 59 struct batadv_bla_backbone_gw *backbone_gw); 60 61 /* return the index of the claim */ 62 static inline u32 batadv_choose_claim(const void *data, u32 size) 63 { 64 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data; 65 u32 hash = 0; 66 67 hash = jhash(&claim->addr, sizeof(claim->addr), hash); 68 hash = jhash(&claim->vid, sizeof(claim->vid), hash); 69 70 return hash % size; 71 } 72 73 /* return the index of the backbone gateway */ 74 static inline u32 batadv_choose_backbone_gw(const void *data, u32 size) 75 { 76 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data; 77 u32 hash = 0; 78 79 hash = jhash(&claim->addr, sizeof(claim->addr), hash); 80 hash = jhash(&claim->vid, sizeof(claim->vid), hash); 81 82 return hash % size; 83 } 84 85 /* compares address and vid of two backbone gws */ 86 static int batadv_compare_backbone_gw(const struct hlist_node *node, 87 const void *data2) 88 { 89 const void *data1 = container_of(node, struct batadv_bla_backbone_gw, 90 hash_entry); 91 const struct batadv_bla_backbone_gw *gw1 = data1; 92 const struct batadv_bla_backbone_gw *gw2 = data2; 93 94 if (!batadv_compare_eth(gw1->orig, gw2->orig)) 95 return 0; 96 97 if (gw1->vid != gw2->vid) 98 return 0; 99 100 return 1; 101 } 102 103 /* compares address and vid of two claims */ 104 static int batadv_compare_claim(const struct hlist_node *node, 105 const void *data2) 106 { 107 const void *data1 = container_of(node, struct batadv_bla_claim, 108 hash_entry); 109 const struct batadv_bla_claim *cl1 = data1; 110 const struct batadv_bla_claim *cl2 = data2; 111 112 if (!batadv_compare_eth(cl1->addr, cl2->addr)) 113 return 0; 114 115 if (cl1->vid != cl2->vid) 116 return 0; 117 118 return 1; 119 } 120 121 /* free a backbone gw */ 122 static void 123 batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw) 124 { 125 if (atomic_dec_and_test(&backbone_gw->refcount)) 126 kfree_rcu(backbone_gw, rcu); 127 } 128 129 /* finally deinitialize the claim */ 130 static void batadv_claim_free_rcu(struct rcu_head *rcu) 131 { 132 struct batadv_bla_claim *claim; 133 134 claim = container_of(rcu, struct batadv_bla_claim, rcu); 135 136 batadv_backbone_gw_free_ref(claim->backbone_gw); 137 kfree(claim); 138 } 139 140 /* free a claim, call claim_free_rcu if its the last reference */ 141 static void batadv_claim_free_ref(struct batadv_bla_claim *claim) 142 { 143 if (atomic_dec_and_test(&claim->refcount)) 144 call_rcu(&claim->rcu, batadv_claim_free_rcu); 145 } 146 147 /** 148 * batadv_claim_hash_find 149 * @bat_priv: the bat priv with all the soft interface information 150 * @data: search data (may be local/static data) 151 * 152 * looks for a claim in the hash, and returns it if found 153 * or NULL otherwise. 154 */ 155 static struct batadv_bla_claim 156 *batadv_claim_hash_find(struct batadv_priv *bat_priv, 157 struct batadv_bla_claim *data) 158 { 159 struct batadv_hashtable *hash = bat_priv->bla.claim_hash; 160 struct hlist_head *head; 161 struct batadv_bla_claim *claim; 162 struct batadv_bla_claim *claim_tmp = NULL; 163 int index; 164 165 if (!hash) 166 return NULL; 167 168 index = batadv_choose_claim(data, hash->size); 169 head = &hash->table[index]; 170 171 rcu_read_lock(); 172 hlist_for_each_entry_rcu(claim, head, hash_entry) { 173 if (!batadv_compare_claim(&claim->hash_entry, data)) 174 continue; 175 176 if (!atomic_inc_not_zero(&claim->refcount)) 177 continue; 178 179 claim_tmp = claim; 180 break; 181 } 182 rcu_read_unlock(); 183 184 return claim_tmp; 185 } 186 187 /** 188 * batadv_backbone_hash_find - looks for a claim in the hash 189 * @bat_priv: the bat priv with all the soft interface information 190 * @addr: the address of the originator 191 * @vid: the VLAN ID 192 * 193 * Returns claim if found or NULL otherwise. 194 */ 195 static struct batadv_bla_backbone_gw * 196 batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr, 197 unsigned short vid) 198 { 199 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash; 200 struct hlist_head *head; 201 struct batadv_bla_backbone_gw search_entry, *backbone_gw; 202 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL; 203 int index; 204 205 if (!hash) 206 return NULL; 207 208 ether_addr_copy(search_entry.orig, addr); 209 search_entry.vid = vid; 210 211 index = batadv_choose_backbone_gw(&search_entry, hash->size); 212 head = &hash->table[index]; 213 214 rcu_read_lock(); 215 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 216 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry, 217 &search_entry)) 218 continue; 219 220 if (!atomic_inc_not_zero(&backbone_gw->refcount)) 221 continue; 222 223 backbone_gw_tmp = backbone_gw; 224 break; 225 } 226 rcu_read_unlock(); 227 228 return backbone_gw_tmp; 229 } 230 231 /* delete all claims for a backbone */ 232 static void 233 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw) 234 { 235 struct batadv_hashtable *hash; 236 struct hlist_node *node_tmp; 237 struct hlist_head *head; 238 struct batadv_bla_claim *claim; 239 int i; 240 spinlock_t *list_lock; /* protects write access to the hash lists */ 241 242 hash = backbone_gw->bat_priv->bla.claim_hash; 243 if (!hash) 244 return; 245 246 for (i = 0; i < hash->size; i++) { 247 head = &hash->table[i]; 248 list_lock = &hash->list_locks[i]; 249 250 spin_lock_bh(list_lock); 251 hlist_for_each_entry_safe(claim, node_tmp, 252 head, hash_entry) { 253 if (claim->backbone_gw != backbone_gw) 254 continue; 255 256 batadv_claim_free_ref(claim); 257 hlist_del_rcu(&claim->hash_entry); 258 } 259 spin_unlock_bh(list_lock); 260 } 261 262 /* all claims gone, initialize CRC */ 263 spin_lock_bh(&backbone_gw->crc_lock); 264 backbone_gw->crc = BATADV_BLA_CRC_INIT; 265 spin_unlock_bh(&backbone_gw->crc_lock); 266 } 267 268 /** 269 * batadv_bla_send_claim - sends a claim frame according to the provided info 270 * @bat_priv: the bat priv with all the soft interface information 271 * @mac: the mac address to be announced within the claim 272 * @vid: the VLAN ID 273 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...) 274 */ 275 static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac, 276 unsigned short vid, int claimtype) 277 { 278 struct sk_buff *skb; 279 struct ethhdr *ethhdr; 280 struct batadv_hard_iface *primary_if; 281 struct net_device *soft_iface; 282 u8 *hw_src; 283 struct batadv_bla_claim_dst local_claim_dest; 284 __be32 zeroip = 0; 285 286 primary_if = batadv_primary_if_get_selected(bat_priv); 287 if (!primary_if) 288 return; 289 290 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest, 291 sizeof(local_claim_dest)); 292 local_claim_dest.type = claimtype; 293 294 soft_iface = primary_if->soft_iface; 295 296 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, 297 /* IP DST: 0.0.0.0 */ 298 zeroip, 299 primary_if->soft_iface, 300 /* IP SRC: 0.0.0.0 */ 301 zeroip, 302 /* Ethernet DST: Broadcast */ 303 NULL, 304 /* Ethernet SRC/HW SRC: originator mac */ 305 primary_if->net_dev->dev_addr, 306 /* HW DST: FF:43:05:XX:YY:YY 307 * with XX = claim type 308 * and YY:YY = group id 309 */ 310 (u8 *)&local_claim_dest); 311 312 if (!skb) 313 goto out; 314 315 ethhdr = (struct ethhdr *)skb->data; 316 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr); 317 318 /* now we pretend that the client would have sent this ... */ 319 switch (claimtype) { 320 case BATADV_CLAIM_TYPE_CLAIM: 321 /* normal claim frame 322 * set Ethernet SRC to the clients mac 323 */ 324 ether_addr_copy(ethhdr->h_source, mac); 325 batadv_dbg(BATADV_DBG_BLA, bat_priv, 326 "bla_send_claim(): CLAIM %pM on vid %d\n", mac, 327 BATADV_PRINT_VID(vid)); 328 break; 329 case BATADV_CLAIM_TYPE_UNCLAIM: 330 /* unclaim frame 331 * set HW SRC to the clients mac 332 */ 333 ether_addr_copy(hw_src, mac); 334 batadv_dbg(BATADV_DBG_BLA, bat_priv, 335 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac, 336 BATADV_PRINT_VID(vid)); 337 break; 338 case BATADV_CLAIM_TYPE_ANNOUNCE: 339 /* announcement frame 340 * set HW SRC to the special mac containg the crc 341 */ 342 ether_addr_copy(hw_src, mac); 343 batadv_dbg(BATADV_DBG_BLA, bat_priv, 344 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n", 345 ethhdr->h_source, BATADV_PRINT_VID(vid)); 346 break; 347 case BATADV_CLAIM_TYPE_REQUEST: 348 /* request frame 349 * set HW SRC and header destination to the receiving backbone 350 * gws mac 351 */ 352 ether_addr_copy(hw_src, mac); 353 ether_addr_copy(ethhdr->h_dest, mac); 354 batadv_dbg(BATADV_DBG_BLA, bat_priv, 355 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n", 356 ethhdr->h_source, ethhdr->h_dest, 357 BATADV_PRINT_VID(vid)); 358 break; 359 } 360 361 if (vid & BATADV_VLAN_HAS_TAG) 362 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q), 363 vid & VLAN_VID_MASK); 364 365 skb_reset_mac_header(skb); 366 skb->protocol = eth_type_trans(skb, soft_iface); 367 batadv_inc_counter(bat_priv, BATADV_CNT_RX); 368 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES, 369 skb->len + ETH_HLEN); 370 soft_iface->last_rx = jiffies; 371 372 netif_rx(skb); 373 out: 374 if (primary_if) 375 batadv_hardif_free_ref(primary_if); 376 } 377 378 /** 379 * batadv_bla_get_backbone_gw 380 * @bat_priv: the bat priv with all the soft interface information 381 * @orig: the mac address of the originator 382 * @vid: the VLAN ID 383 * @own_backbone: set if the requested backbone is local 384 * 385 * searches for the backbone gw or creates a new one if it could not 386 * be found. 387 */ 388 static struct batadv_bla_backbone_gw * 389 batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig, 390 unsigned short vid, bool own_backbone) 391 { 392 struct batadv_bla_backbone_gw *entry; 393 struct batadv_orig_node *orig_node; 394 int hash_added; 395 396 entry = batadv_backbone_hash_find(bat_priv, orig, vid); 397 398 if (entry) 399 return entry; 400 401 batadv_dbg(BATADV_DBG_BLA, bat_priv, 402 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n", 403 orig, BATADV_PRINT_VID(vid)); 404 405 entry = kzalloc(sizeof(*entry), GFP_ATOMIC); 406 if (!entry) 407 return NULL; 408 409 entry->vid = vid; 410 entry->lasttime = jiffies; 411 entry->crc = BATADV_BLA_CRC_INIT; 412 entry->bat_priv = bat_priv; 413 spin_lock_init(&entry->crc_lock); 414 atomic_set(&entry->request_sent, 0); 415 atomic_set(&entry->wait_periods, 0); 416 ether_addr_copy(entry->orig, orig); 417 418 /* one for the hash, one for returning */ 419 atomic_set(&entry->refcount, 2); 420 421 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash, 422 batadv_compare_backbone_gw, 423 batadv_choose_backbone_gw, entry, 424 &entry->hash_entry); 425 426 if (unlikely(hash_added != 0)) { 427 /* hash failed, free the structure */ 428 kfree(entry); 429 return NULL; 430 } 431 432 /* this is a gateway now, remove any TT entry on this VLAN */ 433 orig_node = batadv_orig_hash_find(bat_priv, orig); 434 if (orig_node) { 435 batadv_tt_global_del_orig(bat_priv, orig_node, vid, 436 "became a backbone gateway"); 437 batadv_orig_node_free_ref(orig_node); 438 } 439 440 if (own_backbone) { 441 batadv_bla_send_announce(bat_priv, entry); 442 443 /* this will be decreased in the worker thread */ 444 atomic_inc(&entry->request_sent); 445 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS); 446 atomic_inc(&bat_priv->bla.num_requests); 447 } 448 449 return entry; 450 } 451 452 /* update or add the own backbone gw to make sure we announce 453 * where we receive other backbone gws 454 */ 455 static void 456 batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv, 457 struct batadv_hard_iface *primary_if, 458 unsigned short vid) 459 { 460 struct batadv_bla_backbone_gw *backbone_gw; 461 462 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, 463 primary_if->net_dev->dev_addr, 464 vid, true); 465 if (unlikely(!backbone_gw)) 466 return; 467 468 backbone_gw->lasttime = jiffies; 469 batadv_backbone_gw_free_ref(backbone_gw); 470 } 471 472 /** 473 * batadv_bla_answer_request - answer a bla request by sending own claims 474 * @bat_priv: the bat priv with all the soft interface information 475 * @primary_if: interface where the request came on 476 * @vid: the vid where the request came on 477 * 478 * Repeat all of our own claims, and finally send an ANNOUNCE frame 479 * to allow the requester another check if the CRC is correct now. 480 */ 481 static void batadv_bla_answer_request(struct batadv_priv *bat_priv, 482 struct batadv_hard_iface *primary_if, 483 unsigned short vid) 484 { 485 struct hlist_head *head; 486 struct batadv_hashtable *hash; 487 struct batadv_bla_claim *claim; 488 struct batadv_bla_backbone_gw *backbone_gw; 489 int i; 490 491 batadv_dbg(BATADV_DBG_BLA, bat_priv, 492 "bla_answer_request(): received a claim request, send all of our own claims again\n"); 493 494 backbone_gw = batadv_backbone_hash_find(bat_priv, 495 primary_if->net_dev->dev_addr, 496 vid); 497 if (!backbone_gw) 498 return; 499 500 hash = bat_priv->bla.claim_hash; 501 for (i = 0; i < hash->size; i++) { 502 head = &hash->table[i]; 503 504 rcu_read_lock(); 505 hlist_for_each_entry_rcu(claim, head, hash_entry) { 506 /* only own claims are interesting */ 507 if (claim->backbone_gw != backbone_gw) 508 continue; 509 510 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid, 511 BATADV_CLAIM_TYPE_CLAIM); 512 } 513 rcu_read_unlock(); 514 } 515 516 /* finally, send an announcement frame */ 517 batadv_bla_send_announce(bat_priv, backbone_gw); 518 batadv_backbone_gw_free_ref(backbone_gw); 519 } 520 521 /** 522 * batadv_bla_send_request - send a request to repeat claims 523 * @backbone_gw: the backbone gateway from whom we are out of sync 524 * 525 * When the crc is wrong, ask the backbone gateway for a full table update. 526 * After the request, it will repeat all of his own claims and finally 527 * send an announcement claim with which we can check again. 528 */ 529 static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw) 530 { 531 /* first, remove all old entries */ 532 batadv_bla_del_backbone_claims(backbone_gw); 533 534 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv, 535 "Sending REQUEST to %pM\n", backbone_gw->orig); 536 537 /* send request */ 538 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig, 539 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST); 540 541 /* no local broadcasts should be sent or received, for now. */ 542 if (!atomic_read(&backbone_gw->request_sent)) { 543 atomic_inc(&backbone_gw->bat_priv->bla.num_requests); 544 atomic_set(&backbone_gw->request_sent, 1); 545 } 546 } 547 548 /** 549 * batadv_bla_send_announce 550 * @bat_priv: the bat priv with all the soft interface information 551 * @backbone_gw: our backbone gateway which should be announced 552 * 553 * This function sends an announcement. It is called from multiple 554 * places. 555 */ 556 static void batadv_bla_send_announce(struct batadv_priv *bat_priv, 557 struct batadv_bla_backbone_gw *backbone_gw) 558 { 559 u8 mac[ETH_ALEN]; 560 __be16 crc; 561 562 memcpy(mac, batadv_announce_mac, 4); 563 spin_lock_bh(&backbone_gw->crc_lock); 564 crc = htons(backbone_gw->crc); 565 spin_unlock_bh(&backbone_gw->crc_lock); 566 memcpy(&mac[4], &crc, 2); 567 568 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid, 569 BATADV_CLAIM_TYPE_ANNOUNCE); 570 } 571 572 /** 573 * batadv_bla_add_claim - Adds a claim in the claim hash 574 * @bat_priv: the bat priv with all the soft interface information 575 * @mac: the mac address of the claim 576 * @vid: the VLAN ID of the frame 577 * @backbone_gw: the backbone gateway which claims it 578 */ 579 static void batadv_bla_add_claim(struct batadv_priv *bat_priv, 580 const u8 *mac, const unsigned short vid, 581 struct batadv_bla_backbone_gw *backbone_gw) 582 { 583 struct batadv_bla_claim *claim; 584 struct batadv_bla_claim search_claim; 585 int hash_added; 586 587 ether_addr_copy(search_claim.addr, mac); 588 search_claim.vid = vid; 589 claim = batadv_claim_hash_find(bat_priv, &search_claim); 590 591 /* create a new claim entry if it does not exist yet. */ 592 if (!claim) { 593 claim = kzalloc(sizeof(*claim), GFP_ATOMIC); 594 if (!claim) 595 return; 596 597 ether_addr_copy(claim->addr, mac); 598 claim->vid = vid; 599 claim->lasttime = jiffies; 600 claim->backbone_gw = backbone_gw; 601 602 atomic_set(&claim->refcount, 2); 603 batadv_dbg(BATADV_DBG_BLA, bat_priv, 604 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n", 605 mac, BATADV_PRINT_VID(vid)); 606 hash_added = batadv_hash_add(bat_priv->bla.claim_hash, 607 batadv_compare_claim, 608 batadv_choose_claim, claim, 609 &claim->hash_entry); 610 611 if (unlikely(hash_added != 0)) { 612 /* only local changes happened. */ 613 kfree(claim); 614 return; 615 } 616 } else { 617 claim->lasttime = jiffies; 618 if (claim->backbone_gw == backbone_gw) 619 /* no need to register a new backbone */ 620 goto claim_free_ref; 621 622 batadv_dbg(BATADV_DBG_BLA, bat_priv, 623 "bla_add_claim(): changing ownership for %pM, vid %d\n", 624 mac, BATADV_PRINT_VID(vid)); 625 626 spin_lock_bh(&claim->backbone_gw->crc_lock); 627 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN); 628 spin_unlock_bh(&claim->backbone_gw->crc_lock); 629 batadv_backbone_gw_free_ref(claim->backbone_gw); 630 } 631 /* set (new) backbone gw */ 632 atomic_inc(&backbone_gw->refcount); 633 claim->backbone_gw = backbone_gw; 634 635 spin_lock_bh(&backbone_gw->crc_lock); 636 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN); 637 spin_unlock_bh(&backbone_gw->crc_lock); 638 backbone_gw->lasttime = jiffies; 639 640 claim_free_ref: 641 batadv_claim_free_ref(claim); 642 } 643 644 /* Delete a claim from the claim hash which has the 645 * given mac address and vid. 646 */ 647 static void batadv_bla_del_claim(struct batadv_priv *bat_priv, 648 const u8 *mac, const unsigned short vid) 649 { 650 struct batadv_bla_claim search_claim, *claim; 651 652 ether_addr_copy(search_claim.addr, mac); 653 search_claim.vid = vid; 654 claim = batadv_claim_hash_find(bat_priv, &search_claim); 655 if (!claim) 656 return; 657 658 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n", 659 mac, BATADV_PRINT_VID(vid)); 660 661 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim, 662 batadv_choose_claim, claim); 663 batadv_claim_free_ref(claim); /* reference from the hash is gone */ 664 665 spin_lock_bh(&claim->backbone_gw->crc_lock); 666 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN); 667 spin_unlock_bh(&claim->backbone_gw->crc_lock); 668 669 /* don't need the reference from hash_find() anymore */ 670 batadv_claim_free_ref(claim); 671 } 672 673 /* check for ANNOUNCE frame, return 1 if handled */ 674 static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr, 675 u8 *backbone_addr, unsigned short vid) 676 { 677 struct batadv_bla_backbone_gw *backbone_gw; 678 u16 backbone_crc, crc; 679 680 if (memcmp(an_addr, batadv_announce_mac, 4) != 0) 681 return 0; 682 683 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid, 684 false); 685 686 if (unlikely(!backbone_gw)) 687 return 1; 688 689 /* handle as ANNOUNCE frame */ 690 backbone_gw->lasttime = jiffies; 691 crc = ntohs(*((__be16 *)(&an_addr[4]))); 692 693 batadv_dbg(BATADV_DBG_BLA, bat_priv, 694 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n", 695 BATADV_PRINT_VID(vid), backbone_gw->orig, crc); 696 697 spin_lock_bh(&backbone_gw->crc_lock); 698 backbone_crc = backbone_gw->crc; 699 spin_unlock_bh(&backbone_gw->crc_lock); 700 701 if (backbone_crc != crc) { 702 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv, 703 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n", 704 backbone_gw->orig, 705 BATADV_PRINT_VID(backbone_gw->vid), 706 backbone_crc, crc); 707 708 batadv_bla_send_request(backbone_gw); 709 } else { 710 /* if we have sent a request and the crc was OK, 711 * we can allow traffic again. 712 */ 713 if (atomic_read(&backbone_gw->request_sent)) { 714 atomic_dec(&backbone_gw->bat_priv->bla.num_requests); 715 atomic_set(&backbone_gw->request_sent, 0); 716 } 717 } 718 719 batadv_backbone_gw_free_ref(backbone_gw); 720 return 1; 721 } 722 723 /* check for REQUEST frame, return 1 if handled */ 724 static int batadv_handle_request(struct batadv_priv *bat_priv, 725 struct batadv_hard_iface *primary_if, 726 u8 *backbone_addr, struct ethhdr *ethhdr, 727 unsigned short vid) 728 { 729 /* check for REQUEST frame */ 730 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest)) 731 return 0; 732 733 /* sanity check, this should not happen on a normal switch, 734 * we ignore it in this case. 735 */ 736 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr)) 737 return 1; 738 739 batadv_dbg(BATADV_DBG_BLA, bat_priv, 740 "handle_request(): REQUEST vid %d (sent by %pM)...\n", 741 BATADV_PRINT_VID(vid), ethhdr->h_source); 742 743 batadv_bla_answer_request(bat_priv, primary_if, vid); 744 return 1; 745 } 746 747 /* check for UNCLAIM frame, return 1 if handled */ 748 static int batadv_handle_unclaim(struct batadv_priv *bat_priv, 749 struct batadv_hard_iface *primary_if, 750 u8 *backbone_addr, u8 *claim_addr, 751 unsigned short vid) 752 { 753 struct batadv_bla_backbone_gw *backbone_gw; 754 755 /* unclaim in any case if it is our own */ 756 if (primary_if && batadv_compare_eth(backbone_addr, 757 primary_if->net_dev->dev_addr)) 758 batadv_bla_send_claim(bat_priv, claim_addr, vid, 759 BATADV_CLAIM_TYPE_UNCLAIM); 760 761 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid); 762 763 if (!backbone_gw) 764 return 1; 765 766 /* this must be an UNCLAIM frame */ 767 batadv_dbg(BATADV_DBG_BLA, bat_priv, 768 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n", 769 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig); 770 771 batadv_bla_del_claim(bat_priv, claim_addr, vid); 772 batadv_backbone_gw_free_ref(backbone_gw); 773 return 1; 774 } 775 776 /* check for CLAIM frame, return 1 if handled */ 777 static int batadv_handle_claim(struct batadv_priv *bat_priv, 778 struct batadv_hard_iface *primary_if, 779 u8 *backbone_addr, u8 *claim_addr, 780 unsigned short vid) 781 { 782 struct batadv_bla_backbone_gw *backbone_gw; 783 784 /* register the gateway if not yet available, and add the claim. */ 785 786 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid, 787 false); 788 789 if (unlikely(!backbone_gw)) 790 return 1; 791 792 /* this must be a CLAIM frame */ 793 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw); 794 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr)) 795 batadv_bla_send_claim(bat_priv, claim_addr, vid, 796 BATADV_CLAIM_TYPE_CLAIM); 797 798 /* TODO: we could call something like tt_local_del() here. */ 799 800 batadv_backbone_gw_free_ref(backbone_gw); 801 return 1; 802 } 803 804 /** 805 * batadv_check_claim_group 806 * @bat_priv: the bat priv with all the soft interface information 807 * @primary_if: the primary interface of this batman interface 808 * @hw_src: the Hardware source in the ARP Header 809 * @hw_dst: the Hardware destination in the ARP Header 810 * @ethhdr: pointer to the Ethernet header of the claim frame 811 * 812 * checks if it is a claim packet and if its on the same group. 813 * This function also applies the group ID of the sender 814 * if it is in the same mesh. 815 * 816 * returns: 817 * 2 - if it is a claim packet and on the same group 818 * 1 - if is a claim packet from another group 819 * 0 - if it is not a claim packet 820 */ 821 static int batadv_check_claim_group(struct batadv_priv *bat_priv, 822 struct batadv_hard_iface *primary_if, 823 u8 *hw_src, u8 *hw_dst, 824 struct ethhdr *ethhdr) 825 { 826 u8 *backbone_addr; 827 struct batadv_orig_node *orig_node; 828 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own; 829 830 bla_dst = (struct batadv_bla_claim_dst *)hw_dst; 831 bla_dst_own = &bat_priv->bla.claim_dest; 832 833 /* if announcement packet, use the source, 834 * otherwise assume it is in the hw_src 835 */ 836 switch (bla_dst->type) { 837 case BATADV_CLAIM_TYPE_CLAIM: 838 backbone_addr = hw_src; 839 break; 840 case BATADV_CLAIM_TYPE_REQUEST: 841 case BATADV_CLAIM_TYPE_ANNOUNCE: 842 case BATADV_CLAIM_TYPE_UNCLAIM: 843 backbone_addr = ethhdr->h_source; 844 break; 845 default: 846 return 0; 847 } 848 849 /* don't accept claim frames from ourselves */ 850 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr)) 851 return 0; 852 853 /* if its already the same group, it is fine. */ 854 if (bla_dst->group == bla_dst_own->group) 855 return 2; 856 857 /* lets see if this originator is in our mesh */ 858 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr); 859 860 /* dont accept claims from gateways which are not in 861 * the same mesh or group. 862 */ 863 if (!orig_node) 864 return 1; 865 866 /* if our mesh friends mac is bigger, use it for ourselves. */ 867 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) { 868 batadv_dbg(BATADV_DBG_BLA, bat_priv, 869 "taking other backbones claim group: %#.4x\n", 870 ntohs(bla_dst->group)); 871 bla_dst_own->group = bla_dst->group; 872 } 873 874 batadv_orig_node_free_ref(orig_node); 875 876 return 2; 877 } 878 879 /** 880 * batadv_bla_process_claim 881 * @bat_priv: the bat priv with all the soft interface information 882 * @primary_if: the primary hard interface of this batman soft interface 883 * @skb: the frame to be checked 884 * 885 * Check if this is a claim frame, and process it accordingly. 886 * 887 * returns 1 if it was a claim frame, otherwise return 0 to 888 * tell the callee that it can use the frame on its own. 889 */ 890 static int batadv_bla_process_claim(struct batadv_priv *bat_priv, 891 struct batadv_hard_iface *primary_if, 892 struct sk_buff *skb) 893 { 894 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own; 895 u8 *hw_src, *hw_dst; 896 struct vlan_hdr *vhdr, vhdr_buf; 897 struct ethhdr *ethhdr; 898 struct arphdr *arphdr; 899 unsigned short vid; 900 int vlan_depth = 0; 901 __be16 proto; 902 int headlen; 903 int ret; 904 905 vid = batadv_get_vid(skb, 0); 906 ethhdr = eth_hdr(skb); 907 908 proto = ethhdr->h_proto; 909 headlen = ETH_HLEN; 910 if (vid & BATADV_VLAN_HAS_TAG) { 911 /* Traverse the VLAN/Ethertypes. 912 * 913 * At this point it is known that the first protocol is a VLAN 914 * header, so start checking at the encapsulated protocol. 915 * 916 * The depth of the VLAN headers is recorded to drop BLA claim 917 * frames encapsulated into multiple VLAN headers (QinQ). 918 */ 919 do { 920 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN, 921 &vhdr_buf); 922 if (!vhdr) 923 return 0; 924 925 proto = vhdr->h_vlan_encapsulated_proto; 926 headlen += VLAN_HLEN; 927 vlan_depth++; 928 } while (proto == htons(ETH_P_8021Q)); 929 } 930 931 if (proto != htons(ETH_P_ARP)) 932 return 0; /* not a claim frame */ 933 934 /* this must be a ARP frame. check if it is a claim. */ 935 936 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev)))) 937 return 0; 938 939 /* pskb_may_pull() may have modified the pointers, get ethhdr again */ 940 ethhdr = eth_hdr(skb); 941 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen); 942 943 /* Check whether the ARP frame carries a valid 944 * IP information 945 */ 946 if (arphdr->ar_hrd != htons(ARPHRD_ETHER)) 947 return 0; 948 if (arphdr->ar_pro != htons(ETH_P_IP)) 949 return 0; 950 if (arphdr->ar_hln != ETH_ALEN) 951 return 0; 952 if (arphdr->ar_pln != 4) 953 return 0; 954 955 hw_src = (u8 *)arphdr + sizeof(struct arphdr); 956 hw_dst = hw_src + ETH_ALEN + 4; 957 bla_dst = (struct batadv_bla_claim_dst *)hw_dst; 958 bla_dst_own = &bat_priv->bla.claim_dest; 959 960 /* check if it is a claim frame in general */ 961 if (memcmp(bla_dst->magic, bla_dst_own->magic, 962 sizeof(bla_dst->magic)) != 0) 963 return 0; 964 965 /* check if there is a claim frame encapsulated deeper in (QinQ) and 966 * drop that, as this is not supported by BLA but should also not be 967 * sent via the mesh. 968 */ 969 if (vlan_depth > 1) 970 return 1; 971 972 /* check if it is a claim frame. */ 973 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst, 974 ethhdr); 975 if (ret == 1) 976 batadv_dbg(BATADV_DBG_BLA, bat_priv, 977 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n", 978 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, 979 hw_dst); 980 981 if (ret < 2) 982 return ret; 983 984 /* become a backbone gw ourselves on this vlan if not happened yet */ 985 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid); 986 987 /* check for the different types of claim frames ... */ 988 switch (bla_dst->type) { 989 case BATADV_CLAIM_TYPE_CLAIM: 990 if (batadv_handle_claim(bat_priv, primary_if, hw_src, 991 ethhdr->h_source, vid)) 992 return 1; 993 break; 994 case BATADV_CLAIM_TYPE_UNCLAIM: 995 if (batadv_handle_unclaim(bat_priv, primary_if, 996 ethhdr->h_source, hw_src, vid)) 997 return 1; 998 break; 999 1000 case BATADV_CLAIM_TYPE_ANNOUNCE: 1001 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source, 1002 vid)) 1003 return 1; 1004 break; 1005 case BATADV_CLAIM_TYPE_REQUEST: 1006 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr, 1007 vid)) 1008 return 1; 1009 break; 1010 } 1011 1012 batadv_dbg(BATADV_DBG_BLA, bat_priv, 1013 "bla_process_claim(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n", 1014 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst); 1015 return 1; 1016 } 1017 1018 /* Check when we last heard from other nodes, and remove them in case of 1019 * a time out, or clean all backbone gws if now is set. 1020 */ 1021 static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now) 1022 { 1023 struct batadv_bla_backbone_gw *backbone_gw; 1024 struct hlist_node *node_tmp; 1025 struct hlist_head *head; 1026 struct batadv_hashtable *hash; 1027 spinlock_t *list_lock; /* protects write access to the hash lists */ 1028 int i; 1029 1030 hash = bat_priv->bla.backbone_hash; 1031 if (!hash) 1032 return; 1033 1034 for (i = 0; i < hash->size; i++) { 1035 head = &hash->table[i]; 1036 list_lock = &hash->list_locks[i]; 1037 1038 spin_lock_bh(list_lock); 1039 hlist_for_each_entry_safe(backbone_gw, node_tmp, 1040 head, hash_entry) { 1041 if (now) 1042 goto purge_now; 1043 if (!batadv_has_timed_out(backbone_gw->lasttime, 1044 BATADV_BLA_BACKBONE_TIMEOUT)) 1045 continue; 1046 1047 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv, 1048 "bla_purge_backbone_gw(): backbone gw %pM timed out\n", 1049 backbone_gw->orig); 1050 1051 purge_now: 1052 /* don't wait for the pending request anymore */ 1053 if (atomic_read(&backbone_gw->request_sent)) 1054 atomic_dec(&bat_priv->bla.num_requests); 1055 1056 batadv_bla_del_backbone_claims(backbone_gw); 1057 1058 hlist_del_rcu(&backbone_gw->hash_entry); 1059 batadv_backbone_gw_free_ref(backbone_gw); 1060 } 1061 spin_unlock_bh(list_lock); 1062 } 1063 } 1064 1065 /** 1066 * batadv_bla_purge_claims 1067 * @bat_priv: the bat priv with all the soft interface information 1068 * @primary_if: the selected primary interface, may be NULL if now is set 1069 * @now: whether the whole hash shall be wiped now 1070 * 1071 * Check when we heard last time from our own claims, and remove them in case of 1072 * a time out, or clean all claims if now is set 1073 */ 1074 static void batadv_bla_purge_claims(struct batadv_priv *bat_priv, 1075 struct batadv_hard_iface *primary_if, 1076 int now) 1077 { 1078 struct batadv_bla_claim *claim; 1079 struct hlist_head *head; 1080 struct batadv_hashtable *hash; 1081 int i; 1082 1083 hash = bat_priv->bla.claim_hash; 1084 if (!hash) 1085 return; 1086 1087 for (i = 0; i < hash->size; i++) { 1088 head = &hash->table[i]; 1089 1090 rcu_read_lock(); 1091 hlist_for_each_entry_rcu(claim, head, hash_entry) { 1092 if (now) 1093 goto purge_now; 1094 if (!batadv_compare_eth(claim->backbone_gw->orig, 1095 primary_if->net_dev->dev_addr)) 1096 continue; 1097 if (!batadv_has_timed_out(claim->lasttime, 1098 BATADV_BLA_CLAIM_TIMEOUT)) 1099 continue; 1100 1101 batadv_dbg(BATADV_DBG_BLA, bat_priv, 1102 "bla_purge_claims(): %pM, vid %d, time out\n", 1103 claim->addr, claim->vid); 1104 1105 purge_now: 1106 batadv_handle_unclaim(bat_priv, primary_if, 1107 claim->backbone_gw->orig, 1108 claim->addr, claim->vid); 1109 } 1110 rcu_read_unlock(); 1111 } 1112 } 1113 1114 /** 1115 * batadv_bla_update_orig_address 1116 * @bat_priv: the bat priv with all the soft interface information 1117 * @primary_if: the new selected primary_if 1118 * @oldif: the old primary interface, may be NULL 1119 * 1120 * Update the backbone gateways when the own orig address changes. 1121 */ 1122 void batadv_bla_update_orig_address(struct batadv_priv *bat_priv, 1123 struct batadv_hard_iface *primary_if, 1124 struct batadv_hard_iface *oldif) 1125 { 1126 struct batadv_bla_backbone_gw *backbone_gw; 1127 struct hlist_head *head; 1128 struct batadv_hashtable *hash; 1129 __be16 group; 1130 int i; 1131 1132 /* reset bridge loop avoidance group id */ 1133 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN)); 1134 bat_priv->bla.claim_dest.group = group; 1135 1136 /* purge everything when bridge loop avoidance is turned off */ 1137 if (!atomic_read(&bat_priv->bridge_loop_avoidance)) 1138 oldif = NULL; 1139 1140 if (!oldif) { 1141 batadv_bla_purge_claims(bat_priv, NULL, 1); 1142 batadv_bla_purge_backbone_gw(bat_priv, 1); 1143 return; 1144 } 1145 1146 hash = bat_priv->bla.backbone_hash; 1147 if (!hash) 1148 return; 1149 1150 for (i = 0; i < hash->size; i++) { 1151 head = &hash->table[i]; 1152 1153 rcu_read_lock(); 1154 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 1155 /* own orig still holds the old value. */ 1156 if (!batadv_compare_eth(backbone_gw->orig, 1157 oldif->net_dev->dev_addr)) 1158 continue; 1159 1160 ether_addr_copy(backbone_gw->orig, 1161 primary_if->net_dev->dev_addr); 1162 /* send an announce frame so others will ask for our 1163 * claims and update their tables. 1164 */ 1165 batadv_bla_send_announce(bat_priv, backbone_gw); 1166 } 1167 rcu_read_unlock(); 1168 } 1169 } 1170 1171 /* periodic work to do: 1172 * * purge structures when they are too old 1173 * * send announcements 1174 */ 1175 static void batadv_bla_periodic_work(struct work_struct *work) 1176 { 1177 struct delayed_work *delayed_work; 1178 struct batadv_priv *bat_priv; 1179 struct batadv_priv_bla *priv_bla; 1180 struct hlist_head *head; 1181 struct batadv_bla_backbone_gw *backbone_gw; 1182 struct batadv_hashtable *hash; 1183 struct batadv_hard_iface *primary_if; 1184 int i; 1185 1186 delayed_work = container_of(work, struct delayed_work, work); 1187 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work); 1188 bat_priv = container_of(priv_bla, struct batadv_priv, bla); 1189 primary_if = batadv_primary_if_get_selected(bat_priv); 1190 if (!primary_if) 1191 goto out; 1192 1193 batadv_bla_purge_claims(bat_priv, primary_if, 0); 1194 batadv_bla_purge_backbone_gw(bat_priv, 0); 1195 1196 if (!atomic_read(&bat_priv->bridge_loop_avoidance)) 1197 goto out; 1198 1199 hash = bat_priv->bla.backbone_hash; 1200 if (!hash) 1201 goto out; 1202 1203 for (i = 0; i < hash->size; i++) { 1204 head = &hash->table[i]; 1205 1206 rcu_read_lock(); 1207 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 1208 if (!batadv_compare_eth(backbone_gw->orig, 1209 primary_if->net_dev->dev_addr)) 1210 continue; 1211 1212 backbone_gw->lasttime = jiffies; 1213 1214 batadv_bla_send_announce(bat_priv, backbone_gw); 1215 1216 /* request_sent is only set after creation to avoid 1217 * problems when we are not yet known as backbone gw 1218 * in the backbone. 1219 * 1220 * We can reset this now after we waited some periods 1221 * to give bridge forward delays and bla group forming 1222 * some grace time. 1223 */ 1224 1225 if (atomic_read(&backbone_gw->request_sent) == 0) 1226 continue; 1227 1228 if (!atomic_dec_and_test(&backbone_gw->wait_periods)) 1229 continue; 1230 1231 atomic_dec(&backbone_gw->bat_priv->bla.num_requests); 1232 atomic_set(&backbone_gw->request_sent, 0); 1233 } 1234 rcu_read_unlock(); 1235 } 1236 out: 1237 if (primary_if) 1238 batadv_hardif_free_ref(primary_if); 1239 1240 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work, 1241 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH)); 1242 } 1243 1244 /* The hash for claim and backbone hash receive the same key because they 1245 * are getting initialized by hash_new with the same key. Reinitializing 1246 * them with to different keys to allow nested locking without generating 1247 * lockdep warnings 1248 */ 1249 static struct lock_class_key batadv_claim_hash_lock_class_key; 1250 static struct lock_class_key batadv_backbone_hash_lock_class_key; 1251 1252 /* initialize all bla structures */ 1253 int batadv_bla_init(struct batadv_priv *bat_priv) 1254 { 1255 int i; 1256 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00}; 1257 struct batadv_hard_iface *primary_if; 1258 u16 crc; 1259 unsigned long entrytime; 1260 1261 spin_lock_init(&bat_priv->bla.bcast_duplist_lock); 1262 1263 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n"); 1264 1265 /* setting claim destination address */ 1266 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3); 1267 bat_priv->bla.claim_dest.type = 0; 1268 primary_if = batadv_primary_if_get_selected(bat_priv); 1269 if (primary_if) { 1270 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN); 1271 bat_priv->bla.claim_dest.group = htons(crc); 1272 batadv_hardif_free_ref(primary_if); 1273 } else { 1274 bat_priv->bla.claim_dest.group = 0; /* will be set later */ 1275 } 1276 1277 /* initialize the duplicate list */ 1278 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT); 1279 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) 1280 bat_priv->bla.bcast_duplist[i].entrytime = entrytime; 1281 bat_priv->bla.bcast_duplist_curr = 0; 1282 1283 if (bat_priv->bla.claim_hash) 1284 return 0; 1285 1286 bat_priv->bla.claim_hash = batadv_hash_new(128); 1287 bat_priv->bla.backbone_hash = batadv_hash_new(32); 1288 1289 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash) 1290 return -ENOMEM; 1291 1292 batadv_hash_set_lock_class(bat_priv->bla.claim_hash, 1293 &batadv_claim_hash_lock_class_key); 1294 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash, 1295 &batadv_backbone_hash_lock_class_key); 1296 1297 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n"); 1298 1299 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work); 1300 1301 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work, 1302 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH)); 1303 return 0; 1304 } 1305 1306 /** 1307 * batadv_bla_check_bcast_duplist 1308 * @bat_priv: the bat priv with all the soft interface information 1309 * @skb: contains the bcast_packet to be checked 1310 * 1311 * check if it is on our broadcast list. Another gateway might 1312 * have sent the same packet because it is connected to the same backbone, 1313 * so we have to remove this duplicate. 1314 * 1315 * This is performed by checking the CRC, which will tell us 1316 * with a good chance that it is the same packet. If it is furthermore 1317 * sent by another host, drop it. We allow equal packets from 1318 * the same host however as this might be intended. 1319 */ 1320 int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv, 1321 struct sk_buff *skb) 1322 { 1323 int i, curr, ret = 0; 1324 __be32 crc; 1325 struct batadv_bcast_packet *bcast_packet; 1326 struct batadv_bcast_duplist_entry *entry; 1327 1328 bcast_packet = (struct batadv_bcast_packet *)skb->data; 1329 1330 /* calculate the crc ... */ 1331 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1)); 1332 1333 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock); 1334 1335 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) { 1336 curr = (bat_priv->bla.bcast_duplist_curr + i); 1337 curr %= BATADV_DUPLIST_SIZE; 1338 entry = &bat_priv->bla.bcast_duplist[curr]; 1339 1340 /* we can stop searching if the entry is too old ; 1341 * later entries will be even older 1342 */ 1343 if (batadv_has_timed_out(entry->entrytime, 1344 BATADV_DUPLIST_TIMEOUT)) 1345 break; 1346 1347 if (entry->crc != crc) 1348 continue; 1349 1350 if (batadv_compare_eth(entry->orig, bcast_packet->orig)) 1351 continue; 1352 1353 /* this entry seems to match: same crc, not too old, 1354 * and from another gw. therefore return 1 to forbid it. 1355 */ 1356 ret = 1; 1357 goto out; 1358 } 1359 /* not found, add a new entry (overwrite the oldest entry) 1360 * and allow it, its the first occurrence. 1361 */ 1362 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1); 1363 curr %= BATADV_DUPLIST_SIZE; 1364 entry = &bat_priv->bla.bcast_duplist[curr]; 1365 entry->crc = crc; 1366 entry->entrytime = jiffies; 1367 ether_addr_copy(entry->orig, bcast_packet->orig); 1368 bat_priv->bla.bcast_duplist_curr = curr; 1369 1370 out: 1371 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock); 1372 1373 return ret; 1374 } 1375 1376 /** 1377 * batadv_bla_is_backbone_gw_orig 1378 * @bat_priv: the bat priv with all the soft interface information 1379 * @orig: originator mac address 1380 * @vid: VLAN identifier 1381 * 1382 * Check if the originator is a gateway for the VLAN identified by vid. 1383 * 1384 * Returns true if orig is a backbone for this vid, false otherwise. 1385 */ 1386 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig, 1387 unsigned short vid) 1388 { 1389 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash; 1390 struct hlist_head *head; 1391 struct batadv_bla_backbone_gw *backbone_gw; 1392 int i; 1393 1394 if (!atomic_read(&bat_priv->bridge_loop_avoidance)) 1395 return false; 1396 1397 if (!hash) 1398 return false; 1399 1400 for (i = 0; i < hash->size; i++) { 1401 head = &hash->table[i]; 1402 1403 rcu_read_lock(); 1404 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 1405 if (batadv_compare_eth(backbone_gw->orig, orig) && 1406 backbone_gw->vid == vid) { 1407 rcu_read_unlock(); 1408 return true; 1409 } 1410 } 1411 rcu_read_unlock(); 1412 } 1413 1414 return false; 1415 } 1416 1417 /** 1418 * batadv_bla_is_backbone_gw 1419 * @skb: the frame to be checked 1420 * @orig_node: the orig_node of the frame 1421 * @hdr_size: maximum length of the frame 1422 * 1423 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1 1424 * if the orig_node is also a gateway on the soft interface, otherwise it 1425 * returns 0. 1426 */ 1427 int batadv_bla_is_backbone_gw(struct sk_buff *skb, 1428 struct batadv_orig_node *orig_node, int hdr_size) 1429 { 1430 struct batadv_bla_backbone_gw *backbone_gw; 1431 unsigned short vid; 1432 1433 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance)) 1434 return 0; 1435 1436 /* first, find out the vid. */ 1437 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN)) 1438 return 0; 1439 1440 vid = batadv_get_vid(skb, hdr_size); 1441 1442 /* see if this originator is a backbone gw for this VLAN */ 1443 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv, 1444 orig_node->orig, vid); 1445 if (!backbone_gw) 1446 return 0; 1447 1448 batadv_backbone_gw_free_ref(backbone_gw); 1449 return 1; 1450 } 1451 1452 /* free all bla structures (for softinterface free or module unload) */ 1453 void batadv_bla_free(struct batadv_priv *bat_priv) 1454 { 1455 struct batadv_hard_iface *primary_if; 1456 1457 cancel_delayed_work_sync(&bat_priv->bla.work); 1458 primary_if = batadv_primary_if_get_selected(bat_priv); 1459 1460 if (bat_priv->bla.claim_hash) { 1461 batadv_bla_purge_claims(bat_priv, primary_if, 1); 1462 batadv_hash_destroy(bat_priv->bla.claim_hash); 1463 bat_priv->bla.claim_hash = NULL; 1464 } 1465 if (bat_priv->bla.backbone_hash) { 1466 batadv_bla_purge_backbone_gw(bat_priv, 1); 1467 batadv_hash_destroy(bat_priv->bla.backbone_hash); 1468 bat_priv->bla.backbone_hash = NULL; 1469 } 1470 if (primary_if) 1471 batadv_hardif_free_ref(primary_if); 1472 } 1473 1474 /** 1475 * batadv_bla_rx 1476 * @bat_priv: the bat priv with all the soft interface information 1477 * @skb: the frame to be checked 1478 * @vid: the VLAN ID of the frame 1479 * @is_bcast: the packet came in a broadcast packet type. 1480 * 1481 * bla_rx avoidance checks if: 1482 * * we have to race for a claim 1483 * * if the frame is allowed on the LAN 1484 * 1485 * in these cases, the skb is further handled by this function and 1486 * returns 1, otherwise it returns 0 and the caller shall further 1487 * process the skb. 1488 */ 1489 int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb, 1490 unsigned short vid, bool is_bcast) 1491 { 1492 struct ethhdr *ethhdr; 1493 struct batadv_bla_claim search_claim, *claim = NULL; 1494 struct batadv_hard_iface *primary_if; 1495 int ret; 1496 1497 ethhdr = eth_hdr(skb); 1498 1499 primary_if = batadv_primary_if_get_selected(bat_priv); 1500 if (!primary_if) 1501 goto handled; 1502 1503 if (!atomic_read(&bat_priv->bridge_loop_avoidance)) 1504 goto allow; 1505 1506 if (unlikely(atomic_read(&bat_priv->bla.num_requests))) 1507 /* don't allow broadcasts while requests are in flight */ 1508 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) 1509 goto handled; 1510 1511 ether_addr_copy(search_claim.addr, ethhdr->h_source); 1512 search_claim.vid = vid; 1513 claim = batadv_claim_hash_find(bat_priv, &search_claim); 1514 1515 if (!claim) { 1516 /* possible optimization: race for a claim */ 1517 /* No claim exists yet, claim it for us! 1518 */ 1519 batadv_handle_claim(bat_priv, primary_if, 1520 primary_if->net_dev->dev_addr, 1521 ethhdr->h_source, vid); 1522 goto allow; 1523 } 1524 1525 /* if it is our own claim ... */ 1526 if (batadv_compare_eth(claim->backbone_gw->orig, 1527 primary_if->net_dev->dev_addr)) { 1528 /* ... allow it in any case */ 1529 claim->lasttime = jiffies; 1530 goto allow; 1531 } 1532 1533 /* if it is a broadcast ... */ 1534 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) { 1535 /* ... drop it. the responsible gateway is in charge. 1536 * 1537 * We need to check is_bcast because with the gateway 1538 * feature, broadcasts (like DHCP requests) may be sent 1539 * using a unicast packet type. 1540 */ 1541 goto handled; 1542 } else { 1543 /* seems the client considers us as its best gateway. 1544 * send a claim and update the claim table 1545 * immediately. 1546 */ 1547 batadv_handle_claim(bat_priv, primary_if, 1548 primary_if->net_dev->dev_addr, 1549 ethhdr->h_source, vid); 1550 goto allow; 1551 } 1552 allow: 1553 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid); 1554 ret = 0; 1555 goto out; 1556 1557 handled: 1558 kfree_skb(skb); 1559 ret = 1; 1560 1561 out: 1562 if (primary_if) 1563 batadv_hardif_free_ref(primary_if); 1564 if (claim) 1565 batadv_claim_free_ref(claim); 1566 return ret; 1567 } 1568 1569 /** 1570 * batadv_bla_tx 1571 * @bat_priv: the bat priv with all the soft interface information 1572 * @skb: the frame to be checked 1573 * @vid: the VLAN ID of the frame 1574 * 1575 * bla_tx checks if: 1576 * * a claim was received which has to be processed 1577 * * the frame is allowed on the mesh 1578 * 1579 * in these cases, the skb is further handled by this function and 1580 * returns 1, otherwise it returns 0 and the caller shall further 1581 * process the skb. 1582 * 1583 * This call might reallocate skb data. 1584 */ 1585 int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb, 1586 unsigned short vid) 1587 { 1588 struct ethhdr *ethhdr; 1589 struct batadv_bla_claim search_claim, *claim = NULL; 1590 struct batadv_hard_iface *primary_if; 1591 int ret = 0; 1592 1593 primary_if = batadv_primary_if_get_selected(bat_priv); 1594 if (!primary_if) 1595 goto out; 1596 1597 if (!atomic_read(&bat_priv->bridge_loop_avoidance)) 1598 goto allow; 1599 1600 if (batadv_bla_process_claim(bat_priv, primary_if, skb)) 1601 goto handled; 1602 1603 ethhdr = eth_hdr(skb); 1604 1605 if (unlikely(atomic_read(&bat_priv->bla.num_requests))) 1606 /* don't allow broadcasts while requests are in flight */ 1607 if (is_multicast_ether_addr(ethhdr->h_dest)) 1608 goto handled; 1609 1610 ether_addr_copy(search_claim.addr, ethhdr->h_source); 1611 search_claim.vid = vid; 1612 1613 claim = batadv_claim_hash_find(bat_priv, &search_claim); 1614 1615 /* if no claim exists, allow it. */ 1616 if (!claim) 1617 goto allow; 1618 1619 /* check if we are responsible. */ 1620 if (batadv_compare_eth(claim->backbone_gw->orig, 1621 primary_if->net_dev->dev_addr)) { 1622 /* if yes, the client has roamed and we have 1623 * to unclaim it. 1624 */ 1625 batadv_handle_unclaim(bat_priv, primary_if, 1626 primary_if->net_dev->dev_addr, 1627 ethhdr->h_source, vid); 1628 goto allow; 1629 } 1630 1631 /* check if it is a multicast/broadcast frame */ 1632 if (is_multicast_ether_addr(ethhdr->h_dest)) { 1633 /* drop it. the responsible gateway has forwarded it into 1634 * the backbone network. 1635 */ 1636 goto handled; 1637 } else { 1638 /* we must allow it. at least if we are 1639 * responsible for the DESTINATION. 1640 */ 1641 goto allow; 1642 } 1643 allow: 1644 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid); 1645 ret = 0; 1646 goto out; 1647 handled: 1648 ret = 1; 1649 out: 1650 if (primary_if) 1651 batadv_hardif_free_ref(primary_if); 1652 if (claim) 1653 batadv_claim_free_ref(claim); 1654 return ret; 1655 } 1656 1657 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset) 1658 { 1659 struct net_device *net_dev = (struct net_device *)seq->private; 1660 struct batadv_priv *bat_priv = netdev_priv(net_dev); 1661 struct batadv_hashtable *hash = bat_priv->bla.claim_hash; 1662 struct batadv_bla_claim *claim; 1663 struct batadv_hard_iface *primary_if; 1664 struct hlist_head *head; 1665 u16 backbone_crc; 1666 u32 i; 1667 bool is_own; 1668 u8 *primary_addr; 1669 1670 primary_if = batadv_seq_print_text_primary_if_get(seq); 1671 if (!primary_if) 1672 goto out; 1673 1674 primary_addr = primary_if->net_dev->dev_addr; 1675 seq_printf(seq, 1676 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n", 1677 net_dev->name, primary_addr, 1678 ntohs(bat_priv->bla.claim_dest.group)); 1679 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n", 1680 "Client", "VID", "Originator", "CRC"); 1681 for (i = 0; i < hash->size; i++) { 1682 head = &hash->table[i]; 1683 1684 rcu_read_lock(); 1685 hlist_for_each_entry_rcu(claim, head, hash_entry) { 1686 is_own = batadv_compare_eth(claim->backbone_gw->orig, 1687 primary_addr); 1688 1689 spin_lock_bh(&claim->backbone_gw->crc_lock); 1690 backbone_crc = claim->backbone_gw->crc; 1691 spin_unlock_bh(&claim->backbone_gw->crc_lock); 1692 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n", 1693 claim->addr, BATADV_PRINT_VID(claim->vid), 1694 claim->backbone_gw->orig, 1695 (is_own ? 'x' : ' '), 1696 backbone_crc); 1697 } 1698 rcu_read_unlock(); 1699 } 1700 out: 1701 if (primary_if) 1702 batadv_hardif_free_ref(primary_if); 1703 return 0; 1704 } 1705 1706 int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset) 1707 { 1708 struct net_device *net_dev = (struct net_device *)seq->private; 1709 struct batadv_priv *bat_priv = netdev_priv(net_dev); 1710 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash; 1711 struct batadv_bla_backbone_gw *backbone_gw; 1712 struct batadv_hard_iface *primary_if; 1713 struct hlist_head *head; 1714 int secs, msecs; 1715 u16 backbone_crc; 1716 u32 i; 1717 bool is_own; 1718 u8 *primary_addr; 1719 1720 primary_if = batadv_seq_print_text_primary_if_get(seq); 1721 if (!primary_if) 1722 goto out; 1723 1724 primary_addr = primary_if->net_dev->dev_addr; 1725 seq_printf(seq, 1726 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n", 1727 net_dev->name, primary_addr, 1728 ntohs(bat_priv->bla.claim_dest.group)); 1729 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n", 1730 "Originator", "VID", "last seen", "CRC"); 1731 for (i = 0; i < hash->size; i++) { 1732 head = &hash->table[i]; 1733 1734 rcu_read_lock(); 1735 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 1736 msecs = jiffies_to_msecs(jiffies - 1737 backbone_gw->lasttime); 1738 secs = msecs / 1000; 1739 msecs = msecs % 1000; 1740 1741 is_own = batadv_compare_eth(backbone_gw->orig, 1742 primary_addr); 1743 if (is_own) 1744 continue; 1745 1746 spin_lock_bh(&backbone_gw->crc_lock); 1747 backbone_crc = backbone_gw->crc; 1748 spin_unlock_bh(&backbone_gw->crc_lock); 1749 1750 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n", 1751 backbone_gw->orig, 1752 BATADV_PRINT_VID(backbone_gw->vid), secs, 1753 msecs, backbone_crc); 1754 } 1755 rcu_read_unlock(); 1756 } 1757 out: 1758 if (primary_if) 1759 batadv_hardif_free_ref(primary_if); 1760 return 0; 1761 } 1762