1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
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.
13 #include <linux/if_ether.h>
14 #include <linux/etherdevice.h>
15 #include <linux/slab.h>
16 #include <linux/rculist.h>
18 #include "hsr_framereg.h"
19 #include "hsr_netlink.h"
21 /* TODO: use hash lists for mac addresses (linux/jhash.h)? */
23 /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
26 static bool seq_nr_after(u16 a, u16 b)
28 /* Remove inconsistency where
29 * seq_nr_after(a, b) == seq_nr_before(a, b)
31 if ((int)b - a == 32768)
34 return (((s16)(b - a)) < 0);
37 #define seq_nr_before(a, b) seq_nr_after((b), (a))
38 #define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
39 #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
41 bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
43 struct hsr_node *node;
45 node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node,
48 WARN_ONCE(1, "HSR: No self node\n");
52 if (ether_addr_equal(addr, node->macaddress_A))
54 if (ether_addr_equal(addr, node->macaddress_B))
60 /* Search for mac entry. Caller must hold rcu read lock.
62 static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
63 const unsigned char addr[ETH_ALEN])
65 struct hsr_node *node;
67 list_for_each_entry_rcu(node, node_db, mac_list) {
68 if (ether_addr_equal(node->macaddress_A, addr))
75 /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
76 * frames from self that's been looped over the HSR ring.
78 int hsr_create_self_node(struct list_head *self_node_db,
79 unsigned char addr_a[ETH_ALEN],
80 unsigned char addr_b[ETH_ALEN])
82 struct hsr_node *node, *oldnode;
84 node = kmalloc(sizeof(*node), GFP_KERNEL);
88 ether_addr_copy(node->macaddress_A, addr_a);
89 ether_addr_copy(node->macaddress_B, addr_b);
92 oldnode = list_first_or_null_rcu(self_node_db,
93 struct hsr_node, mac_list);
95 list_replace_rcu(&oldnode->mac_list, &node->mac_list);
101 list_add_tail_rcu(&node->mac_list, self_node_db);
107 void hsr_del_node(struct list_head *self_node_db)
109 struct hsr_node *node;
112 node = list_first_or_null_rcu(self_node_db, struct hsr_node, mac_list);
115 list_del_rcu(&node->mac_list);
120 /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A;
121 * seq_out is used to initialize filtering of outgoing duplicate frames
122 * originating from the newly added node.
124 struct hsr_node *hsr_add_node(struct list_head *node_db, unsigned char addr[],
127 struct hsr_node *node;
131 node = kzalloc(sizeof(*node), GFP_ATOMIC);
135 ether_addr_copy(node->macaddress_A, addr);
137 /* We are only interested in time diffs here, so use current jiffies
138 * as initialization. (0 could trigger an spurious ring error warning).
141 for (i = 0; i < HSR_PT_PORTS; i++)
142 node->time_in[i] = now;
143 for (i = 0; i < HSR_PT_PORTS; i++)
144 node->seq_out[i] = seq_out;
146 list_add_tail_rcu(&node->mac_list, node_db);
151 /* Get the hsr_node from which 'skb' was sent.
153 struct hsr_node *hsr_get_node(struct hsr_port *port, struct sk_buff *skb,
156 struct list_head *node_db = &port->hsr->node_db;
157 struct hsr_node *node;
158 struct ethhdr *ethhdr;
161 if (!skb_mac_header_was_set(skb))
164 ethhdr = (struct ethhdr *)skb_mac_header(skb);
166 list_for_each_entry_rcu(node, node_db, mac_list) {
167 if (ether_addr_equal(node->macaddress_A, ethhdr->h_source))
169 if (ether_addr_equal(node->macaddress_B, ethhdr->h_source))
173 /* Everyone may create a node entry, connected node to a HSR device. */
175 if (ethhdr->h_proto == htons(ETH_P_PRP) ||
176 ethhdr->h_proto == htons(ETH_P_HSR)) {
177 /* Use the existing sequence_nr from the tag as starting point
178 * for filtering duplicate frames.
180 seq_out = hsr_get_skb_sequence_nr(skb) - 1;
182 /* this is called also for frames from master port and
183 * so warn only for non master ports
185 if (port->type != HSR_PT_MASTER)
186 WARN_ONCE(1, "%s: Non-HSR frame\n", __func__);
187 seq_out = HSR_SEQNR_START;
190 return hsr_add_node(node_db, ethhdr->h_source, seq_out);
193 /* Use the Supervision frame's info about an eventual macaddress_B for merging
194 * nodes that has previously had their macaddress_B registered as a separate
197 void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr,
198 struct hsr_port *port_rcv)
200 struct ethhdr *ethhdr;
201 struct hsr_node *node_real;
202 struct hsr_sup_payload *hsr_sp;
203 struct list_head *node_db;
206 ethhdr = (struct ethhdr *)skb_mac_header(skb);
208 /* Leave the ethernet header. */
209 skb_pull(skb, sizeof(struct ethhdr));
211 /* And leave the HSR tag. */
212 if (ethhdr->h_proto == htons(ETH_P_HSR))
213 skb_pull(skb, sizeof(struct hsr_tag));
215 /* And leave the HSR sup tag. */
216 skb_pull(skb, sizeof(struct hsr_sup_tag));
218 hsr_sp = (struct hsr_sup_payload *)skb->data;
220 /* Merge node_curr (registered on macaddress_B) into node_real */
221 node_db = &port_rcv->hsr->node_db;
222 node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
224 /* No frame received from AddrA of this node yet */
225 node_real = hsr_add_node(node_db, hsr_sp->macaddress_A,
226 HSR_SEQNR_START - 1);
228 goto done; /* No mem */
229 if (node_real == node_curr)
230 /* Node has already been merged */
233 ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
234 for (i = 0; i < HSR_PT_PORTS; i++) {
235 if (!node_curr->time_in_stale[i] &&
236 time_after(node_curr->time_in[i], node_real->time_in[i])) {
237 node_real->time_in[i] = node_curr->time_in[i];
238 node_real->time_in_stale[i] =
239 node_curr->time_in_stale[i];
241 if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
242 node_real->seq_out[i] = node_curr->seq_out[i];
244 node_real->addr_B_port = port_rcv->type;
246 list_del_rcu(&node_curr->mac_list);
247 kfree_rcu(node_curr, rcu_head);
250 skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
253 /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
255 * If the frame was sent by a node's B interface, replace the source
256 * address with that node's "official" address (macaddress_A) so that upper
257 * layers recognize where it came from.
259 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
261 if (!skb_mac_header_was_set(skb)) {
262 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
266 memcpy(ð_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
269 /* 'skb' is a frame meant for another host.
270 * 'port' is the outgoing interface
272 * Substitute the target (dest) MAC address if necessary, so the it matches the
273 * recipient interface MAC address, regardless of whether that is the
274 * recipient's A or B interface.
275 * This is needed to keep the packets flowing through switches that learn on
276 * which "side" the different interfaces are.
278 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
279 struct hsr_port *port)
281 struct hsr_node *node_dst;
283 if (!skb_mac_header_was_set(skb)) {
284 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
288 if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
291 node_dst = find_node_by_addr_A(&port->hsr->node_db,
292 eth_hdr(skb)->h_dest);
294 WARN_ONCE(1, "%s: Unknown node\n", __func__);
297 if (port->type != node_dst->addr_B_port)
300 ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
303 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
306 /* Don't register incoming frames without a valid sequence number. This
307 * ensures entries of restarted nodes gets pruned so that they can
308 * re-register and resume communications.
310 if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
313 node->time_in[port->type] = jiffies;
314 node->time_in_stale[port->type] = false;
317 /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
318 * ethhdr->h_source address and skb->mac_header set.
321 * 1 if frame can be shown to have been sent recently on this interface,
323 * negative error code on error
325 int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
328 if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
331 node->seq_out[port->type] = sequence_nr;
335 static struct hsr_port *get_late_port(struct hsr_priv *hsr,
336 struct hsr_node *node)
338 if (node->time_in_stale[HSR_PT_SLAVE_A])
339 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
340 if (node->time_in_stale[HSR_PT_SLAVE_B])
341 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
343 if (time_after(node->time_in[HSR_PT_SLAVE_B],
344 node->time_in[HSR_PT_SLAVE_A] +
345 msecs_to_jiffies(MAX_SLAVE_DIFF)))
346 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
347 if (time_after(node->time_in[HSR_PT_SLAVE_A],
348 node->time_in[HSR_PT_SLAVE_B] +
349 msecs_to_jiffies(MAX_SLAVE_DIFF)))
350 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
355 /* Remove stale sequence_nr records. Called by timer every
356 * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
358 void hsr_prune_nodes(struct timer_list *t)
360 struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
361 struct hsr_node *node;
362 struct hsr_port *port;
363 unsigned long timestamp;
364 unsigned long time_a, time_b;
367 list_for_each_entry_rcu(node, &hsr->node_db, mac_list) {
369 time_a = node->time_in[HSR_PT_SLAVE_A];
370 time_b = node->time_in[HSR_PT_SLAVE_B];
372 /* Check for timestamps old enough to risk wrap-around */
373 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
374 node->time_in_stale[HSR_PT_SLAVE_A] = true;
375 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
376 node->time_in_stale[HSR_PT_SLAVE_B] = true;
378 /* Get age of newest frame from node.
379 * At least one time_in is OK here; nodes get pruned long
380 * before both time_ins can get stale
383 if (node->time_in_stale[HSR_PT_SLAVE_A] ||
384 (!node->time_in_stale[HSR_PT_SLAVE_B] &&
385 time_after(time_b, time_a)))
388 /* Warn of ring error only as long as we get frames at all */
389 if (time_is_after_jiffies(timestamp +
390 msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
392 port = get_late_port(hsr, node);
394 hsr_nl_ringerror(hsr, node->macaddress_A, port);
398 /* Prune old entries */
399 if (time_is_before_jiffies(timestamp +
400 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
401 hsr_nl_nodedown(hsr, node->macaddress_A);
402 list_del_rcu(&node->mac_list);
403 /* Note that we need to free this entry later: */
404 kfree_rcu(node, rcu_head);
410 mod_timer(&hsr->prune_timer,
411 jiffies + msecs_to_jiffies(PRUNE_PERIOD));
414 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
415 unsigned char addr[ETH_ALEN])
417 struct hsr_node *node;
420 node = list_first_or_null_rcu(&hsr->node_db,
421 struct hsr_node, mac_list);
423 ether_addr_copy(addr, node->macaddress_A);
428 list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
429 ether_addr_copy(addr, node->macaddress_A);
436 int hsr_get_node_data(struct hsr_priv *hsr,
437 const unsigned char *addr,
438 unsigned char addr_b[ETH_ALEN],
439 unsigned int *addr_b_ifindex,
445 struct hsr_node *node;
446 struct hsr_port *port;
450 node = find_node_by_addr_A(&hsr->node_db, addr);
453 return -ENOENT; /* No such entry */
456 ether_addr_copy(addr_b, node->macaddress_B);
458 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
459 if (node->time_in_stale[HSR_PT_SLAVE_A])
461 #if HZ <= MSEC_PER_SEC
462 else if (tdiff > msecs_to_jiffies(INT_MAX))
466 *if1_age = jiffies_to_msecs(tdiff);
468 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
469 if (node->time_in_stale[HSR_PT_SLAVE_B])
471 #if HZ <= MSEC_PER_SEC
472 else if (tdiff > msecs_to_jiffies(INT_MAX))
476 *if2_age = jiffies_to_msecs(tdiff);
478 /* Present sequence numbers as if they were incoming on interface */
479 *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
480 *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
482 if (node->addr_B_port != HSR_PT_NONE) {
483 port = hsr_port_get_hsr(hsr, node->addr_B_port);
484 *addr_b_ifindex = port->dev->ifindex;
486 *addr_b_ifindex = -1;