c9b78c52c75b7a66c2cf0a9ffc2bb4ae807b49df
[firefly-linux-kernel-4.4.55.git] / net / hsr / hsr_framereg.c
1 /* Copyright 2011-2014 Autronica Fire and Security AS
2  *
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * Author(s):
9  *      2011-2014 Arvid Brodin, arvid.brodin@alten.se
10  *
11  * The HSR spec says never to forward the same frame twice on the same
12  * interface. A frame is identified by its source MAC address and its HSR
13  * sequence number. This code keeps track of senders and their sequence numbers
14  * to allow filtering of duplicate frames, and to detect HSR ring errors.
15  */
16
17 #include <linux/if_ether.h>
18 #include <linux/etherdevice.h>
19 #include <linux/slab.h>
20 #include <linux/rculist.h>
21 #include "hsr_main.h"
22 #include "hsr_framereg.h"
23 #include "hsr_netlink.h"
24
25
26 struct hsr_node {
27         struct list_head        mac_list;
28         unsigned char           MacAddressA[ETH_ALEN];
29         unsigned char           MacAddressB[ETH_ALEN];
30         /* Local slave through which AddrB frames are received from this node */
31         enum hsr_port_type      AddrB_port;
32         unsigned long           time_in[HSR_PT_PORTS];
33         bool                    time_in_stale[HSR_PT_PORTS];
34         u16                     seq_out[HSR_PT_PORTS];
35         struct rcu_head         rcu_head;
36 };
37
38 /*      TODO: use hash lists for mac addresses (linux/jhash.h)?    */
39
40
41
42 /* Search for mac entry. Caller must hold rcu read lock.
43  */
44 static struct hsr_node *find_node_by_AddrA(struct list_head *node_db,
45                                            const unsigned char addr[ETH_ALEN])
46 {
47         struct hsr_node *node;
48
49         list_for_each_entry_rcu(node, node_db, mac_list) {
50                 if (ether_addr_equal(node->MacAddressA, addr))
51                         return node;
52         }
53
54         return NULL;
55 }
56
57
58 /* Search for mac entry. Caller must hold rcu read lock.
59  */
60 static struct hsr_node *find_node_by_AddrB(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->MacAddressB, addr))
67                         return node;
68         }
69
70         return NULL;
71 }
72
73
74 /* Search for mac entry. Caller must hold rcu read lock.
75  */
76 struct hsr_node *hsr_find_node(struct list_head *node_db, struct sk_buff *skb)
77 {
78         struct hsr_node *node;
79         struct ethhdr *ethhdr;
80
81         if (!skb_mac_header_was_set(skb))
82                 return NULL;
83
84         ethhdr = (struct ethhdr *) skb_mac_header(skb);
85
86         list_for_each_entry_rcu(node, node_db, mac_list) {
87                 if (ether_addr_equal(node->MacAddressA, ethhdr->h_source))
88                         return node;
89                 if (ether_addr_equal(node->MacAddressB, ethhdr->h_source))
90                         return node;
91         }
92
93         return NULL;
94 }
95
96
97 /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
98  * frames from self that's been looped over the HSR ring.
99  */
100 int hsr_create_self_node(struct list_head *self_node_db,
101                          unsigned char addr_a[ETH_ALEN],
102                          unsigned char addr_b[ETH_ALEN])
103 {
104         struct hsr_node *node, *oldnode;
105
106         node = kmalloc(sizeof(*node), GFP_KERNEL);
107         if (!node)
108                 return -ENOMEM;
109
110         ether_addr_copy(node->MacAddressA, addr_a);
111         ether_addr_copy(node->MacAddressB, addr_b);
112
113         rcu_read_lock();
114         oldnode = list_first_or_null_rcu(self_node_db,
115                                                 struct hsr_node, mac_list);
116         if (oldnode) {
117                 list_replace_rcu(&oldnode->mac_list, &node->mac_list);
118                 rcu_read_unlock();
119                 synchronize_rcu();
120                 kfree(oldnode);
121         } else {
122                 rcu_read_unlock();
123                 list_add_tail_rcu(&node->mac_list, self_node_db);
124         }
125
126         return 0;
127 }
128
129
130 /* Add/merge node to the database of nodes. 'skb' must contain an HSR
131  * supervision frame.
132  * - If the supervision header's MacAddressA field is not yet in the database,
133  * this frame is from an hitherto unknown node - add it to the database.
134  * - If the sender's MAC address is not the same as its MacAddressA address,
135  * the node is using PICS_SUBS (address substitution). Record the sender's
136  * address as the node's MacAddressB.
137  *
138  * This function needs to work even if the sender node has changed one of its
139  * slaves' MAC addresses. In this case, there are four different cases described
140  * by (Addr-changed, received-from) pairs as follows. Note that changing the
141  * SlaveA address is equal to changing the node's own address:
142  *
143  * - (AddrB, SlaveB): The new AddrB will be recorded by PICS_SUBS code since
144  *                    node == NULL.
145  * - (AddrB, SlaveA): Will work as usual (the AddrB change won't be detected
146  *                    from this frame).
147  *
148  * - (AddrA, SlaveB): The old node will be found. We need to detect this and
149  *                    remove the node.
150  * - (AddrA, SlaveA): A new node will be registered (non-PICS_SUBS at first).
151  *                    The old one will be pruned after HSR_NODE_FORGET_TIME.
152  *
153  * We also need to detect if the sender's SlaveA and SlaveB cables have been
154  * swapped.
155  */
156 struct hsr_node *hsr_merge_node(struct hsr_node *node, struct sk_buff *skb,
157                                 struct hsr_port *port)
158 {
159         struct hsr_priv *hsr;
160         struct hsr_sup_payload *hsr_sp;
161         struct hsr_ethhdr_sp *hsr_ethsup;
162         int i;
163         unsigned long now;
164
165         hsr_ethsup = (struct hsr_ethhdr_sp *) skb_mac_header(skb);
166         hsr_sp = (struct hsr_sup_payload *) skb->data;
167         hsr = port->hsr;
168
169         if (node && !ether_addr_equal(node->MacAddressA, hsr_sp->MacAddressA)) {
170                 /* Node has changed its AddrA, frame was received from SlaveB */
171                 list_del_rcu(&node->mac_list);
172                 kfree_rcu(node, rcu_head);
173                 node = NULL;
174         }
175
176         if (node && (port->type == node->AddrB_port) &&
177             !ether_addr_equal(node->MacAddressB, hsr_ethsup->ethhdr.h_source)) {
178                 /* Cables have been swapped */
179                 list_del_rcu(&node->mac_list);
180                 kfree_rcu(node, rcu_head);
181                 node = NULL;
182         }
183
184         if (node && (port->type != node->AddrB_port) &&
185             (node->AddrB_port != HSR_PT_NONE) &&
186             !ether_addr_equal(node->MacAddressA, hsr_ethsup->ethhdr.h_source)) {
187                 /* Cables have been swapped */
188                 list_del_rcu(&node->mac_list);
189                 kfree_rcu(node, rcu_head);
190                 node = NULL;
191         }
192
193         if (node)
194                 return node;
195
196         node = find_node_by_AddrA(&hsr->node_db, hsr_sp->MacAddressA);
197         if (node) {
198                 /* Node is known, but frame was received from an unknown
199                  * address. Node is PICS_SUBS capable; merge its AddrB.
200                  */
201                 ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source);
202                 node->AddrB_port = port->type;
203                 return node;
204         }
205
206         node = kzalloc(sizeof(*node), GFP_ATOMIC);
207         if (!node)
208                 return NULL;
209
210         ether_addr_copy(node->MacAddressA, hsr_sp->MacAddressA);
211         ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source);
212         if (!ether_addr_equal(hsr_sp->MacAddressA, hsr_ethsup->ethhdr.h_source))
213                 node->AddrB_port = port->type;
214
215         /* We are only interested in time diffs here, so use current jiffies
216          * as initialization. (0 could trigger an spurious ring error warning).
217          */
218         now = jiffies;
219         for (i = 0; i < HSR_PT_PORTS; i++)
220                 node->time_in[i] = now;
221         for (i = 0; i < HSR_PT_PORTS; i++)
222                 node->seq_out[i] = ntohs(hsr_ethsup->hsr_sup.sequence_nr) - 1;
223
224         list_add_tail_rcu(&node->mac_list, &hsr->node_db);
225
226         return node;
227 }
228
229
230 /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
231  *
232  * If the frame was sent by a node's B interface, replace the sender
233  * address with that node's "official" address (MacAddressA) so that upper
234  * layers recognize where it came from.
235  */
236 void hsr_addr_subst_source(struct hsr_priv *hsr, struct sk_buff *skb)
237 {
238         struct ethhdr *ethhdr;
239         struct hsr_node *node;
240
241         if (!skb_mac_header_was_set(skb)) {
242                 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
243                 return;
244         }
245         ethhdr = (struct ethhdr *) skb_mac_header(skb);
246
247         rcu_read_lock();
248         node = find_node_by_AddrB(&hsr->node_db, ethhdr->h_source);
249         if (node)
250                 ether_addr_copy(ethhdr->h_source, node->MacAddressA);
251         rcu_read_unlock();
252 }
253
254
255 /* 'skb' is a frame meant for another host.
256  * 'hsr_dev_idx' is the HSR index of the outgoing device
257  *
258  * Substitute the target (dest) MAC address if necessary, so the it matches the
259  * recipient interface MAC address, regardless of whether that is the
260  * recipient's A or B interface.
261  * This is needed to keep the packets flowing through switches that learn on
262  * which "side" the different interfaces are.
263  */
264 void hsr_addr_subst_dest(struct hsr_priv *hsr, struct ethhdr *ethhdr,
265                          struct hsr_port *port)
266 {
267         struct hsr_node *node;
268
269         rcu_read_lock();
270         node = find_node_by_AddrA(&hsr->node_db, ethhdr->h_dest);
271         if (node && (node->AddrB_port == port->type))
272                 ether_addr_copy(ethhdr->h_dest, node->MacAddressB);
273         rcu_read_unlock();
274 }
275
276
277 /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
278  * false otherwise.
279  */
280 static bool seq_nr_after(u16 a, u16 b)
281 {
282         /* Remove inconsistency where
283          * seq_nr_after(a, b) == seq_nr_before(a, b)
284          */
285         if ((int) b - a == 32768)
286                 return false;
287
288         return (((s16) (b - a)) < 0);
289 }
290 #define seq_nr_before(a, b)             seq_nr_after((b), (a))
291 #define seq_nr_after_or_eq(a, b)        (!seq_nr_before((a), (b)))
292 #define seq_nr_before_or_eq(a, b)       (!seq_nr_after((a), (b)))
293
294
295 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port)
296 {
297         node->time_in[port->type] = jiffies;
298         node->time_in_stale[port->type] = false;
299 }
300
301
302 /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
303  * ethhdr->h_source address and skb->mac_header set.
304  *
305  * Return:
306  *       1 if frame can be shown to have been sent recently on this interface,
307  *       0 otherwise, or
308  *       negative error code on error
309  */
310 int hsr_register_frame_out(struct hsr_node *node, struct hsr_port *port,
311                            struct sk_buff *skb)
312 {
313         struct hsr_ethhdr *hsr_ethhdr;
314         u16 sequence_nr;
315
316         if (!skb_mac_header_was_set(skb)) {
317                 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
318                 return -EINVAL;
319         }
320         hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb);
321
322         sequence_nr = ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
323         if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
324                 return 1;
325
326         node->seq_out[port->type] = sequence_nr;
327         return 0;
328 }
329
330
331 static struct hsr_port *get_late_port(struct hsr_priv *hsr,
332                                       struct hsr_node *node)
333 {
334         if (node->time_in_stale[HSR_PT_SLAVE_A])
335                 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
336         if (node->time_in_stale[HSR_PT_SLAVE_B])
337                 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
338
339         if (time_after(node->time_in[HSR_PT_SLAVE_B],
340                        node->time_in[HSR_PT_SLAVE_A] +
341                                         msecs_to_jiffies(MAX_SLAVE_DIFF)))
342                 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
343         if (time_after(node->time_in[HSR_PT_SLAVE_A],
344                        node->time_in[HSR_PT_SLAVE_B] +
345                                         msecs_to_jiffies(MAX_SLAVE_DIFF)))
346                 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
347
348         return NULL;
349 }
350
351
352 /* Remove stale sequence_nr records. Called by timer every
353  * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
354  */
355 void hsr_prune_nodes(unsigned long data)
356 {
357         struct hsr_priv *hsr;
358         struct hsr_node *node;
359         struct hsr_port *port;
360         unsigned long timestamp;
361         unsigned long time_a, time_b;
362
363         hsr = (struct hsr_priv *) data;
364
365         rcu_read_lock();
366         list_for_each_entry_rcu(node, &hsr->node_db, mac_list) {
367                 /* Shorthand */
368                 time_a = node->time_in[HSR_PT_SLAVE_A];
369                 time_b = node->time_in[HSR_PT_SLAVE_B];
370
371                 /* Check for timestamps old enough to risk wrap-around */
372                 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2))
373                         node->time_in_stale[HSR_PT_SLAVE_A] = true;
374                 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2))
375                         node->time_in_stale[HSR_PT_SLAVE_B] = true;
376
377                 /* Get age of newest frame from node.
378                  * At least one time_in is OK here; nodes get pruned long
379                  * before both time_ins can get stale
380                  */
381                 timestamp = time_a;
382                 if (node->time_in_stale[HSR_PT_SLAVE_A] ||
383                     (!node->time_in_stale[HSR_PT_SLAVE_B] &&
384                     time_after(time_b, time_a)))
385                         timestamp = time_b;
386
387                 /* Warn of ring error only as long as we get frames at all */
388                 if (time_is_after_jiffies(timestamp +
389                                         msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) {
390                         rcu_read_lock();
391                         port = get_late_port(hsr, node);
392                         if (port != NULL)
393                                 hsr_nl_ringerror(hsr, node->MacAddressA, port);
394                         rcu_read_unlock();
395                 }
396
397                 /* Prune old entries */
398                 if (time_is_before_jiffies(timestamp +
399                                         msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
400                         hsr_nl_nodedown(hsr, node->MacAddressA);
401                         list_del_rcu(&node->mac_list);
402                         /* Note that we need to free this entry later: */
403                         kfree_rcu(node, rcu_head);
404                 }
405         }
406         rcu_read_unlock();
407 }
408
409
410 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
411                         unsigned char addr[ETH_ALEN])
412 {
413         struct hsr_node *node;
414
415         if (!_pos) {
416                 node = list_first_or_null_rcu(&hsr->node_db,
417                                               struct hsr_node, mac_list);
418                 if (node)
419                         ether_addr_copy(addr, node->MacAddressA);
420                 return node;
421         }
422
423         node = _pos;
424         list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
425                 ether_addr_copy(addr, node->MacAddressA);
426                 return node;
427         }
428
429         return NULL;
430 }
431
432
433 int hsr_get_node_data(struct hsr_priv *hsr,
434                       const unsigned char *addr,
435                       unsigned char addr_b[ETH_ALEN],
436                       unsigned int *addr_b_ifindex,
437                       int *if1_age,
438                       u16 *if1_seq,
439                       int *if2_age,
440                       u16 *if2_seq)
441 {
442         struct hsr_node *node;
443         struct hsr_port *port;
444         unsigned long tdiff;
445
446
447         rcu_read_lock();
448         node = find_node_by_AddrA(&hsr->node_db, addr);
449         if (!node) {
450                 rcu_read_unlock();
451                 return -ENOENT; /* No such entry */
452         }
453
454         ether_addr_copy(addr_b, node->MacAddressB);
455
456         tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
457         if (node->time_in_stale[HSR_PT_SLAVE_A])
458                 *if1_age = INT_MAX;
459 #if HZ <= MSEC_PER_SEC
460         else if (tdiff > msecs_to_jiffies(INT_MAX))
461                 *if1_age = INT_MAX;
462 #endif
463         else
464                 *if1_age = jiffies_to_msecs(tdiff);
465
466         tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
467         if (node->time_in_stale[HSR_PT_SLAVE_B])
468                 *if2_age = INT_MAX;
469 #if HZ <= MSEC_PER_SEC
470         else if (tdiff > msecs_to_jiffies(INT_MAX))
471                 *if2_age = INT_MAX;
472 #endif
473         else
474                 *if2_age = jiffies_to_msecs(tdiff);
475
476         /* Present sequence numbers as if they were incoming on interface */
477         *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
478         *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
479
480         if (node->AddrB_port != HSR_PT_NONE) {
481                 port = hsr_port_get_hsr(hsr, node->AddrB_port);
482                 *addr_b_ifindex = port->dev->ifindex;
483         } else {
484                 *addr_b_ifindex = -1;
485         }
486
487         rcu_read_unlock();
488
489         return 0;
490 }