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 <linux/if_ether.h>
15 #include <linux/etherdevice.h>
16 #include <linux/slab.h>
17 #include <linux/rculist.h>
18 #include "hsr_main.h"
19 #include "hsr_framereg.h"
20 #include "hsr_netlink.h"
21 
22 /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
23  * false otherwise.
24  */
seq_nr_after(u16 a,u16 b)25 static bool seq_nr_after(u16 a, u16 b)
26 {
27 	/* Remove inconsistency where
28 	 * seq_nr_after(a, b) == seq_nr_before(a, b)
29 	 */
30 	if ((int)b - a == 32768)
31 		return false;
32 
33 	return (((s16)(b - a)) < 0);
34 }
35 
36 #define seq_nr_before(a, b)		seq_nr_after((b), (a))
37 #define seq_nr_before_or_eq(a, b)	(!seq_nr_after((a), (b)))
38 
hsr_addr_is_self(struct hsr_priv * hsr,unsigned char * addr)39 bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
40 {
41 	struct hsr_node *node;
42 
43 	node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node,
44 				      mac_list);
45 	if (!node) {
46 		WARN_ONCE(1, "HSR: No self node\n");
47 		return false;
48 	}
49 
50 	if (ether_addr_equal(addr, node->macaddress_A))
51 		return true;
52 	if (ether_addr_equal(addr, node->macaddress_B))
53 		return true;
54 
55 	return false;
56 }
57 
58 /* Search for mac entry. Caller must hold rcu read lock.
59  */
find_node_by_addr_A(struct list_head * node_db,const unsigned char addr[ETH_ALEN])60 static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
61 					    const unsigned char addr[ETH_ALEN])
62 {
63 	struct hsr_node *node;
64 
65 	list_for_each_entry_rcu(node, node_db, mac_list) {
66 		if (ether_addr_equal(node->macaddress_A, addr))
67 			return node;
68 	}
69 
70 	return NULL;
71 }
72 
73 /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
74  * frames from self that's been looped over the HSR ring.
75  */
hsr_create_self_node(struct hsr_priv * hsr,const unsigned char addr_a[ETH_ALEN],const unsigned char addr_b[ETH_ALEN])76 int hsr_create_self_node(struct hsr_priv *hsr,
77 			 const unsigned char addr_a[ETH_ALEN],
78 			 const unsigned char addr_b[ETH_ALEN])
79 {
80 	struct list_head *self_node_db = &hsr->self_node_db;
81 	struct hsr_node *node, *oldnode;
82 
83 	node = kmalloc(sizeof(*node), GFP_KERNEL);
84 	if (!node)
85 		return -ENOMEM;
86 
87 	ether_addr_copy(node->macaddress_A, addr_a);
88 	ether_addr_copy(node->macaddress_B, addr_b);
89 
90 	spin_lock_bh(&hsr->list_lock);
91 	oldnode = list_first_or_null_rcu(self_node_db,
92 					 struct hsr_node, mac_list);
93 	if (oldnode) {
94 		list_replace_rcu(&oldnode->mac_list, &node->mac_list);
95 		spin_unlock_bh(&hsr->list_lock);
96 		kfree_rcu(oldnode, rcu_head);
97 	} else {
98 		list_add_tail_rcu(&node->mac_list, self_node_db);
99 		spin_unlock_bh(&hsr->list_lock);
100 	}
101 
102 	return 0;
103 }
104 
hsr_del_self_node(struct hsr_priv * hsr)105 void hsr_del_self_node(struct hsr_priv *hsr)
106 {
107 	struct list_head *self_node_db = &hsr->self_node_db;
108 	struct hsr_node *node;
109 
110 	spin_lock_bh(&hsr->list_lock);
111 	node = list_first_or_null_rcu(self_node_db, struct hsr_node, mac_list);
112 	if (node) {
113 		list_del_rcu(&node->mac_list);
114 		kfree_rcu(node, rcu_head);
115 	}
116 	spin_unlock_bh(&hsr->list_lock);
117 }
118 
hsr_del_nodes(struct list_head * node_db)119 void hsr_del_nodes(struct list_head *node_db)
120 {
121 	struct hsr_node *node;
122 	struct hsr_node *tmp;
123 
124 	list_for_each_entry_safe(node, tmp, node_db, mac_list)
125 		kfree(node);
126 }
127 
prp_handle_san_frame(bool san,enum hsr_port_type port,struct hsr_node * node)128 void prp_handle_san_frame(bool san, enum hsr_port_type port,
129 			  struct hsr_node *node)
130 {
131 	/* Mark if the SAN node is over LAN_A or LAN_B */
132 	if (port == HSR_PT_SLAVE_A) {
133 		node->san_a = true;
134 		return;
135 	}
136 
137 	if (port == HSR_PT_SLAVE_B)
138 		node->san_b = true;
139 }
140 
141 /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A;
142  * seq_out is used to initialize filtering of outgoing duplicate frames
143  * originating from the newly added node.
144  */
hsr_add_node(struct hsr_priv * hsr,struct list_head * node_db,unsigned char addr[],u16 seq_out,bool san,enum hsr_port_type rx_port)145 static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
146 				     struct list_head *node_db,
147 				     unsigned char addr[],
148 				     u16 seq_out, bool san,
149 				     enum hsr_port_type rx_port)
150 {
151 	struct hsr_node *new_node, *node;
152 	unsigned long now;
153 	int i;
154 
155 	new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
156 	if (!new_node)
157 		return NULL;
158 
159 	ether_addr_copy(new_node->macaddress_A, addr);
160 	spin_lock_init(&new_node->seq_out_lock);
161 
162 	/* We are only interested in time diffs here, so use current jiffies
163 	 * as initialization. (0 could trigger an spurious ring error warning).
164 	 */
165 	now = jiffies;
166 	for (i = 0; i < HSR_PT_PORTS; i++) {
167 		new_node->time_in[i] = now;
168 		new_node->time_out[i] = now;
169 	}
170 	for (i = 0; i < HSR_PT_PORTS; i++)
171 		new_node->seq_out[i] = seq_out;
172 
173 	if (san && hsr->proto_ops->handle_san_frame)
174 		hsr->proto_ops->handle_san_frame(san, rx_port, new_node);
175 
176 	spin_lock_bh(&hsr->list_lock);
177 	list_for_each_entry_rcu(node, node_db, mac_list,
178 				lockdep_is_held(&hsr->list_lock)) {
179 		if (ether_addr_equal(node->macaddress_A, addr))
180 			goto out;
181 		if (ether_addr_equal(node->macaddress_B, addr))
182 			goto out;
183 	}
184 	list_add_tail_rcu(&new_node->mac_list, node_db);
185 	spin_unlock_bh(&hsr->list_lock);
186 	return new_node;
187 out:
188 	spin_unlock_bh(&hsr->list_lock);
189 	kfree(new_node);
190 	return node;
191 }
192 
prp_update_san_info(struct hsr_node * node,bool is_sup)193 void prp_update_san_info(struct hsr_node *node, bool is_sup)
194 {
195 	if (!is_sup)
196 		return;
197 
198 	node->san_a = false;
199 	node->san_b = false;
200 }
201 
202 /* Get the hsr_node from which 'skb' was sent.
203  */
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)204 struct hsr_node *hsr_get_node(struct hsr_port *port, struct list_head *node_db,
205 			      struct sk_buff *skb, bool is_sup,
206 			      enum hsr_port_type rx_port)
207 {
208 	struct hsr_priv *hsr = port->hsr;
209 	struct hsr_node *node;
210 	struct ethhdr *ethhdr;
211 	struct prp_rct *rct;
212 	bool san = false;
213 	u16 seq_out;
214 
215 	if (!skb_mac_header_was_set(skb))
216 		return NULL;
217 
218 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
219 
220 	list_for_each_entry_rcu(node, node_db, mac_list) {
221 		if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
222 			if (hsr->proto_ops->update_san_info)
223 				hsr->proto_ops->update_san_info(node, is_sup);
224 			return node;
225 		}
226 		if (ether_addr_equal(node->macaddress_B, ethhdr->h_source)) {
227 			if (hsr->proto_ops->update_san_info)
228 				hsr->proto_ops->update_san_info(node, is_sup);
229 			return node;
230 		}
231 	}
232 
233 	/* Everyone may create a node entry, connected node to a HSR/PRP
234 	 * device.
235 	 */
236 	if (ethhdr->h_proto == htons(ETH_P_PRP) ||
237 	    ethhdr->h_proto == htons(ETH_P_HSR)) {
238 		/* Use the existing sequence_nr from the tag as starting point
239 		 * for filtering duplicate frames.
240 		 */
241 		seq_out = hsr_get_skb_sequence_nr(skb) - 1;
242 	} else {
243 		rct = skb_get_PRP_rct(skb);
244 		if (rct && prp_check_lsdu_size(skb, rct, is_sup)) {
245 			seq_out = prp_get_skb_sequence_nr(rct);
246 		} else {
247 			if (rx_port != HSR_PT_MASTER)
248 				san = true;
249 			seq_out = HSR_SEQNR_START;
250 		}
251 	}
252 
253 	return hsr_add_node(hsr, node_db, ethhdr->h_source, seq_out,
254 			    san, rx_port);
255 }
256 
257 /* Use the Supervision frame's info about an eventual macaddress_B for merging
258  * nodes that has previously had their macaddress_B registered as a separate
259  * node.
260  */
hsr_handle_sup_frame(struct hsr_frame_info * frame)261 void hsr_handle_sup_frame(struct hsr_frame_info *frame)
262 {
263 	struct hsr_node *node_curr = frame->node_src;
264 	struct hsr_port *port_rcv = frame->port_rcv;
265 	struct hsr_priv *hsr = port_rcv->hsr;
266 	struct hsr_sup_payload *hsr_sp;
267 	struct hsr_sup_tlv *hsr_sup_tlv;
268 	struct hsr_node *node_real;
269 	struct sk_buff *skb = NULL;
270 	struct list_head *node_db;
271 	struct ethhdr *ethhdr;
272 	int i;
273 	unsigned int pull_size = 0;
274 	unsigned int total_pull_size = 0;
275 
276 	/* Here either frame->skb_hsr or frame->skb_prp should be
277 	 * valid as supervision frame always will have protocol
278 	 * header info.
279 	 */
280 	if (frame->skb_hsr)
281 		skb = frame->skb_hsr;
282 	else if (frame->skb_prp)
283 		skb = frame->skb_prp;
284 	else if (frame->skb_std)
285 		skb = frame->skb_std;
286 	if (!skb)
287 		return;
288 
289 	/* Leave the ethernet header. */
290 	pull_size = sizeof(struct ethhdr);
291 	skb_pull(skb, pull_size);
292 	total_pull_size += pull_size;
293 
294 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
295 
296 	/* And leave the HSR tag. */
297 	if (ethhdr->h_proto == htons(ETH_P_HSR)) {
298 		pull_size = sizeof(struct ethhdr);
299 		skb_pull(skb, pull_size);
300 		total_pull_size += pull_size;
301 	}
302 
303 	/* And leave the HSR sup tag. */
304 	pull_size = sizeof(struct hsr_tag);
305 	skb_pull(skb, pull_size);
306 	total_pull_size += pull_size;
307 
308 	/* get HSR sup payload */
309 	hsr_sp = (struct hsr_sup_payload *)skb->data;
310 
311 	/* Merge node_curr (registered on macaddress_B) into node_real */
312 	node_db = &port_rcv->hsr->node_db;
313 	node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
314 	if (!node_real)
315 		/* No frame received from AddrA of this node yet */
316 		node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
317 					 HSR_SEQNR_START - 1, true,
318 					 port_rcv->type);
319 	if (!node_real)
320 		goto done; /* No mem */
321 	if (node_real == node_curr)
322 		/* Node has already been merged */
323 		goto done;
324 
325 	/* Leave the first HSR sup payload. */
326 	pull_size = sizeof(struct hsr_sup_payload);
327 	skb_pull(skb, pull_size);
328 	total_pull_size += pull_size;
329 
330 	/* Get second supervision tlv */
331 	hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data;
332 	/* And check if it is a redbox mac TLV */
333 	if (hsr_sup_tlv->HSR_TLV_type == PRP_TLV_REDBOX_MAC) {
334 		/* We could stop here after pushing hsr_sup_payload,
335 		 * or proceed and allow macaddress_B and for redboxes.
336 		 */
337 		/* Sanity check length */
338 		if (hsr_sup_tlv->HSR_TLV_length != 6)
339 			goto done;
340 
341 		/* Leave the second HSR sup tlv. */
342 		pull_size = sizeof(struct hsr_sup_tlv);
343 		skb_pull(skb, pull_size);
344 		total_pull_size += pull_size;
345 
346 		/* Get redbox mac address. */
347 		hsr_sp = (struct hsr_sup_payload *)skb->data;
348 
349 		/* Check if redbox mac and node mac are equal. */
350 		if (!ether_addr_equal(node_real->macaddress_A, hsr_sp->macaddress_A)) {
351 			/* This is a redbox supervision frame for a VDAN! */
352 			goto done;
353 		}
354 	}
355 
356 	ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
357 	spin_lock_bh(&node_real->seq_out_lock);
358 	for (i = 0; i < HSR_PT_PORTS; i++) {
359 		if (!node_curr->time_in_stale[i] &&
360 		    time_after(node_curr->time_in[i], node_real->time_in[i])) {
361 			node_real->time_in[i] = node_curr->time_in[i];
362 			node_real->time_in_stale[i] =
363 						node_curr->time_in_stale[i];
364 		}
365 		if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
366 			node_real->seq_out[i] = node_curr->seq_out[i];
367 	}
368 	spin_unlock_bh(&node_real->seq_out_lock);
369 	node_real->addr_B_port = port_rcv->type;
370 
371 	spin_lock_bh(&hsr->list_lock);
372 	if (!node_curr->removed) {
373 		list_del_rcu(&node_curr->mac_list);
374 		node_curr->removed = true;
375 		kfree_rcu(node_curr, rcu_head);
376 	}
377 	spin_unlock_bh(&hsr->list_lock);
378 
379 done:
380 	/* Push back here */
381 	skb_push(skb, total_pull_size);
382 }
383 
384 /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
385  *
386  * If the frame was sent by a node's B interface, replace the source
387  * address with that node's "official" address (macaddress_A) so that upper
388  * layers recognize where it came from.
389  */
hsr_addr_subst_source(struct hsr_node * node,struct sk_buff * skb)390 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
391 {
392 	if (!skb_mac_header_was_set(skb)) {
393 		WARN_ONCE(1, "%s: Mac header not set\n", __func__);
394 		return;
395 	}
396 
397 	memcpy(&eth_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
398 }
399 
400 /* 'skb' is a frame meant for another host.
401  * 'port' is the outgoing interface
402  *
403  * Substitute the target (dest) MAC address if necessary, so the it matches the
404  * recipient interface MAC address, regardless of whether that is the
405  * recipient's A or B interface.
406  * This is needed to keep the packets flowing through switches that learn on
407  * which "side" the different interfaces are.
408  */
hsr_addr_subst_dest(struct hsr_node * node_src,struct sk_buff * skb,struct hsr_port * port)409 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
410 			 struct hsr_port *port)
411 {
412 	struct hsr_node *node_dst;
413 
414 	if (!skb_mac_header_was_set(skb)) {
415 		WARN_ONCE(1, "%s: Mac header not set\n", __func__);
416 		return;
417 	}
418 
419 	if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
420 		return;
421 
422 	node_dst = find_node_by_addr_A(&port->hsr->node_db,
423 				       eth_hdr(skb)->h_dest);
424 	if (!node_dst) {
425 		if (net_ratelimit())
426 			netdev_err(skb->dev, "%s: Unknown node\n", __func__);
427 		return;
428 	}
429 	if (port->type != node_dst->addr_B_port)
430 		return;
431 
432 	if (is_valid_ether_addr(node_dst->macaddress_B))
433 		ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
434 }
435 
hsr_register_frame_in(struct hsr_node * node,struct hsr_port * port,u16 sequence_nr)436 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
437 			   u16 sequence_nr)
438 {
439 	/* Don't register incoming frames without a valid sequence number. This
440 	 * ensures entries of restarted nodes gets pruned so that they can
441 	 * re-register and resume communications.
442 	 */
443 	if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) &&
444 	    seq_nr_before(sequence_nr, node->seq_out[port->type]))
445 		return;
446 
447 	node->time_in[port->type] = jiffies;
448 	node->time_in_stale[port->type] = false;
449 }
450 
451 /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
452  * ethhdr->h_source address and skb->mac_header set.
453  *
454  * Return:
455  *	 1 if frame can be shown to have been sent recently on this interface,
456  *	 0 otherwise, or
457  *	 negative error code on error
458  */
hsr_register_frame_out(struct hsr_port * port,struct hsr_node * node,u16 sequence_nr)459 int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
460 			   u16 sequence_nr)
461 {
462 	spin_lock_bh(&node->seq_out_lock);
463 	if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]) &&
464 	    time_is_after_jiffies(node->time_out[port->type] +
465 	    msecs_to_jiffies(HSR_ENTRY_FORGET_TIME))) {
466 		spin_unlock_bh(&node->seq_out_lock);
467 		return 1;
468 	}
469 
470 	node->time_out[port->type] = jiffies;
471 	node->seq_out[port->type] = sequence_nr;
472 	spin_unlock_bh(&node->seq_out_lock);
473 	return 0;
474 }
475 
get_late_port(struct hsr_priv * hsr,struct hsr_node * node)476 static struct hsr_port *get_late_port(struct hsr_priv *hsr,
477 				      struct hsr_node *node)
478 {
479 	if (node->time_in_stale[HSR_PT_SLAVE_A])
480 		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
481 	if (node->time_in_stale[HSR_PT_SLAVE_B])
482 		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
483 
484 	if (time_after(node->time_in[HSR_PT_SLAVE_B],
485 		       node->time_in[HSR_PT_SLAVE_A] +
486 					msecs_to_jiffies(MAX_SLAVE_DIFF)))
487 		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
488 	if (time_after(node->time_in[HSR_PT_SLAVE_A],
489 		       node->time_in[HSR_PT_SLAVE_B] +
490 					msecs_to_jiffies(MAX_SLAVE_DIFF)))
491 		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
492 
493 	return NULL;
494 }
495 
496 /* Remove stale sequence_nr records. Called by timer every
497  * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
498  */
hsr_prune_nodes(struct timer_list * t)499 void hsr_prune_nodes(struct timer_list *t)
500 {
501 	struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
502 	struct hsr_node *node;
503 	struct hsr_node *tmp;
504 	struct hsr_port *port;
505 	unsigned long timestamp;
506 	unsigned long time_a, time_b;
507 
508 	spin_lock_bh(&hsr->list_lock);
509 	list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
510 		/* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A]
511 		 * nor time_in[HSR_PT_SLAVE_B], will ever be updated for
512 		 * the master port. Thus the master node will be repeatedly
513 		 * pruned leading to packet loss.
514 		 */
515 		if (hsr_addr_is_self(hsr, node->macaddress_A))
516 			continue;
517 
518 		/* Shorthand */
519 		time_a = node->time_in[HSR_PT_SLAVE_A];
520 		time_b = node->time_in[HSR_PT_SLAVE_B];
521 
522 		/* Check for timestamps old enough to risk wrap-around */
523 		if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
524 			node->time_in_stale[HSR_PT_SLAVE_A] = true;
525 		if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
526 			node->time_in_stale[HSR_PT_SLAVE_B] = true;
527 
528 		/* Get age of newest frame from node.
529 		 * At least one time_in is OK here; nodes get pruned long
530 		 * before both time_ins can get stale
531 		 */
532 		timestamp = time_a;
533 		if (node->time_in_stale[HSR_PT_SLAVE_A] ||
534 		    (!node->time_in_stale[HSR_PT_SLAVE_B] &&
535 		    time_after(time_b, time_a)))
536 			timestamp = time_b;
537 
538 		/* Warn of ring error only as long as we get frames at all */
539 		if (time_is_after_jiffies(timestamp +
540 				msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
541 			rcu_read_lock();
542 			port = get_late_port(hsr, node);
543 			if (port)
544 				hsr_nl_ringerror(hsr, node->macaddress_A, port);
545 			rcu_read_unlock();
546 		}
547 
548 		/* Prune old entries */
549 		if (time_is_before_jiffies(timestamp +
550 				msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
551 			hsr_nl_nodedown(hsr, node->macaddress_A);
552 			if (!node->removed) {
553 				list_del_rcu(&node->mac_list);
554 				node->removed = true;
555 				/* Note that we need to free this entry later: */
556 				kfree_rcu(node, rcu_head);
557 			}
558 		}
559 	}
560 	spin_unlock_bh(&hsr->list_lock);
561 
562 	/* Restart timer */
563 	mod_timer(&hsr->prune_timer,
564 		  jiffies + msecs_to_jiffies(PRUNE_PERIOD));
565 }
566 
hsr_get_next_node(struct hsr_priv * hsr,void * _pos,unsigned char addr[ETH_ALEN])567 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
568 			unsigned char addr[ETH_ALEN])
569 {
570 	struct hsr_node *node;
571 
572 	if (!_pos) {
573 		node = list_first_or_null_rcu(&hsr->node_db,
574 					      struct hsr_node, mac_list);
575 		if (node)
576 			ether_addr_copy(addr, node->macaddress_A);
577 		return node;
578 	}
579 
580 	node = _pos;
581 	list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
582 		ether_addr_copy(addr, node->macaddress_A);
583 		return node;
584 	}
585 
586 	return NULL;
587 }
588 
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)589 int hsr_get_node_data(struct hsr_priv *hsr,
590 		      const unsigned char *addr,
591 		      unsigned char addr_b[ETH_ALEN],
592 		      unsigned int *addr_b_ifindex,
593 		      int *if1_age,
594 		      u16 *if1_seq,
595 		      int *if2_age,
596 		      u16 *if2_seq)
597 {
598 	struct hsr_node *node;
599 	struct hsr_port *port;
600 	unsigned long tdiff;
601 
602 	node = find_node_by_addr_A(&hsr->node_db, addr);
603 	if (!node)
604 		return -ENOENT;
605 
606 	ether_addr_copy(addr_b, node->macaddress_B);
607 
608 	tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
609 	if (node->time_in_stale[HSR_PT_SLAVE_A])
610 		*if1_age = INT_MAX;
611 #if HZ <= MSEC_PER_SEC
612 	else if (tdiff > msecs_to_jiffies(INT_MAX))
613 		*if1_age = INT_MAX;
614 #endif
615 	else
616 		*if1_age = jiffies_to_msecs(tdiff);
617 
618 	tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
619 	if (node->time_in_stale[HSR_PT_SLAVE_B])
620 		*if2_age = INT_MAX;
621 #if HZ <= MSEC_PER_SEC
622 	else if (tdiff > msecs_to_jiffies(INT_MAX))
623 		*if2_age = INT_MAX;
624 #endif
625 	else
626 		*if2_age = jiffies_to_msecs(tdiff);
627 
628 	/* Present sequence numbers as if they were incoming on interface */
629 	*if1_seq = node->seq_out[HSR_PT_SLAVE_B];
630 	*if2_seq = node->seq_out[HSR_PT_SLAVE_A];
631 
632 	if (node->addr_B_port != HSR_PT_NONE) {
633 		port = hsr_port_get_hsr(hsr, node->addr_B_port);
634 		*addr_b_ifindex = port->dev->ifindex;
635 	} else {
636 		*addr_b_ifindex = -1;
637 	}
638 
639 	return 0;
640 }
641