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_before_or_eq(a, b) (!seq_nr_after((a), (b)))
40 bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
42 struct hsr_node *node;
44 node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node,
47 WARN_ONCE(1, "HSR: No self node\n");
51 if (ether_addr_equal(addr, node->macaddress_A))
53 if (ether_addr_equal(addr, node->macaddress_B))
59 /* Search for mac entry. Caller must hold rcu read lock.
61 static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
62 const unsigned char addr[ETH_ALEN])
64 struct hsr_node *node;
66 list_for_each_entry_rcu(node, node_db, mac_list) {
67 if (ether_addr_equal(node->macaddress_A, addr))
74 /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
75 * frames from self that's been looped over the HSR ring.
77 int hsr_create_self_node(struct hsr_priv *hsr,
78 unsigned char addr_a[ETH_ALEN],
79 unsigned char addr_b[ETH_ALEN])
81 struct list_head *self_node_db = &hsr->self_node_db;
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);
91 spin_lock_bh(&hsr->list_lock);
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);
96 spin_unlock_bh(&hsr->list_lock);
97 kfree_rcu(oldnode, rcu_head);
99 list_add_tail_rcu(&node->mac_list, self_node_db);
100 spin_unlock_bh(&hsr->list_lock);
106 void hsr_del_self_node(struct hsr_priv *hsr)
108 struct list_head *self_node_db = &hsr->self_node_db;
109 struct hsr_node *node;
111 spin_lock_bh(&hsr->list_lock);
112 node = list_first_or_null_rcu(self_node_db, struct hsr_node, mac_list);
114 list_del_rcu(&node->mac_list);
115 kfree_rcu(node, rcu_head);
117 spin_unlock_bh(&hsr->list_lock);
120 void hsr_del_nodes(struct list_head *node_db)
122 struct hsr_node *node;
123 struct hsr_node *tmp;
125 list_for_each_entry_safe(node, tmp, node_db, mac_list)
129 /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A;
130 * seq_out is used to initialize filtering of outgoing duplicate frames
131 * originating from the newly added node.
133 static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
134 struct list_head *node_db,
135 unsigned char addr[],
138 struct hsr_node *new_node, *node;
142 new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
146 ether_addr_copy(new_node->macaddress_A, addr);
148 /* We are only interested in time diffs here, so use current jiffies
149 * as initialization. (0 could trigger an spurious ring error warning).
152 for (i = 0; i < HSR_PT_PORTS; i++)
153 new_node->time_in[i] = now;
154 for (i = 0; i < HSR_PT_PORTS; i++)
155 new_node->seq_out[i] = seq_out;
157 spin_lock_bh(&hsr->list_lock);
158 list_for_each_entry_rcu(node, node_db, mac_list,
159 lockdep_is_held(&hsr->list_lock)) {
160 if (ether_addr_equal(node->macaddress_A, addr))
162 if (ether_addr_equal(node->macaddress_B, addr))
165 list_add_tail_rcu(&new_node->mac_list, node_db);
166 spin_unlock_bh(&hsr->list_lock);
169 spin_unlock_bh(&hsr->list_lock);
174 /* Get the hsr_node from which 'skb' was sent.
176 struct hsr_node *hsr_get_node(struct hsr_port *port, struct sk_buff *skb,
179 struct list_head *node_db = &port->hsr->node_db;
180 struct hsr_priv *hsr = port->hsr;
181 struct hsr_node *node;
182 struct ethhdr *ethhdr;
185 if (!skb_mac_header_was_set(skb))
188 ethhdr = (struct ethhdr *)skb_mac_header(skb);
190 list_for_each_entry_rcu(node, node_db, mac_list) {
191 if (ether_addr_equal(node->macaddress_A, ethhdr->h_source))
193 if (ether_addr_equal(node->macaddress_B, ethhdr->h_source))
197 /* Everyone may create a node entry, connected node to a HSR device. */
199 if (ethhdr->h_proto == htons(ETH_P_PRP) ||
200 ethhdr->h_proto == htons(ETH_P_HSR)) {
201 /* Use the existing sequence_nr from the tag as starting point
202 * for filtering duplicate frames.
204 seq_out = hsr_get_skb_sequence_nr(skb) - 1;
206 /* this is called also for frames from master port and
207 * so warn only for non master ports
209 if (port->type != HSR_PT_MASTER)
210 WARN_ONCE(1, "%s: Non-HSR frame\n", __func__);
211 seq_out = HSR_SEQNR_START;
214 return hsr_add_node(hsr, node_db, ethhdr->h_source, seq_out);
217 /* Use the Supervision frame's info about an eventual macaddress_B for merging
218 * nodes that has previously had their macaddress_B registered as a separate
221 void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr,
222 struct hsr_port *port_rcv)
224 struct hsr_priv *hsr = port_rcv->hsr;
225 struct hsr_sup_payload *hsr_sp;
226 struct hsr_node *node_real;
227 struct list_head *node_db;
228 struct ethhdr *ethhdr;
231 ethhdr = (struct ethhdr *)skb_mac_header(skb);
233 /* Leave the ethernet header. */
234 skb_pull(skb, sizeof(struct ethhdr));
236 /* And leave the HSR tag. */
237 if (ethhdr->h_proto == htons(ETH_P_HSR))
238 skb_pull(skb, sizeof(struct hsr_tag));
240 /* And leave the HSR sup tag. */
241 skb_pull(skb, sizeof(struct hsr_sup_tag));
243 hsr_sp = (struct hsr_sup_payload *)skb->data;
245 /* Merge node_curr (registered on macaddress_B) into node_real */
246 node_db = &port_rcv->hsr->node_db;
247 node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
249 /* No frame received from AddrA of this node yet */
250 node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
251 HSR_SEQNR_START - 1);
253 goto done; /* No mem */
254 if (node_real == node_curr)
255 /* Node has already been merged */
258 ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
259 for (i = 0; i < HSR_PT_PORTS; i++) {
260 if (!node_curr->time_in_stale[i] &&
261 time_after(node_curr->time_in[i], node_real->time_in[i])) {
262 node_real->time_in[i] = node_curr->time_in[i];
263 node_real->time_in_stale[i] =
264 node_curr->time_in_stale[i];
266 if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
267 node_real->seq_out[i] = node_curr->seq_out[i];
269 node_real->addr_B_port = port_rcv->type;
271 spin_lock_bh(&hsr->list_lock);
272 list_del_rcu(&node_curr->mac_list);
273 spin_unlock_bh(&hsr->list_lock);
274 kfree_rcu(node_curr, rcu_head);
277 skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
280 /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
282 * If the frame was sent by a node's B interface, replace the source
283 * address with that node's "official" address (macaddress_A) so that upper
284 * layers recognize where it came from.
286 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
288 if (!skb_mac_header_was_set(skb)) {
289 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
293 memcpy(ð_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
296 /* 'skb' is a frame meant for another host.
297 * 'port' is the outgoing interface
299 * Substitute the target (dest) MAC address if necessary, so the it matches the
300 * recipient interface MAC address, regardless of whether that is the
301 * recipient's A or B interface.
302 * This is needed to keep the packets flowing through switches that learn on
303 * which "side" the different interfaces are.
305 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
306 struct hsr_port *port)
308 struct hsr_node *node_dst;
310 if (!skb_mac_header_was_set(skb)) {
311 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
315 if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
318 node_dst = find_node_by_addr_A(&port->hsr->node_db,
319 eth_hdr(skb)->h_dest);
321 WARN_ONCE(1, "%s: Unknown node\n", __func__);
324 if (port->type != node_dst->addr_B_port)
327 ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
330 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
333 /* Don't register incoming frames without a valid sequence number. This
334 * ensures entries of restarted nodes gets pruned so that they can
335 * re-register and resume communications.
337 if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
340 node->time_in[port->type] = jiffies;
341 node->time_in_stale[port->type] = false;
344 /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
345 * ethhdr->h_source address and skb->mac_header set.
348 * 1 if frame can be shown to have been sent recently on this interface,
350 * negative error code on error
352 int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
355 if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
358 node->seq_out[port->type] = sequence_nr;
362 static struct hsr_port *get_late_port(struct hsr_priv *hsr,
363 struct hsr_node *node)
365 if (node->time_in_stale[HSR_PT_SLAVE_A])
366 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
367 if (node->time_in_stale[HSR_PT_SLAVE_B])
368 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
370 if (time_after(node->time_in[HSR_PT_SLAVE_B],
371 node->time_in[HSR_PT_SLAVE_A] +
372 msecs_to_jiffies(MAX_SLAVE_DIFF)))
373 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
374 if (time_after(node->time_in[HSR_PT_SLAVE_A],
375 node->time_in[HSR_PT_SLAVE_B] +
376 msecs_to_jiffies(MAX_SLAVE_DIFF)))
377 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
382 /* Remove stale sequence_nr records. Called by timer every
383 * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
385 void hsr_prune_nodes(struct timer_list *t)
387 struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
388 struct hsr_node *node;
389 struct hsr_node *tmp;
390 struct hsr_port *port;
391 unsigned long timestamp;
392 unsigned long time_a, time_b;
394 spin_lock_bh(&hsr->list_lock);
395 list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
396 /* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A]
397 * nor time_in[HSR_PT_SLAVE_B], will ever be updated for
398 * the master port. Thus the master node will be repeatedly
399 * pruned leading to packet loss.
401 if (hsr_addr_is_self(hsr, node->macaddress_A))
405 time_a = node->time_in[HSR_PT_SLAVE_A];
406 time_b = node->time_in[HSR_PT_SLAVE_B];
408 /* Check for timestamps old enough to risk wrap-around */
409 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
410 node->time_in_stale[HSR_PT_SLAVE_A] = true;
411 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
412 node->time_in_stale[HSR_PT_SLAVE_B] = true;
414 /* Get age of newest frame from node.
415 * At least one time_in is OK here; nodes get pruned long
416 * before both time_ins can get stale
419 if (node->time_in_stale[HSR_PT_SLAVE_A] ||
420 (!node->time_in_stale[HSR_PT_SLAVE_B] &&
421 time_after(time_b, time_a)))
424 /* Warn of ring error only as long as we get frames at all */
425 if (time_is_after_jiffies(timestamp +
426 msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
428 port = get_late_port(hsr, node);
430 hsr_nl_ringerror(hsr, node->macaddress_A, port);
434 /* Prune old entries */
435 if (time_is_before_jiffies(timestamp +
436 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
437 hsr_nl_nodedown(hsr, node->macaddress_A);
438 list_del_rcu(&node->mac_list);
439 /* Note that we need to free this entry later: */
440 kfree_rcu(node, rcu_head);
443 spin_unlock_bh(&hsr->list_lock);
446 mod_timer(&hsr->prune_timer,
447 jiffies + msecs_to_jiffies(PRUNE_PERIOD));
450 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
451 unsigned char addr[ETH_ALEN])
453 struct hsr_node *node;
456 node = list_first_or_null_rcu(&hsr->node_db,
457 struct hsr_node, mac_list);
459 ether_addr_copy(addr, node->macaddress_A);
464 list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
465 ether_addr_copy(addr, node->macaddress_A);
472 int hsr_get_node_data(struct hsr_priv *hsr,
473 const unsigned char *addr,
474 unsigned char addr_b[ETH_ALEN],
475 unsigned int *addr_b_ifindex,
481 struct hsr_node *node;
482 struct hsr_port *port;
486 node = find_node_by_addr_A(&hsr->node_db, addr);
489 return -ENOENT; /* No such entry */
492 ether_addr_copy(addr_b, node->macaddress_B);
494 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
495 if (node->time_in_stale[HSR_PT_SLAVE_A])
497 #if HZ <= MSEC_PER_SEC
498 else if (tdiff > msecs_to_jiffies(INT_MAX))
502 *if1_age = jiffies_to_msecs(tdiff);
504 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
505 if (node->time_in_stale[HSR_PT_SLAVE_B])
507 #if HZ <= MSEC_PER_SEC
508 else if (tdiff > msecs_to_jiffies(INT_MAX))
512 *if2_age = jiffies_to_msecs(tdiff);
514 /* Present sequence numbers as if they were incoming on interface */
515 *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
516 *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
518 if (node->addr_B_port != HSR_PT_NONE) {
519 port = hsr_port_get_hsr(hsr, node->addr_B_port);
520 *addr_b_ifindex = port->dev->ifindex;
522 *addr_b_ifindex = -1;