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