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