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