Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 1 | /* Copyright 2011-2014 Autronica Fire and Security AS |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify it |
| 4 | * under the terms of the GNU General Public License as published by the Free |
| 5 | * Software Foundation; either version 2 of the License, or (at your option) |
| 6 | * any later version. |
| 7 | * |
| 8 | * Author(s): |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 9 | * 2011-2014 Arvid Brodin, arvid.brodin@alten.se |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 10 | * |
| 11 | * The HSR spec says never to forward the same frame twice on the same |
| 12 | * interface. A frame is identified by its source MAC address and its HSR |
| 13 | * sequence number. This code keeps track of senders and their sequence numbers |
| 14 | * to allow filtering of duplicate frames, and to detect HSR ring errors. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/if_ether.h> |
| 18 | #include <linux/etherdevice.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/rculist.h> |
| 21 | #include "hsr_main.h" |
| 22 | #include "hsr_framereg.h" |
| 23 | #include "hsr_netlink.h" |
| 24 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 25 | struct hsr_node { |
| 26 | struct list_head mac_list; |
| 27 | unsigned char MacAddressA[ETH_ALEN]; |
| 28 | unsigned char MacAddressB[ETH_ALEN]; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 29 | /* Local slave through which AddrB frames are received from this node */ |
| 30 | enum hsr_port_type AddrB_port; |
| 31 | unsigned long time_in[HSR_PT_PORTS]; |
| 32 | bool time_in_stale[HSR_PT_PORTS]; |
| 33 | u16 seq_out[HSR_PT_PORTS]; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 34 | struct rcu_head rcu_head; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | /* TODO: use hash lists for mac addresses (linux/jhash.h)? */ |
| 38 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 39 | /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b, |
| 40 | * false otherwise. |
| 41 | */ |
| 42 | static bool seq_nr_after(u16 a, u16 b) |
| 43 | { |
| 44 | /* Remove inconsistency where |
| 45 | * seq_nr_after(a, b) == seq_nr_before(a, b) |
| 46 | */ |
| 47 | if ((int) b - a == 32768) |
| 48 | return false; |
| 49 | |
| 50 | return (((s16) (b - a)) < 0); |
| 51 | } |
| 52 | #define seq_nr_before(a, b) seq_nr_after((b), (a)) |
| 53 | #define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b))) |
| 54 | #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b))) |
| 55 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 56 | bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr) |
| 57 | { |
| 58 | struct hsr_node *node; |
| 59 | |
| 60 | node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node, |
| 61 | mac_list); |
| 62 | if (!node) { |
| 63 | WARN_ONCE(1, "HSR: No self node\n"); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if (ether_addr_equal(addr, node->MacAddressA)) |
| 68 | return true; |
| 69 | if (ether_addr_equal(addr, node->MacAddressB)) |
| 70 | return true; |
| 71 | |
| 72 | return false; |
| 73 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 74 | |
| 75 | /* Search for mac entry. Caller must hold rcu read lock. |
| 76 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 77 | static struct hsr_node *find_node_by_AddrA(struct list_head *node_db, |
| 78 | const unsigned char addr[ETH_ALEN]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 79 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 80 | struct hsr_node *node; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 81 | |
| 82 | list_for_each_entry_rcu(node, node_db, mac_list) { |
| 83 | if (ether_addr_equal(node->MacAddressA, addr)) |
| 84 | return node; |
| 85 | } |
| 86 | |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 90 | /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize |
| 91 | * frames from self that's been looped over the HSR ring. |
| 92 | */ |
| 93 | int hsr_create_self_node(struct list_head *self_node_db, |
| 94 | unsigned char addr_a[ETH_ALEN], |
| 95 | unsigned char addr_b[ETH_ALEN]) |
| 96 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 97 | struct hsr_node *node, *oldnode; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 98 | |
| 99 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 100 | if (!node) |
| 101 | return -ENOMEM; |
| 102 | |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 103 | ether_addr_copy(node->MacAddressA, addr_a); |
| 104 | ether_addr_copy(node->MacAddressB, addr_b); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 105 | |
| 106 | rcu_read_lock(); |
| 107 | oldnode = list_first_or_null_rcu(self_node_db, |
Murali Karicheri | 4fe25bd | 2019-04-05 13:31:26 -0400 | [diff] [blame] | 108 | struct hsr_node, mac_list); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 109 | if (oldnode) { |
| 110 | list_replace_rcu(&oldnode->mac_list, &node->mac_list); |
| 111 | rcu_read_unlock(); |
| 112 | synchronize_rcu(); |
| 113 | kfree(oldnode); |
| 114 | } else { |
| 115 | rcu_read_unlock(); |
| 116 | list_add_tail_rcu(&node->mac_list, self_node_db); |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Mao Wenan | 6caabe7 | 2019-03-06 22:45:01 +0800 | [diff] [blame] | 122 | void hsr_del_node(struct list_head *self_node_db) |
| 123 | { |
| 124 | struct hsr_node *node; |
| 125 | |
| 126 | rcu_read_lock(); |
| 127 | node = list_first_or_null_rcu(self_node_db, struct hsr_node, mac_list); |
| 128 | rcu_read_unlock(); |
| 129 | if (node) { |
| 130 | list_del_rcu(&node->mac_list); |
| 131 | kfree(node); |
| 132 | } |
| 133 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 134 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 135 | /* Allocate an hsr_node and add it to node_db. 'addr' is the node's AddressA; |
| 136 | * seq_out is used to initialize filtering of outgoing duplicate frames |
| 137 | * originating from the newly added node. |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 138 | */ |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 139 | struct hsr_node *hsr_add_node(struct list_head *node_db, unsigned char addr[], |
| 140 | u16 seq_out) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 141 | { |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 142 | struct hsr_node *node; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 143 | unsigned long now; |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 144 | int i; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 145 | |
| 146 | node = kzalloc(sizeof(*node), GFP_ATOMIC); |
| 147 | if (!node) |
| 148 | return NULL; |
| 149 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 150 | ether_addr_copy(node->MacAddressA, addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 151 | |
| 152 | /* We are only interested in time diffs here, so use current jiffies |
| 153 | * as initialization. (0 could trigger an spurious ring error warning). |
| 154 | */ |
| 155 | now = jiffies; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 156 | for (i = 0; i < HSR_PT_PORTS; i++) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 157 | node->time_in[i] = now; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 158 | for (i = 0; i < HSR_PT_PORTS; i++) |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 159 | node->seq_out[i] = seq_out; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 160 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 161 | list_add_tail_rcu(&node->mac_list, node_db); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 162 | |
| 163 | return node; |
| 164 | } |
| 165 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 166 | /* Get the hsr_node from which 'skb' was sent. |
| 167 | */ |
Karicheri, Muralidharan | 675c8da | 2017-06-12 15:06:26 -0400 | [diff] [blame] | 168 | struct hsr_node *hsr_get_node(struct hsr_port *port, struct sk_buff *skb, |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 169 | bool is_sup) |
| 170 | { |
Karicheri, Muralidharan | 675c8da | 2017-06-12 15:06:26 -0400 | [diff] [blame] | 171 | struct list_head *node_db = &port->hsr->node_db; |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 172 | struct hsr_node *node; |
| 173 | struct ethhdr *ethhdr; |
| 174 | u16 seq_out; |
| 175 | |
| 176 | if (!skb_mac_header_was_set(skb)) |
| 177 | return NULL; |
| 178 | |
| 179 | ethhdr = (struct ethhdr *) skb_mac_header(skb); |
| 180 | |
| 181 | list_for_each_entry_rcu(node, node_db, mac_list) { |
| 182 | if (ether_addr_equal(node->MacAddressA, ethhdr->h_source)) |
| 183 | return node; |
| 184 | if (ether_addr_equal(node->MacAddressB, ethhdr->h_source)) |
| 185 | return node; |
| 186 | } |
| 187 | |
Peter Heise | ee1c279 | 2016-04-13 13:52:22 +0200 | [diff] [blame] | 188 | /* Everyone may create a node entry, connected node to a HSR device. */ |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 189 | |
Peter Heise | ee1c279 | 2016-04-13 13:52:22 +0200 | [diff] [blame] | 190 | if (ethhdr->h_proto == htons(ETH_P_PRP) |
| 191 | || ethhdr->h_proto == htons(ETH_P_HSR)) { |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 192 | /* Use the existing sequence_nr from the tag as starting point |
| 193 | * for filtering duplicate frames. |
| 194 | */ |
| 195 | seq_out = hsr_get_skb_sequence_nr(skb) - 1; |
| 196 | } else { |
Karicheri, Muralidharan | 675c8da | 2017-06-12 15:06:26 -0400 | [diff] [blame] | 197 | /* this is called also for frames from master port and |
| 198 | * so warn only for non master ports |
| 199 | */ |
| 200 | if (port->type != HSR_PT_MASTER) |
| 201 | WARN_ONCE(1, "%s: Non-HSR frame\n", __func__); |
Peter Heise | ee1c279 | 2016-04-13 13:52:22 +0200 | [diff] [blame] | 202 | seq_out = HSR_SEQNR_START; |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | return hsr_add_node(node_db, ethhdr->h_source, seq_out); |
| 206 | } |
| 207 | |
| 208 | /* Use the Supervision frame's info about an eventual MacAddressB for merging |
| 209 | * nodes that has previously had their MacAddressB registered as a separate |
| 210 | * node. |
| 211 | */ |
| 212 | void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr, |
| 213 | struct hsr_port *port_rcv) |
| 214 | { |
Peter Heise | ee1c279 | 2016-04-13 13:52:22 +0200 | [diff] [blame] | 215 | struct ethhdr *ethhdr; |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 216 | struct hsr_node *node_real; |
| 217 | struct hsr_sup_payload *hsr_sp; |
| 218 | struct list_head *node_db; |
| 219 | int i; |
| 220 | |
Peter Heise | ee1c279 | 2016-04-13 13:52:22 +0200 | [diff] [blame] | 221 | ethhdr = (struct ethhdr *) skb_mac_header(skb); |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 222 | |
Peter Heise | ee1c279 | 2016-04-13 13:52:22 +0200 | [diff] [blame] | 223 | /* Leave the ethernet header. */ |
| 224 | skb_pull(skb, sizeof(struct ethhdr)); |
| 225 | |
| 226 | /* And leave the HSR tag. */ |
| 227 | if (ethhdr->h_proto == htons(ETH_P_HSR)) |
| 228 | skb_pull(skb, sizeof(struct hsr_tag)); |
| 229 | |
| 230 | /* And leave the HSR sup tag. */ |
| 231 | skb_pull(skb, sizeof(struct hsr_sup_tag)); |
| 232 | |
| 233 | hsr_sp = (struct hsr_sup_payload *) skb->data; |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 234 | |
| 235 | /* Merge node_curr (registered on MacAddressB) into node_real */ |
| 236 | node_db = &port_rcv->hsr->node_db; |
| 237 | node_real = find_node_by_AddrA(node_db, hsr_sp->MacAddressA); |
| 238 | if (!node_real) |
| 239 | /* No frame received from AddrA of this node yet */ |
| 240 | node_real = hsr_add_node(node_db, hsr_sp->MacAddressA, |
| 241 | HSR_SEQNR_START - 1); |
| 242 | if (!node_real) |
| 243 | goto done; /* No mem */ |
| 244 | if (node_real == node_curr) |
| 245 | /* Node has already been merged */ |
| 246 | goto done; |
| 247 | |
Peter Heise | ee1c279 | 2016-04-13 13:52:22 +0200 | [diff] [blame] | 248 | ether_addr_copy(node_real->MacAddressB, ethhdr->h_source); |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 249 | for (i = 0; i < HSR_PT_PORTS; i++) { |
| 250 | if (!node_curr->time_in_stale[i] && |
| 251 | time_after(node_curr->time_in[i], node_real->time_in[i])) { |
| 252 | node_real->time_in[i] = node_curr->time_in[i]; |
Murali Karicheri | d595b85 | 2019-04-05 13:31:23 -0400 | [diff] [blame] | 253 | node_real->time_in_stale[i] = |
| 254 | node_curr->time_in_stale[i]; |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 255 | } |
| 256 | if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i])) |
| 257 | node_real->seq_out[i] = node_curr->seq_out[i]; |
| 258 | } |
| 259 | node_real->AddrB_port = port_rcv->type; |
| 260 | |
| 261 | list_del_rcu(&node_curr->mac_list); |
| 262 | kfree_rcu(node_curr, rcu_head); |
| 263 | |
| 264 | done: |
Peter Heise | ee1c279 | 2016-04-13 13:52:22 +0200 | [diff] [blame] | 265 | skb_push(skb, sizeof(struct hsrv1_ethhdr_sp)); |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 268 | /* 'skb' is a frame meant for this host, that is to be passed to upper layers. |
| 269 | * |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 270 | * If the frame was sent by a node's B interface, replace the source |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 271 | * address with that node's "official" address (MacAddressA) so that upper |
| 272 | * layers recognize where it came from. |
| 273 | */ |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 274 | void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 275 | { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 276 | if (!skb_mac_header_was_set(skb)) { |
| 277 | WARN_ONCE(1, "%s: Mac header not set\n", __func__); |
| 278 | return; |
| 279 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 280 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 281 | memcpy(ð_hdr(skb)->h_source, node->MacAddressA, ETH_ALEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 284 | /* 'skb' is a frame meant for another host. |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 285 | * 'port' is the outgoing interface |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 286 | * |
| 287 | * Substitute the target (dest) MAC address if necessary, so the it matches the |
| 288 | * recipient interface MAC address, regardless of whether that is the |
| 289 | * recipient's A or B interface. |
| 290 | * This is needed to keep the packets flowing through switches that learn on |
| 291 | * which "side" the different interfaces are. |
| 292 | */ |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 293 | void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb, |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 294 | struct hsr_port *port) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 295 | { |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 296 | struct hsr_node *node_dst; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 297 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 298 | if (!skb_mac_header_was_set(skb)) { |
| 299 | WARN_ONCE(1, "%s: Mac header not set\n", __func__); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest)) |
| 304 | return; |
| 305 | |
Murali Karicheri | d595b85 | 2019-04-05 13:31:23 -0400 | [diff] [blame] | 306 | node_dst = find_node_by_AddrA(&port->hsr->node_db, |
| 307 | eth_hdr(skb)->h_dest); |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 308 | if (!node_dst) { |
| 309 | WARN_ONCE(1, "%s: Unknown node\n", __func__); |
| 310 | return; |
| 311 | } |
| 312 | if (port->type != node_dst->AddrB_port) |
| 313 | return; |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 314 | |
| 315 | ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->MacAddressB); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 318 | void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port, |
| 319 | u16 sequence_nr) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 320 | { |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 321 | /* Don't register incoming frames without a valid sequence number. This |
| 322 | * ensures entries of restarted nodes gets pruned so that they can |
| 323 | * re-register and resume communications. |
Arvid Brodin | 213e3bc | 2013-11-29 23:37:07 +0100 | [diff] [blame] | 324 | */ |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 325 | if (seq_nr_before(sequence_nr, node->seq_out[port->type])) |
| 326 | return; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 327 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 328 | node->time_in[port->type] = jiffies; |
| 329 | node->time_in_stale[port->type] = false; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 332 | /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid |
| 333 | * ethhdr->h_source address and skb->mac_header set. |
| 334 | * |
| 335 | * Return: |
| 336 | * 1 if frame can be shown to have been sent recently on this interface, |
| 337 | * 0 otherwise, or |
| 338 | * negative error code on error |
| 339 | */ |
Arvid Brodin | f266a68 | 2014-07-04 23:41:03 +0200 | [diff] [blame] | 340 | int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node, |
| 341 | u16 sequence_nr) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 342 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 343 | if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type])) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 344 | return 1; |
| 345 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 346 | node->seq_out[port->type] = sequence_nr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 350 | static struct hsr_port *get_late_port(struct hsr_priv *hsr, |
| 351 | struct hsr_node *node) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 352 | { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 353 | if (node->time_in_stale[HSR_PT_SLAVE_A]) |
| 354 | return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A); |
| 355 | if (node->time_in_stale[HSR_PT_SLAVE_B]) |
| 356 | return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 357 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 358 | if (time_after(node->time_in[HSR_PT_SLAVE_B], |
| 359 | node->time_in[HSR_PT_SLAVE_A] + |
| 360 | msecs_to_jiffies(MAX_SLAVE_DIFF))) |
| 361 | return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A); |
| 362 | if (time_after(node->time_in[HSR_PT_SLAVE_A], |
| 363 | node->time_in[HSR_PT_SLAVE_B] + |
| 364 | msecs_to_jiffies(MAX_SLAVE_DIFF))) |
| 365 | return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 366 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 367 | return NULL; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 370 | /* Remove stale sequence_nr records. Called by timer every |
| 371 | * HSR_LIFE_CHECK_INTERVAL (two seconds or so). |
| 372 | */ |
Kees Cook | dda436b | 2017-10-24 01:46:16 -0700 | [diff] [blame] | 373 | void hsr_prune_nodes(struct timer_list *t) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 374 | { |
Kees Cook | dda436b | 2017-10-24 01:46:16 -0700 | [diff] [blame] | 375 | struct hsr_priv *hsr = from_timer(hsr, t, prune_timer); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 376 | struct hsr_node *node; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 377 | struct hsr_port *port; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 378 | unsigned long timestamp; |
| 379 | unsigned long time_a, time_b; |
| 380 | |
| 381 | rcu_read_lock(); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 382 | list_for_each_entry_rcu(node, &hsr->node_db, mac_list) { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 383 | /* Shorthand */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 384 | time_a = node->time_in[HSR_PT_SLAVE_A]; |
| 385 | time_b = node->time_in[HSR_PT_SLAVE_B]; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 386 | |
| 387 | /* Check for timestamps old enough to risk wrap-around */ |
| 388 | if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2)) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 389 | node->time_in_stale[HSR_PT_SLAVE_A] = true; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 390 | if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2)) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 391 | node->time_in_stale[HSR_PT_SLAVE_B] = true; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 392 | |
| 393 | /* Get age of newest frame from node. |
| 394 | * At least one time_in is OK here; nodes get pruned long |
| 395 | * before both time_ins can get stale |
| 396 | */ |
| 397 | timestamp = time_a; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 398 | if (node->time_in_stale[HSR_PT_SLAVE_A] || |
| 399 | (!node->time_in_stale[HSR_PT_SLAVE_B] && |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 400 | time_after(time_b, time_a))) |
| 401 | timestamp = time_b; |
| 402 | |
| 403 | /* Warn of ring error only as long as we get frames at all */ |
| 404 | if (time_is_after_jiffies(timestamp + |
| 405 | msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) { |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 406 | rcu_read_lock(); |
| 407 | port = get_late_port(hsr, node); |
Murali Karicheri | 05ca6e6 | 2019-04-05 13:31:28 -0400 | [diff] [blame^] | 408 | if (port) |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 409 | hsr_nl_ringerror(hsr, node->MacAddressA, port); |
| 410 | rcu_read_unlock(); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /* Prune old entries */ |
| 414 | if (time_is_before_jiffies(timestamp + |
Murali Karicheri | d595b85 | 2019-04-05 13:31:23 -0400 | [diff] [blame] | 415 | msecs_to_jiffies(HSR_NODE_FORGET_TIME))) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 416 | hsr_nl_nodedown(hsr, node->MacAddressA); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 417 | list_del_rcu(&node->mac_list); |
| 418 | /* Note that we need to free this entry later: */ |
Wei Yongjun | 1aee6cc | 2013-12-16 14:05:50 +0800 | [diff] [blame] | 419 | kfree_rcu(node, rcu_head); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | rcu_read_unlock(); |
| 423 | } |
| 424 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 425 | void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 426 | unsigned char addr[ETH_ALEN]) |
| 427 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 428 | struct hsr_node *node; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 429 | |
| 430 | if (!_pos) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 431 | node = list_first_or_null_rcu(&hsr->node_db, |
| 432 | struct hsr_node, mac_list); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 433 | if (node) |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 434 | ether_addr_copy(addr, node->MacAddressA); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 435 | return node; |
| 436 | } |
| 437 | |
| 438 | node = _pos; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 439 | list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) { |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 440 | ether_addr_copy(addr, node->MacAddressA); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 441 | return node; |
| 442 | } |
| 443 | |
| 444 | return NULL; |
| 445 | } |
| 446 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 447 | int hsr_get_node_data(struct hsr_priv *hsr, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 448 | const unsigned char *addr, |
| 449 | unsigned char addr_b[ETH_ALEN], |
| 450 | unsigned int *addr_b_ifindex, |
| 451 | int *if1_age, |
| 452 | u16 *if1_seq, |
| 453 | int *if2_age, |
| 454 | u16 *if2_seq) |
| 455 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 456 | struct hsr_node *node; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 457 | struct hsr_port *port; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 458 | unsigned long tdiff; |
| 459 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 460 | rcu_read_lock(); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 461 | node = find_node_by_AddrA(&hsr->node_db, addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 462 | if (!node) { |
| 463 | rcu_read_unlock(); |
| 464 | return -ENOENT; /* No such entry */ |
| 465 | } |
| 466 | |
Joe Perches | e83abe3 | 2014-02-18 10:37:20 -0800 | [diff] [blame] | 467 | ether_addr_copy(addr_b, node->MacAddressB); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 468 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 469 | tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A]; |
| 470 | if (node->time_in_stale[HSR_PT_SLAVE_A]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 471 | *if1_age = INT_MAX; |
| 472 | #if HZ <= MSEC_PER_SEC |
| 473 | else if (tdiff > msecs_to_jiffies(INT_MAX)) |
| 474 | *if1_age = INT_MAX; |
| 475 | #endif |
| 476 | else |
| 477 | *if1_age = jiffies_to_msecs(tdiff); |
| 478 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 479 | tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B]; |
| 480 | if (node->time_in_stale[HSR_PT_SLAVE_B]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 481 | *if2_age = INT_MAX; |
| 482 | #if HZ <= MSEC_PER_SEC |
| 483 | else if (tdiff > msecs_to_jiffies(INT_MAX)) |
| 484 | *if2_age = INT_MAX; |
| 485 | #endif |
| 486 | else |
| 487 | *if2_age = jiffies_to_msecs(tdiff); |
| 488 | |
| 489 | /* Present sequence numbers as if they were incoming on interface */ |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 490 | *if1_seq = node->seq_out[HSR_PT_SLAVE_B]; |
| 491 | *if2_seq = node->seq_out[HSR_PT_SLAVE_A]; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 492 | |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 493 | if (node->AddrB_port != HSR_PT_NONE) { |
| 494 | port = hsr_port_get_hsr(hsr, node->AddrB_port); |
| 495 | *addr_b_ifindex = port->dev->ifindex; |
| 496 | } else { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 497 | *addr_b_ifindex = -1; |
Arvid Brodin | c5a7591 | 2014-07-04 23:38:05 +0200 | [diff] [blame] | 498 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 499 | |
| 500 | rcu_read_unlock(); |
| 501 | |
| 502 | return 0; |
| 503 | } |