1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
3 *
4 * Author(s):
5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
6 *
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.
12 */
13
14 #include <kunit/visibility.h>
15 #include <linux/if_ether.h>
16 #include <linux/etherdevice.h>
17 #include <linux/slab.h>
18 #include <linux/rculist.h>
19 #include "hsr_main.h"
20 #include "hsr_framereg.h"
21 #include "hsr_netlink.h"
22
hsr_addr_is_redbox(struct hsr_priv * hsr,unsigned char * addr)23 bool hsr_addr_is_redbox(struct hsr_priv *hsr, unsigned char *addr)
24 {
25 if (!hsr->redbox || !is_valid_ether_addr(hsr->macaddress_redbox))
26 return false;
27
28 return ether_addr_equal(addr, hsr->macaddress_redbox);
29 }
30
hsr_addr_is_self(struct hsr_priv * hsr,unsigned char * addr)31 bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
32 {
33 struct hsr_self_node *sn;
34 bool ret = false;
35
36 rcu_read_lock();
37 sn = rcu_dereference(hsr->self_node);
38 if (!sn)
39 goto out;
40
41 if (ether_addr_equal(addr, sn->macaddress_A) ||
42 ether_addr_equal(addr, sn->macaddress_B))
43 ret = true;
44 out:
45 rcu_read_unlock();
46 return ret;
47 }
48
49 /* Search for mac entry. Caller must hold rcu read lock.
50 */
find_node_by_addr_A(struct list_head * node_db,const unsigned char addr[ETH_ALEN])51 static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
52 const unsigned char addr[ETH_ALEN])
53 {
54 struct hsr_node *node;
55
56 list_for_each_entry_rcu(node, node_db, mac_list) {
57 if (ether_addr_equal(node->macaddress_A, addr))
58 return node;
59 }
60
61 return NULL;
62 }
63
64 /* Check if node for a given MAC address is already present in data base
65 */
hsr_is_node_in_db(struct list_head * node_db,const unsigned char addr[ETH_ALEN])66 bool hsr_is_node_in_db(struct list_head *node_db,
67 const unsigned char addr[ETH_ALEN])
68 {
69 return !!find_node_by_addr_A(node_db, addr);
70 }
71
72 /* Helper for device init; the self_node is used in hsr_handle_frame() to
73 * recognize frames from self that's been looped over the HSR ring.
74 */
hsr_create_self_node(struct hsr_priv * hsr,const unsigned char addr_a[ETH_ALEN],const unsigned char addr_b[ETH_ALEN])75 int hsr_create_self_node(struct hsr_priv *hsr,
76 const unsigned char addr_a[ETH_ALEN],
77 const unsigned char addr_b[ETH_ALEN])
78 {
79 struct hsr_self_node *sn, *old;
80
81 sn = kmalloc_obj(*sn);
82 if (!sn)
83 return -ENOMEM;
84
85 ether_addr_copy(sn->macaddress_A, addr_a);
86 ether_addr_copy(sn->macaddress_B, addr_b);
87
88 spin_lock_bh(&hsr->list_lock);
89 old = rcu_replace_pointer(hsr->self_node, sn,
90 lockdep_is_held(&hsr->list_lock));
91 spin_unlock_bh(&hsr->list_lock);
92
93 if (old)
94 kfree_rcu(old, rcu_head);
95 return 0;
96 }
97
hsr_del_self_node(struct hsr_priv * hsr)98 void hsr_del_self_node(struct hsr_priv *hsr)
99 {
100 struct hsr_self_node *old;
101
102 spin_lock_bh(&hsr->list_lock);
103 old = rcu_replace_pointer(hsr->self_node, NULL,
104 lockdep_is_held(&hsr->list_lock));
105 spin_unlock_bh(&hsr->list_lock);
106 if (old)
107 kfree_rcu(old, rcu_head);
108 }
109
hsr_free_node(struct hsr_node * node)110 static void hsr_free_node(struct hsr_node *node)
111 {
112 xa_destroy(&node->seq_blocks);
113 kfree(node->block_buf);
114 kfree(node);
115 }
116
hsr_free_node_rcu(struct rcu_head * rn)117 static void hsr_free_node_rcu(struct rcu_head *rn)
118 {
119 struct hsr_node *node = container_of(rn, struct hsr_node, rcu_head);
120
121 hsr_free_node(node);
122 }
123
hsr_lock_seq_out_pair(struct hsr_node * node_a,struct hsr_node * node_b)124 static void hsr_lock_seq_out_pair(struct hsr_node *node_a,
125 struct hsr_node *node_b)
126 {
127 if (node_a == node_b) {
128 spin_lock_bh(&node_a->seq_out_lock);
129 return;
130 }
131
132 if (node_a < node_b) {
133 spin_lock_bh(&node_a->seq_out_lock);
134 spin_lock_nested(&node_b->seq_out_lock, SINGLE_DEPTH_NESTING);
135 } else {
136 spin_lock_bh(&node_b->seq_out_lock);
137 spin_lock_nested(&node_a->seq_out_lock, SINGLE_DEPTH_NESTING);
138 }
139 }
140
hsr_unlock_seq_out_pair(struct hsr_node * node_a,struct hsr_node * node_b)141 static void hsr_unlock_seq_out_pair(struct hsr_node *node_a,
142 struct hsr_node *node_b)
143 {
144 if (node_a == node_b) {
145 spin_unlock_bh(&node_a->seq_out_lock);
146 return;
147 }
148
149 if (node_a < node_b) {
150 spin_unlock(&node_b->seq_out_lock);
151 spin_unlock_bh(&node_a->seq_out_lock);
152 } else {
153 spin_unlock(&node_a->seq_out_lock);
154 spin_unlock_bh(&node_b->seq_out_lock);
155 }
156 }
157
hsr_del_nodes(struct list_head * node_db)158 void hsr_del_nodes(struct list_head *node_db)
159 {
160 struct hsr_node *node;
161 struct hsr_node *tmp;
162
163 list_for_each_entry_safe(node, tmp, node_db, mac_list) {
164 list_del_rcu(&node->mac_list);
165 call_rcu(&node->rcu_head, hsr_free_node_rcu);
166 }
167 }
168
prp_handle_san_frame(bool san,enum hsr_port_type port,struct hsr_node * node)169 void prp_handle_san_frame(bool san, enum hsr_port_type port,
170 struct hsr_node *node)
171 {
172 /* Mark if the SAN node is over LAN_A or LAN_B */
173 if (port == HSR_PT_SLAVE_A) {
174 node->san_a = true;
175 return;
176 }
177
178 if (port == HSR_PT_SLAVE_B)
179 node->san_b = true;
180 }
181
182 /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A.
183 */
hsr_add_node(struct hsr_priv * hsr,struct list_head * node_db,unsigned char addr[],bool san,enum hsr_port_type rx_port)184 static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
185 struct list_head *node_db,
186 unsigned char addr[], bool san,
187 enum hsr_port_type rx_port)
188 {
189 struct hsr_node *new_node, *node = NULL;
190 unsigned long now;
191 size_t block_sz;
192 int i;
193
194 new_node = kzalloc_obj(*new_node, GFP_ATOMIC);
195 if (!new_node)
196 return NULL;
197
198 ether_addr_copy(new_node->macaddress_A, addr);
199 spin_lock_init(&new_node->seq_out_lock);
200
201 if (hsr->prot_version == PRP_V1)
202 new_node->seq_port_cnt = hsr->redbox ? 2 : 1;
203 else
204 new_node->seq_port_cnt = HSR_PT_PORTS - 1;
205
206 block_sz = hsr_seq_block_size(new_node);
207 new_node->block_buf = kcalloc(HSR_MAX_SEQ_BLOCKS, block_sz, GFP_ATOMIC);
208 if (!new_node->block_buf)
209 goto free;
210
211 xa_init(&new_node->seq_blocks);
212
213 /* We are only interested in time diffs here, so use current jiffies
214 * as initialization. (0 could trigger an spurious ring error warning).
215 */
216 now = jiffies;
217 for (i = 0; i < HSR_PT_PORTS; i++) {
218 new_node->time_in[i] = now;
219 }
220
221 if (san && hsr->proto_ops->handle_san_frame)
222 hsr->proto_ops->handle_san_frame(san, rx_port, new_node);
223
224 spin_lock_bh(&hsr->list_lock);
225 list_for_each_entry_rcu(node, node_db, mac_list,
226 lockdep_is_held(&hsr->list_lock)) {
227 if (ether_addr_equal(node->macaddress_A, addr))
228 goto out;
229 if (ether_addr_equal(node->macaddress_B, addr))
230 goto out;
231 }
232 list_add_tail_rcu(&new_node->mac_list, node_db);
233 spin_unlock_bh(&hsr->list_lock);
234 return new_node;
235 out:
236 spin_unlock_bh(&hsr->list_lock);
237 kfree(new_node->block_buf);
238 free:
239 kfree(new_node);
240 return node;
241 }
242
prp_update_san_info(struct hsr_node * node,bool is_sup)243 void prp_update_san_info(struct hsr_node *node, bool is_sup)
244 {
245 if (!is_sup)
246 return;
247
248 node->san_a = false;
249 node->san_b = false;
250 }
251
252 /* Get the hsr_node from which 'skb' was sent.
253 */
hsr_get_node(struct hsr_port * port,struct list_head * node_db,struct sk_buff * skb,bool is_sup,enum hsr_port_type rx_port)254 struct hsr_node *hsr_get_node(struct hsr_port *port, struct list_head *node_db,
255 struct sk_buff *skb, bool is_sup,
256 enum hsr_port_type rx_port)
257 {
258 struct hsr_priv *hsr = port->hsr;
259 struct hsr_node *node;
260 struct ethhdr *ethhdr;
261 struct prp_rct *rct;
262 bool san = false;
263
264 if (!skb_mac_header_was_set(skb))
265 return NULL;
266
267 ethhdr = (struct ethhdr *)skb_mac_header(skb);
268
269 list_for_each_entry_rcu(node, node_db, mac_list) {
270 if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
271 if (hsr->proto_ops->update_san_info)
272 hsr->proto_ops->update_san_info(node, is_sup);
273 return node;
274 }
275 if (ether_addr_equal(node->macaddress_B, ethhdr->h_source)) {
276 if (hsr->proto_ops->update_san_info)
277 hsr->proto_ops->update_san_info(node, is_sup);
278 return node;
279 }
280 }
281
282 /* Check if required node is not in proxy nodes table */
283 list_for_each_entry_rcu(node, &hsr->proxy_node_db, mac_list) {
284 if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
285 if (hsr->proto_ops->update_san_info)
286 hsr->proto_ops->update_san_info(node, is_sup);
287 return node;
288 }
289 }
290
291 /* Everyone may create a node entry, connected node to a HSR/PRP
292 * device.
293 */
294 if (ethhdr->h_proto == htons(ETH_P_PRP) ||
295 ethhdr->h_proto == htons(ETH_P_HSR)) {
296 bool prp_sup;
297
298 /* A PRP supervision frame is an untagged ETH_P_PRP frame
299 * (mac_len == ETH_HLEN); its RCT is appended only on egress.
300 * HSR (ETH_P_HSR) supervision is front-tagged and still must
301 * contain a struct hsr_ethhdr.
302 */
303 prp_sup = hsr->prot_version == PRP_V1 &&
304 ethhdr->h_proto == htons(ETH_P_PRP) && is_sup;
305
306 if (!prp_sup && skb->mac_len < sizeof(struct hsr_ethhdr))
307 return NULL;
308 } else {
309 rct = skb_get_PRP_rct(skb);
310 if (!rct && rx_port != HSR_PT_MASTER)
311 san = true;
312 }
313
314 return hsr_add_node(hsr, node_db, ethhdr->h_source, san, rx_port);
315 }
316
hsr_seq_block_is_old(struct hsr_seq_block * block)317 static bool hsr_seq_block_is_old(struct hsr_seq_block *block)
318 {
319 unsigned long expiry = msecs_to_jiffies(HSR_ENTRY_FORGET_TIME);
320
321 return time_is_before_jiffies(block->time + expiry);
322 }
323
hsr_forget_seq_block(struct hsr_node * node,struct hsr_seq_block * block)324 static void hsr_forget_seq_block(struct hsr_node *node,
325 struct hsr_seq_block *block)
326 {
327 if (block->time)
328 xa_erase(&node->seq_blocks, block->block_idx);
329 block->time = 0;
330 }
331
332 /* Get the currently active sequence number block. If there is no block yet, or
333 * the existing one is expired, a new block is created. The idea is to maintain
334 * a "sparse bitmap" where a bitmap for the whole sequence number space is
335 * split into blocks and not all blocks exist all the time. The blocks can
336 * expire after time (in low traffic situations) or when they are replaced in
337 * the backing fixed size buffer (in high traffic situations).
338 */
hsr_get_seq_block(struct hsr_node * node,u16 block_idx)339 VISIBLE_IF_KUNIT struct hsr_seq_block *hsr_get_seq_block(struct hsr_node *node,
340 u16 block_idx)
341 {
342 struct hsr_seq_block *block, *res;
343 size_t block_sz;
344
345 block = xa_load(&node->seq_blocks, block_idx);
346
347 if (block && hsr_seq_block_is_old(block)) {
348 hsr_forget_seq_block(node, block);
349 block = NULL;
350 }
351
352 if (!block) {
353 block_sz = hsr_seq_block_size(node);
354 block = node->block_buf + node->next_block * block_sz;
355 hsr_forget_seq_block(node, block);
356
357 memset(block, 0, block_sz);
358 block->time = jiffies;
359 block->block_idx = block_idx;
360
361 res = xa_store(&node->seq_blocks, block_idx, block, GFP_ATOMIC);
362 if (xa_is_err(res)) {
363 block->time = 0;
364 return NULL;
365 }
366
367 node->next_block =
368 (node->next_block + 1) & (HSR_MAX_SEQ_BLOCKS - 1);
369 }
370
371 return block;
372 }
373 EXPORT_SYMBOL_IF_KUNIT(hsr_get_seq_block);
374
375 /* Use the Supervision frame's info about an eventual macaddress_B for merging
376 * nodes that has previously had their macaddress_B registered as a separate
377 * node.
378 */
hsr_handle_sup_frame(struct hsr_frame_info * frame)379 void hsr_handle_sup_frame(struct hsr_frame_info *frame)
380 {
381 struct hsr_node *node_curr = frame->node_src;
382 struct hsr_port *port_rcv = frame->port_rcv;
383 struct hsr_seq_block *src_blk, *merge_blk;
384 struct hsr_priv *hsr = port_rcv->hsr;
385 struct hsr_sup_tlv *hsr_sup_tlv;
386 struct hsr_sup_payload *hsr_sp;
387 struct hsr_node *node_real;
388 struct sk_buff *skb = NULL;
389 struct list_head *node_db;
390 struct ethhdr *ethhdr;
391 unsigned int total_pull_size = 0;
392 unsigned int pull_size = 0;
393 unsigned int seq_port_cnt;
394 unsigned long idx;
395 int i;
396
397 /* Here either frame->skb_hsr or frame->skb_prp should be
398 * valid as supervision frame always will have protocol
399 * header info.
400 */
401 if (frame->skb_hsr)
402 skb = frame->skb_hsr;
403 else if (frame->skb_prp)
404 skb = frame->skb_prp;
405 else if (frame->skb_std)
406 skb = frame->skb_std;
407 if (!skb)
408 return;
409
410 /* Leave the ethernet header. */
411 pull_size = sizeof(struct ethhdr);
412 skb_pull(skb, pull_size);
413 total_pull_size += pull_size;
414
415 ethhdr = (struct ethhdr *)skb_mac_header(skb);
416
417 /* And leave the HSR tag. */
418 if (ethhdr->h_proto == htons(ETH_P_HSR)) {
419 pull_size = sizeof(struct hsr_tag);
420 skb_pull(skb, pull_size);
421 total_pull_size += pull_size;
422 }
423
424 /* And leave the HSR sup tag. */
425 pull_size = sizeof(struct hsr_sup_tag);
426 skb_pull(skb, pull_size);
427 total_pull_size += pull_size;
428
429 /* get HSR sup payload */
430 hsr_sp = (struct hsr_sup_payload *)skb->data;
431
432 /* Merge node_curr (registered on macaddress_B) into node_real */
433 node_db = &port_rcv->hsr->node_db;
434 node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
435 if (!node_real)
436 /* No frame received from AddrA of this node yet */
437 node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
438 true, port_rcv->type);
439 if (!node_real)
440 goto done; /* No mem */
441 if (node_real == node_curr)
442 /* Node has already been merged */
443 goto done;
444
445 /* Leave the first HSR sup payload. */
446 pull_size = sizeof(struct hsr_sup_payload);
447 skb_pull(skb, pull_size);
448 total_pull_size += pull_size;
449
450 /* Get second supervision tlv */
451 hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data;
452 /* And check if it is a redbox mac TLV */
453 if (hsr_sup_tlv->HSR_TLV_type == PRP_TLV_REDBOX_MAC) {
454 /* We could stop here after pushing hsr_sup_payload,
455 * or proceed and allow macaddress_B and for redboxes.
456 */
457 /* Sanity check length */
458 if (hsr_sup_tlv->HSR_TLV_length != 6)
459 goto done;
460
461 /* Leave the second HSR sup tlv. */
462 pull_size = sizeof(struct hsr_sup_tlv);
463 skb_pull(skb, pull_size);
464 total_pull_size += pull_size;
465
466 /* Get redbox mac address. */
467 hsr_sp = (struct hsr_sup_payload *)skb->data;
468
469 /* Check if redbox mac and node mac are equal. */
470 if (!ether_addr_equal(node_real->macaddress_A, hsr_sp->macaddress_A)) {
471 /* This is a redbox supervision frame for a VDAN! */
472 goto done;
473 }
474 }
475
476 ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
477 hsr_lock_seq_out_pair(node_real, node_curr);
478 for (i = 0; i < HSR_PT_PORTS; i++) {
479 if (!node_curr->time_in_stale[i] &&
480 time_after(node_curr->time_in[i], node_real->time_in[i])) {
481 node_real->time_in[i] = node_curr->time_in[i];
482 node_real->time_in_stale[i] =
483 node_curr->time_in_stale[i];
484 }
485 }
486
487 seq_port_cnt = min(node_real->seq_port_cnt, node_curr->seq_port_cnt);
488 xa_for_each(&node_curr->seq_blocks, idx, src_blk) {
489 if (hsr_seq_block_is_old(src_blk))
490 continue;
491
492 merge_blk = hsr_get_seq_block(node_real, src_blk->block_idx);
493 if (!merge_blk)
494 continue;
495 merge_blk->time = min(merge_blk->time, src_blk->time);
496 for (i = 0; i < seq_port_cnt; i++) {
497 bitmap_or(merge_blk->seq_nrs[i], merge_blk->seq_nrs[i],
498 src_blk->seq_nrs[i], HSR_SEQ_BLOCK_SIZE);
499 }
500 }
501 hsr_unlock_seq_out_pair(node_real, node_curr);
502 node_real->addr_B_port = port_rcv->type;
503
504 spin_lock_bh(&hsr->list_lock);
505 if (!node_curr->removed) {
506 list_del_rcu(&node_curr->mac_list);
507 node_curr->removed = true;
508 call_rcu(&node_curr->rcu_head, hsr_free_node_rcu);
509 }
510 spin_unlock_bh(&hsr->list_lock);
511
512 done:
513 /* Push back here */
514 skb_push(skb, total_pull_size);
515 }
516
517 /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
518 *
519 * If the frame was sent by a node's B interface, replace the source
520 * address with that node's "official" address (macaddress_A) so that upper
521 * layers recognize where it came from.
522 */
hsr_addr_subst_source(struct hsr_node * node,struct sk_buff * skb)523 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
524 {
525 if (!skb_mac_header_was_set(skb)) {
526 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
527 return;
528 }
529
530 memcpy(ð_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
531 }
532
533 /* 'skb' is a frame meant for another host.
534 * 'port' is the outgoing interface
535 *
536 * Substitute the target (dest) MAC address if necessary, so the it matches the
537 * recipient interface MAC address, regardless of whether that is the
538 * recipient's A or B interface.
539 * This is needed to keep the packets flowing through switches that learn on
540 * which "side" the different interfaces are.
541 */
hsr_addr_subst_dest(struct hsr_node * node_src,struct sk_buff * skb,struct hsr_port * port)542 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
543 struct hsr_port *port)
544 {
545 struct hsr_node *node_dst;
546
547 if (!skb_mac_header_was_set(skb)) {
548 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
549 return;
550 }
551
552 if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
553 return;
554
555 node_dst = find_node_by_addr_A(&port->hsr->node_db,
556 eth_hdr(skb)->h_dest);
557 if (!node_dst && port->hsr->redbox)
558 node_dst = find_node_by_addr_A(&port->hsr->proxy_node_db,
559 eth_hdr(skb)->h_dest);
560
561 if (!node_dst) {
562 if (port->hsr->prot_version != PRP_V1 && net_ratelimit())
563 netdev_err(skb->dev, "%s: Unknown node\n", __func__);
564 return;
565 }
566 if (port->type != node_dst->addr_B_port)
567 return;
568
569 if (is_valid_ether_addr(node_dst->macaddress_B))
570 ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
571 }
572
hsr_register_frame_in(struct hsr_node * node,struct hsr_port * port,u16 sequence_nr)573 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
574 u16 sequence_nr)
575 {
576 node->time_in[port->type] = jiffies;
577 node->time_in_stale[port->type] = false;
578 }
579
580 /* Duplicate discard algorithm: we maintain a bitmap where we set a bit for
581 * every seen sequence number. The bitmap is split into blocks and the block
582 * management is detailed in hsr_get_seq_block(). In any case, we err on the
583 * side of accepting a packet, as the specification requires the algorithm to
584 * be "designed such that it never rejects a legitimate frame, while occasional
585 * acceptance of a duplicate can be tolerated." (IEC 62439-3:2021, 4.1.10.3).
586 * While this requirement is explicit for PRP, applying it to HSR does no harm
587 * either.
588 *
589 * 'frame' is the frame to be sent
590 * 'port_type' is the type of the outgoing interface
591 *
592 * Return:
593 * 1 if frame can be shown to have been sent recently on this interface,
594 * 0 otherwise
595 */
hsr_check_duplicate(struct hsr_frame_info * frame,unsigned int port_type)596 static int hsr_check_duplicate(struct hsr_frame_info *frame,
597 unsigned int port_type)
598 {
599 u16 sequence_nr, seq_bit, block_idx;
600 struct hsr_seq_block *block;
601 struct hsr_node *node;
602
603 node = frame->node_src;
604 sequence_nr = frame->sequence_nr;
605
606 if (WARN_ON_ONCE(port_type >= node->seq_port_cnt))
607 return 0;
608
609 spin_lock_bh(&node->seq_out_lock);
610
611 block_idx = hsr_seq_block_index(sequence_nr);
612 block = hsr_get_seq_block(node, block_idx);
613 if (!block)
614 goto out_new;
615
616 seq_bit = hsr_seq_block_bit(sequence_nr);
617 if (__test_and_set_bit(seq_bit, block->seq_nrs[port_type]))
618 goto out_seen;
619
620 out_new:
621 spin_unlock_bh(&node->seq_out_lock);
622 return 0;
623
624 out_seen:
625 spin_unlock_bh(&node->seq_out_lock);
626 return 1;
627 }
628
629 /* HSR duplicate discard: we check if the same frame has already been sent on
630 * this outgoing interface. The check follows the general duplicate discard
631 * algorithm.
632 *
633 * 'port' is the outgoing interface
634 * 'frame' is the frame to be sent
635 *
636 * Return:
637 * 1 if frame can be shown to have been sent recently on this interface,
638 * 0 otherwise
639 */
hsr_register_frame_out(struct hsr_port * port,struct hsr_frame_info * frame)640 int hsr_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame)
641 {
642 return hsr_check_duplicate(frame, port->type - 1);
643 }
644
645 /* PRP duplicate discard: we only consider frames that are received on port A
646 * or port B and should go to the master port. For those, we check if they have
647 * already been received by the host, i.e., master port. The check uses the
648 * general duplicate discard algorithm, but without tracking multiple ports.
649 *
650 * 'port' is the outgoing interface
651 * 'frame' is the frame to be sent
652 *
653 * Return:
654 * 1 if frame can be shown to have been sent recently on this interface,
655 * 0 otherwise
656 */
prp_register_frame_out(struct hsr_port * port,struct hsr_frame_info * frame)657 int prp_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame)
658 {
659 /* out-going frames are always in order */
660 if (frame->port_rcv->type == HSR_PT_MASTER)
661 return 0;
662
663 /* RedBox: forward LAN frames out the interlink to a SAN, deduping the
664 * two LAN copies on a dedicated slot.
665 */
666 if (port->type == HSR_PT_INTERLINK)
667 return hsr_check_duplicate(frame, 1);
668
669 /* For PRP only slave-to-master frames are forwarded. */
670 if (port->type != HSR_PT_MASTER)
671 return 1;
672
673 return hsr_check_duplicate(frame, 0);
674 }
675 EXPORT_SYMBOL_IF_KUNIT(prp_register_frame_out);
676
get_late_port(struct hsr_priv * hsr,struct hsr_node * node)677 static struct hsr_port *get_late_port(struct hsr_priv *hsr,
678 struct hsr_node *node)
679 {
680 if (node->time_in_stale[HSR_PT_SLAVE_A])
681 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
682 if (node->time_in_stale[HSR_PT_SLAVE_B])
683 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
684
685 if (time_after(node->time_in[HSR_PT_SLAVE_B],
686 node->time_in[HSR_PT_SLAVE_A] +
687 msecs_to_jiffies(MAX_SLAVE_DIFF)))
688 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
689 if (time_after(node->time_in[HSR_PT_SLAVE_A],
690 node->time_in[HSR_PT_SLAVE_B] +
691 msecs_to_jiffies(MAX_SLAVE_DIFF)))
692 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
693
694 return NULL;
695 }
696
697 /* Remove stale sequence_nr records. Called by timer every
698 * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
699 */
hsr_prune_nodes(struct timer_list * t)700 void hsr_prune_nodes(struct timer_list *t)
701 {
702 struct hsr_priv *hsr = timer_container_of(hsr, t, prune_timer);
703 struct hsr_node *node;
704 struct hsr_node *tmp;
705 struct hsr_port *port;
706 unsigned long timestamp;
707 unsigned long time_a, time_b;
708
709 spin_lock_bh(&hsr->list_lock);
710 list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
711 /* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A]
712 * nor time_in[HSR_PT_SLAVE_B], will ever be updated for
713 * the master port. Thus the master node will be repeatedly
714 * pruned leading to packet loss.
715 */
716 if (hsr_addr_is_self(hsr, node->macaddress_A))
717 continue;
718
719 /* Shorthand */
720 time_a = node->time_in[HSR_PT_SLAVE_A];
721 time_b = node->time_in[HSR_PT_SLAVE_B];
722
723 /* Check for timestamps old enough to risk wrap-around */
724 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
725 node->time_in_stale[HSR_PT_SLAVE_A] = true;
726 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
727 node->time_in_stale[HSR_PT_SLAVE_B] = true;
728
729 /* Get age of newest frame from node.
730 * At least one time_in is OK here; nodes get pruned long
731 * before both time_ins can get stale
732 */
733 timestamp = time_a;
734 if (node->time_in_stale[HSR_PT_SLAVE_A] ||
735 (!node->time_in_stale[HSR_PT_SLAVE_B] &&
736 time_after(time_b, time_a)))
737 timestamp = time_b;
738
739 /* Warn of ring error only as long as we get frames at all */
740 if (time_is_after_jiffies(timestamp +
741 msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
742 rcu_read_lock();
743 port = get_late_port(hsr, node);
744 if (port)
745 hsr_nl_ringerror(hsr, node->macaddress_A, port);
746 rcu_read_unlock();
747 }
748
749 /* Prune old entries */
750 if (time_is_before_jiffies(timestamp +
751 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
752 hsr_nl_nodedown(hsr, node->macaddress_A);
753 if (!node->removed) {
754 list_del_rcu(&node->mac_list);
755 node->removed = true;
756 /* Note that we need to free this entry later: */
757 call_rcu(&node->rcu_head, hsr_free_node_rcu);
758 }
759 }
760 }
761 spin_unlock_bh(&hsr->list_lock);
762
763 /* Restart timer */
764 mod_timer(&hsr->prune_timer,
765 jiffies + msecs_to_jiffies(PRUNE_PERIOD));
766 }
767
hsr_prune_proxy_nodes(struct timer_list * t)768 void hsr_prune_proxy_nodes(struct timer_list *t)
769 {
770 struct hsr_priv *hsr = timer_container_of(hsr, t, prune_proxy_timer);
771 unsigned long timestamp;
772 struct hsr_node *node;
773 struct hsr_node *tmp;
774
775 spin_lock_bh(&hsr->list_lock);
776 list_for_each_entry_safe(node, tmp, &hsr->proxy_node_db, mac_list) {
777 /* Don't prune RedBox node. */
778 if (hsr_addr_is_redbox(hsr, node->macaddress_A))
779 continue;
780
781 timestamp = node->time_in[HSR_PT_INTERLINK];
782
783 /* Prune old entries */
784 if (time_is_before_jiffies(timestamp +
785 msecs_to_jiffies(HSR_PROXY_NODE_FORGET_TIME))) {
786 hsr_nl_nodedown(hsr, node->macaddress_A);
787 if (!node->removed) {
788 list_del_rcu(&node->mac_list);
789 node->removed = true;
790 /* Note that we need to free this entry later: */
791 call_rcu(&node->rcu_head, hsr_free_node_rcu);
792 }
793 }
794 }
795
796 spin_unlock_bh(&hsr->list_lock);
797
798 /* Restart timer */
799 mod_timer(&hsr->prune_proxy_timer,
800 jiffies + msecs_to_jiffies(PRUNE_PROXY_PERIOD));
801 }
802
hsr_get_next_node(struct hsr_priv * hsr,void * _pos,unsigned char addr[ETH_ALEN])803 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
804 unsigned char addr[ETH_ALEN])
805 {
806 struct hsr_node *node;
807
808 if (!_pos) {
809 node = list_first_or_null_rcu(&hsr->node_db,
810 struct hsr_node, mac_list);
811 if (node)
812 ether_addr_copy(addr, node->macaddress_A);
813 return node;
814 }
815
816 node = _pos;
817 list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
818 ether_addr_copy(addr, node->macaddress_A);
819 return node;
820 }
821
822 return NULL;
823 }
824
825 /* Fill the last sequence number that has been received from node on if1 by
826 * finding the last sequence number sent on port B; accordingly get the last
827 * received sequence number for if2 using sent sequence numbers on port A.
828 */
fill_last_seq_nrs(struct hsr_node * node,u16 * if1_seq,u16 * if2_seq)829 static void fill_last_seq_nrs(struct hsr_node *node, u16 *if1_seq, u16 *if2_seq)
830 {
831 struct hsr_seq_block *block;
832 unsigned int block_off;
833 size_t block_sz;
834 u16 seq_bit;
835
836 spin_lock_bh(&node->seq_out_lock);
837
838 /* Get last inserted block */
839 block_off = (node->next_block - 1) & (HSR_MAX_SEQ_BLOCKS - 1);
840 block_sz = hsr_seq_block_size(node);
841 block = node->block_buf + block_off * block_sz;
842
843 seq_bit = find_last_bit(block->seq_nrs[HSR_PT_SLAVE_B - 1],
844 HSR_SEQ_BLOCK_SIZE);
845 if (seq_bit < HSR_SEQ_BLOCK_SIZE)
846 *if1_seq = (block->block_idx << HSR_SEQ_BLOCK_SHIFT) | seq_bit;
847
848 seq_bit = find_last_bit(block->seq_nrs[HSR_PT_SLAVE_A - 1],
849 HSR_SEQ_BLOCK_SIZE);
850 if (seq_bit < HSR_SEQ_BLOCK_SIZE)
851 *if2_seq = (block->block_idx << HSR_SEQ_BLOCK_SHIFT) | seq_bit;
852
853 spin_unlock_bh(&node->seq_out_lock);
854 }
855
hsr_get_node_data(struct hsr_priv * hsr,const unsigned char * addr,unsigned char addr_b[ETH_ALEN],unsigned int * addr_b_ifindex,int * if1_age,u16 * if1_seq,int * if2_age,u16 * if2_seq)856 int hsr_get_node_data(struct hsr_priv *hsr,
857 const unsigned char *addr,
858 unsigned char addr_b[ETH_ALEN],
859 unsigned int *addr_b_ifindex,
860 int *if1_age,
861 u16 *if1_seq,
862 int *if2_age,
863 u16 *if2_seq)
864 {
865 struct hsr_node *node;
866 struct hsr_port *port;
867 unsigned long tdiff;
868
869 node = find_node_by_addr_A(&hsr->node_db, addr);
870 if (!node)
871 return -ENOENT;
872
873 ether_addr_copy(addr_b, node->macaddress_B);
874
875 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
876 if (node->time_in_stale[HSR_PT_SLAVE_A])
877 *if1_age = INT_MAX;
878 #if HZ <= MSEC_PER_SEC
879 else if (tdiff > msecs_to_jiffies(INT_MAX))
880 *if1_age = INT_MAX;
881 #endif
882 else
883 *if1_age = jiffies_to_msecs(tdiff);
884
885 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
886 if (node->time_in_stale[HSR_PT_SLAVE_B])
887 *if2_age = INT_MAX;
888 #if HZ <= MSEC_PER_SEC
889 else if (tdiff > msecs_to_jiffies(INT_MAX))
890 *if2_age = INT_MAX;
891 #endif
892 else
893 *if2_age = jiffies_to_msecs(tdiff);
894
895 /* Present sequence numbers as if they were incoming on interface */
896 *if1_seq = 0;
897 *if2_seq = 0;
898 if (hsr->prot_version != PRP_V1)
899 fill_last_seq_nrs(node, if1_seq, if2_seq);
900
901 if (node->addr_B_port != HSR_PT_NONE) {
902 port = hsr_port_get_hsr(hsr, node->addr_B_port);
903 if (port)
904 *addr_b_ifindex = port->dev->ifindex;
905 else
906 *addr_b_ifindex = -1;
907 } else {
908 *addr_b_ifindex = -1;
909 }
910
911 return 0;
912 }
913