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