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.
11 * Same code handles filtering of duplicates for PRP as well.
14 #include <linux/if_ether.h>
15 #include <linux/etherdevice.h>
16 #include <linux/slab.h>
17 #include <linux/rculist.h>
18 #include <linux/jhash.h>
20 #include "hsr_framereg.h"
21 #include "hsr_netlink.h"
24 int lockdep_hsr_is_held(spinlock_t *lock)
26 return lockdep_is_held(lock);
30 u32 hsr_mac_hash(struct hsr_priv *hsr, const unsigned char *addr)
32 u32 hash = jhash(addr, ETH_ALEN, hsr->hash_seed);
34 return reciprocal_scale(hash, hsr->hash_buckets);
37 struct hsr_node *hsr_node_get_first(struct hlist_head *head, spinlock_t *lock)
39 struct hlist_node *first;
41 first = rcu_dereference_bh_check(hlist_first_rcu(head),
42 lockdep_hsr_is_held(lock));
44 return hlist_entry(first, struct hsr_node, mac_list);
49 /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
52 static bool seq_nr_after(u16 a, u16 b)
54 /* Remove inconsistency where
55 * seq_nr_after(a, b) == seq_nr_before(a, b)
57 if ((int)b - a == 32768)
60 return (((s16)(b - a)) < 0);
63 #define seq_nr_before(a, b) seq_nr_after((b), (a))
64 #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
66 bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
68 struct hsr_node *node;
70 node = hsr_node_get_first(&hsr->self_node_db, &hsr->list_lock);
72 WARN_ONCE(1, "HSR: No self node\n");
76 if (ether_addr_equal(addr, node->macaddress_A))
78 if (ether_addr_equal(addr, node->macaddress_B))
84 /* Search for mac entry. Caller must hold rcu read lock.
86 static struct hsr_node *find_node_by_addr_A(struct hlist_head *node_db,
87 const unsigned char addr[ETH_ALEN])
89 struct hsr_node *node;
91 hlist_for_each_entry_rcu(node, node_db, mac_list) {
92 if (ether_addr_equal(node->macaddress_A, addr))
99 /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
100 * frames from self that's been looped over the HSR ring.
102 int hsr_create_self_node(struct hsr_priv *hsr,
103 const unsigned char addr_a[ETH_ALEN],
104 const unsigned char addr_b[ETH_ALEN])
106 struct hlist_head *self_node_db = &hsr->self_node_db;
107 struct hsr_node *node, *oldnode;
109 node = kmalloc(sizeof(*node), GFP_KERNEL);
113 ether_addr_copy(node->macaddress_A, addr_a);
114 ether_addr_copy(node->macaddress_B, addr_b);
116 spin_lock_bh(&hsr->list_lock);
117 oldnode = hsr_node_get_first(self_node_db, &hsr->list_lock);
119 hlist_replace_rcu(&oldnode->mac_list, &node->mac_list);
120 spin_unlock_bh(&hsr->list_lock);
121 kfree_rcu(oldnode, rcu_head);
123 hlist_add_tail_rcu(&node->mac_list, self_node_db);
124 spin_unlock_bh(&hsr->list_lock);
130 void hsr_del_self_node(struct hsr_priv *hsr)
132 struct hlist_head *self_node_db = &hsr->self_node_db;
133 struct hsr_node *node;
135 spin_lock_bh(&hsr->list_lock);
136 node = hsr_node_get_first(self_node_db, &hsr->list_lock);
138 hlist_del_rcu(&node->mac_list);
139 kfree_rcu(node, rcu_head);
141 spin_unlock_bh(&hsr->list_lock);
144 void hsr_del_nodes(struct hlist_head *node_db)
146 struct hsr_node *node;
147 struct hlist_node *tmp;
149 hlist_for_each_entry_safe(node, tmp, node_db, mac_list)
150 kfree_rcu(node, rcu_head);
153 void prp_handle_san_frame(bool san, enum hsr_port_type port,
154 struct hsr_node *node)
156 /* Mark if the SAN node is over LAN_A or LAN_B */
157 if (port == HSR_PT_SLAVE_A) {
162 if (port == HSR_PT_SLAVE_B)
166 /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A;
167 * seq_out is used to initialize filtering of outgoing duplicate frames
168 * originating from the newly added node.
170 static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
171 struct hlist_head *node_db,
172 unsigned char addr[],
173 u16 seq_out, bool san,
174 enum hsr_port_type rx_port)
176 struct hsr_node *new_node, *node;
180 new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
184 ether_addr_copy(new_node->macaddress_A, addr);
186 /* We are only interested in time diffs here, so use current jiffies
187 * as initialization. (0 could trigger an spurious ring error warning).
190 for (i = 0; i < HSR_PT_PORTS; i++) {
191 new_node->time_in[i] = now;
192 new_node->time_out[i] = now;
194 for (i = 0; i < HSR_PT_PORTS; i++)
195 new_node->seq_out[i] = seq_out;
197 if (san && hsr->proto_ops->handle_san_frame)
198 hsr->proto_ops->handle_san_frame(san, rx_port, new_node);
200 spin_lock_bh(&hsr->list_lock);
201 hlist_for_each_entry_rcu(node, node_db, mac_list,
202 lockdep_hsr_is_held(&hsr->list_lock)) {
203 if (ether_addr_equal(node->macaddress_A, addr))
205 if (ether_addr_equal(node->macaddress_B, addr))
208 hlist_add_tail_rcu(&new_node->mac_list, node_db);
209 spin_unlock_bh(&hsr->list_lock);
212 spin_unlock_bh(&hsr->list_lock);
217 void prp_update_san_info(struct hsr_node *node, bool is_sup)
226 /* Get the hsr_node from which 'skb' was sent.
228 struct hsr_node *hsr_get_node(struct hsr_port *port, struct hlist_head *node_db,
229 struct sk_buff *skb, bool is_sup,
230 enum hsr_port_type rx_port)
232 struct hsr_priv *hsr = port->hsr;
233 struct hsr_node *node;
234 struct ethhdr *ethhdr;
239 if (!skb_mac_header_was_set(skb))
242 ethhdr = (struct ethhdr *)skb_mac_header(skb);
244 hlist_for_each_entry_rcu(node, node_db, mac_list) {
245 if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
246 if (hsr->proto_ops->update_san_info)
247 hsr->proto_ops->update_san_info(node, is_sup);
250 if (ether_addr_equal(node->macaddress_B, ethhdr->h_source)) {
251 if (hsr->proto_ops->update_san_info)
252 hsr->proto_ops->update_san_info(node, is_sup);
257 /* Everyone may create a node entry, connected node to a HSR/PRP
260 if (ethhdr->h_proto == htons(ETH_P_PRP) ||
261 ethhdr->h_proto == htons(ETH_P_HSR)) {
262 /* Use the existing sequence_nr from the tag as starting point
263 * for filtering duplicate frames.
265 seq_out = hsr_get_skb_sequence_nr(skb) - 1;
267 rct = skb_get_PRP_rct(skb);
268 if (rct && prp_check_lsdu_size(skb, rct, is_sup)) {
269 seq_out = prp_get_skb_sequence_nr(rct);
271 if (rx_port != HSR_PT_MASTER)
273 seq_out = HSR_SEQNR_START;
277 return hsr_add_node(hsr, node_db, ethhdr->h_source, seq_out,
281 /* Use the Supervision frame's info about an eventual macaddress_B for merging
282 * nodes that has previously had their macaddress_B registered as a separate
285 void hsr_handle_sup_frame(struct hsr_frame_info *frame)
287 struct hsr_node *node_curr = frame->node_src;
288 struct hsr_port *port_rcv = frame->port_rcv;
289 struct hsr_priv *hsr = port_rcv->hsr;
290 struct hsr_sup_payload *hsr_sp;
291 struct hsr_sup_tlv *hsr_sup_tlv;
292 struct hsr_node *node_real;
293 struct sk_buff *skb = NULL;
294 struct hlist_head *node_db;
295 struct ethhdr *ethhdr;
297 unsigned int pull_size = 0;
298 unsigned int total_pull_size = 0;
301 /* Here either frame->skb_hsr or frame->skb_prp should be
302 * valid as supervision frame always will have protocol
306 skb = frame->skb_hsr;
307 else if (frame->skb_prp)
308 skb = frame->skb_prp;
309 else if (frame->skb_std)
310 skb = frame->skb_std;
314 /* Leave the ethernet header. */
315 pull_size = sizeof(struct ethhdr);
316 skb_pull(skb, pull_size);
317 total_pull_size += pull_size;
319 ethhdr = (struct ethhdr *)skb_mac_header(skb);
321 /* And leave the HSR tag. */
322 if (ethhdr->h_proto == htons(ETH_P_HSR)) {
323 pull_size = sizeof(struct ethhdr);
324 skb_pull(skb, pull_size);
325 total_pull_size += pull_size;
328 /* And leave the HSR sup tag. */
329 pull_size = sizeof(struct hsr_tag);
330 skb_pull(skb, pull_size);
331 total_pull_size += pull_size;
333 /* get HSR sup payload */
334 hsr_sp = (struct hsr_sup_payload *)skb->data;
336 /* Merge node_curr (registered on macaddress_B) into node_real */
337 node_db = port_rcv->hsr->node_db;
338 hash = hsr_mac_hash(hsr, hsr_sp->macaddress_A);
339 node_real = find_node_by_addr_A(&node_db[hash], hsr_sp->macaddress_A);
341 /* No frame received from AddrA of this node yet */
342 node_real = hsr_add_node(hsr, &node_db[hash],
343 hsr_sp->macaddress_A,
344 HSR_SEQNR_START - 1, true,
347 goto done; /* No mem */
348 if (node_real == node_curr)
349 /* Node has already been merged */
352 /* Leave the first HSR sup payload. */
353 pull_size = sizeof(struct hsr_sup_payload);
354 skb_pull(skb, pull_size);
355 total_pull_size += pull_size;
357 /* Get second supervision tlv */
358 hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data;
359 /* And check if it is a redbox mac TLV */
360 if (hsr_sup_tlv->HSR_TLV_type == PRP_TLV_REDBOX_MAC) {
361 /* We could stop here after pushing hsr_sup_payload,
362 * or proceed and allow macaddress_B and for redboxes.
364 /* Sanity check length */
365 if (hsr_sup_tlv->HSR_TLV_length != 6)
368 /* Leave the second HSR sup tlv. */
369 pull_size = sizeof(struct hsr_sup_tlv);
370 skb_pull(skb, pull_size);
371 total_pull_size += pull_size;
373 /* Get redbox mac address. */
374 hsr_sp = (struct hsr_sup_payload *)skb->data;
376 /* Check if redbox mac and node mac are equal. */
377 if (!ether_addr_equal(node_real->macaddress_A,
378 hsr_sp->macaddress_A)) {
379 /* This is a redbox supervision frame for a VDAN! */
384 ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
385 for (i = 0; i < HSR_PT_PORTS; i++) {
386 if (!node_curr->time_in_stale[i] &&
387 time_after(node_curr->time_in[i], node_real->time_in[i])) {
388 node_real->time_in[i] = node_curr->time_in[i];
389 node_real->time_in_stale[i] =
390 node_curr->time_in_stale[i];
392 if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
393 node_real->seq_out[i] = node_curr->seq_out[i];
395 node_real->addr_B_port = port_rcv->type;
397 spin_lock_bh(&hsr->list_lock);
398 hlist_del_rcu(&node_curr->mac_list);
399 spin_unlock_bh(&hsr->list_lock);
400 kfree_rcu(node_curr, rcu_head);
404 skb_push(skb, total_pull_size);
407 /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
409 * If the frame was sent by a node's B interface, replace the source
410 * address with that node's "official" address (macaddress_A) so that upper
411 * layers recognize where it came from.
413 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
415 if (!skb_mac_header_was_set(skb)) {
416 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
420 memcpy(ð_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
423 /* 'skb' is a frame meant for another host.
424 * 'port' is the outgoing interface
426 * Substitute the target (dest) MAC address if necessary, so the it matches the
427 * recipient interface MAC address, regardless of whether that is the
428 * recipient's A or B interface.
429 * This is needed to keep the packets flowing through switches that learn on
430 * which "side" the different interfaces are.
432 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
433 struct hsr_port *port)
435 struct hsr_node *node_dst;
438 if (!skb_mac_header_was_set(skb)) {
439 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
443 if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
446 hash = hsr_mac_hash(port->hsr, eth_hdr(skb)->h_dest);
447 node_dst = find_node_by_addr_A(&port->hsr->node_db[hash],
448 eth_hdr(skb)->h_dest);
451 netdev_err(skb->dev, "%s: Unknown node\n", __func__);
454 if (port->type != node_dst->addr_B_port)
457 if (is_valid_ether_addr(node_dst->macaddress_B))
458 ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
461 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
464 /* Don't register incoming frames without a valid sequence number. This
465 * ensures entries of restarted nodes gets pruned so that they can
466 * re-register and resume communications.
468 if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) &&
469 seq_nr_before(sequence_nr, node->seq_out[port->type]))
472 node->time_in[port->type] = jiffies;
473 node->time_in_stale[port->type] = false;
476 /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
477 * ethhdr->h_source address and skb->mac_header set.
480 * 1 if frame can be shown to have been sent recently on this interface,
482 * negative error code on error
484 int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
487 if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]) &&
488 time_is_after_jiffies(node->time_out[port->type] +
489 msecs_to_jiffies(HSR_ENTRY_FORGET_TIME)))
492 node->time_out[port->type] = jiffies;
493 node->seq_out[port->type] = sequence_nr;
497 static struct hsr_port *get_late_port(struct hsr_priv *hsr,
498 struct hsr_node *node)
500 if (node->time_in_stale[HSR_PT_SLAVE_A])
501 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
502 if (node->time_in_stale[HSR_PT_SLAVE_B])
503 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
505 if (time_after(node->time_in[HSR_PT_SLAVE_B],
506 node->time_in[HSR_PT_SLAVE_A] +
507 msecs_to_jiffies(MAX_SLAVE_DIFF)))
508 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
509 if (time_after(node->time_in[HSR_PT_SLAVE_A],
510 node->time_in[HSR_PT_SLAVE_B] +
511 msecs_to_jiffies(MAX_SLAVE_DIFF)))
512 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
517 /* Remove stale sequence_nr records. Called by timer every
518 * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
520 void hsr_prune_nodes(struct timer_list *t)
522 struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
523 struct hlist_node *tmp;
524 struct hsr_node *node;
525 struct hsr_port *port;
526 unsigned long timestamp;
527 unsigned long time_a, time_b;
530 spin_lock_bh(&hsr->list_lock);
532 for (i = 0; i < hsr->hash_buckets; i++) {
533 hlist_for_each_entry_safe(node, tmp, &hsr->node_db[i],
535 /* Don't prune own node.
536 * Neither time_in[HSR_PT_SLAVE_A]
537 * nor time_in[HSR_PT_SLAVE_B], will ever be updated
538 * for the master port. Thus the master node will be
539 * repeatedly pruned leading to packet loss.
541 if (hsr_addr_is_self(hsr, node->macaddress_A))
545 time_a = node->time_in[HSR_PT_SLAVE_A];
546 time_b = node->time_in[HSR_PT_SLAVE_B];
548 /* Check for timestamps old enough to
551 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
552 node->time_in_stale[HSR_PT_SLAVE_A] = true;
553 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
554 node->time_in_stale[HSR_PT_SLAVE_B] = true;
556 /* Get age of newest frame from node.
557 * At least one time_in is OK here; nodes get pruned
558 * long before both time_ins can get stale
561 if (node->time_in_stale[HSR_PT_SLAVE_A] ||
562 (!node->time_in_stale[HSR_PT_SLAVE_B] &&
563 time_after(time_b, time_a)))
566 /* Warn of ring error only as long as we get
569 if (time_is_after_jiffies(timestamp +
570 msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
572 port = get_late_port(hsr, node);
574 hsr_nl_ringerror(hsr,
580 /* Prune old entries */
581 if (time_is_before_jiffies(timestamp +
582 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
583 hsr_nl_nodedown(hsr, node->macaddress_A);
584 hlist_del_rcu(&node->mac_list);
585 /* Note that we need to free this
588 kfree_rcu(node, rcu_head);
592 spin_unlock_bh(&hsr->list_lock);
595 mod_timer(&hsr->prune_timer,
596 jiffies + msecs_to_jiffies(PRUNE_PERIOD));
599 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
600 unsigned char addr[ETH_ALEN])
602 struct hsr_node *node;
605 hash = hsr_mac_hash(hsr, addr);
608 node = hsr_node_get_first(&hsr->node_db[hash],
611 ether_addr_copy(addr, node->macaddress_A);
616 hlist_for_each_entry_continue_rcu(node, mac_list) {
617 ether_addr_copy(addr, node->macaddress_A);
624 int hsr_get_node_data(struct hsr_priv *hsr,
625 const unsigned char *addr,
626 unsigned char addr_b[ETH_ALEN],
627 unsigned int *addr_b_ifindex,
633 struct hsr_node *node;
634 struct hsr_port *port;
638 hash = hsr_mac_hash(hsr, addr);
640 node = find_node_by_addr_A(&hsr->node_db[hash], addr);
644 ether_addr_copy(addr_b, node->macaddress_B);
646 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
647 if (node->time_in_stale[HSR_PT_SLAVE_A])
649 #if HZ <= MSEC_PER_SEC
650 else if (tdiff > msecs_to_jiffies(INT_MAX))
654 *if1_age = jiffies_to_msecs(tdiff);
656 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
657 if (node->time_in_stale[HSR_PT_SLAVE_B])
659 #if HZ <= MSEC_PER_SEC
660 else if (tdiff > msecs_to_jiffies(INT_MAX))
664 *if2_age = jiffies_to_msecs(tdiff);
666 /* Present sequence numbers as if they were incoming on interface */
667 *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
668 *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
670 if (node->addr_B_port != HSR_PT_NONE) {
671 port = hsr_port_get_hsr(hsr, node->addr_B_port);
672 *addr_b_ifindex = port->dev->ifindex;
674 *addr_b_ifindex = -1;