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