1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright 2011-2014 Autronica Fire and Security AS 3 * 4 * Author(s): 5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se 6 * 7 * The HSR spec says never to forward the same frame twice on the same 8 * interface. A frame is identified by its source MAC address and its HSR 9 * sequence number. This code keeps track of senders and their sequence numbers 10 * to allow filtering of duplicate frames, and to detect HSR ring errors. 11 * Same code handles filtering of duplicates for PRP as well. 12 */ 13 14 #include <kunit/visibility.h> 15 #include <linux/if_ether.h> 16 #include <linux/etherdevice.h> 17 #include <linux/slab.h> 18 #include <linux/rculist.h> 19 #include "hsr_main.h" 20 #include "hsr_framereg.h" 21 #include "hsr_netlink.h" 22 23 bool hsr_addr_is_redbox(struct hsr_priv *hsr, unsigned char *addr) 24 { 25 if (!hsr->redbox || !is_valid_ether_addr(hsr->macaddress_redbox)) 26 return false; 27 28 return ether_addr_equal(addr, hsr->macaddress_redbox); 29 } 30 31 bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr) 32 { 33 struct hsr_self_node *sn; 34 bool ret = false; 35 36 rcu_read_lock(); 37 sn = rcu_dereference(hsr->self_node); 38 if (!sn) { 39 WARN_ONCE(1, "HSR: No self node\n"); 40 goto out; 41 } 42 43 if (ether_addr_equal(addr, sn->macaddress_A) || 44 ether_addr_equal(addr, sn->macaddress_B)) 45 ret = true; 46 out: 47 rcu_read_unlock(); 48 return ret; 49 } 50 51 /* Search for mac entry. Caller must hold rcu read lock. 52 */ 53 static struct hsr_node *find_node_by_addr_A(struct list_head *node_db, 54 const unsigned char addr[ETH_ALEN]) 55 { 56 struct hsr_node *node; 57 58 list_for_each_entry_rcu(node, node_db, mac_list) { 59 if (ether_addr_equal(node->macaddress_A, addr)) 60 return node; 61 } 62 63 return NULL; 64 } 65 66 /* Check if node for a given MAC address is already present in data base 67 */ 68 bool hsr_is_node_in_db(struct list_head *node_db, 69 const unsigned char addr[ETH_ALEN]) 70 { 71 return !!find_node_by_addr_A(node_db, addr); 72 } 73 74 /* Helper for device init; the self_node is used in hsr_rcv() to recognize 75 * frames from self that's been looped over the HSR ring. 76 */ 77 int hsr_create_self_node(struct hsr_priv *hsr, 78 const unsigned char addr_a[ETH_ALEN], 79 const unsigned char addr_b[ETH_ALEN]) 80 { 81 struct hsr_self_node *sn, *old; 82 83 sn = kmalloc_obj(*sn); 84 if (!sn) 85 return -ENOMEM; 86 87 ether_addr_copy(sn->macaddress_A, addr_a); 88 ether_addr_copy(sn->macaddress_B, addr_b); 89 90 spin_lock_bh(&hsr->list_lock); 91 old = rcu_replace_pointer(hsr->self_node, sn, 92 lockdep_is_held(&hsr->list_lock)); 93 spin_unlock_bh(&hsr->list_lock); 94 95 if (old) 96 kfree_rcu(old, rcu_head); 97 return 0; 98 } 99 100 void hsr_del_self_node(struct hsr_priv *hsr) 101 { 102 struct hsr_self_node *old; 103 104 spin_lock_bh(&hsr->list_lock); 105 old = rcu_replace_pointer(hsr->self_node, NULL, 106 lockdep_is_held(&hsr->list_lock)); 107 spin_unlock_bh(&hsr->list_lock); 108 if (old) 109 kfree_rcu(old, rcu_head); 110 } 111 112 static void hsr_free_node(struct hsr_node *node) 113 { 114 xa_destroy(&node->seq_blocks); 115 kfree(node->block_buf); 116 kfree(node); 117 } 118 119 static void hsr_free_node_rcu(struct rcu_head *rn) 120 { 121 struct hsr_node *node = container_of(rn, struct hsr_node, rcu_head); 122 123 hsr_free_node(node); 124 } 125 126 static void hsr_lock_seq_out_pair(struct hsr_node *node_a, 127 struct hsr_node *node_b) 128 { 129 if (node_a == node_b) { 130 spin_lock_bh(&node_a->seq_out_lock); 131 return; 132 } 133 134 if (node_a < node_b) { 135 spin_lock_bh(&node_a->seq_out_lock); 136 spin_lock_nested(&node_b->seq_out_lock, SINGLE_DEPTH_NESTING); 137 } else { 138 spin_lock_bh(&node_b->seq_out_lock); 139 spin_lock_nested(&node_a->seq_out_lock, SINGLE_DEPTH_NESTING); 140 } 141 } 142 143 static void hsr_unlock_seq_out_pair(struct hsr_node *node_a, 144 struct hsr_node *node_b) 145 { 146 if (node_a == node_b) { 147 spin_unlock_bh(&node_a->seq_out_lock); 148 return; 149 } 150 151 if (node_a < node_b) { 152 spin_unlock(&node_b->seq_out_lock); 153 spin_unlock_bh(&node_a->seq_out_lock); 154 } else { 155 spin_unlock(&node_a->seq_out_lock); 156 spin_unlock_bh(&node_b->seq_out_lock); 157 } 158 } 159 160 void hsr_del_nodes(struct list_head *node_db) 161 { 162 struct hsr_node *node; 163 struct hsr_node *tmp; 164 165 list_for_each_entry_safe(node, tmp, node_db, mac_list) { 166 list_del(&node->mac_list); 167 hsr_free_node(node); 168 } 169 } 170 171 void prp_handle_san_frame(bool san, enum hsr_port_type port, 172 struct hsr_node *node) 173 { 174 /* Mark if the SAN node is over LAN_A or LAN_B */ 175 if (port == HSR_PT_SLAVE_A) { 176 node->san_a = true; 177 return; 178 } 179 180 if (port == HSR_PT_SLAVE_B) 181 node->san_b = true; 182 } 183 184 /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A. 185 */ 186 static struct hsr_node *hsr_add_node(struct hsr_priv *hsr, 187 struct list_head *node_db, 188 unsigned char addr[], bool san, 189 enum hsr_port_type rx_port) 190 { 191 struct hsr_node *new_node, *node = NULL; 192 unsigned long now; 193 size_t block_sz; 194 int i; 195 196 new_node = kzalloc_obj(*new_node, GFP_ATOMIC); 197 if (!new_node) 198 return NULL; 199 200 ether_addr_copy(new_node->macaddress_A, addr); 201 spin_lock_init(&new_node->seq_out_lock); 202 203 if (hsr->prot_version == PRP_V1) 204 new_node->seq_port_cnt = 1; 205 else 206 new_node->seq_port_cnt = HSR_PT_PORTS - 1; 207 208 block_sz = hsr_seq_block_size(new_node); 209 new_node->block_buf = kcalloc(HSR_MAX_SEQ_BLOCKS, block_sz, GFP_ATOMIC); 210 if (!new_node->block_buf) 211 goto free; 212 213 xa_init(&new_node->seq_blocks); 214 215 /* We are only interested in time diffs here, so use current jiffies 216 * as initialization. (0 could trigger an spurious ring error warning). 217 */ 218 now = jiffies; 219 for (i = 0; i < HSR_PT_PORTS; i++) { 220 new_node->time_in[i] = now; 221 } 222 223 if (san && hsr->proto_ops->handle_san_frame) 224 hsr->proto_ops->handle_san_frame(san, rx_port, new_node); 225 226 spin_lock_bh(&hsr->list_lock); 227 list_for_each_entry_rcu(node, node_db, mac_list, 228 lockdep_is_held(&hsr->list_lock)) { 229 if (ether_addr_equal(node->macaddress_A, addr)) 230 goto out; 231 if (ether_addr_equal(node->macaddress_B, addr)) 232 goto out; 233 } 234 list_add_tail_rcu(&new_node->mac_list, node_db); 235 spin_unlock_bh(&hsr->list_lock); 236 return new_node; 237 out: 238 spin_unlock_bh(&hsr->list_lock); 239 kfree(new_node->block_buf); 240 free: 241 kfree(new_node); 242 return node; 243 } 244 245 void prp_update_san_info(struct hsr_node *node, bool is_sup) 246 { 247 if (!is_sup) 248 return; 249 250 node->san_a = false; 251 node->san_b = false; 252 } 253 254 /* Get the hsr_node from which 'skb' was sent. 255 */ 256 struct hsr_node *hsr_get_node(struct hsr_port *port, struct list_head *node_db, 257 struct sk_buff *skb, bool is_sup, 258 enum hsr_port_type rx_port) 259 { 260 struct hsr_priv *hsr = port->hsr; 261 struct hsr_node *node; 262 struct ethhdr *ethhdr; 263 struct prp_rct *rct; 264 bool san = false; 265 266 if (!skb_mac_header_was_set(skb)) 267 return NULL; 268 269 ethhdr = (struct ethhdr *)skb_mac_header(skb); 270 271 list_for_each_entry_rcu(node, node_db, mac_list) { 272 if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) { 273 if (hsr->proto_ops->update_san_info) 274 hsr->proto_ops->update_san_info(node, is_sup); 275 return node; 276 } 277 if (ether_addr_equal(node->macaddress_B, ethhdr->h_source)) { 278 if (hsr->proto_ops->update_san_info) 279 hsr->proto_ops->update_san_info(node, is_sup); 280 return node; 281 } 282 } 283 284 /* Check if required node is not in proxy nodes table */ 285 list_for_each_entry_rcu(node, &hsr->proxy_node_db, mac_list) { 286 if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) { 287 if (hsr->proto_ops->update_san_info) 288 hsr->proto_ops->update_san_info(node, is_sup); 289 return node; 290 } 291 } 292 293 /* Everyone may create a node entry, connected node to a HSR/PRP 294 * device. 295 */ 296 if (ethhdr->h_proto == htons(ETH_P_PRP) || 297 ethhdr->h_proto == htons(ETH_P_HSR)) { 298 /* Check if skb contains hsr_ethhdr */ 299 if (skb->mac_len < sizeof(struct hsr_ethhdr)) 300 return NULL; 301 } else { 302 rct = skb_get_PRP_rct(skb); 303 if (!rct && rx_port != HSR_PT_MASTER) 304 san = true; 305 } 306 307 return hsr_add_node(hsr, node_db, ethhdr->h_source, san, rx_port); 308 } 309 310 static bool hsr_seq_block_is_old(struct hsr_seq_block *block) 311 { 312 unsigned long expiry = msecs_to_jiffies(HSR_ENTRY_FORGET_TIME); 313 314 return time_is_before_jiffies(block->time + expiry); 315 } 316 317 static void hsr_forget_seq_block(struct hsr_node *node, 318 struct hsr_seq_block *block) 319 { 320 if (block->time) 321 xa_erase(&node->seq_blocks, block->block_idx); 322 block->time = 0; 323 } 324 325 /* Get the currently active sequence number block. If there is no block yet, or 326 * the existing one is expired, a new block is created. The idea is to maintain 327 * a "sparse bitmap" where a bitmap for the whole sequence number space is 328 * split into blocks and not all blocks exist all the time. The blocks can 329 * expire after time (in low traffic situations) or when they are replaced in 330 * the backing fixed size buffer (in high traffic situations). 331 */ 332 VISIBLE_IF_KUNIT struct hsr_seq_block *hsr_get_seq_block(struct hsr_node *node, 333 u16 block_idx) 334 { 335 struct hsr_seq_block *block, *res; 336 size_t block_sz; 337 338 block = xa_load(&node->seq_blocks, block_idx); 339 340 if (block && hsr_seq_block_is_old(block)) { 341 hsr_forget_seq_block(node, block); 342 block = NULL; 343 } 344 345 if (!block) { 346 block_sz = hsr_seq_block_size(node); 347 block = node->block_buf + node->next_block * block_sz; 348 hsr_forget_seq_block(node, block); 349 350 memset(block, 0, block_sz); 351 block->time = jiffies; 352 block->block_idx = block_idx; 353 354 res = xa_store(&node->seq_blocks, block_idx, block, GFP_ATOMIC); 355 if (xa_is_err(res)) { 356 block->time = 0; 357 return NULL; 358 } 359 360 node->next_block = 361 (node->next_block + 1) & (HSR_MAX_SEQ_BLOCKS - 1); 362 } 363 364 return block; 365 } 366 EXPORT_SYMBOL_IF_KUNIT(hsr_get_seq_block); 367 368 /* Use the Supervision frame's info about an eventual macaddress_B for merging 369 * nodes that has previously had their macaddress_B registered as a separate 370 * node. 371 */ 372 void hsr_handle_sup_frame(struct hsr_frame_info *frame) 373 { 374 struct hsr_node *node_curr = frame->node_src; 375 struct hsr_port *port_rcv = frame->port_rcv; 376 struct hsr_seq_block *src_blk, *merge_blk; 377 struct hsr_priv *hsr = port_rcv->hsr; 378 struct hsr_sup_tlv *hsr_sup_tlv; 379 struct hsr_sup_payload *hsr_sp; 380 struct hsr_node *node_real; 381 struct sk_buff *skb = NULL; 382 struct list_head *node_db; 383 struct ethhdr *ethhdr; 384 unsigned int total_pull_size = 0; 385 unsigned int pull_size = 0; 386 unsigned long idx; 387 int i; 388 389 /* Here either frame->skb_hsr or frame->skb_prp should be 390 * valid as supervision frame always will have protocol 391 * header info. 392 */ 393 if (frame->skb_hsr) 394 skb = frame->skb_hsr; 395 else if (frame->skb_prp) 396 skb = frame->skb_prp; 397 else if (frame->skb_std) 398 skb = frame->skb_std; 399 if (!skb) 400 return; 401 402 /* Leave the ethernet header. */ 403 pull_size = sizeof(struct ethhdr); 404 skb_pull(skb, pull_size); 405 total_pull_size += pull_size; 406 407 ethhdr = (struct ethhdr *)skb_mac_header(skb); 408 409 /* And leave the HSR tag. */ 410 if (ethhdr->h_proto == htons(ETH_P_HSR)) { 411 pull_size = sizeof(struct hsr_tag); 412 skb_pull(skb, pull_size); 413 total_pull_size += pull_size; 414 } 415 416 /* And leave the HSR sup tag. */ 417 pull_size = sizeof(struct hsr_sup_tag); 418 skb_pull(skb, pull_size); 419 total_pull_size += pull_size; 420 421 /* get HSR sup payload */ 422 hsr_sp = (struct hsr_sup_payload *)skb->data; 423 424 /* Merge node_curr (registered on macaddress_B) into node_real */ 425 node_db = &port_rcv->hsr->node_db; 426 node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A); 427 if (!node_real) 428 /* No frame received from AddrA of this node yet */ 429 node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A, 430 true, port_rcv->type); 431 if (!node_real) 432 goto done; /* No mem */ 433 if (node_real == node_curr) 434 /* Node has already been merged */ 435 goto done; 436 437 /* Leave the first HSR sup payload. */ 438 pull_size = sizeof(struct hsr_sup_payload); 439 skb_pull(skb, pull_size); 440 total_pull_size += pull_size; 441 442 /* Get second supervision tlv */ 443 hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data; 444 /* And check if it is a redbox mac TLV */ 445 if (hsr_sup_tlv->HSR_TLV_type == PRP_TLV_REDBOX_MAC) { 446 /* We could stop here after pushing hsr_sup_payload, 447 * or proceed and allow macaddress_B and for redboxes. 448 */ 449 /* Sanity check length */ 450 if (hsr_sup_tlv->HSR_TLV_length != 6) 451 goto done; 452 453 /* Leave the second HSR sup tlv. */ 454 pull_size = sizeof(struct hsr_sup_tlv); 455 skb_pull(skb, pull_size); 456 total_pull_size += pull_size; 457 458 /* Get redbox mac address. */ 459 hsr_sp = (struct hsr_sup_payload *)skb->data; 460 461 /* Check if redbox mac and node mac are equal. */ 462 if (!ether_addr_equal(node_real->macaddress_A, hsr_sp->macaddress_A)) { 463 /* This is a redbox supervision frame for a VDAN! */ 464 goto done; 465 } 466 } 467 468 ether_addr_copy(node_real->macaddress_B, ethhdr->h_source); 469 hsr_lock_seq_out_pair(node_real, node_curr); 470 for (i = 0; i < HSR_PT_PORTS; i++) { 471 if (!node_curr->time_in_stale[i] && 472 time_after(node_curr->time_in[i], node_real->time_in[i])) { 473 node_real->time_in[i] = node_curr->time_in[i]; 474 node_real->time_in_stale[i] = 475 node_curr->time_in_stale[i]; 476 } 477 } 478 479 xa_for_each(&node_curr->seq_blocks, idx, src_blk) { 480 if (hsr_seq_block_is_old(src_blk)) 481 continue; 482 483 merge_blk = hsr_get_seq_block(node_real, src_blk->block_idx); 484 if (!merge_blk) 485 continue; 486 merge_blk->time = min(merge_blk->time, src_blk->time); 487 for (i = 0; i < node_real->seq_port_cnt; i++) { 488 bitmap_or(merge_blk->seq_nrs[i], merge_blk->seq_nrs[i], 489 src_blk->seq_nrs[i], HSR_SEQ_BLOCK_SIZE); 490 } 491 } 492 hsr_unlock_seq_out_pair(node_real, node_curr); 493 node_real->addr_B_port = port_rcv->type; 494 495 spin_lock_bh(&hsr->list_lock); 496 if (!node_curr->removed) { 497 list_del_rcu(&node_curr->mac_list); 498 node_curr->removed = true; 499 call_rcu(&node_curr->rcu_head, hsr_free_node_rcu); 500 } 501 spin_unlock_bh(&hsr->list_lock); 502 503 done: 504 /* Push back here */ 505 skb_push(skb, total_pull_size); 506 } 507 508 /* 'skb' is a frame meant for this host, that is to be passed to upper layers. 509 * 510 * If the frame was sent by a node's B interface, replace the source 511 * address with that node's "official" address (macaddress_A) so that upper 512 * layers recognize where it came from. 513 */ 514 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb) 515 { 516 if (!skb_mac_header_was_set(skb)) { 517 WARN_ONCE(1, "%s: Mac header not set\n", __func__); 518 return; 519 } 520 521 memcpy(ð_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN); 522 } 523 524 /* 'skb' is a frame meant for another host. 525 * 'port' is the outgoing interface 526 * 527 * Substitute the target (dest) MAC address if necessary, so the it matches the 528 * recipient interface MAC address, regardless of whether that is the 529 * recipient's A or B interface. 530 * This is needed to keep the packets flowing through switches that learn on 531 * which "side" the different interfaces are. 532 */ 533 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb, 534 struct hsr_port *port) 535 { 536 struct hsr_node *node_dst; 537 538 if (!skb_mac_header_was_set(skb)) { 539 WARN_ONCE(1, "%s: Mac header not set\n", __func__); 540 return; 541 } 542 543 if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest)) 544 return; 545 546 node_dst = find_node_by_addr_A(&port->hsr->node_db, 547 eth_hdr(skb)->h_dest); 548 if (!node_dst && port->hsr->redbox) 549 node_dst = find_node_by_addr_A(&port->hsr->proxy_node_db, 550 eth_hdr(skb)->h_dest); 551 552 if (!node_dst) { 553 if (port->hsr->prot_version != PRP_V1 && net_ratelimit()) 554 netdev_err(skb->dev, "%s: Unknown node\n", __func__); 555 return; 556 } 557 if (port->type != node_dst->addr_B_port) 558 return; 559 560 if (is_valid_ether_addr(node_dst->macaddress_B)) 561 ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B); 562 } 563 564 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port, 565 u16 sequence_nr) 566 { 567 node->time_in[port->type] = jiffies; 568 node->time_in_stale[port->type] = false; 569 } 570 571 /* Duplicate discard algorithm: we maintain a bitmap where we set a bit for 572 * every seen sequence number. The bitmap is split into blocks and the block 573 * management is detailed in hsr_get_seq_block(). In any case, we err on the 574 * side of accepting a packet, as the specification requires the algorithm to 575 * be "designed such that it never rejects a legitimate frame, while occasional 576 * acceptance of a duplicate can be tolerated." (IEC 62439-3:2021, 4.1.10.3). 577 * While this requirement is explicit for PRP, applying it to HSR does no harm 578 * either. 579 * 580 * 'frame' is the frame to be sent 581 * 'port_type' is the type of the outgoing interface 582 * 583 * Return: 584 * 1 if frame can be shown to have been sent recently on this interface, 585 * 0 otherwise 586 */ 587 static int hsr_check_duplicate(struct hsr_frame_info *frame, 588 unsigned int port_type) 589 { 590 u16 sequence_nr, seq_bit, block_idx; 591 struct hsr_seq_block *block; 592 struct hsr_node *node; 593 594 node = frame->node_src; 595 sequence_nr = frame->sequence_nr; 596 597 if (WARN_ON_ONCE(port_type >= node->seq_port_cnt)) 598 return 0; 599 600 spin_lock_bh(&node->seq_out_lock); 601 602 block_idx = hsr_seq_block_index(sequence_nr); 603 block = hsr_get_seq_block(node, block_idx); 604 if (!block) 605 goto out_new; 606 607 seq_bit = hsr_seq_block_bit(sequence_nr); 608 if (__test_and_set_bit(seq_bit, block->seq_nrs[port_type])) 609 goto out_seen; 610 611 out_new: 612 spin_unlock_bh(&node->seq_out_lock); 613 return 0; 614 615 out_seen: 616 spin_unlock_bh(&node->seq_out_lock); 617 return 1; 618 } 619 620 /* HSR duplicate discard: we check if the same frame has already been sent on 621 * this outgoing interface. The check follows the general duplicate discard 622 * algorithm. 623 * 624 * 'port' is the outgoing interface 625 * 'frame' is the frame to be sent 626 * 627 * Return: 628 * 1 if frame can be shown to have been sent recently on this interface, 629 * 0 otherwise 630 */ 631 int hsr_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame) 632 { 633 return hsr_check_duplicate(frame, port->type - 1); 634 } 635 636 /* PRP duplicate discard: we only consider frames that are received on port A 637 * or port B and should go to the master port. For those, we check if they have 638 * already been received by the host, i.e., master port. The check uses the 639 * general duplicate discard algorithm, but without tracking multiple ports. 640 * 641 * 'port' is the outgoing interface 642 * 'frame' is the frame to be sent 643 * 644 * Return: 645 * 1 if frame can be shown to have been sent recently on this interface, 646 * 0 otherwise 647 */ 648 int prp_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame) 649 { 650 /* out-going frames are always in order */ 651 if (frame->port_rcv->type == HSR_PT_MASTER) 652 return 0; 653 654 /* for PRP we should only forward frames from the slave ports 655 * to the master port 656 */ 657 if (port->type != HSR_PT_MASTER) 658 return 1; 659 660 return hsr_check_duplicate(frame, 0); 661 } 662 EXPORT_SYMBOL_IF_KUNIT(prp_register_frame_out); 663 664 static struct hsr_port *get_late_port(struct hsr_priv *hsr, 665 struct hsr_node *node) 666 { 667 if (node->time_in_stale[HSR_PT_SLAVE_A]) 668 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A); 669 if (node->time_in_stale[HSR_PT_SLAVE_B]) 670 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); 671 672 if (time_after(node->time_in[HSR_PT_SLAVE_B], 673 node->time_in[HSR_PT_SLAVE_A] + 674 msecs_to_jiffies(MAX_SLAVE_DIFF))) 675 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A); 676 if (time_after(node->time_in[HSR_PT_SLAVE_A], 677 node->time_in[HSR_PT_SLAVE_B] + 678 msecs_to_jiffies(MAX_SLAVE_DIFF))) 679 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); 680 681 return NULL; 682 } 683 684 /* Remove stale sequence_nr records. Called by timer every 685 * HSR_LIFE_CHECK_INTERVAL (two seconds or so). 686 */ 687 void hsr_prune_nodes(struct timer_list *t) 688 { 689 struct hsr_priv *hsr = timer_container_of(hsr, t, prune_timer); 690 struct hsr_node *node; 691 struct hsr_node *tmp; 692 struct hsr_port *port; 693 unsigned long timestamp; 694 unsigned long time_a, time_b; 695 696 spin_lock_bh(&hsr->list_lock); 697 list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) { 698 /* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A] 699 * nor time_in[HSR_PT_SLAVE_B], will ever be updated for 700 * the master port. Thus the master node will be repeatedly 701 * pruned leading to packet loss. 702 */ 703 if (hsr_addr_is_self(hsr, node->macaddress_A)) 704 continue; 705 706 /* Shorthand */ 707 time_a = node->time_in[HSR_PT_SLAVE_A]; 708 time_b = node->time_in[HSR_PT_SLAVE_B]; 709 710 /* Check for timestamps old enough to risk wrap-around */ 711 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2)) 712 node->time_in_stale[HSR_PT_SLAVE_A] = true; 713 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2)) 714 node->time_in_stale[HSR_PT_SLAVE_B] = true; 715 716 /* Get age of newest frame from node. 717 * At least one time_in is OK here; nodes get pruned long 718 * before both time_ins can get stale 719 */ 720 timestamp = time_a; 721 if (node->time_in_stale[HSR_PT_SLAVE_A] || 722 (!node->time_in_stale[HSR_PT_SLAVE_B] && 723 time_after(time_b, time_a))) 724 timestamp = time_b; 725 726 /* Warn of ring error only as long as we get frames at all */ 727 if (time_is_after_jiffies(timestamp + 728 msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) { 729 rcu_read_lock(); 730 port = get_late_port(hsr, node); 731 if (port) 732 hsr_nl_ringerror(hsr, node->macaddress_A, port); 733 rcu_read_unlock(); 734 } 735 736 /* Prune old entries */ 737 if (time_is_before_jiffies(timestamp + 738 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) { 739 hsr_nl_nodedown(hsr, node->macaddress_A); 740 if (!node->removed) { 741 list_del_rcu(&node->mac_list); 742 node->removed = true; 743 /* Note that we need to free this entry later: */ 744 call_rcu(&node->rcu_head, hsr_free_node_rcu); 745 } 746 } 747 } 748 spin_unlock_bh(&hsr->list_lock); 749 750 /* Restart timer */ 751 mod_timer(&hsr->prune_timer, 752 jiffies + msecs_to_jiffies(PRUNE_PERIOD)); 753 } 754 755 void hsr_prune_proxy_nodes(struct timer_list *t) 756 { 757 struct hsr_priv *hsr = timer_container_of(hsr, t, prune_proxy_timer); 758 unsigned long timestamp; 759 struct hsr_node *node; 760 struct hsr_node *tmp; 761 762 spin_lock_bh(&hsr->list_lock); 763 list_for_each_entry_safe(node, tmp, &hsr->proxy_node_db, mac_list) { 764 /* Don't prune RedBox node. */ 765 if (hsr_addr_is_redbox(hsr, node->macaddress_A)) 766 continue; 767 768 timestamp = node->time_in[HSR_PT_INTERLINK]; 769 770 /* Prune old entries */ 771 if (time_is_before_jiffies(timestamp + 772 msecs_to_jiffies(HSR_PROXY_NODE_FORGET_TIME))) { 773 hsr_nl_nodedown(hsr, node->macaddress_A); 774 if (!node->removed) { 775 list_del_rcu(&node->mac_list); 776 node->removed = true; 777 /* Note that we need to free this entry later: */ 778 call_rcu(&node->rcu_head, hsr_free_node_rcu); 779 } 780 } 781 } 782 783 spin_unlock_bh(&hsr->list_lock); 784 785 /* Restart timer */ 786 mod_timer(&hsr->prune_proxy_timer, 787 jiffies + msecs_to_jiffies(PRUNE_PROXY_PERIOD)); 788 } 789 790 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos, 791 unsigned char addr[ETH_ALEN]) 792 { 793 struct hsr_node *node; 794 795 if (!_pos) { 796 node = list_first_or_null_rcu(&hsr->node_db, 797 struct hsr_node, mac_list); 798 if (node) 799 ether_addr_copy(addr, node->macaddress_A); 800 return node; 801 } 802 803 node = _pos; 804 list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) { 805 ether_addr_copy(addr, node->macaddress_A); 806 return node; 807 } 808 809 return NULL; 810 } 811 812 /* Fill the last sequence number that has been received from node on if1 by 813 * finding the last sequence number sent on port B; accordingly get the last 814 * received sequence number for if2 using sent sequence numbers on port A. 815 */ 816 static void fill_last_seq_nrs(struct hsr_node *node, u16 *if1_seq, u16 *if2_seq) 817 { 818 struct hsr_seq_block *block; 819 unsigned int block_off; 820 size_t block_sz; 821 u16 seq_bit; 822 823 spin_lock_bh(&node->seq_out_lock); 824 825 /* Get last inserted block */ 826 block_off = (node->next_block - 1) & (HSR_MAX_SEQ_BLOCKS - 1); 827 block_sz = hsr_seq_block_size(node); 828 block = node->block_buf + block_off * block_sz; 829 830 if (!bitmap_empty(block->seq_nrs[HSR_PT_SLAVE_B - 1], 831 HSR_SEQ_BLOCK_SIZE)) { 832 seq_bit = find_last_bit(block->seq_nrs[HSR_PT_SLAVE_B - 1], 833 HSR_SEQ_BLOCK_SIZE); 834 *if1_seq = (block->block_idx << HSR_SEQ_BLOCK_SHIFT) | seq_bit; 835 } 836 if (!bitmap_empty(block->seq_nrs[HSR_PT_SLAVE_A - 1], 837 HSR_SEQ_BLOCK_SIZE)) { 838 seq_bit = find_last_bit(block->seq_nrs[HSR_PT_SLAVE_A - 1], 839 HSR_SEQ_BLOCK_SIZE); 840 *if2_seq = (block->block_idx << HSR_SEQ_BLOCK_SHIFT) | seq_bit; 841 } 842 spin_unlock_bh(&node->seq_out_lock); 843 } 844 845 int hsr_get_node_data(struct hsr_priv *hsr, 846 const unsigned char *addr, 847 unsigned char addr_b[ETH_ALEN], 848 unsigned int *addr_b_ifindex, 849 int *if1_age, 850 u16 *if1_seq, 851 int *if2_age, 852 u16 *if2_seq) 853 { 854 struct hsr_node *node; 855 struct hsr_port *port; 856 unsigned long tdiff; 857 858 node = find_node_by_addr_A(&hsr->node_db, addr); 859 if (!node) 860 return -ENOENT; 861 862 ether_addr_copy(addr_b, node->macaddress_B); 863 864 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A]; 865 if (node->time_in_stale[HSR_PT_SLAVE_A]) 866 *if1_age = INT_MAX; 867 #if HZ <= MSEC_PER_SEC 868 else if (tdiff > msecs_to_jiffies(INT_MAX)) 869 *if1_age = INT_MAX; 870 #endif 871 else 872 *if1_age = jiffies_to_msecs(tdiff); 873 874 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B]; 875 if (node->time_in_stale[HSR_PT_SLAVE_B]) 876 *if2_age = INT_MAX; 877 #if HZ <= MSEC_PER_SEC 878 else if (tdiff > msecs_to_jiffies(INT_MAX)) 879 *if2_age = INT_MAX; 880 #endif 881 else 882 *if2_age = jiffies_to_msecs(tdiff); 883 884 /* Present sequence numbers as if they were incoming on interface */ 885 *if1_seq = 0; 886 *if2_seq = 0; 887 if (hsr->prot_version != PRP_V1) 888 fill_last_seq_nrs(node, if1_seq, if2_seq); 889 890 if (node->addr_B_port != HSR_PT_NONE) { 891 port = hsr_port_get_hsr(hsr, node->addr_B_port); 892 *addr_b_ifindex = port->dev->ifindex; 893 } else { 894 *addr_b_ifindex = -1; 895 } 896 897 return 0; 898 } 899