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