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