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