^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright 2011-2014 Autronica Fire and Security AS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author(s):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * The HSR spec says never to forward the same frame twice on the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * interface. A frame is identified by its source MAC address and its HSR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * sequence number. This code keeps track of senders and their sequence numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * to allow filtering of duplicate frames, and to detect HSR ring errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Same code handles filtering of duplicates for PRP as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "hsr_main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "hsr_framereg.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "hsr_netlink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* TODO: use hash lists for mac addresses (linux/jhash.h)? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static bool seq_nr_after(u16 a, u16 b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* Remove inconsistency where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * seq_nr_after(a, b) == seq_nr_before(a, b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if ((int)b - a == 32768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return (((s16)(b - a)) < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define seq_nr_before(a, b) seq_nr_after((b), (a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct hsr_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) mac_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) WARN_ONCE(1, "HSR: No self node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (ether_addr_equal(addr, node->macaddress_A))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (ether_addr_equal(addr, node->macaddress_B))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* Search for mac entry. Caller must hold rcu read lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) const unsigned char addr[ETH_ALEN])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct hsr_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) list_for_each_entry_rcu(node, node_db, mac_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (ether_addr_equal(node->macaddress_A, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * frames from self that's been looped over the HSR ring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int hsr_create_self_node(struct hsr_priv *hsr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned char addr_a[ETH_ALEN],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned char addr_b[ETH_ALEN])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct list_head *self_node_db = &hsr->self_node_db;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct hsr_node *node, *oldnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) node = kmalloc(sizeof(*node), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ether_addr_copy(node->macaddress_A, addr_a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ether_addr_copy(node->macaddress_B, addr_b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) spin_lock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) oldnode = list_first_or_null_rcu(self_node_db,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct hsr_node, mac_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (oldnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) list_replace_rcu(&oldnode->mac_list, &node->mac_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) spin_unlock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) kfree_rcu(oldnode, rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) list_add_tail_rcu(&node->mac_list, self_node_db);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) spin_unlock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void hsr_del_self_node(struct hsr_priv *hsr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct list_head *self_node_db = &hsr->self_node_db;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct hsr_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) spin_lock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) node = list_first_or_null_rcu(self_node_db, struct hsr_node, mac_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) list_del_rcu(&node->mac_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) kfree_rcu(node, rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) spin_unlock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) void hsr_del_nodes(struct list_head *node_db)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct hsr_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct hsr_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) list_for_each_entry_safe(node, tmp, node_db, mac_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) kfree(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) void prp_handle_san_frame(bool san, enum hsr_port_type port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct hsr_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /* Mark if the SAN node is over LAN_A or LAN_B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (port == HSR_PT_SLAVE_A) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) node->san_a = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (port == HSR_PT_SLAVE_B)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) node->san_b = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * seq_out is used to initialize filtering of outgoing duplicate frames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * originating from the newly added node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct list_head *node_db,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) unsigned char addr[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) u16 seq_out, bool san,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) enum hsr_port_type rx_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct hsr_node *new_node, *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned long now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!new_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ether_addr_copy(new_node->macaddress_A, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* We are only interested in time diffs here, so use current jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * as initialization. (0 could trigger an spurious ring error warning).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) now = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) for (i = 0; i < HSR_PT_PORTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) new_node->time_in[i] = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) new_node->time_out[i] = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) for (i = 0; i < HSR_PT_PORTS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) new_node->seq_out[i] = seq_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (san && hsr->proto_ops->handle_san_frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) hsr->proto_ops->handle_san_frame(san, rx_port, new_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) spin_lock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) list_for_each_entry_rcu(node, node_db, mac_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) lockdep_is_held(&hsr->list_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (ether_addr_equal(node->macaddress_A, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (ether_addr_equal(node->macaddress_B, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) list_add_tail_rcu(&new_node->mac_list, node_db);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) spin_unlock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return new_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) spin_unlock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) kfree(new_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) void prp_update_san_info(struct hsr_node *node, bool is_sup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (!is_sup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) node->san_a = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) node->san_b = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* Get the hsr_node from which 'skb' was sent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct hsr_node *hsr_get_node(struct hsr_port *port, struct list_head *node_db,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct sk_buff *skb, bool is_sup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) enum hsr_port_type rx_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct hsr_priv *hsr = port->hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct hsr_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct prp_rct *rct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) bool san = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) u16 seq_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (!skb_mac_header_was_set(skb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) ethhdr = (struct ethhdr *)skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) list_for_each_entry_rcu(node, node_db, mac_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (hsr->proto_ops->update_san_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) hsr->proto_ops->update_san_info(node, is_sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (ether_addr_equal(node->macaddress_B, ethhdr->h_source)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (hsr->proto_ops->update_san_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) hsr->proto_ops->update_san_info(node, is_sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* Everyone may create a node entry, connected node to a HSR/PRP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (ethhdr->h_proto == htons(ETH_P_PRP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ethhdr->h_proto == htons(ETH_P_HSR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* Use the existing sequence_nr from the tag as starting point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * for filtering duplicate frames.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) seq_out = hsr_get_skb_sequence_nr(skb) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) rct = skb_get_PRP_rct(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (rct && prp_check_lsdu_size(skb, rct, is_sup)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) seq_out = prp_get_skb_sequence_nr(rct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (rx_port != HSR_PT_MASTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) san = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) seq_out = HSR_SEQNR_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return hsr_add_node(hsr, node_db, ethhdr->h_source, seq_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) san, rx_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* Use the Supervision frame's info about an eventual macaddress_B for merging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * nodes that has previously had their macaddress_B registered as a separate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) void hsr_handle_sup_frame(struct hsr_frame_info *frame)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct hsr_node *node_curr = frame->node_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct hsr_port *port_rcv = frame->port_rcv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct hsr_priv *hsr = port_rcv->hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct hsr_sup_payload *hsr_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct hsr_node *node_real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct sk_buff *skb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct list_head *node_db;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) struct ethhdr *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* Here either frame->skb_hsr or frame->skb_prp should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * valid as supervision frame always will have protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * header info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (frame->skb_hsr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) skb = frame->skb_hsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) else if (frame->skb_prp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) skb = frame->skb_prp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ethhdr = (struct ethhdr *)skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /* Leave the ethernet header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) skb_pull(skb, sizeof(struct ethhdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /* And leave the HSR tag. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (ethhdr->h_proto == htons(ETH_P_HSR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) skb_pull(skb, sizeof(struct hsr_tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* And leave the HSR sup tag. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) skb_pull(skb, sizeof(struct hsr_sup_tag));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) hsr_sp = (struct hsr_sup_payload *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /* Merge node_curr (registered on macaddress_B) into node_real */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) node_db = &port_rcv->hsr->node_db;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (!node_real)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* No frame received from AddrA of this node yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) HSR_SEQNR_START - 1, true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) port_rcv->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (!node_real)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) goto done; /* No mem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (node_real == node_curr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* Node has already been merged */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) for (i = 0; i < HSR_PT_PORTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!node_curr->time_in_stale[i] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) time_after(node_curr->time_in[i], node_real->time_in[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) node_real->time_in[i] = node_curr->time_in[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) node_real->time_in_stale[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) node_curr->time_in_stale[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) node_real->seq_out[i] = node_curr->seq_out[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) node_real->addr_B_port = port_rcv->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) spin_lock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) list_del_rcu(&node_curr->mac_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) spin_unlock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) kfree_rcu(node_curr, rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* PRP uses v0 header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (ethhdr->h_proto == htons(ETH_P_HSR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) skb_push(skb, sizeof(struct hsrv0_ethhdr_sp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * If the frame was sent by a node's B interface, replace the source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * address with that node's "official" address (macaddress_A) so that upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * layers recognize where it came from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (!skb_mac_header_was_set(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) WARN_ONCE(1, "%s: Mac header not set\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) memcpy(ð_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /* 'skb' is a frame meant for another host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * 'port' is the outgoing interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * Substitute the target (dest) MAC address if necessary, so the it matches the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * recipient interface MAC address, regardless of whether that is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * recipient's A or B interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * This is needed to keep the packets flowing through switches that learn on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * which "side" the different interfaces are.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct hsr_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct hsr_node *node_dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (!skb_mac_header_was_set(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) WARN_ONCE(1, "%s: Mac header not set\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) node_dst = find_node_by_addr_A(&port->hsr->node_db,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) eth_hdr(skb)->h_dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (!node_dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (net_ratelimit())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) netdev_err(skb->dev, "%s: Unknown node\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (port->type != node_dst->addr_B_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (is_valid_ether_addr(node_dst->macaddress_B))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) u16 sequence_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /* Don't register incoming frames without a valid sequence number. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * ensures entries of restarted nodes gets pruned so that they can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * re-register and resume communications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) node->time_in[port->type] = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) node->time_in_stale[port->type] = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * ethhdr->h_source address and skb->mac_header set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * 1 if frame can be shown to have been sent recently on this interface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * 0 otherwise, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * negative error code on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) u16 sequence_nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) time_is_after_jiffies(node->time_out[port->type] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) msecs_to_jiffies(HSR_ENTRY_FORGET_TIME)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) node->time_out[port->type] = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) node->seq_out[port->type] = sequence_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static struct hsr_port *get_late_port(struct hsr_priv *hsr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct hsr_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (node->time_in_stale[HSR_PT_SLAVE_A])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (node->time_in_stale[HSR_PT_SLAVE_B])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (time_after(node->time_in[HSR_PT_SLAVE_B],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) node->time_in[HSR_PT_SLAVE_A] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) msecs_to_jiffies(MAX_SLAVE_DIFF)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (time_after(node->time_in[HSR_PT_SLAVE_A],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) node->time_in[HSR_PT_SLAVE_B] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) msecs_to_jiffies(MAX_SLAVE_DIFF)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) /* Remove stale sequence_nr records. Called by timer every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) void hsr_prune_nodes(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct hsr_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) struct hsr_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) struct hsr_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) unsigned long timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) unsigned long time_a, time_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) spin_lock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * nor time_in[HSR_PT_SLAVE_B], will ever be updated for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * the master port. Thus the master node will be repeatedly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * pruned leading to packet loss.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (hsr_addr_is_self(hsr, node->macaddress_A))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /* Shorthand */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) time_a = node->time_in[HSR_PT_SLAVE_A];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) time_b = node->time_in[HSR_PT_SLAVE_B];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) /* Check for timestamps old enough to risk wrap-around */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) node->time_in_stale[HSR_PT_SLAVE_A] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) node->time_in_stale[HSR_PT_SLAVE_B] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /* Get age of newest frame from node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * At least one time_in is OK here; nodes get pruned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * before both time_ins can get stale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) timestamp = time_a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (node->time_in_stale[HSR_PT_SLAVE_A] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) (!node->time_in_stale[HSR_PT_SLAVE_B] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) time_after(time_b, time_a)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) timestamp = time_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /* Warn of ring error only as long as we get frames at all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (time_is_after_jiffies(timestamp +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) port = get_late_port(hsr, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) hsr_nl_ringerror(hsr, node->macaddress_A, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /* Prune old entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (time_is_before_jiffies(timestamp +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) hsr_nl_nodedown(hsr, node->macaddress_A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) list_del_rcu(&node->mac_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /* Note that we need to free this entry later: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) kfree_rcu(node, rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) spin_unlock_bh(&hsr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /* Restart timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) mod_timer(&hsr->prune_timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) jiffies + msecs_to_jiffies(PRUNE_PERIOD));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) unsigned char addr[ETH_ALEN])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct hsr_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (!_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) node = list_first_or_null_rcu(&hsr->node_db,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct hsr_node, mac_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) ether_addr_copy(addr, node->macaddress_A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) node = _pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) ether_addr_copy(addr, node->macaddress_A);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) int hsr_get_node_data(struct hsr_priv *hsr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) const unsigned char *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) unsigned char addr_b[ETH_ALEN],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) unsigned int *addr_b_ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) int *if1_age,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) u16 *if1_seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) int *if2_age,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) u16 *if2_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) struct hsr_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) struct hsr_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) unsigned long tdiff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) node = find_node_by_addr_A(&hsr->node_db, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) ether_addr_copy(addr_b, node->macaddress_B);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (node->time_in_stale[HSR_PT_SLAVE_A])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) *if1_age = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) #if HZ <= MSEC_PER_SEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) else if (tdiff > msecs_to_jiffies(INT_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) *if1_age = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) *if1_age = jiffies_to_msecs(tdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (node->time_in_stale[HSR_PT_SLAVE_B])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) *if2_age = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) #if HZ <= MSEC_PER_SEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) else if (tdiff > msecs_to_jiffies(INT_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) *if2_age = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) *if2_age = jiffies_to_msecs(tdiff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) /* Present sequence numbers as if they were incoming on interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (node->addr_B_port != HSR_PT_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) port = hsr_port_get_hsr(hsr, node->addr_B_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) *addr_b_ifindex = port->dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) *addr_b_ifindex = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }