xref: /linux/net/hsr/hsr_framereg.c (revision ddd664bbff63e09e7a7f9acae9c43605d4cf185f)
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 = 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 		/* Check if skb contains hsr_ethhdr */
297 		if (skb->mac_len < sizeof(struct hsr_ethhdr))
298 			return NULL;
299 	} else {
300 		rct = skb_get_PRP_rct(skb);
301 		if (!rct && rx_port != HSR_PT_MASTER)
302 			san = true;
303 	}
304 
305 	return hsr_add_node(hsr, node_db, ethhdr->h_source, san, rx_port);
306 }
307 
hsr_seq_block_is_old(struct hsr_seq_block * block)308 static bool hsr_seq_block_is_old(struct hsr_seq_block *block)
309 {
310 	unsigned long expiry = msecs_to_jiffies(HSR_ENTRY_FORGET_TIME);
311 
312 	return time_is_before_jiffies(block->time + expiry);
313 }
314 
hsr_forget_seq_block(struct hsr_node * node,struct hsr_seq_block * block)315 static void hsr_forget_seq_block(struct hsr_node *node,
316 				 struct hsr_seq_block *block)
317 {
318 	if (block->time)
319 		xa_erase(&node->seq_blocks, block->block_idx);
320 	block->time = 0;
321 }
322 
323 /* Get the currently active sequence number block. If there is no block yet, or
324  * the existing one is expired, a new block is created. The idea is to maintain
325  * a "sparse bitmap" where a bitmap for the whole sequence number space is
326  * split into blocks and not all blocks exist all the time. The blocks can
327  * expire after time (in low traffic situations) or when they are replaced in
328  * the backing fixed size buffer (in high traffic situations).
329  */
hsr_get_seq_block(struct hsr_node * node,u16 block_idx)330 VISIBLE_IF_KUNIT struct hsr_seq_block *hsr_get_seq_block(struct hsr_node *node,
331 							 u16 block_idx)
332 {
333 	struct hsr_seq_block *block, *res;
334 	size_t block_sz;
335 
336 	block = xa_load(&node->seq_blocks, block_idx);
337 
338 	if (block && hsr_seq_block_is_old(block)) {
339 		hsr_forget_seq_block(node, block);
340 		block = NULL;
341 	}
342 
343 	if (!block) {
344 		block_sz = hsr_seq_block_size(node);
345 		block = node->block_buf + node->next_block * block_sz;
346 		hsr_forget_seq_block(node, block);
347 
348 		memset(block, 0, block_sz);
349 		block->time = jiffies;
350 		block->block_idx = block_idx;
351 
352 		res = xa_store(&node->seq_blocks, block_idx, block, GFP_ATOMIC);
353 		if (xa_is_err(res)) {
354 			block->time = 0;
355 			return NULL;
356 		}
357 
358 		node->next_block =
359 			(node->next_block + 1) & (HSR_MAX_SEQ_BLOCKS - 1);
360 	}
361 
362 	return block;
363 }
364 EXPORT_SYMBOL_IF_KUNIT(hsr_get_seq_block);
365 
366 /* Use the Supervision frame's info about an eventual macaddress_B for merging
367  * nodes that has previously had their macaddress_B registered as a separate
368  * node.
369  */
hsr_handle_sup_frame(struct hsr_frame_info * frame)370 void hsr_handle_sup_frame(struct hsr_frame_info *frame)
371 {
372 	struct hsr_node *node_curr = frame->node_src;
373 	struct hsr_port *port_rcv = frame->port_rcv;
374 	struct hsr_seq_block *src_blk, *merge_blk;
375 	struct hsr_priv *hsr = port_rcv->hsr;
376 	struct hsr_sup_tlv *hsr_sup_tlv;
377 	struct hsr_sup_payload *hsr_sp;
378 	struct hsr_node *node_real;
379 	struct sk_buff *skb = NULL;
380 	struct list_head *node_db;
381 	struct ethhdr *ethhdr;
382 	unsigned int total_pull_size = 0;
383 	unsigned int pull_size = 0;
384 	unsigned long idx;
385 	int i;
386 
387 	/* Here either frame->skb_hsr or frame->skb_prp should be
388 	 * valid as supervision frame always will have protocol
389 	 * header info.
390 	 */
391 	if (frame->skb_hsr)
392 		skb = frame->skb_hsr;
393 	else if (frame->skb_prp)
394 		skb = frame->skb_prp;
395 	else if (frame->skb_std)
396 		skb = frame->skb_std;
397 	if (!skb)
398 		return;
399 
400 	/* Leave the ethernet header. */
401 	pull_size = sizeof(struct ethhdr);
402 	skb_pull(skb, pull_size);
403 	total_pull_size += pull_size;
404 
405 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
406 
407 	/* And leave the HSR tag. */
408 	if (ethhdr->h_proto == htons(ETH_P_HSR)) {
409 		pull_size = sizeof(struct hsr_tag);
410 		skb_pull(skb, pull_size);
411 		total_pull_size += pull_size;
412 	}
413 
414 	/* And leave the HSR sup tag. */
415 	pull_size = sizeof(struct hsr_sup_tag);
416 	skb_pull(skb, pull_size);
417 	total_pull_size += pull_size;
418 
419 	/* get HSR sup payload */
420 	hsr_sp = (struct hsr_sup_payload *)skb->data;
421 
422 	/* Merge node_curr (registered on macaddress_B) into node_real */
423 	node_db = &port_rcv->hsr->node_db;
424 	node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
425 	if (!node_real)
426 		/* No frame received from AddrA of this node yet */
427 		node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
428 					 true, port_rcv->type);
429 	if (!node_real)
430 		goto done; /* No mem */
431 	if (node_real == node_curr)
432 		/* Node has already been merged */
433 		goto done;
434 
435 	/* Leave the first HSR sup payload. */
436 	pull_size = sizeof(struct hsr_sup_payload);
437 	skb_pull(skb, pull_size);
438 	total_pull_size += pull_size;
439 
440 	/* Get second supervision tlv */
441 	hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data;
442 	/* And check if it is a redbox mac TLV */
443 	if (hsr_sup_tlv->HSR_TLV_type == PRP_TLV_REDBOX_MAC) {
444 		/* We could stop here after pushing hsr_sup_payload,
445 		 * or proceed and allow macaddress_B and for redboxes.
446 		 */
447 		/* Sanity check length */
448 		if (hsr_sup_tlv->HSR_TLV_length != 6)
449 			goto done;
450 
451 		/* Leave the second HSR sup tlv. */
452 		pull_size = sizeof(struct hsr_sup_tlv);
453 		skb_pull(skb, pull_size);
454 		total_pull_size += pull_size;
455 
456 		/* Get redbox mac address. */
457 		hsr_sp = (struct hsr_sup_payload *)skb->data;
458 
459 		/* Check if redbox mac and node mac are equal. */
460 		if (!ether_addr_equal(node_real->macaddress_A, hsr_sp->macaddress_A)) {
461 			/* This is a redbox supervision frame for a VDAN! */
462 			goto done;
463 		}
464 	}
465 
466 	ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
467 	hsr_lock_seq_out_pair(node_real, node_curr);
468 	for (i = 0; i < HSR_PT_PORTS; i++) {
469 		if (!node_curr->time_in_stale[i] &&
470 		    time_after(node_curr->time_in[i], node_real->time_in[i])) {
471 			node_real->time_in[i] = node_curr->time_in[i];
472 			node_real->time_in_stale[i] =
473 						node_curr->time_in_stale[i];
474 		}
475 	}
476 
477 	xa_for_each(&node_curr->seq_blocks, idx, src_blk) {
478 		if (hsr_seq_block_is_old(src_blk))
479 			continue;
480 
481 		merge_blk = hsr_get_seq_block(node_real, src_blk->block_idx);
482 		if (!merge_blk)
483 			continue;
484 		merge_blk->time = min(merge_blk->time, src_blk->time);
485 		for (i = 0; i < node_real->seq_port_cnt; i++) {
486 			bitmap_or(merge_blk->seq_nrs[i], merge_blk->seq_nrs[i],
487 				  src_blk->seq_nrs[i], HSR_SEQ_BLOCK_SIZE);
488 		}
489 	}
490 	hsr_unlock_seq_out_pair(node_real, node_curr);
491 	node_real->addr_B_port = port_rcv->type;
492 
493 	spin_lock_bh(&hsr->list_lock);
494 	if (!node_curr->removed) {
495 		list_del_rcu(&node_curr->mac_list);
496 		node_curr->removed = true;
497 		call_rcu(&node_curr->rcu_head, hsr_free_node_rcu);
498 	}
499 	spin_unlock_bh(&hsr->list_lock);
500 
501 done:
502 	/* Push back here */
503 	skb_push(skb, total_pull_size);
504 }
505 
506 /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
507  *
508  * If the frame was sent by a node's B interface, replace the source
509  * address with that node's "official" address (macaddress_A) so that upper
510  * layers recognize where it came from.
511  */
hsr_addr_subst_source(struct hsr_node * node,struct sk_buff * skb)512 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
513 {
514 	if (!skb_mac_header_was_set(skb)) {
515 		WARN_ONCE(1, "%s: Mac header not set\n", __func__);
516 		return;
517 	}
518 
519 	memcpy(&eth_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
520 }
521 
522 /* 'skb' is a frame meant for another host.
523  * 'port' is the outgoing interface
524  *
525  * Substitute the target (dest) MAC address if necessary, so the it matches the
526  * recipient interface MAC address, regardless of whether that is the
527  * recipient's A or B interface.
528  * This is needed to keep the packets flowing through switches that learn on
529  * which "side" the different interfaces are.
530  */
hsr_addr_subst_dest(struct hsr_node * node_src,struct sk_buff * skb,struct hsr_port * port)531 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
532 			 struct hsr_port *port)
533 {
534 	struct hsr_node *node_dst;
535 
536 	if (!skb_mac_header_was_set(skb)) {
537 		WARN_ONCE(1, "%s: Mac header not set\n", __func__);
538 		return;
539 	}
540 
541 	if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
542 		return;
543 
544 	node_dst = find_node_by_addr_A(&port->hsr->node_db,
545 				       eth_hdr(skb)->h_dest);
546 	if (!node_dst && port->hsr->redbox)
547 		node_dst = find_node_by_addr_A(&port->hsr->proxy_node_db,
548 					       eth_hdr(skb)->h_dest);
549 
550 	if (!node_dst) {
551 		if (port->hsr->prot_version != PRP_V1 && net_ratelimit())
552 			netdev_err(skb->dev, "%s: Unknown node\n", __func__);
553 		return;
554 	}
555 	if (port->type != node_dst->addr_B_port)
556 		return;
557 
558 	if (is_valid_ether_addr(node_dst->macaddress_B))
559 		ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
560 }
561 
hsr_register_frame_in(struct hsr_node * node,struct hsr_port * port,u16 sequence_nr)562 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
563 			   u16 sequence_nr)
564 {
565 	node->time_in[port->type] = jiffies;
566 	node->time_in_stale[port->type] = false;
567 }
568 
569 /* Duplicate discard algorithm: we maintain a bitmap where we set a bit for
570  * every seen sequence number. The bitmap is split into blocks and the block
571  * management is detailed in hsr_get_seq_block(). In any case, we err on the
572  * side of accepting a packet, as the specification requires the algorithm to
573  * be "designed such that it never rejects a legitimate frame, while occasional
574  * acceptance of a duplicate can be tolerated." (IEC 62439-3:2021, 4.1.10.3).
575  * While this requirement is explicit for PRP, applying it to HSR does no harm
576  * either.
577  *
578  * 'frame' is the frame to be sent
579  * 'port_type' is the type of the outgoing interface
580  *
581  * Return:
582  *	 1 if frame can be shown to have been sent recently on this interface,
583  *	 0 otherwise
584  */
hsr_check_duplicate(struct hsr_frame_info * frame,unsigned int port_type)585 static int hsr_check_duplicate(struct hsr_frame_info *frame,
586 			       unsigned int port_type)
587 {
588 	u16 sequence_nr, seq_bit, block_idx;
589 	struct hsr_seq_block *block;
590 	struct hsr_node *node;
591 
592 	node = frame->node_src;
593 	sequence_nr = frame->sequence_nr;
594 
595 	if (WARN_ON_ONCE(port_type >= node->seq_port_cnt))
596 		return 0;
597 
598 	spin_lock_bh(&node->seq_out_lock);
599 
600 	block_idx = hsr_seq_block_index(sequence_nr);
601 	block = hsr_get_seq_block(node, block_idx);
602 	if (!block)
603 		goto out_new;
604 
605 	seq_bit = hsr_seq_block_bit(sequence_nr);
606 	if (__test_and_set_bit(seq_bit, block->seq_nrs[port_type]))
607 		goto out_seen;
608 
609 out_new:
610 	spin_unlock_bh(&node->seq_out_lock);
611 	return 0;
612 
613 out_seen:
614 	spin_unlock_bh(&node->seq_out_lock);
615 	return 1;
616 }
617 
618 /* HSR duplicate discard: we check if the same frame has already been sent on
619  * this outgoing interface. The check follows the general duplicate discard
620  * algorithm.
621  *
622  * 'port' is the outgoing interface
623  * 'frame' is the frame to be sent
624  *
625  * Return:
626  *	 1 if frame can be shown to have been sent recently on this interface,
627  *	 0 otherwise
628  */
hsr_register_frame_out(struct hsr_port * port,struct hsr_frame_info * frame)629 int hsr_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame)
630 {
631 	return hsr_check_duplicate(frame, port->type - 1);
632 }
633 
634 /* PRP duplicate discard: we only consider frames that are received on port A
635  * or port B and should go to the master port. For those, we check if they have
636  * already been received by the host, i.e., master port. The check uses the
637  * general duplicate discard algorithm, but without tracking multiple ports.
638  *
639  * 'port' is the outgoing interface
640  * 'frame' is the frame to be sent
641  *
642  * Return:
643  *	 1 if frame can be shown to have been sent recently on this interface,
644  *	 0 otherwise
645  */
prp_register_frame_out(struct hsr_port * port,struct hsr_frame_info * frame)646 int prp_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame)
647 {
648 	/* out-going frames are always in order */
649 	if (frame->port_rcv->type == HSR_PT_MASTER)
650 		return 0;
651 
652 	/* for PRP we should only forward frames from the slave ports
653 	 * to the master port
654 	 */
655 	if (port->type != HSR_PT_MASTER)
656 		return 1;
657 
658 	return hsr_check_duplicate(frame, 0);
659 }
660 EXPORT_SYMBOL_IF_KUNIT(prp_register_frame_out);
661 
get_late_port(struct hsr_priv * hsr,struct hsr_node * node)662 static struct hsr_port *get_late_port(struct hsr_priv *hsr,
663 				      struct hsr_node *node)
664 {
665 	if (node->time_in_stale[HSR_PT_SLAVE_A])
666 		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
667 	if (node->time_in_stale[HSR_PT_SLAVE_B])
668 		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
669 
670 	if (time_after(node->time_in[HSR_PT_SLAVE_B],
671 		       node->time_in[HSR_PT_SLAVE_A] +
672 					msecs_to_jiffies(MAX_SLAVE_DIFF)))
673 		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
674 	if (time_after(node->time_in[HSR_PT_SLAVE_A],
675 		       node->time_in[HSR_PT_SLAVE_B] +
676 					msecs_to_jiffies(MAX_SLAVE_DIFF)))
677 		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
678 
679 	return NULL;
680 }
681 
682 /* Remove stale sequence_nr records. Called by timer every
683  * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
684  */
hsr_prune_nodes(struct timer_list * t)685 void hsr_prune_nodes(struct timer_list *t)
686 {
687 	struct hsr_priv *hsr = timer_container_of(hsr, t, prune_timer);
688 	struct hsr_node *node;
689 	struct hsr_node *tmp;
690 	struct hsr_port *port;
691 	unsigned long timestamp;
692 	unsigned long time_a, time_b;
693 
694 	spin_lock_bh(&hsr->list_lock);
695 	list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
696 		/* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A]
697 		 * nor time_in[HSR_PT_SLAVE_B], will ever be updated for
698 		 * the master port. Thus the master node will be repeatedly
699 		 * pruned leading to packet loss.
700 		 */
701 		if (hsr_addr_is_self(hsr, node->macaddress_A))
702 			continue;
703 
704 		/* Shorthand */
705 		time_a = node->time_in[HSR_PT_SLAVE_A];
706 		time_b = node->time_in[HSR_PT_SLAVE_B];
707 
708 		/* Check for timestamps old enough to risk wrap-around */
709 		if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
710 			node->time_in_stale[HSR_PT_SLAVE_A] = true;
711 		if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
712 			node->time_in_stale[HSR_PT_SLAVE_B] = true;
713 
714 		/* Get age of newest frame from node.
715 		 * At least one time_in is OK here; nodes get pruned long
716 		 * before both time_ins can get stale
717 		 */
718 		timestamp = time_a;
719 		if (node->time_in_stale[HSR_PT_SLAVE_A] ||
720 		    (!node->time_in_stale[HSR_PT_SLAVE_B] &&
721 		    time_after(time_b, time_a)))
722 			timestamp = time_b;
723 
724 		/* Warn of ring error only as long as we get frames at all */
725 		if (time_is_after_jiffies(timestamp +
726 				msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
727 			rcu_read_lock();
728 			port = get_late_port(hsr, node);
729 			if (port)
730 				hsr_nl_ringerror(hsr, node->macaddress_A, port);
731 			rcu_read_unlock();
732 		}
733 
734 		/* Prune old entries */
735 		if (time_is_before_jiffies(timestamp +
736 				msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
737 			hsr_nl_nodedown(hsr, node->macaddress_A);
738 			if (!node->removed) {
739 				list_del_rcu(&node->mac_list);
740 				node->removed = true;
741 				/* Note that we need to free this entry later: */
742 				call_rcu(&node->rcu_head, hsr_free_node_rcu);
743 			}
744 		}
745 	}
746 	spin_unlock_bh(&hsr->list_lock);
747 
748 	/* Restart timer */
749 	mod_timer(&hsr->prune_timer,
750 		  jiffies + msecs_to_jiffies(PRUNE_PERIOD));
751 }
752 
hsr_prune_proxy_nodes(struct timer_list * t)753 void hsr_prune_proxy_nodes(struct timer_list *t)
754 {
755 	struct hsr_priv *hsr = timer_container_of(hsr, t, prune_proxy_timer);
756 	unsigned long timestamp;
757 	struct hsr_node *node;
758 	struct hsr_node *tmp;
759 
760 	spin_lock_bh(&hsr->list_lock);
761 	list_for_each_entry_safe(node, tmp, &hsr->proxy_node_db, mac_list) {
762 		/* Don't prune RedBox node. */
763 		if (hsr_addr_is_redbox(hsr, node->macaddress_A))
764 			continue;
765 
766 		timestamp = node->time_in[HSR_PT_INTERLINK];
767 
768 		/* Prune old entries */
769 		if (time_is_before_jiffies(timestamp +
770 				msecs_to_jiffies(HSR_PROXY_NODE_FORGET_TIME))) {
771 			hsr_nl_nodedown(hsr, node->macaddress_A);
772 			if (!node->removed) {
773 				list_del_rcu(&node->mac_list);
774 				node->removed = true;
775 				/* Note that we need to free this entry later: */
776 				call_rcu(&node->rcu_head, hsr_free_node_rcu);
777 			}
778 		}
779 	}
780 
781 	spin_unlock_bh(&hsr->list_lock);
782 
783 	/* Restart timer */
784 	mod_timer(&hsr->prune_proxy_timer,
785 		  jiffies + msecs_to_jiffies(PRUNE_PROXY_PERIOD));
786 }
787 
hsr_get_next_node(struct hsr_priv * hsr,void * _pos,unsigned char addr[ETH_ALEN])788 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
789 			unsigned char addr[ETH_ALEN])
790 {
791 	struct hsr_node *node;
792 
793 	if (!_pos) {
794 		node = list_first_or_null_rcu(&hsr->node_db,
795 					      struct hsr_node, mac_list);
796 		if (node)
797 			ether_addr_copy(addr, node->macaddress_A);
798 		return node;
799 	}
800 
801 	node = _pos;
802 	list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
803 		ether_addr_copy(addr, node->macaddress_A);
804 		return node;
805 	}
806 
807 	return NULL;
808 }
809 
810 /* Fill the last sequence number that has been received from node on if1 by
811  * finding the last sequence number sent on port B; accordingly get the last
812  * received sequence number for if2 using sent sequence numbers on port A.
813  */
fill_last_seq_nrs(struct hsr_node * node,u16 * if1_seq,u16 * if2_seq)814 static void fill_last_seq_nrs(struct hsr_node *node, u16 *if1_seq, u16 *if2_seq)
815 {
816 	struct hsr_seq_block *block;
817 	unsigned int block_off;
818 	size_t block_sz;
819 	u16 seq_bit;
820 
821 	spin_lock_bh(&node->seq_out_lock);
822 
823 	/* Get last inserted block */
824 	block_off = (node->next_block - 1) & (HSR_MAX_SEQ_BLOCKS - 1);
825 	block_sz = hsr_seq_block_size(node);
826 	block = node->block_buf + block_off * block_sz;
827 
828 	if (!bitmap_empty(block->seq_nrs[HSR_PT_SLAVE_B - 1],
829 			  HSR_SEQ_BLOCK_SIZE)) {
830 		seq_bit = find_last_bit(block->seq_nrs[HSR_PT_SLAVE_B - 1],
831 					HSR_SEQ_BLOCK_SIZE);
832 		*if1_seq = (block->block_idx << HSR_SEQ_BLOCK_SHIFT) | seq_bit;
833 	}
834 	if (!bitmap_empty(block->seq_nrs[HSR_PT_SLAVE_A - 1],
835 			  HSR_SEQ_BLOCK_SIZE)) {
836 		seq_bit = find_last_bit(block->seq_nrs[HSR_PT_SLAVE_A - 1],
837 					HSR_SEQ_BLOCK_SIZE);
838 		*if2_seq = (block->block_idx << HSR_SEQ_BLOCK_SHIFT) | seq_bit;
839 	}
840 	spin_unlock_bh(&node->seq_out_lock);
841 }
842 
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)843 int hsr_get_node_data(struct hsr_priv *hsr,
844 		      const unsigned char *addr,
845 		      unsigned char addr_b[ETH_ALEN],
846 		      unsigned int *addr_b_ifindex,
847 		      int *if1_age,
848 		      u16 *if1_seq,
849 		      int *if2_age,
850 		      u16 *if2_seq)
851 {
852 	struct hsr_node *node;
853 	struct hsr_port *port;
854 	unsigned long tdiff;
855 
856 	node = find_node_by_addr_A(&hsr->node_db, addr);
857 	if (!node)
858 		return -ENOENT;
859 
860 	ether_addr_copy(addr_b, node->macaddress_B);
861 
862 	tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
863 	if (node->time_in_stale[HSR_PT_SLAVE_A])
864 		*if1_age = INT_MAX;
865 #if HZ <= MSEC_PER_SEC
866 	else if (tdiff > msecs_to_jiffies(INT_MAX))
867 		*if1_age = INT_MAX;
868 #endif
869 	else
870 		*if1_age = jiffies_to_msecs(tdiff);
871 
872 	tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
873 	if (node->time_in_stale[HSR_PT_SLAVE_B])
874 		*if2_age = INT_MAX;
875 #if HZ <= MSEC_PER_SEC
876 	else if (tdiff > msecs_to_jiffies(INT_MAX))
877 		*if2_age = INT_MAX;
878 #endif
879 	else
880 		*if2_age = jiffies_to_msecs(tdiff);
881 
882 	/* Present sequence numbers as if they were incoming on interface */
883 	*if1_seq = 0;
884 	*if2_seq = 0;
885 	if (hsr->prot_version != PRP_V1)
886 		fill_last_seq_nrs(node, if1_seq, if2_seq);
887 
888 	if (node->addr_B_port != HSR_PT_NONE) {
889 		port = hsr_port_get_hsr(hsr, node->addr_B_port);
890 		if (port)
891 			*addr_b_ifindex = port->dev->ifindex;
892 		else
893 			*addr_b_ifindex = -1;
894 	} else {
895 		*addr_b_ifindex = -1;
896 	}
897 
898 	return 0;
899 }
900