1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) B.A.T.M.A.N. contributors: 3 * 4 * Simon Wunderlich 5 */ 6 7 #include "bridge_loop_avoidance.h" 8 #include "main.h" 9 10 #include <linux/atomic.h> 11 #include <linux/bug.h> 12 #include <linux/byteorder/generic.h> 13 #include <linux/compiler.h> 14 #include <linux/container_of.h> 15 #include <linux/crc16.h> 16 #include <linux/err.h> 17 #include <linux/errno.h> 18 #include <linux/etherdevice.h> 19 #include <linux/gfp.h> 20 #include <linux/if_arp.h> 21 #include <linux/if_ether.h> 22 #include <linux/if_vlan.h> 23 #include <linux/jhash.h> 24 #include <linux/jiffies.h> 25 #include <linux/kref.h> 26 #include <linux/list.h> 27 #include <linux/lockdep.h> 28 #include <linux/netdevice.h> 29 #include <linux/netlink.h> 30 #include <linux/rculist.h> 31 #include <linux/rcupdate.h> 32 #include <linux/skbuff.h> 33 #include <linux/slab.h> 34 #include <linux/spinlock.h> 35 #include <linux/sprintf.h> 36 #include <linux/stddef.h> 37 #include <linux/string.h> 38 #include <linux/string_choices.h> 39 #include <linux/workqueue.h> 40 #include <net/arp.h> 41 #include <net/genetlink.h> 42 #include <net/netlink.h> 43 #include <uapi/linux/batadv_packet.h> 44 #include <uapi/linux/batman_adv.h> 45 46 #include "hard-interface.h" 47 #include "hash.h" 48 #include "log.h" 49 #include "netlink.h" 50 #include "originator.h" 51 #include "translation-table.h" 52 53 static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05}; 54 55 static void batadv_bla_periodic_work(struct work_struct *work); 56 static void 57 batadv_bla_send_announce(struct batadv_priv *bat_priv, 58 struct batadv_bla_backbone_gw *backbone_gw); 59 60 /** 61 * batadv_choose_claim() - choose the right bucket for a claim. 62 * @data: data to hash 63 * @size: size of the hash table 64 * 65 * Return: the hash index of the claim 66 */ 67 static inline u32 batadv_choose_claim(const void *data, u32 size) 68 { 69 const struct batadv_bla_claim *claim = data; 70 u32 hash = 0; 71 72 hash = jhash(&claim->addr, sizeof(claim->addr), hash); 73 hash = jhash(&claim->vid, sizeof(claim->vid), hash); 74 75 return hash % size; 76 } 77 78 /** 79 * batadv_choose_backbone_gw() - choose the right bucket for a backbone gateway. 80 * @data: data to hash 81 * @size: size of the hash table 82 * 83 * Return: the hash index of the backbone gateway 84 */ 85 static inline u32 batadv_choose_backbone_gw(const void *data, u32 size) 86 { 87 const struct batadv_bla_backbone_gw *gw; 88 u32 hash = 0; 89 90 gw = data; 91 hash = jhash(&gw->orig, sizeof(gw->orig), hash); 92 hash = jhash(&gw->vid, sizeof(gw->vid), hash); 93 94 return hash % size; 95 } 96 97 /** 98 * batadv_compare_backbone_gw() - compare address and vid of two backbone gws 99 * @node: list node of the first entry to compare 100 * @data2: pointer to the second backbone gateway 101 * 102 * Return: true if the backbones have the same data, false otherwise 103 */ 104 static bool batadv_compare_backbone_gw(const struct hlist_node *node, 105 const void *data2) 106 { 107 const void *data1 = container_of(node, struct batadv_bla_backbone_gw, 108 hash_entry); 109 const struct batadv_bla_backbone_gw *gw1 = data1; 110 const struct batadv_bla_backbone_gw *gw2 = data2; 111 112 if (!batadv_compare_eth(gw1->orig, gw2->orig)) 113 return false; 114 115 if (gw1->vid != gw2->vid) 116 return false; 117 118 return true; 119 } 120 121 /** 122 * batadv_compare_claim() - compare address and vid of two claims 123 * @node: list node of the first entry to compare 124 * @data2: pointer to the second claims 125 * 126 * Return: true if the claims have the same data, false otherwise 127 */ 128 static bool batadv_compare_claim(const struct hlist_node *node, 129 const void *data2) 130 { 131 const void *data1 = container_of(node, struct batadv_bla_claim, 132 hash_entry); 133 const struct batadv_bla_claim *cl1 = data1; 134 const struct batadv_bla_claim *cl2 = data2; 135 136 if (!batadv_compare_eth(cl1->addr, cl2->addr)) 137 return false; 138 139 if (cl1->vid != cl2->vid) 140 return false; 141 142 return true; 143 } 144 145 /** 146 * batadv_backbone_gw_release() - release backbone gw from lists and queue for 147 * free after rcu grace period 148 * @ref: kref pointer of the backbone gw 149 */ 150 static void batadv_backbone_gw_release(struct kref *ref) 151 { 152 struct batadv_bla_backbone_gw *backbone_gw; 153 154 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw, 155 refcount); 156 157 kfree_rcu(backbone_gw, rcu); 158 } 159 160 /** 161 * batadv_backbone_gw_put() - decrement the backbone gw refcounter and possibly 162 * release it 163 * @backbone_gw: backbone gateway to be free'd 164 */ 165 static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw) 166 { 167 if (!backbone_gw) 168 return; 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 175 * rcu grace period 176 * @ref: kref pointer of the claim 177 */ 178 static void batadv_claim_release(struct kref *ref) 179 { 180 struct batadv_bla_backbone_gw *old_backbone_gw; 181 struct batadv_bla_claim *claim; 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 release it 201 * @claim: claim to be free'd 202 */ 203 static void batadv_claim_put(struct batadv_bla_claim *claim) 204 { 205 if (!claim) 206 return; 207 208 kref_put(&claim->refcount, batadv_claim_release); 209 } 210 211 /** 212 * batadv_claim_hash_find() - looks for a claim in the claim hash 213 * @bat_priv: the bat priv with all the mesh interface information 214 * @data: search data (may be local/static data) 215 * 216 * Return: claim if found or NULL otherwise. 217 */ 218 static struct batadv_bla_claim * 219 batadv_claim_hash_find(struct batadv_priv *bat_priv, 220 struct batadv_bla_claim *data) 221 { 222 struct batadv_hashtable *hash = bat_priv->bla.claim_hash; 223 struct batadv_bla_claim *claim_tmp = NULL; 224 struct batadv_bla_claim *claim; 225 struct hlist_head *head; 226 int index; 227 228 if (!hash) 229 return NULL; 230 231 index = batadv_choose_claim(data, hash->size); 232 head = &hash->table[index]; 233 234 rcu_read_lock(); 235 hlist_for_each_entry_rcu(claim, head, hash_entry) { 236 if (!batadv_compare_claim(&claim->hash_entry, data)) 237 continue; 238 239 if (!kref_get_unless_zero(&claim->refcount)) 240 continue; 241 242 claim_tmp = claim; 243 break; 244 } 245 rcu_read_unlock(); 246 247 return claim_tmp; 248 } 249 250 /** 251 * batadv_backbone_hash_find() - looks for a backbone gateway in the hash 252 * @bat_priv: the bat priv with all the mesh interface information 253 * @addr: the address of the originator 254 * @vid: the VLAN ID 255 * 256 * Return: backbone gateway if found or NULL otherwise 257 */ 258 static struct batadv_bla_backbone_gw * 259 batadv_backbone_hash_find(struct batadv_priv *bat_priv, const u8 *addr, 260 unsigned short vid) 261 { 262 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash; 263 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL; 264 struct batadv_bla_backbone_gw search_entry; 265 struct batadv_bla_backbone_gw *backbone_gw; 266 struct hlist_head *head; 267 int index; 268 269 if (!hash) 270 return NULL; 271 272 ether_addr_copy(search_entry.orig, addr); 273 search_entry.vid = vid; 274 275 index = batadv_choose_backbone_gw(&search_entry, hash->size); 276 head = &hash->table[index]; 277 278 rcu_read_lock(); 279 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 280 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry, 281 &search_entry)) 282 continue; 283 284 if (!kref_get_unless_zero(&backbone_gw->refcount)) 285 continue; 286 287 backbone_gw_tmp = backbone_gw; 288 break; 289 } 290 rcu_read_unlock(); 291 292 return backbone_gw_tmp; 293 } 294 295 /** 296 * batadv_bla_del_backbone_claims() - delete all claims for a backbone 297 * @backbone_gw: backbone gateway where the claims should be removed 298 */ 299 static void 300 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw) 301 { 302 spinlock_t *list_lock; /* protects write access to the hash lists */ 303 struct batadv_bla_claim *claim; 304 struct batadv_hashtable *hash; 305 struct hlist_node *node_tmp; 306 struct hlist_head *head; 307 int i; 308 309 hash = backbone_gw->bat_priv->bla.claim_hash; 310 if (!hash) 311 return; 312 313 for (i = 0; i < hash->size; i++) { 314 head = &hash->table[i]; 315 list_lock = &hash->list_locks[i]; 316 317 spin_lock_bh(list_lock); 318 hlist_for_each_entry_safe(claim, node_tmp, 319 head, hash_entry) { 320 if (claim->backbone_gw != backbone_gw) 321 continue; 322 323 hlist_del_rcu(&claim->hash_entry); 324 batadv_claim_put(claim); 325 } 326 spin_unlock_bh(list_lock); 327 } 328 } 329 330 /** 331 * batadv_bla_send_claim() - sends a claim frame according to the provided info 332 * @bat_priv: the bat priv with all the mesh interface information 333 * @mac: the mac address to be announced within the claim 334 * @vid: the VLAN ID 335 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...) 336 */ 337 static void batadv_bla_send_claim(struct batadv_priv *bat_priv, const u8 *mac, 338 unsigned short vid, int claimtype) 339 { 340 struct batadv_bla_claim_dst local_claim_dest; 341 struct batadv_hard_iface *primary_if; 342 struct ethhdr *ethhdr; 343 struct sk_buff *skb; 344 __be32 zeroip = 0; 345 u8 *hw_src; 346 347 primary_if = batadv_primary_if_get_selected(bat_priv); 348 if (!primary_if) 349 return; 350 351 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest, 352 sizeof(local_claim_dest)); 353 local_claim_dest.type = claimtype; 354 355 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, 356 /* IP DST: 0.0.0.0 */ 357 zeroip, 358 primary_if->mesh_iface, 359 /* IP SRC: 0.0.0.0 */ 360 zeroip, 361 /* Ethernet DST: Broadcast */ 362 NULL, 363 /* Ethernet SRC/HW SRC: originator mac */ 364 primary_if->net_dev->dev_addr, 365 /* HW DST: FF:43:05:XX:YY:YY 366 * with XX = claim type 367 * and YY:YY = group id 368 */ 369 (u8 *)&local_claim_dest); 370 371 if (!skb) 372 goto out; 373 374 ethhdr = (struct ethhdr *)skb->data; 375 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr); 376 377 /* now we pretend that the client would have sent this ... */ 378 switch (claimtype) { 379 case BATADV_CLAIM_TYPE_CLAIM: 380 /* normal claim frame 381 * set Ethernet SRC to the clients mac 382 */ 383 ether_addr_copy(ethhdr->h_source, mac); 384 batadv_dbg(BATADV_DBG_BLA, bat_priv, 385 "%s(): CLAIM %pM on vid %d\n", __func__, mac, 386 batadv_print_vid(vid)); 387 break; 388 case BATADV_CLAIM_TYPE_UNCLAIM: 389 /* unclaim frame 390 * set HW SRC to the clients mac 391 */ 392 ether_addr_copy(hw_src, mac); 393 batadv_dbg(BATADV_DBG_BLA, bat_priv, 394 "%s(): UNCLAIM %pM on vid %d\n", __func__, mac, 395 batadv_print_vid(vid)); 396 break; 397 case BATADV_CLAIM_TYPE_ANNOUNCE: 398 /* announcement frame 399 * set HW SRC to the special mac containing the crc 400 */ 401 ether_addr_copy(hw_src, mac); 402 batadv_dbg(BATADV_DBG_BLA, bat_priv, 403 "%s(): ANNOUNCE of %pM on vid %d\n", __func__, 404 ethhdr->h_source, batadv_print_vid(vid)); 405 break; 406 case BATADV_CLAIM_TYPE_REQUEST: 407 /* request frame 408 * set HW SRC and header destination to the receiving backbone 409 * gws mac 410 */ 411 ether_addr_copy(hw_src, mac); 412 ether_addr_copy(ethhdr->h_dest, mac); 413 batadv_dbg(BATADV_DBG_BLA, bat_priv, 414 "%s(): REQUEST of %pM to %pM on vid %d\n", __func__, 415 ethhdr->h_source, ethhdr->h_dest, 416 batadv_print_vid(vid)); 417 break; 418 case BATADV_CLAIM_TYPE_LOOPDETECT: 419 ether_addr_copy(ethhdr->h_source, mac); 420 batadv_dbg(BATADV_DBG_BLA, bat_priv, 421 "%s(): LOOPDETECT of %pM to %pM on vid %d\n", 422 __func__, ethhdr->h_source, ethhdr->h_dest, 423 batadv_print_vid(vid)); 424 425 break; 426 } 427 428 if (vid & BATADV_VLAN_HAS_TAG) { 429 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q), 430 vid & VLAN_VID_MASK); 431 if (!skb) 432 goto out; 433 } 434 435 skb_reset_mac_header(skb); 436 skb->protocol = eth_type_trans(skb, primary_if->mesh_iface); 437 batadv_inc_counter(bat_priv, BATADV_CNT_RX); 438 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES, 439 skb->len + ETH_HLEN); 440 441 netif_rx(skb); 442 out: 443 batadv_hardif_put(primary_if); 444 } 445 446 /** 447 * batadv_bla_loopdetect_report() - worker for reporting the loop 448 * @work: work queue item 449 * 450 * Throws an uevent, as the loopdetect check function can't do that itself 451 * since the kernel may sleep while throwing uevents. 452 */ 453 static void batadv_bla_loopdetect_report(struct work_struct *work) 454 { 455 struct batadv_bla_backbone_gw *backbone_gw; 456 struct batadv_priv *bat_priv; 457 char vid_str[6] = { '\0' }; 458 459 backbone_gw = container_of(work, struct batadv_bla_backbone_gw, 460 report_work); 461 bat_priv = backbone_gw->bat_priv; 462 463 batadv_info(bat_priv->mesh_iface, 464 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n", 465 batadv_print_vid(backbone_gw->vid)); 466 snprintf(vid_str, sizeof(vid_str), "%d", 467 batadv_print_vid(backbone_gw->vid)); 468 vid_str[sizeof(vid_str) - 1] = 0; 469 470 batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT, 471 vid_str); 472 473 batadv_backbone_gw_put(backbone_gw); 474 } 475 476 /** 477 * batadv_bla_get_backbone_gw() - finds or creates a backbone gateway 478 * @bat_priv: the bat priv with all the mesh interface information 479 * @orig: the mac address of the originator 480 * @vid: the VLAN ID 481 * @own_backbone: set if the requested backbone is local 482 * 483 * Return: the (possibly created) backbone gateway or NULL on error 484 */ 485 static struct batadv_bla_backbone_gw * 486 batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, const u8 *orig, 487 unsigned short vid, bool own_backbone) 488 { 489 struct batadv_bla_backbone_gw *entry; 490 struct batadv_orig_node *orig_node; 491 int hash_added; 492 493 entry = batadv_backbone_hash_find(bat_priv, orig, vid); 494 495 if (entry) 496 return entry; 497 498 batadv_dbg(BATADV_DBG_BLA, bat_priv, 499 "%s(): not found (%pM, %d), creating new entry\n", __func__, 500 orig, batadv_print_vid(vid)); 501 502 entry = kzalloc_obj(*entry, GFP_ATOMIC); 503 if (!entry) 504 return NULL; 505 506 entry->vid = vid; 507 WRITE_ONCE(entry->lasttime, jiffies); 508 entry->crc = BATADV_BLA_CRC_INIT; 509 entry->bat_priv = bat_priv; 510 spin_lock_init(&entry->crc_lock); 511 entry->state = BATADV_BLA_BACKBONE_GW_SYNCED; 512 entry->wait_periods = 0; 513 ether_addr_copy(entry->orig, orig); 514 INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report); 515 kref_init(&entry->refcount); 516 517 kref_get(&entry->refcount); 518 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash, 519 batadv_compare_backbone_gw, 520 batadv_choose_backbone_gw, entry, 521 &entry->hash_entry); 522 523 if (unlikely(hash_added != 0)) { 524 /* hash failed, free the structure */ 525 kfree(entry); 526 return NULL; 527 } 528 529 /* this is a gateway now, remove any TT entry on this VLAN */ 530 orig_node = batadv_orig_hash_find(bat_priv, orig); 531 if (orig_node) { 532 batadv_tt_global_del_orig(bat_priv, orig_node, vid, 533 "became a backbone gateway"); 534 batadv_orig_node_put(orig_node); 535 } 536 537 if (own_backbone) { 538 batadv_bla_send_announce(bat_priv, entry); 539 540 /* this will be decreased in the worker thread */ 541 spin_lock_bh(&bat_priv->bla.num_requests_lock); 542 if (entry->state == BATADV_BLA_BACKBONE_GW_SYNCED) { 543 entry->state = BATADV_BLA_BACKBONE_GW_UNSYNCED; 544 entry->wait_periods = BATADV_BLA_WAIT_PERIODS; 545 atomic_inc(&bat_priv->bla.num_requests); 546 } 547 spin_unlock_bh(&bat_priv->bla.num_requests_lock); 548 } 549 550 return entry; 551 } 552 553 /** 554 * batadv_bla_update_own_backbone_gw() - updates the own backbone gw for a VLAN 555 * @bat_priv: the bat priv with all the mesh interface information 556 * @primary_if: the selected primary interface 557 * @vid: VLAN identifier 558 * 559 * update or add the own backbone gw to make sure we announce 560 * where we receive other backbone gws 561 */ 562 static void 563 batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv, 564 struct batadv_hard_iface *primary_if, 565 unsigned short vid) 566 { 567 struct batadv_bla_backbone_gw *backbone_gw; 568 569 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, 570 primary_if->net_dev->dev_addr, 571 vid, true); 572 if (unlikely(!backbone_gw)) 573 return; 574 575 WRITE_ONCE(backbone_gw->lasttime, jiffies); 576 batadv_backbone_gw_put(backbone_gw); 577 } 578 579 /** 580 * batadv_bla_answer_request() - answer a bla request by sending own claims 581 * @bat_priv: the bat priv with all the mesh interface information 582 * @primary_if: interface where the request came on 583 * @vid: the vid where the request came on 584 * 585 * Repeat all of our own claims, and finally send an ANNOUNCE frame 586 * to allow the requester another check if the CRC is correct now. 587 */ 588 static void batadv_bla_answer_request(struct batadv_priv *bat_priv, 589 struct batadv_hard_iface *primary_if, 590 unsigned short vid) 591 { 592 struct batadv_bla_backbone_gw *backbone_gw; 593 struct batadv_bla_claim *claim; 594 struct batadv_hashtable *hash; 595 struct hlist_head *head; 596 int i; 597 598 batadv_dbg(BATADV_DBG_BLA, bat_priv, 599 "%s(): received a claim request, send all of our own claims again\n", 600 __func__); 601 602 backbone_gw = batadv_backbone_hash_find(bat_priv, 603 primary_if->net_dev->dev_addr, 604 vid); 605 if (!backbone_gw) 606 return; 607 608 hash = bat_priv->bla.claim_hash; 609 for (i = 0; i < hash->size; i++) { 610 head = &hash->table[i]; 611 612 rcu_read_lock(); 613 hlist_for_each_entry_rcu(claim, head, hash_entry) { 614 /* only own claims are interesting */ 615 if (claim->backbone_gw != backbone_gw) 616 continue; 617 618 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid, 619 BATADV_CLAIM_TYPE_CLAIM); 620 } 621 rcu_read_unlock(); 622 } 623 624 /* finally, send an announcement frame */ 625 batadv_bla_send_announce(bat_priv, backbone_gw); 626 batadv_backbone_gw_put(backbone_gw); 627 } 628 629 /** 630 * batadv_bla_send_request() - send a request to repeat claims 631 * @backbone_gw: the backbone gateway from whom we are out of sync 632 * 633 * When the crc is wrong, ask the backbone gateway for a full table update. 634 * After the request, it will repeat all of his own claims and finally 635 * send an announcement claim with which we can check again. 636 */ 637 static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw) 638 { 639 /* first, remove all old entries */ 640 batadv_bla_del_backbone_claims(backbone_gw); 641 642 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv, 643 "Sending REQUEST to %pM\n", backbone_gw->orig); 644 645 /* send request */ 646 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig, 647 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST); 648 649 /* no local broadcasts should be sent or received, for now. */ 650 spin_lock_bh(&backbone_gw->bat_priv->bla.num_requests_lock); 651 if (backbone_gw->state == BATADV_BLA_BACKBONE_GW_SYNCED) { 652 backbone_gw->state = BATADV_BLA_BACKBONE_GW_UNSYNCED; 653 atomic_inc(&backbone_gw->bat_priv->bla.num_requests); 654 } 655 spin_unlock_bh(&backbone_gw->bat_priv->bla.num_requests_lock); 656 } 657 658 /** 659 * batadv_bla_send_announce() - Send an announcement frame 660 * @bat_priv: the bat priv with all the mesh interface information 661 * @backbone_gw: our backbone gateway which should be announced 662 */ 663 static void batadv_bla_send_announce(struct batadv_priv *bat_priv, 664 struct batadv_bla_backbone_gw *backbone_gw) 665 { 666 u8 mac[ETH_ALEN]; 667 __be16 crc; 668 669 memcpy(mac, batadv_announce_mac, 4); 670 spin_lock_bh(&backbone_gw->crc_lock); 671 crc = htons(backbone_gw->crc); 672 spin_unlock_bh(&backbone_gw->crc_lock); 673 memcpy(&mac[4], &crc, 2); 674 675 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid, 676 BATADV_CLAIM_TYPE_ANNOUNCE); 677 } 678 679 /** 680 * batadv_bla_add_claim() - Adds a claim in the claim hash 681 * @bat_priv: the bat priv with all the mesh interface information 682 * @mac: the mac address of the claim 683 * @vid: the VLAN ID of the frame 684 * @backbone_gw: the backbone gateway which claims it 685 */ 686 static void batadv_bla_add_claim(struct batadv_priv *bat_priv, 687 const u8 *mac, const unsigned short vid, 688 struct batadv_bla_backbone_gw *backbone_gw) 689 { 690 struct batadv_bla_backbone_gw *old_backbone_gw; 691 struct batadv_bla_claim search_claim; 692 struct batadv_bla_claim *claim; 693 int hash_added; 694 u16 claim_crc; 695 bool changed; 696 697 ether_addr_copy(search_claim.addr, mac); 698 search_claim.vid = vid; 699 claim = batadv_claim_hash_find(bat_priv, &search_claim); 700 claim_crc = crc16(0, mac, ETH_ALEN); 701 702 /* create a new claim entry if it does not exist yet. */ 703 if (!claim) { 704 claim = kzalloc_obj(*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 WRITE_ONCE(claim->lasttime, jiffies); 712 kref_get(&backbone_gw->refcount); 713 claim->backbone_gw = backbone_gw; 714 kref_init(&claim->refcount); 715 716 batadv_dbg(BATADV_DBG_BLA, bat_priv, 717 "%s(): adding new entry %pM, vid %d to hash ...\n", 718 __func__, mac, batadv_print_vid(vid)); 719 720 kref_get(&claim->refcount); 721 hash_added = batadv_hash_add(bat_priv->bla.claim_hash, 722 batadv_compare_claim, 723 batadv_choose_claim, claim, 724 &claim->hash_entry); 725 726 if (unlikely(hash_added != 0)) { 727 /* only local changes happened. */ 728 batadv_backbone_gw_put(backbone_gw); 729 kfree(claim); 730 return; 731 } 732 733 spin_lock_bh(&backbone_gw->crc_lock); 734 backbone_gw->crc ^= claim_crc; 735 spin_unlock_bh(&backbone_gw->crc_lock); 736 737 WRITE_ONCE(backbone_gw->lasttime, jiffies); 738 739 batadv_claim_put(claim); 740 return; 741 } 742 743 WRITE_ONCE(claim->lasttime, jiffies); 744 745 /* replace backbone_gw atomically and adjust reference counters */ 746 spin_lock_bh(&claim->backbone_lock); 747 if (claim->backbone_gw != backbone_gw) { 748 changed = true; 749 750 old_backbone_gw = claim->backbone_gw; 751 kref_get(&backbone_gw->refcount); 752 claim->backbone_gw = backbone_gw; 753 } else { 754 old_backbone_gw = NULL; 755 changed = false; 756 } 757 spin_unlock_bh(&claim->backbone_lock); 758 759 if (changed) { 760 batadv_dbg(BATADV_DBG_BLA, bat_priv, 761 "%s(): changing ownership for %pM, vid %d to gw %pM\n", 762 __func__, mac, batadv_print_vid(vid), 763 backbone_gw->orig); 764 765 /* add claim address to new backbone_gw */ 766 spin_lock_bh(&backbone_gw->crc_lock); 767 backbone_gw->crc ^= claim_crc; 768 spin_unlock_bh(&backbone_gw->crc_lock); 769 770 WRITE_ONCE(backbone_gw->lasttime, jiffies); 771 } 772 773 if (old_backbone_gw) { 774 /* remove claim address from old backbone_gw */ 775 spin_lock_bh(&old_backbone_gw->crc_lock); 776 old_backbone_gw->crc ^= claim_crc; 777 spin_unlock_bh(&old_backbone_gw->crc_lock); 778 779 batadv_backbone_gw_put(old_backbone_gw); 780 } 781 782 batadv_claim_put(claim); 783 } 784 785 /** 786 * batadv_bla_claim_get_backbone_gw() - Get valid reference for backbone_gw of 787 * claim 788 * @claim: claim whose backbone_gw should be returned 789 * 790 * Return: valid reference to claim::backbone_gw 791 */ 792 static struct batadv_bla_backbone_gw * 793 batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim) 794 { 795 struct batadv_bla_backbone_gw *backbone_gw; 796 797 spin_lock_bh(&claim->backbone_lock); 798 backbone_gw = claim->backbone_gw; 799 kref_get(&backbone_gw->refcount); 800 spin_unlock_bh(&claim->backbone_lock); 801 802 return backbone_gw; 803 } 804 805 /** 806 * batadv_bla_del_claim() - delete a claim from the claim hash 807 * @bat_priv: the bat priv with all the mesh interface information 808 * @mac: mac address of the claim to be removed 809 * @vid: VLAN id for the claim to be removed 810 */ 811 static void batadv_bla_del_claim(struct batadv_priv *bat_priv, 812 const u8 *mac, const unsigned short vid) 813 { 814 struct batadv_bla_claim *claim_removed_entry; 815 struct hlist_node *claim_removed_node; 816 struct batadv_bla_claim search_claim; 817 struct batadv_bla_claim *claim; 818 819 ether_addr_copy(search_claim.addr, mac); 820 search_claim.vid = vid; 821 claim = batadv_claim_hash_find(bat_priv, &search_claim); 822 if (!claim) 823 return; 824 825 batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): %pM, vid %d\n", __func__, 826 mac, batadv_print_vid(vid)); 827 828 claim_removed_node = batadv_hash_remove(bat_priv->bla.claim_hash, 829 batadv_compare_claim, 830 batadv_choose_claim, claim); 831 if (!claim_removed_node) 832 goto free_claim; 833 834 /* reference from the hash is gone */ 835 claim_removed_entry = hlist_entry(claim_removed_node, 836 struct batadv_bla_claim, hash_entry); 837 batadv_claim_put(claim_removed_entry); 838 839 free_claim: 840 /* don't need the reference from hash_find() anymore */ 841 batadv_claim_put(claim); 842 } 843 844 /** 845 * batadv_handle_announce() - check for ANNOUNCE frame 846 * @bat_priv: the bat priv with all the mesh interface information 847 * @an_addr: announcement mac address (ARP Sender HW address) 848 * @backbone_addr: originator address of the sender (Ethernet source MAC) 849 * @vid: the VLAN ID of the frame 850 * 851 * Return: true if handled 852 */ 853 static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr, 854 u8 *backbone_addr, unsigned short vid) 855 { 856 struct batadv_bla_backbone_gw *backbone_gw; 857 u16 backbone_crc; 858 u16 crc; 859 860 if (memcmp(an_addr, batadv_announce_mac, 4) != 0) 861 return false; 862 863 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid, 864 false); 865 866 if (unlikely(!backbone_gw)) 867 return true; 868 869 /* handle as ANNOUNCE frame */ 870 WRITE_ONCE(backbone_gw->lasttime, jiffies); 871 crc = ntohs(*((__force __be16 *)(&an_addr[4]))); 872 873 batadv_dbg(BATADV_DBG_BLA, bat_priv, 874 "%s(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n", 875 __func__, batadv_print_vid(vid), backbone_gw->orig, crc); 876 877 spin_lock_bh(&backbone_gw->crc_lock); 878 backbone_crc = backbone_gw->crc; 879 spin_unlock_bh(&backbone_gw->crc_lock); 880 881 if (backbone_crc != crc) { 882 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv, 883 "%s(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n", 884 __func__, backbone_gw->orig, 885 batadv_print_vid(backbone_gw->vid), 886 backbone_crc, crc); 887 888 batadv_bla_send_request(backbone_gw); 889 } else { 890 /* if we have sent a request and the crc was OK, 891 * we can allow traffic again. 892 */ 893 spin_lock_bh(&bat_priv->bla.num_requests_lock); 894 if (backbone_gw->state == BATADV_BLA_BACKBONE_GW_UNSYNCED) { 895 backbone_gw->state = BATADV_BLA_BACKBONE_GW_SYNCED; 896 atomic_dec(&backbone_gw->bat_priv->bla.num_requests); 897 } 898 spin_unlock_bh(&bat_priv->bla.num_requests_lock); 899 } 900 901 batadv_backbone_gw_put(backbone_gw); 902 return true; 903 } 904 905 /** 906 * batadv_handle_request() - check for REQUEST frame 907 * @bat_priv: the bat priv with all the mesh interface information 908 * @primary_if: the primary hard interface of this batman mesh interface 909 * @backbone_addr: backbone address to be requested (ARP sender HW MAC) 910 * @ethhdr: ethernet header of a packet 911 * @vid: the VLAN ID of the frame 912 * 913 * Return: true if handled 914 */ 915 static bool batadv_handle_request(struct batadv_priv *bat_priv, 916 struct batadv_hard_iface *primary_if, 917 u8 *backbone_addr, struct ethhdr *ethhdr, 918 unsigned short vid) 919 { 920 /* check for REQUEST frame */ 921 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest)) 922 return false; 923 924 /* sanity check, this should not happen on a normal switch, 925 * we ignore it in this case. 926 */ 927 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr)) 928 return true; 929 930 batadv_dbg(BATADV_DBG_BLA, bat_priv, 931 "%s(): REQUEST vid %d (sent by %pM)...\n", 932 __func__, batadv_print_vid(vid), ethhdr->h_source); 933 934 batadv_bla_answer_request(bat_priv, primary_if, vid); 935 return true; 936 } 937 938 /** 939 * batadv_handle_unclaim() - check for UNCLAIM frame 940 * @bat_priv: the bat priv with all the mesh interface information 941 * @primary_if: the primary hard interface of this batman mesh interface 942 * @backbone_addr: originator address of the backbone (Ethernet source) 943 * @claim_addr: Client to be unclaimed (ARP sender HW MAC) 944 * @vid: the VLAN ID of the frame 945 * 946 * Return: true if handled 947 */ 948 static bool batadv_handle_unclaim(struct batadv_priv *bat_priv, 949 struct batadv_hard_iface *primary_if, 950 const u8 *backbone_addr, const u8 *claim_addr, 951 unsigned short vid) 952 { 953 /* unclaim in any case if it is our own */ 954 if (primary_if && batadv_compare_eth(backbone_addr, 955 primary_if->net_dev->dev_addr)) 956 batadv_bla_send_claim(bat_priv, claim_addr, vid, 957 BATADV_CLAIM_TYPE_UNCLAIM); 958 959 /* this must be an UNCLAIM frame */ 960 batadv_dbg(BATADV_DBG_BLA, bat_priv, 961 "%s(): UNCLAIM %pM on vid %d (sent by %pM)...\n", __func__, 962 claim_addr, batadv_print_vid(vid), backbone_addr); 963 964 batadv_bla_del_claim(bat_priv, claim_addr, vid); 965 return true; 966 } 967 968 /** 969 * batadv_handle_claim() - check for CLAIM frame 970 * @bat_priv: the bat priv with all the mesh interface information 971 * @primary_if: the primary hard interface of this batman mesh interface 972 * @backbone_addr: originator address of the backbone (Ethernet Source) 973 * @claim_addr: client mac address to be claimed (ARP sender HW MAC) 974 * @vid: the VLAN ID of the frame 975 * 976 * Return: true if handled 977 */ 978 static bool batadv_handle_claim(struct batadv_priv *bat_priv, 979 struct batadv_hard_iface *primary_if, 980 const u8 *backbone_addr, const u8 *claim_addr, 981 unsigned short vid) 982 { 983 struct batadv_bla_backbone_gw *backbone_gw; 984 985 /* register the gateway if not yet available, and add the claim. */ 986 987 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid, 988 false); 989 990 if (unlikely(!backbone_gw)) 991 return true; 992 993 /* this must be a CLAIM frame */ 994 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw); 995 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr)) 996 batadv_bla_send_claim(bat_priv, claim_addr, vid, 997 BATADV_CLAIM_TYPE_CLAIM); 998 999 /* TODO: we could call something like tt_local_del() here. */ 1000 1001 batadv_backbone_gw_put(backbone_gw); 1002 return true; 1003 } 1004 1005 /** 1006 * batadv_check_claim_group() - check for claim group membership 1007 * @bat_priv: the bat priv with all the mesh interface information 1008 * @primary_if: the primary interface of this batman interface 1009 * @hw_src: the Hardware source in the ARP Header 1010 * @hw_dst: the Hardware destination in the ARP Header 1011 * @ethhdr: pointer to the Ethernet header of the claim frame 1012 * 1013 * checks if it is a claim packet and if it's on the same group. 1014 * This function also applies the group ID of the sender 1015 * if it is in the same mesh. 1016 * 1017 * Return: 1018 * 2 - if it is a claim packet and on the same group 1019 * 1 - if is a claim packet from another group 1020 * 0 - if it is not a claim packet 1021 */ 1022 static int batadv_check_claim_group(struct batadv_priv *bat_priv, 1023 struct batadv_hard_iface *primary_if, 1024 u8 *hw_src, u8 *hw_dst, 1025 struct ethhdr *ethhdr) 1026 { 1027 struct batadv_bla_claim_dst *bla_dst_own; 1028 struct batadv_bla_claim_dst *bla_dst; 1029 struct batadv_orig_node *orig_node; 1030 u8 *backbone_addr; 1031 1032 bla_dst = (struct batadv_bla_claim_dst *)hw_dst; 1033 bla_dst_own = &bat_priv->bla.claim_dest; 1034 1035 /* if announcement packet, use the source, 1036 * otherwise assume it is in the hw_src 1037 */ 1038 switch (bla_dst->type) { 1039 case BATADV_CLAIM_TYPE_CLAIM: 1040 backbone_addr = hw_src; 1041 break; 1042 case BATADV_CLAIM_TYPE_REQUEST: 1043 case BATADV_CLAIM_TYPE_ANNOUNCE: 1044 case BATADV_CLAIM_TYPE_UNCLAIM: 1045 backbone_addr = ethhdr->h_source; 1046 break; 1047 default: 1048 return 0; 1049 } 1050 1051 /* don't accept claim frames from ourselves */ 1052 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr)) 1053 return 0; 1054 1055 /* if its already the same group, it is fine. */ 1056 if (bla_dst->group == bla_dst_own->group) 1057 return 2; 1058 1059 /* lets see if this originator is in our mesh */ 1060 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr); 1061 1062 /* don't accept claims from gateways which are not in 1063 * the same mesh or group. 1064 */ 1065 if (!orig_node) 1066 return 1; 1067 1068 /* if our mesh friends mac is bigger, use it for ourselves. */ 1069 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) { 1070 batadv_dbg(BATADV_DBG_BLA, bat_priv, 1071 "taking other backbones claim group: %#.4x\n", 1072 ntohs(bla_dst->group)); 1073 bla_dst_own->group = bla_dst->group; 1074 } 1075 1076 batadv_orig_node_put(orig_node); 1077 1078 return 2; 1079 } 1080 1081 /** 1082 * batadv_bla_process_claim() - Check if this is a claim frame, and process it 1083 * @bat_priv: the bat priv with all the mesh interface information 1084 * @primary_if: the primary hard interface of this batman mesh interface 1085 * @skb: the frame to be checked 1086 * 1087 * Warning: This function may reallocate the skb data buffer via 1088 * batadv_get_vid()/... Any pointer into the skb data (e.g. obtained 1089 * from skb->data or eth_hdr()) before this call must be considered 1090 * invalid afterwards and has to be reacquired. 1091 * 1092 * Return: true if it was a claim frame, otherwise return false to 1093 * tell the callee that it can use the frame on its own. 1094 */ 1095 static bool batadv_bla_process_claim(struct batadv_priv *bat_priv, 1096 struct batadv_hard_iface *primary_if, 1097 struct sk_buff *skb) 1098 { 1099 struct batadv_bla_claim_dst *bla_dst_own; 1100 struct batadv_bla_claim_dst *bla_dst; 1101 struct vlan_hdr vhdr_buf; 1102 struct vlan_hdr *vhdr; 1103 struct ethhdr *ethhdr; 1104 struct arphdr *arphdr; 1105 unsigned short vid; 1106 int vlan_depth = 0; 1107 __be16 proto; 1108 int headlen; 1109 u8 *hw_src; 1110 u8 *hw_dst; 1111 int ret; 1112 1113 vid = batadv_get_vid(skb, 0); 1114 ethhdr = eth_hdr(skb); 1115 1116 proto = ethhdr->h_proto; 1117 headlen = ETH_HLEN; 1118 if (vid & BATADV_VLAN_HAS_TAG) { 1119 /* Traverse the VLAN/Ethertypes. 1120 * 1121 * At this point it is known that the first protocol is a VLAN 1122 * header, so start checking at the encapsulated protocol. 1123 * 1124 * The depth of the VLAN headers is recorded to drop BLA claim 1125 * frames encapsulated into multiple VLAN headers (QinQ). 1126 */ 1127 do { 1128 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN, 1129 &vhdr_buf); 1130 if (!vhdr) 1131 return false; 1132 1133 proto = vhdr->h_vlan_encapsulated_proto; 1134 headlen += VLAN_HLEN; 1135 vlan_depth++; 1136 } while (proto == htons(ETH_P_8021Q)); 1137 } 1138 1139 if (proto != htons(ETH_P_ARP)) 1140 return false; /* not a claim frame */ 1141 1142 /* this must be a ARP frame. check if it is a claim. */ 1143 1144 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev)))) 1145 return false; 1146 1147 /* pskb_may_pull() may have modified the pointers, get ethhdr again */ 1148 ethhdr = eth_hdr(skb); 1149 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen); 1150 1151 /* Check whether the ARP frame carries a valid 1152 * IP information 1153 */ 1154 if (arphdr->ar_hrd != htons(ARPHRD_ETHER)) 1155 return false; 1156 if (arphdr->ar_pro != htons(ETH_P_IP)) 1157 return false; 1158 if (arphdr->ar_hln != ETH_ALEN) 1159 return false; 1160 if (arphdr->ar_pln != 4) 1161 return false; 1162 1163 hw_src = (u8 *)arphdr + sizeof(struct arphdr); 1164 hw_dst = hw_src + ETH_ALEN + 4; 1165 bla_dst = (struct batadv_bla_claim_dst *)hw_dst; 1166 bla_dst_own = &bat_priv->bla.claim_dest; 1167 1168 /* check if it is a claim frame in general */ 1169 if (memcmp(bla_dst->magic, bla_dst_own->magic, 1170 sizeof(bla_dst->magic)) != 0) 1171 return false; 1172 1173 /* check if there is a claim frame encapsulated deeper in (QinQ) and 1174 * drop that, as this is not supported by BLA but should also not be 1175 * sent via the mesh. 1176 */ 1177 if (vlan_depth > 1) 1178 return true; 1179 1180 /* Let the loopdetect frames on the mesh in any case. */ 1181 if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT) 1182 return false; 1183 1184 /* check if it is a claim frame. */ 1185 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst, 1186 ethhdr); 1187 if (ret == 1) 1188 batadv_dbg(BATADV_DBG_BLA, bat_priv, 1189 "%s(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n", 1190 __func__, ethhdr->h_source, batadv_print_vid(vid), 1191 hw_src, hw_dst); 1192 1193 if (ret < 2) 1194 return !!ret; 1195 1196 /* become a backbone gw ourselves on this vlan if not happened yet */ 1197 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid); 1198 1199 /* check for the different types of claim frames ... */ 1200 switch (bla_dst->type) { 1201 case BATADV_CLAIM_TYPE_CLAIM: 1202 if (batadv_handle_claim(bat_priv, primary_if, hw_src, 1203 ethhdr->h_source, vid)) 1204 return true; 1205 break; 1206 case BATADV_CLAIM_TYPE_UNCLAIM: 1207 if (batadv_handle_unclaim(bat_priv, primary_if, 1208 ethhdr->h_source, hw_src, vid)) 1209 return true; 1210 break; 1211 1212 case BATADV_CLAIM_TYPE_ANNOUNCE: 1213 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source, 1214 vid)) 1215 return true; 1216 break; 1217 case BATADV_CLAIM_TYPE_REQUEST: 1218 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr, 1219 vid)) 1220 return true; 1221 break; 1222 } 1223 1224 batadv_dbg(BATADV_DBG_BLA, bat_priv, 1225 "%s(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n", 1226 __func__, ethhdr->h_source, batadv_print_vid(vid), hw_src, 1227 hw_dst); 1228 return true; 1229 } 1230 1231 /** 1232 * batadv_bla_purge_backbone_gw() - Remove backbone gateways after a timeout or 1233 * immediately 1234 * @bat_priv: the bat priv with all the mesh interface information 1235 * @now: whether the whole hash shall be wiped now 1236 * 1237 * Check when we last heard from other nodes, and remove them in case of 1238 * a time out, or clean all backbone gws if now is set. 1239 */ 1240 static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now) 1241 { 1242 spinlock_t *list_lock; /* protects write access to the hash lists */ 1243 struct batadv_bla_backbone_gw *backbone_gw; 1244 struct batadv_hashtable *hash; 1245 struct hlist_node *node_tmp; 1246 struct hlist_head *head; 1247 bool purged; 1248 int i; 1249 1250 hash = bat_priv->bla.backbone_hash; 1251 if (!hash) 1252 return; 1253 1254 for (i = 0; i < hash->size; i++) { 1255 head = &hash->table[i]; 1256 list_lock = &hash->list_locks[i]; 1257 1258 do { 1259 purged = false; 1260 1261 spin_lock_bh(list_lock); 1262 hlist_for_each_entry_safe(backbone_gw, node_tmp, 1263 head, hash_entry) { 1264 if (now) 1265 goto purge_now; 1266 if (!batadv_has_timed_out(READ_ONCE(backbone_gw->lasttime), 1267 BATADV_BLA_BACKBONE_TIMEOUT)) 1268 continue; 1269 1270 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv, 1271 "%s(): backbone gw %pM timed out\n", 1272 __func__, backbone_gw->orig); 1273 1274 purge_now: 1275 purged = true; 1276 1277 /* don't wait for the pending request anymore */ 1278 spin_lock_bh(&bat_priv->bla.num_requests_lock); 1279 if (backbone_gw->state == BATADV_BLA_BACKBONE_GW_UNSYNCED) 1280 atomic_dec(&bat_priv->bla.num_requests); 1281 1282 backbone_gw->state = BATADV_BLA_BACKBONE_GW_STOPPED; 1283 spin_unlock_bh(&bat_priv->bla.num_requests_lock); 1284 1285 batadv_bla_del_backbone_claims(backbone_gw); 1286 1287 hlist_del_rcu(&backbone_gw->hash_entry); 1288 break; 1289 } 1290 spin_unlock_bh(list_lock); 1291 1292 if (purged) { 1293 /* reference for pending report_work */ 1294 if (disable_work_sync(&backbone_gw->report_work)) 1295 batadv_backbone_gw_put(backbone_gw); 1296 1297 /* reference for hash_entry */ 1298 batadv_backbone_gw_put(backbone_gw); 1299 } 1300 } while (purged); 1301 } 1302 } 1303 1304 /** 1305 * batadv_bla_purge_claims() - Remove claims after a timeout or immediately 1306 * @bat_priv: the bat priv with all the mesh interface information 1307 * @primary_if: the selected primary interface, may be NULL if now is set 1308 * @now: whether the whole hash shall be wiped now 1309 * 1310 * Check when we heard last time from our own claims, and remove them in case of 1311 * a time out, or clean all claims if now is set 1312 */ 1313 static void batadv_bla_purge_claims(struct batadv_priv *bat_priv, 1314 struct batadv_hard_iface *primary_if, 1315 int now) 1316 { 1317 struct batadv_bla_backbone_gw *backbone_gw; 1318 struct batadv_bla_claim *claim; 1319 struct batadv_hashtable *hash; 1320 struct hlist_head *head; 1321 int i; 1322 1323 hash = bat_priv->bla.claim_hash; 1324 if (!hash) 1325 return; 1326 1327 for (i = 0; i < hash->size; i++) { 1328 head = &hash->table[i]; 1329 1330 rcu_read_lock(); 1331 hlist_for_each_entry_rcu(claim, head, hash_entry) { 1332 /* only purge claims not currently in the process of being released. 1333 * Such claims could otherwise have a NULL-ptr backbone_gw set because 1334 * they already went through batadv_claim_release() 1335 */ 1336 if (!kref_get_unless_zero(&claim->refcount)) 1337 continue; 1338 1339 backbone_gw = batadv_bla_claim_get_backbone_gw(claim); 1340 if (now) 1341 goto purge_now; 1342 1343 if (!batadv_compare_eth(backbone_gw->orig, 1344 primary_if->net_dev->dev_addr)) 1345 goto skip; 1346 1347 if (!batadv_has_timed_out(READ_ONCE(claim->lasttime), 1348 BATADV_BLA_CLAIM_TIMEOUT)) 1349 goto skip; 1350 1351 batadv_dbg(BATADV_DBG_BLA, bat_priv, 1352 "%s(): timed out.\n", __func__); 1353 1354 purge_now: 1355 batadv_dbg(BATADV_DBG_BLA, bat_priv, 1356 "%s(): %pM, vid %d\n", __func__, 1357 claim->addr, claim->vid); 1358 1359 batadv_handle_unclaim(bat_priv, primary_if, 1360 backbone_gw->orig, 1361 claim->addr, claim->vid); 1362 skip: 1363 batadv_backbone_gw_put(backbone_gw); 1364 batadv_claim_put(claim); 1365 } 1366 rcu_read_unlock(); 1367 } 1368 } 1369 1370 /** 1371 * batadv_bla_update_orig_address() - Update the backbone gateways when the own 1372 * originator address changes 1373 * @bat_priv: the bat priv with all the mesh interface information 1374 * @primary_if: the new selected primary_if 1375 * @oldif: the old primary interface, may be NULL 1376 */ 1377 void batadv_bla_update_orig_address(struct batadv_priv *bat_priv, 1378 struct batadv_hard_iface *primary_if, 1379 struct batadv_hard_iface *oldif) 1380 { 1381 struct batadv_bla_backbone_gw *backbone_gw; 1382 struct batadv_hashtable *hash; 1383 struct hlist_head *head; 1384 __be16 group; 1385 int i; 1386 1387 /* reset bridge loop avoidance group id */ 1388 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN)); 1389 bat_priv->bla.claim_dest.group = group; 1390 1391 /* purge everything when bridge loop avoidance is turned off */ 1392 if (!READ_ONCE(bat_priv->bridge_loop_avoidance)) 1393 oldif = NULL; 1394 1395 if (!oldif) { 1396 batadv_bla_purge_claims(bat_priv, NULL, 1); 1397 batadv_bla_purge_backbone_gw(bat_priv, 1); 1398 return; 1399 } 1400 1401 hash = bat_priv->bla.backbone_hash; 1402 if (!hash) 1403 return; 1404 1405 for (i = 0; i < hash->size; i++) { 1406 head = &hash->table[i]; 1407 1408 rcu_read_lock(); 1409 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 1410 /* own orig still holds the old value. */ 1411 if (!batadv_compare_eth(backbone_gw->orig, 1412 oldif->net_dev->dev_addr)) 1413 continue; 1414 1415 ether_addr_copy(backbone_gw->orig, 1416 primary_if->net_dev->dev_addr); 1417 /* send an announce frame so others will ask for our 1418 * claims and update their tables. 1419 */ 1420 batadv_bla_send_announce(bat_priv, backbone_gw); 1421 } 1422 rcu_read_unlock(); 1423 } 1424 } 1425 1426 /** 1427 * batadv_bla_send_loopdetect() - send a loopdetect frame 1428 * @bat_priv: the bat priv with all the mesh interface information 1429 * @backbone_gw: the backbone gateway for which a loop should be detected 1430 * 1431 * To detect loops that the bridge loop avoidance can't handle, send a loop 1432 * detection packet on the backbone. Unlike other BLA frames, this frame will 1433 * be allowed on the mesh by other nodes. If it is received on the mesh, this 1434 * indicates that there is a loop. 1435 */ 1436 static void 1437 batadv_bla_send_loopdetect(struct batadv_priv *bat_priv, 1438 struct batadv_bla_backbone_gw *backbone_gw) 1439 { 1440 batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n", 1441 backbone_gw->vid); 1442 batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr, 1443 backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT); 1444 } 1445 1446 /** 1447 * batadv_bla_status_update() - purge bla interfaces if necessary 1448 * @net_dev: the mesh interface net device 1449 */ 1450 void batadv_bla_status_update(struct net_device *net_dev) 1451 { 1452 struct batadv_priv *bat_priv = netdev_priv(net_dev); 1453 struct batadv_hard_iface *primary_if; 1454 1455 primary_if = batadv_primary_if_get_selected(bat_priv); 1456 if (!primary_if) 1457 return; 1458 1459 /* this function already purges everything when bla is disabled, 1460 * so just call that one. 1461 */ 1462 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if); 1463 batadv_hardif_put(primary_if); 1464 } 1465 1466 /** 1467 * batadv_bla_periodic_work() - performs periodic bla work 1468 * @work: kernel work struct 1469 * 1470 * periodic work to do: 1471 * * purge structures when they are too old 1472 * * send announcements 1473 */ 1474 static void batadv_bla_periodic_work(struct work_struct *work) 1475 { 1476 struct batadv_bla_backbone_gw *backbone_gw; 1477 struct batadv_hard_iface *primary_if; 1478 struct delayed_work *delayed_work; 1479 struct batadv_priv_bla *priv_bla; 1480 struct batadv_hashtable *hash; 1481 struct batadv_priv *bat_priv; 1482 bool send_loopdetect = false; 1483 struct hlist_head *head; 1484 int i; 1485 1486 delayed_work = to_delayed_work(work); 1487 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work); 1488 bat_priv = container_of(priv_bla, struct batadv_priv, bla); 1489 primary_if = batadv_primary_if_get_selected(bat_priv); 1490 if (!primary_if) 1491 goto out; 1492 1493 batadv_bla_purge_claims(bat_priv, primary_if, 0); 1494 batadv_bla_purge_backbone_gw(bat_priv, 0); 1495 1496 if (!READ_ONCE(bat_priv->bridge_loop_avoidance)) 1497 goto out; 1498 1499 if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) { 1500 /* set a new random mac address for the next bridge loop 1501 * detection frames. Set the locally administered bit to avoid 1502 * collisions with users mac addresses. 1503 */ 1504 eth_random_addr(bat_priv->bla.loopdetect_addr); 1505 bat_priv->bla.loopdetect_addr[0] = 0xba; 1506 bat_priv->bla.loopdetect_addr[1] = 0xbe; 1507 WRITE_ONCE(bat_priv->bla.loopdetect_lasttime, jiffies); 1508 atomic_set(&bat_priv->bla.loopdetect_next, 1509 BATADV_BLA_LOOPDETECT_PERIODS); 1510 1511 /* mark for sending loop detect on all VLANs */ 1512 send_loopdetect = true; 1513 } 1514 1515 hash = bat_priv->bla.backbone_hash; 1516 if (!hash) 1517 goto out; 1518 1519 for (i = 0; i < hash->size; i++) { 1520 head = &hash->table[i]; 1521 1522 rcu_read_lock(); 1523 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 1524 if (!batadv_compare_eth(backbone_gw->orig, 1525 primary_if->net_dev->dev_addr)) 1526 continue; 1527 1528 WRITE_ONCE(backbone_gw->lasttime, jiffies); 1529 1530 batadv_bla_send_announce(bat_priv, backbone_gw); 1531 if (send_loopdetect) 1532 batadv_bla_send_loopdetect(bat_priv, 1533 backbone_gw); 1534 1535 /* state is only set to unsynced after creation to avoid 1536 * problems when we are not yet known as backbone gw 1537 * in the backbone. 1538 * 1539 * We can reset this now after we waited some periods 1540 * to give bridge forward delays and bla group forming 1541 * some grace time. 1542 */ 1543 1544 spin_lock_bh(&bat_priv->bla.num_requests_lock); 1545 if (backbone_gw->state != BATADV_BLA_BACKBONE_GW_UNSYNCED) 1546 goto unlock_next; 1547 1548 if (backbone_gw->wait_periods > 0) 1549 backbone_gw->wait_periods--; 1550 1551 if (backbone_gw->wait_periods > 0) 1552 goto unlock_next; 1553 1554 backbone_gw->state = BATADV_BLA_BACKBONE_GW_SYNCED; 1555 atomic_dec(&backbone_gw->bat_priv->bla.num_requests); 1556 1557 unlock_next: 1558 spin_unlock_bh(&bat_priv->bla.num_requests_lock); 1559 } 1560 rcu_read_unlock(); 1561 } 1562 out: 1563 batadv_hardif_put(primary_if); 1564 1565 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work, 1566 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH)); 1567 } 1568 1569 /* The hash for claim and backbone hash receive the same key because they 1570 * are getting initialized by hash_new with the same key. Reinitializing 1571 * them with to different keys to allow nested locking without generating 1572 * lockdep warnings 1573 */ 1574 static struct lock_class_key batadv_claim_hash_lock_class_key; 1575 static struct lock_class_key batadv_backbone_hash_lock_class_key; 1576 1577 /** 1578 * batadv_bla_init() - initialize all bla structures 1579 * @bat_priv: the bat priv with all the mesh interface information 1580 * 1581 * Return: 0 on success, < 0 on error. 1582 */ 1583 int batadv_bla_init(struct batadv_priv *bat_priv) 1584 { 1585 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00}; 1586 struct batadv_hard_iface *primary_if; 1587 unsigned long entrytime; 1588 u16 crc; 1589 int i; 1590 1591 spin_lock_init(&bat_priv->bla.bcast_duplist_lock); 1592 1593 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n"); 1594 1595 /* setting claim destination address */ 1596 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3); 1597 bat_priv->bla.claim_dest.type = 0; 1598 primary_if = batadv_primary_if_get_selected(bat_priv); 1599 if (primary_if) { 1600 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN); 1601 bat_priv->bla.claim_dest.group = htons(crc); 1602 batadv_hardif_put(primary_if); 1603 } else { 1604 bat_priv->bla.claim_dest.group = 0; /* will be set later */ 1605 } 1606 1607 /* initialize the duplicate list */ 1608 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT); 1609 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) 1610 bat_priv->bla.bcast_duplist[i].entrytime = entrytime; 1611 bat_priv->bla.bcast_duplist_curr = 0; 1612 1613 atomic_set(&bat_priv->bla.loopdetect_next, 1614 BATADV_BLA_LOOPDETECT_PERIODS); 1615 1616 if (bat_priv->bla.claim_hash) 1617 return 0; 1618 1619 bat_priv->bla.claim_hash = batadv_hash_new(128); 1620 if (!bat_priv->bla.claim_hash) 1621 return -ENOMEM; 1622 1623 bat_priv->bla.backbone_hash = batadv_hash_new(32); 1624 if (!bat_priv->bla.backbone_hash) { 1625 batadv_hash_destroy(bat_priv->bla.claim_hash); 1626 return -ENOMEM; 1627 } 1628 1629 batadv_hash_set_lock_class(bat_priv->bla.claim_hash, 1630 &batadv_claim_hash_lock_class_key); 1631 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash, 1632 &batadv_backbone_hash_lock_class_key); 1633 1634 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n"); 1635 1636 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work); 1637 1638 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work, 1639 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH)); 1640 return 0; 1641 } 1642 1643 /** 1644 * batadv_bla_check_duplist() - Check if a frame is in the broadcast dup. 1645 * @bat_priv: the bat priv with all the mesh interface information 1646 * @skb: contains the multicast packet to be checked 1647 * @payload_offset: offset in the skb, marking the start of the data to be CRC'ed 1648 * @orig: originator mac address, NULL if unknown 1649 * 1650 * Check if it is on our broadcast list. Another gateway might have sent the 1651 * same packet because it is connected to the same backbone, so we have to 1652 * remove this duplicate. 1653 * 1654 * This is performed by checking the CRC, which will tell us 1655 * with a good chance that it is the same packet. If it is furthermore 1656 * sent by another host, drop it. We allow equal packets from 1657 * the same host however as this might be intended. 1658 * 1659 * Return: true if a packet is in the duplicate list, false otherwise. 1660 */ 1661 static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv, 1662 struct sk_buff *skb, int payload_offset, 1663 const u8 *orig) 1664 { 1665 struct batadv_bcast_duplist_entry *entry; 1666 bool ret = false; 1667 int payload_len; 1668 int curr; 1669 u32 crc; 1670 int i; 1671 1672 /* calculate the crc ... */ 1673 payload_len = skb->len - payload_offset; 1674 crc = skb_crc32c(skb, payload_offset, payload_len, 0); 1675 1676 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock); 1677 1678 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) { 1679 curr = (bat_priv->bla.bcast_duplist_curr + i); 1680 curr %= BATADV_DUPLIST_SIZE; 1681 entry = &bat_priv->bla.bcast_duplist[curr]; 1682 1683 /* we can stop searching if the entry is too old ; 1684 * later entries will be even older 1685 */ 1686 if (batadv_has_timed_out(entry->entrytime, 1687 BATADV_DUPLIST_TIMEOUT)) 1688 break; 1689 1690 if (entry->crc != crc) 1691 continue; 1692 1693 /* are the originators both known and not anonymous? */ 1694 if (orig && !is_zero_ether_addr(orig) && 1695 !is_zero_ether_addr(entry->orig)) { 1696 /* If known, check if the new frame came from 1697 * the same originator: 1698 * We are safe to take identical frames from the 1699 * same orig, if known, as multiplications in 1700 * the mesh are detected via the (orig, seqno) pair. 1701 * So we can be a bit more liberal here and allow 1702 * identical frames from the same orig which the source 1703 * host might have sent multiple times on purpose. 1704 */ 1705 if (batadv_compare_eth(entry->orig, orig)) 1706 continue; 1707 } 1708 1709 /* this entry seems to match: same crc, not too old, 1710 * and from another gw. therefore return true to forbid it. 1711 */ 1712 ret = true; 1713 goto out; 1714 } 1715 /* not found, add a new entry (overwrite the oldest entry) 1716 * and allow it, its the first occurrence. 1717 */ 1718 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1); 1719 curr %= BATADV_DUPLIST_SIZE; 1720 entry = &bat_priv->bla.bcast_duplist[curr]; 1721 entry->crc = crc; 1722 entry->entrytime = jiffies; 1723 1724 /* known originator */ 1725 if (orig) 1726 ether_addr_copy(entry->orig, orig); 1727 /* anonymous originator */ 1728 else 1729 eth_zero_addr(entry->orig); 1730 1731 bat_priv->bla.bcast_duplist_curr = curr; 1732 1733 out: 1734 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock); 1735 1736 return ret; 1737 } 1738 1739 /** 1740 * batadv_bla_check_ucast_duplist() - Check if a frame is in the broadcast dup. 1741 * @bat_priv: the bat priv with all the mesh interface information 1742 * @skb: contains the multicast packet to be checked, decapsulated from a 1743 * unicast_packet 1744 * 1745 * Check if it is on our broadcast list. Another gateway might have sent the 1746 * same packet because it is connected to the same backbone, so we have to 1747 * remove this duplicate. 1748 * 1749 * Return: true if a packet is in the duplicate list, false otherwise. 1750 */ 1751 static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv, 1752 struct sk_buff *skb) 1753 { 1754 return batadv_bla_check_duplist(bat_priv, skb, 0, NULL); 1755 } 1756 1757 /** 1758 * batadv_bla_check_bcast_duplist() - Check if a frame is in the broadcast dup. 1759 * @bat_priv: the bat priv with all the mesh interface information 1760 * @skb: contains the bcast_packet to be checked 1761 * 1762 * Check if it is on our broadcast list. Another gateway might have sent the 1763 * same packet because it is connected to the same backbone, so we have to 1764 * remove this duplicate. 1765 * 1766 * Return: true if a packet is in the duplicate list, false otherwise. 1767 */ 1768 bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv, 1769 struct sk_buff *skb) 1770 { 1771 struct batadv_bcast_packet *bcast_packet; 1772 1773 bcast_packet = (struct batadv_bcast_packet *)skb->data; 1774 1775 return batadv_bla_check_duplist(bat_priv, skb, sizeof(*bcast_packet), 1776 bcast_packet->orig); 1777 } 1778 1779 /** 1780 * batadv_bla_is_backbone_gw_orig() - Check if the originator is a gateway for 1781 * the VLAN identified by vid. 1782 * @bat_priv: the bat priv with all the mesh interface information 1783 * @orig: originator mac address 1784 * @vid: VLAN identifier 1785 * 1786 * Return: true if orig is a backbone for this vid, false otherwise. 1787 */ 1788 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig, 1789 unsigned short vid) 1790 { 1791 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash; 1792 struct batadv_bla_backbone_gw *backbone_gw; 1793 struct hlist_head *head; 1794 int i; 1795 1796 if (!READ_ONCE(bat_priv->bridge_loop_avoidance)) 1797 return false; 1798 1799 if (!hash) 1800 return false; 1801 1802 for (i = 0; i < hash->size; i++) { 1803 head = &hash->table[i]; 1804 1805 rcu_read_lock(); 1806 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 1807 if (batadv_compare_eth(backbone_gw->orig, orig) && 1808 backbone_gw->vid == vid) { 1809 rcu_read_unlock(); 1810 return true; 1811 } 1812 } 1813 rcu_read_unlock(); 1814 } 1815 1816 return false; 1817 } 1818 1819 /** 1820 * batadv_bla_is_backbone_gw() - check if originator is a backbone gw for a VLAN 1821 * @skb: the frame to be checked 1822 * @orig_node: the orig_node of the frame 1823 * @hdr_size: maximum length of the frame 1824 * 1825 * Warning: This function may reallocate the skb data buffer via 1826 * pskb_may_pull()/batadv_get_vid()/... Any pointer into the skb data (e.g. 1827 * obtained from skb->data or eth_hdr()) before this call must be considered 1828 * invalid afterwards and has to be reacquired. 1829 * 1830 * Return: true if the orig_node is also a gateway on the mesh interface, 1831 * otherwise it returns false. 1832 */ 1833 bool batadv_bla_is_backbone_gw(struct sk_buff *skb, 1834 struct batadv_orig_node *orig_node, int hdr_size) 1835 { 1836 struct batadv_bla_backbone_gw *backbone_gw; 1837 unsigned short vid; 1838 1839 if (!READ_ONCE(orig_node->bat_priv->bridge_loop_avoidance)) 1840 return false; 1841 1842 /* first, find out the vid. */ 1843 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN)) 1844 return false; 1845 1846 vid = batadv_get_vid(skb, hdr_size); 1847 1848 /* see if this originator is a backbone gw for this VLAN */ 1849 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv, 1850 orig_node->orig, vid); 1851 if (!backbone_gw) 1852 return false; 1853 1854 batadv_backbone_gw_put(backbone_gw); 1855 return true; 1856 } 1857 1858 /** 1859 * batadv_bla_free() - free all bla structures 1860 * @bat_priv: the bat priv with all the mesh interface information 1861 * 1862 * for meshinterface free or module unload 1863 */ 1864 void batadv_bla_free(struct batadv_priv *bat_priv) 1865 { 1866 struct batadv_hard_iface *primary_if; 1867 1868 disable_delayed_work_sync(&bat_priv->bla.work); 1869 primary_if = batadv_primary_if_get_selected(bat_priv); 1870 1871 if (bat_priv->bla.claim_hash) { 1872 batadv_bla_purge_claims(bat_priv, primary_if, 1); 1873 batadv_hash_destroy(bat_priv->bla.claim_hash); 1874 bat_priv->bla.claim_hash = NULL; 1875 } 1876 if (bat_priv->bla.backbone_hash) { 1877 batadv_bla_purge_backbone_gw(bat_priv, 1); 1878 batadv_hash_destroy(bat_priv->bla.backbone_hash); 1879 bat_priv->bla.backbone_hash = NULL; 1880 } 1881 batadv_hardif_put(primary_if); 1882 } 1883 1884 /** 1885 * batadv_bla_loopdetect_check() - check and handle a detected loop 1886 * @bat_priv: the bat priv with all the mesh interface information 1887 * @skb: the packet to check 1888 * @primary_if: interface where the request came on 1889 * @vid: the VLAN ID of the frame 1890 * 1891 * Checks if this packet is a loop detect frame which has been sent by us, 1892 * throws an uevent and logs the event if that is the case. 1893 * 1894 * Return: true if it is a loop detect frame which is to be dropped, false 1895 * otherwise. 1896 */ 1897 static bool 1898 batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb, 1899 struct batadv_hard_iface *primary_if, 1900 unsigned short vid) 1901 { 1902 struct batadv_bla_backbone_gw *backbone_gw; 1903 struct ethhdr *ethhdr; 1904 bool ret; 1905 1906 ethhdr = eth_hdr(skb); 1907 1908 /* Only check for the MAC address and skip more checks here for 1909 * performance reasons - this function is on the hotpath, after all. 1910 */ 1911 if (!batadv_compare_eth(ethhdr->h_source, 1912 bat_priv->bla.loopdetect_addr)) 1913 return false; 1914 1915 /* If the packet came too late, don't forward it on the mesh 1916 * but don't consider that as loop. It might be a coincidence. 1917 */ 1918 if (batadv_has_timed_out(READ_ONCE(bat_priv->bla.loopdetect_lasttime), 1919 BATADV_BLA_LOOPDETECT_TIMEOUT)) 1920 return true; 1921 1922 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, 1923 primary_if->net_dev->dev_addr, 1924 vid, true); 1925 if (unlikely(!backbone_gw)) 1926 return true; 1927 1928 ret = queue_work(batadv_event_workqueue, &backbone_gw->report_work); 1929 1930 /* backbone_gw is unreferenced in the report work function 1931 * if queue_work() call was successful 1932 */ 1933 if (!ret) 1934 batadv_backbone_gw_put(backbone_gw); 1935 1936 return true; 1937 } 1938 1939 /** 1940 * batadv_bla_rx() - check packets coming from the mesh. 1941 * @bat_priv: the bat priv with all the mesh interface information 1942 * @skb: the frame to be checked 1943 * @vid: the VLAN ID of the frame 1944 * @packet_type: the batman packet type this frame came in 1945 * 1946 * batadv_bla_rx avoidance checks if: 1947 * * we have to race for a claim 1948 * * if the frame is allowed on the LAN 1949 * 1950 * In these cases, the skb is further handled by this function 1951 * 1952 * Return: true if handled, otherwise it returns false and the caller shall 1953 * further process the skb. 1954 */ 1955 bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb, 1956 unsigned short vid, int packet_type) 1957 { 1958 struct batadv_bla_backbone_gw *backbone_gw; 1959 struct batadv_bla_claim *claim = NULL; 1960 struct batadv_bla_claim search_claim; 1961 struct batadv_hard_iface *primary_if; 1962 struct ethhdr *ethhdr; 1963 bool own_claim; 1964 bool ret; 1965 1966 ethhdr = eth_hdr(skb); 1967 1968 primary_if = batadv_primary_if_get_selected(bat_priv); 1969 if (!primary_if) 1970 goto handled; 1971 1972 if (!READ_ONCE(bat_priv->bridge_loop_avoidance)) 1973 goto allow; 1974 1975 if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid)) 1976 goto handled; 1977 1978 if (unlikely(atomic_read(&bat_priv->bla.num_requests))) 1979 /* don't allow multicast packets while requests are in flight */ 1980 if (is_multicast_ether_addr(ethhdr->h_dest)) 1981 /* Both broadcast flooding or multicast-via-unicasts 1982 * delivery might send to multiple backbone gateways 1983 * sharing the same LAN and therefore need to coordinate 1984 * which backbone gateway forwards into the LAN, 1985 * by claiming the payload source address. 1986 * 1987 * Broadcast flooding and multicast-via-unicasts 1988 * delivery use the following two batman packet types. 1989 * Note: explicitly exclude BATADV_UNICAST_4ADDR, 1990 * as the DHCP gateway feature will send explicitly 1991 * to only one BLA gateway, so the claiming process 1992 * should be avoided there. 1993 */ 1994 if (packet_type == BATADV_BCAST || 1995 packet_type == BATADV_UNICAST) 1996 goto handled; 1997 1998 /* potential duplicates from foreign BLA backbone gateways via 1999 * multicast-in-unicast packets 2000 */ 2001 if (is_multicast_ether_addr(ethhdr->h_dest) && 2002 packet_type == BATADV_UNICAST && 2003 batadv_bla_check_ucast_duplist(bat_priv, skb)) 2004 goto handled; 2005 2006 ether_addr_copy(search_claim.addr, ethhdr->h_source); 2007 search_claim.vid = vid; 2008 claim = batadv_claim_hash_find(bat_priv, &search_claim); 2009 2010 if (!claim) { 2011 bool local = batadv_is_my_client(bat_priv, ethhdr->h_source, vid); 2012 2013 /* possible optimization: race for a claim */ 2014 /* No claim exists yet, claim it for us! 2015 */ 2016 2017 batadv_dbg(BATADV_DBG_BLA, bat_priv, 2018 "%s(): Unclaimed MAC %pM found. Claim it. Local: %s\n", 2019 __func__, ethhdr->h_source, str_yes_no(local)); 2020 batadv_handle_claim(bat_priv, primary_if, 2021 primary_if->net_dev->dev_addr, 2022 ethhdr->h_source, vid); 2023 goto allow; 2024 } 2025 2026 /* if it is our own claim ... */ 2027 backbone_gw = batadv_bla_claim_get_backbone_gw(claim); 2028 own_claim = batadv_compare_eth(backbone_gw->orig, 2029 primary_if->net_dev->dev_addr); 2030 batadv_backbone_gw_put(backbone_gw); 2031 2032 if (own_claim) { 2033 /* ... allow it in any case */ 2034 WRITE_ONCE(claim->lasttime, jiffies); 2035 goto allow; 2036 } 2037 2038 /* if it is a multicast ... */ 2039 if (is_multicast_ether_addr(ethhdr->h_dest) && 2040 (packet_type == BATADV_BCAST || packet_type == BATADV_UNICAST)) { 2041 /* ... drop it. the responsible gateway is in charge. 2042 * 2043 * We need to check packet type because with the gateway 2044 * feature, broadcasts (like DHCP requests) may be sent 2045 * using a unicast 4 address packet type. See comment above. 2046 */ 2047 goto handled; 2048 } else { 2049 /* seems the client considers us as its best gateway. 2050 * send a claim and update the claim table 2051 * immediately. 2052 */ 2053 batadv_handle_claim(bat_priv, primary_if, 2054 primary_if->net_dev->dev_addr, 2055 ethhdr->h_source, vid); 2056 goto allow; 2057 } 2058 allow: 2059 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid); 2060 ret = false; 2061 goto out; 2062 2063 handled: 2064 kfree_skb(skb); 2065 ret = true; 2066 2067 out: 2068 batadv_hardif_put(primary_if); 2069 batadv_claim_put(claim); 2070 return ret; 2071 } 2072 2073 /** 2074 * batadv_bla_tx() - check packets going into the mesh 2075 * @bat_priv: the bat priv with all the mesh interface information 2076 * @skb: the frame to be checked 2077 * @vid: the VLAN ID of the frame 2078 * 2079 * batadv_bla_tx checks if: 2080 * * a claim was received which has to be processed 2081 * * the frame is allowed on the mesh 2082 * 2083 * in these cases, the skb is further handled by this function. 2084 * 2085 * Warning: This function may reallocate the skb data buffer via 2086 * batadv_bla_process_claim()/... Any pointer into the skb data (e.g. 2087 * obtained from skb->data or eth_hdr()) before this call must be considered 2088 * invalid afterwards and has to be reacquired. 2089 * 2090 * Return: true if handled, otherwise it returns false and the caller shall 2091 * further process the skb. 2092 */ 2093 bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb, 2094 unsigned short vid) 2095 { 2096 struct batadv_bla_backbone_gw *backbone_gw; 2097 struct batadv_bla_claim *claim = NULL; 2098 struct batadv_bla_claim search_claim; 2099 struct batadv_hard_iface *primary_if; 2100 struct ethhdr *ethhdr; 2101 bool client_roamed; 2102 bool ret = false; 2103 2104 primary_if = batadv_primary_if_get_selected(bat_priv); 2105 if (!primary_if) 2106 goto out; 2107 2108 if (!READ_ONCE(bat_priv->bridge_loop_avoidance)) 2109 goto allow; 2110 2111 if (batadv_bla_process_claim(bat_priv, primary_if, skb)) 2112 goto handled; 2113 2114 ethhdr = eth_hdr(skb); 2115 2116 if (unlikely(atomic_read(&bat_priv->bla.num_requests))) 2117 /* don't allow broadcasts while requests are in flight */ 2118 if (is_multicast_ether_addr(ethhdr->h_dest)) 2119 goto handled; 2120 2121 ether_addr_copy(search_claim.addr, ethhdr->h_source); 2122 search_claim.vid = vid; 2123 2124 claim = batadv_claim_hash_find(bat_priv, &search_claim); 2125 2126 /* if no claim exists, allow it. */ 2127 if (!claim) 2128 goto allow; 2129 2130 /* check if we are responsible. */ 2131 backbone_gw = batadv_bla_claim_get_backbone_gw(claim); 2132 client_roamed = batadv_compare_eth(backbone_gw->orig, 2133 primary_if->net_dev->dev_addr); 2134 batadv_backbone_gw_put(backbone_gw); 2135 2136 if (client_roamed) { 2137 /* if yes, the client has roamed and we have 2138 * to unclaim it. 2139 */ 2140 if (batadv_has_timed_out(READ_ONCE(claim->lasttime), 100)) { 2141 /* only unclaim if the last claim entry is 2142 * older than 100 ms to make sure we really 2143 * have a roaming client here. 2144 */ 2145 batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Roaming client %pM detected. Unclaim it.\n", 2146 __func__, ethhdr->h_source); 2147 batadv_handle_unclaim(bat_priv, primary_if, 2148 primary_if->net_dev->dev_addr, 2149 ethhdr->h_source, vid); 2150 goto allow; 2151 } else { 2152 batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Race for claim %pM detected. Drop packet.\n", 2153 __func__, ethhdr->h_source); 2154 goto handled; 2155 } 2156 } 2157 2158 /* check if it is a multicast/broadcast frame */ 2159 if (is_multicast_ether_addr(ethhdr->h_dest)) { 2160 /* drop it. the responsible gateway has forwarded it into 2161 * the backbone network. 2162 */ 2163 goto handled; 2164 } else { 2165 /* we must allow it. at least if we are 2166 * responsible for the DESTINATION. 2167 */ 2168 goto allow; 2169 } 2170 allow: 2171 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid); 2172 ret = false; 2173 goto out; 2174 handled: 2175 ret = true; 2176 out: 2177 batadv_hardif_put(primary_if); 2178 batadv_claim_put(claim); 2179 return ret; 2180 } 2181 2182 /** 2183 * batadv_bla_claim_dump_entry() - dump one entry of the claim table 2184 * to a netlink socket 2185 * @msg: buffer for the message 2186 * @portid: netlink port 2187 * @cb: Control block containing additional options 2188 * @primary_if: primary interface 2189 * @claim: entry to dump 2190 * 2191 * Return: 0 or error code. 2192 */ 2193 static int 2194 batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid, 2195 struct netlink_callback *cb, 2196 struct batadv_hard_iface *primary_if, 2197 struct batadv_bla_claim *claim) 2198 { 2199 const u8 *primary_addr = primary_if->net_dev->dev_addr; 2200 struct batadv_bla_backbone_gw *backbone_gw; 2201 int ret = -EINVAL; 2202 u16 backbone_crc; 2203 bool is_own; 2204 void *hdr; 2205 2206 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq, 2207 &batadv_netlink_family, NLM_F_MULTI, 2208 BATADV_CMD_GET_BLA_CLAIM); 2209 if (!hdr) { 2210 ret = -ENOBUFS; 2211 goto out; 2212 } 2213 2214 genl_dump_check_consistent(cb, hdr); 2215 2216 backbone_gw = batadv_bla_claim_get_backbone_gw(claim); 2217 2218 is_own = batadv_compare_eth(backbone_gw->orig, primary_addr); 2219 2220 spin_lock_bh(&backbone_gw->crc_lock); 2221 backbone_crc = backbone_gw->crc; 2222 spin_unlock_bh(&backbone_gw->crc_lock); 2223 2224 if (is_own) 2225 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) { 2226 genlmsg_cancel(msg, hdr); 2227 goto put_backbone_gw; 2228 } 2229 2230 if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) || 2231 nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) || 2232 nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN, 2233 backbone_gw->orig) || 2234 nla_put_u16(msg, BATADV_ATTR_BLA_CRC, 2235 backbone_crc)) { 2236 genlmsg_cancel(msg, hdr); 2237 goto put_backbone_gw; 2238 } 2239 2240 genlmsg_end(msg, hdr); 2241 ret = 0; 2242 2243 put_backbone_gw: 2244 batadv_backbone_gw_put(backbone_gw); 2245 out: 2246 return ret; 2247 } 2248 2249 /** 2250 * batadv_bla_claim_dump_bucket() - dump one bucket of the claim table 2251 * to a netlink socket 2252 * @msg: buffer for the message 2253 * @portid: netlink port 2254 * @cb: Control block containing additional options 2255 * @primary_if: primary interface 2256 * @hash: hash to dump 2257 * @bucket: bucket index to dump 2258 * @idx_skip: How many entries to skip 2259 * 2260 * Return: always 0. 2261 */ 2262 static int 2263 batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid, 2264 struct netlink_callback *cb, 2265 struct batadv_hard_iface *primary_if, 2266 struct batadv_hashtable *hash, unsigned int bucket, 2267 int *idx_skip) 2268 { 2269 struct batadv_bla_claim *claim; 2270 int idx = 0; 2271 int ret = 0; 2272 2273 spin_lock_bh(&hash->list_locks[bucket]); 2274 cb->seq = atomic_read(&hash->generation) << 1 | 1; 2275 2276 hlist_for_each_entry(claim, &hash->table[bucket], hash_entry) { 2277 if (idx++ < *idx_skip) 2278 continue; 2279 2280 ret = batadv_bla_claim_dump_entry(msg, portid, cb, 2281 primary_if, claim); 2282 if (ret) { 2283 *idx_skip = idx - 1; 2284 goto unlock; 2285 } 2286 } 2287 2288 *idx_skip = 0; 2289 unlock: 2290 spin_unlock_bh(&hash->list_locks[bucket]); 2291 return ret; 2292 } 2293 2294 /** 2295 * batadv_bla_claim_dump() - dump claim table to a netlink socket 2296 * @msg: buffer for the message 2297 * @cb: callback structure containing arguments 2298 * 2299 * Return: message length. 2300 */ 2301 int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb) 2302 { 2303 struct batadv_hard_iface *primary_if = NULL; 2304 int portid = NETLINK_CB(cb->skb).portid; 2305 struct net_device *mesh_iface; 2306 struct batadv_hashtable *hash; 2307 struct batadv_priv *bat_priv; 2308 int bucket = cb->args[0]; 2309 int idx = cb->args[1]; 2310 int ret = 0; 2311 2312 mesh_iface = batadv_netlink_get_meshif(cb); 2313 if (IS_ERR(mesh_iface)) 2314 return PTR_ERR(mesh_iface); 2315 2316 bat_priv = netdev_priv(mesh_iface); 2317 hash = bat_priv->bla.claim_hash; 2318 2319 primary_if = batadv_primary_if_get_selected(bat_priv); 2320 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) { 2321 ret = -ENOENT; 2322 goto out; 2323 } 2324 2325 while (bucket < hash->size) { 2326 if (batadv_bla_claim_dump_bucket(msg, portid, cb, primary_if, 2327 hash, bucket, &idx)) 2328 break; 2329 bucket++; 2330 } 2331 2332 cb->args[0] = bucket; 2333 cb->args[1] = idx; 2334 2335 ret = msg->len; 2336 2337 out: 2338 batadv_hardif_put(primary_if); 2339 2340 dev_put(mesh_iface); 2341 2342 return ret; 2343 } 2344 2345 /** 2346 * batadv_bla_backbone_dump_entry() - dump one entry of the backbone table to a 2347 * netlink socket 2348 * @msg: buffer for the message 2349 * @portid: netlink port 2350 * @cb: Control block containing additional options 2351 * @primary_if: primary interface 2352 * @backbone_gw: entry to dump 2353 * 2354 * Return: 0 or error code. 2355 */ 2356 static int 2357 batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid, 2358 struct netlink_callback *cb, 2359 struct batadv_hard_iface *primary_if, 2360 struct batadv_bla_backbone_gw *backbone_gw) 2361 { 2362 const u8 *primary_addr = primary_if->net_dev->dev_addr; 2363 int ret = -EINVAL; 2364 u16 backbone_crc; 2365 bool is_own; 2366 int msecs; 2367 void *hdr; 2368 2369 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq, 2370 &batadv_netlink_family, NLM_F_MULTI, 2371 BATADV_CMD_GET_BLA_BACKBONE); 2372 if (!hdr) { 2373 ret = -ENOBUFS; 2374 goto out; 2375 } 2376 2377 genl_dump_check_consistent(cb, hdr); 2378 2379 is_own = batadv_compare_eth(backbone_gw->orig, primary_addr); 2380 2381 spin_lock_bh(&backbone_gw->crc_lock); 2382 backbone_crc = backbone_gw->crc; 2383 spin_unlock_bh(&backbone_gw->crc_lock); 2384 2385 msecs = jiffies_to_msecs(jiffies - READ_ONCE(backbone_gw->lasttime)); 2386 2387 if (is_own) 2388 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) { 2389 genlmsg_cancel(msg, hdr); 2390 goto out; 2391 } 2392 2393 if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN, 2394 backbone_gw->orig) || 2395 nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) || 2396 nla_put_u16(msg, BATADV_ATTR_BLA_CRC, 2397 backbone_crc) || 2398 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) { 2399 genlmsg_cancel(msg, hdr); 2400 goto out; 2401 } 2402 2403 genlmsg_end(msg, hdr); 2404 ret = 0; 2405 2406 out: 2407 return ret; 2408 } 2409 2410 /** 2411 * batadv_bla_backbone_dump_bucket() - dump one bucket of the backbone table to 2412 * a netlink socket 2413 * @msg: buffer for the message 2414 * @portid: netlink port 2415 * @cb: Control block containing additional options 2416 * @primary_if: primary interface 2417 * @hash: hash to dump 2418 * @bucket: bucket index to dump 2419 * @idx_skip: How many entries to skip 2420 * 2421 * Return: always 0. 2422 */ 2423 static int 2424 batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid, 2425 struct netlink_callback *cb, 2426 struct batadv_hard_iface *primary_if, 2427 struct batadv_hashtable *hash, 2428 unsigned int bucket, int *idx_skip) 2429 { 2430 struct batadv_bla_backbone_gw *backbone_gw; 2431 int idx = 0; 2432 int ret = 0; 2433 2434 spin_lock_bh(&hash->list_locks[bucket]); 2435 cb->seq = atomic_read(&hash->generation) << 1 | 1; 2436 2437 hlist_for_each_entry(backbone_gw, &hash->table[bucket], hash_entry) { 2438 if (idx++ < *idx_skip) 2439 continue; 2440 2441 ret = batadv_bla_backbone_dump_entry(msg, portid, cb, 2442 primary_if, backbone_gw); 2443 if (ret) { 2444 *idx_skip = idx - 1; 2445 goto unlock; 2446 } 2447 } 2448 2449 *idx_skip = 0; 2450 unlock: 2451 spin_unlock_bh(&hash->list_locks[bucket]); 2452 return ret; 2453 } 2454 2455 /** 2456 * batadv_bla_backbone_dump() - dump backbone table to a netlink socket 2457 * @msg: buffer for the message 2458 * @cb: callback structure containing arguments 2459 * 2460 * Return: message length. 2461 */ 2462 int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb) 2463 { 2464 struct batadv_hard_iface *primary_if = NULL; 2465 int portid = NETLINK_CB(cb->skb).portid; 2466 struct net_device *mesh_iface; 2467 struct batadv_hashtable *hash; 2468 struct batadv_priv *bat_priv; 2469 int bucket = cb->args[0]; 2470 int idx = cb->args[1]; 2471 int ret = 0; 2472 2473 mesh_iface = batadv_netlink_get_meshif(cb); 2474 if (IS_ERR(mesh_iface)) 2475 return PTR_ERR(mesh_iface); 2476 2477 bat_priv = netdev_priv(mesh_iface); 2478 hash = bat_priv->bla.backbone_hash; 2479 2480 primary_if = batadv_primary_if_get_selected(bat_priv); 2481 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) { 2482 ret = -ENOENT; 2483 goto out; 2484 } 2485 2486 while (bucket < hash->size) { 2487 if (batadv_bla_backbone_dump_bucket(msg, portid, cb, primary_if, 2488 hash, bucket, &idx)) 2489 break; 2490 bucket++; 2491 } 2492 2493 cb->args[0] = bucket; 2494 cb->args[1] = idx; 2495 2496 ret = msg->len; 2497 2498 out: 2499 batadv_hardif_put(primary_if); 2500 2501 dev_put(mesh_iface); 2502 2503 return ret; 2504 } 2505 2506 #ifdef CONFIG_BATMAN_ADV_DAT 2507 /** 2508 * batadv_bla_check_claim() - check if address is claimed 2509 * 2510 * @bat_priv: the bat priv with all the mesh interface information 2511 * @addr: mac address of which the claim status is checked 2512 * @vid: the VLAN ID 2513 * 2514 * addr is checked if this address is claimed by the local device itself. 2515 * 2516 * Return: true if bla is disabled or the mac is claimed by the device, 2517 * false if the device addr is already claimed by another gateway 2518 */ 2519 bool batadv_bla_check_claim(struct batadv_priv *bat_priv, 2520 u8 *addr, unsigned short vid) 2521 { 2522 struct batadv_hard_iface *primary_if = NULL; 2523 struct batadv_bla_backbone_gw *backbone_gw; 2524 struct batadv_bla_claim *claim = NULL; 2525 struct batadv_bla_claim search_claim; 2526 bool ret = true; 2527 2528 if (!READ_ONCE(bat_priv->bridge_loop_avoidance)) 2529 return ret; 2530 2531 primary_if = batadv_primary_if_get_selected(bat_priv); 2532 if (!primary_if) 2533 return ret; 2534 2535 /* First look if the mac address is claimed */ 2536 ether_addr_copy(search_claim.addr, addr); 2537 search_claim.vid = vid; 2538 2539 claim = batadv_claim_hash_find(bat_priv, &search_claim); 2540 2541 /* If there is a claim and we are not owner of the claim, 2542 * return false. 2543 */ 2544 if (claim) { 2545 backbone_gw = batadv_bla_claim_get_backbone_gw(claim); 2546 2547 if (!batadv_compare_eth(backbone_gw->orig, 2548 primary_if->net_dev->dev_addr)) 2549 ret = false; 2550 2551 batadv_backbone_gw_put(backbone_gw); 2552 batadv_claim_put(claim); 2553 } 2554 2555 batadv_hardif_put(primary_if); 2556 return ret; 2557 } 2558 #endif 2559