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