This source file includes following definitions.
- seq_nr_after
- hsr_addr_is_self
- find_node_by_addr_A
- hsr_create_self_node
- hsr_del_self_node
- hsr_del_nodes
- hsr_add_node
- hsr_get_node
- hsr_handle_sup_frame
- hsr_addr_subst_source
- hsr_addr_subst_dest
- hsr_register_frame_in
- hsr_register_frame_out
- get_late_port
- hsr_prune_nodes
- hsr_get_next_node
- hsr_get_node_data
1
2
3
4
5
6
7
8
9
10
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
22
23
24
25
26 static bool seq_nr_after(u16 a, u16 b)
27 {
28
29
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
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
76
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
131
132
133
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
150
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
175
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
198
199 if (ethhdr->h_proto == htons(ETH_P_PRP) ||
200 ethhdr->h_proto == htons(ETH_P_HSR)) {
201
202
203
204 seq_out = hsr_get_skb_sequence_nr(skb) - 1;
205 } else {
206
207
208
209 if (port->type != HSR_PT_MASTER)
210 WARN_ONCE(1, "%s: Non-HSR frame\n", __func__);
211 seq_out = HSR_SEQNR_START;
212 }
213
214 return hsr_add_node(hsr, node_db, ethhdr->h_source, seq_out);
215 }
216
217
218
219
220
221 void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr,
222 struct hsr_port *port_rcv)
223 {
224 struct hsr_priv *hsr = port_rcv->hsr;
225 struct hsr_sup_payload *hsr_sp;
226 struct hsr_node *node_real;
227 struct list_head *node_db;
228 struct ethhdr *ethhdr;
229 int i;
230
231 ethhdr = (struct ethhdr *)skb_mac_header(skb);
232
233
234 skb_pull(skb, sizeof(struct ethhdr));
235
236
237 if (ethhdr->h_proto == htons(ETH_P_HSR))
238 skb_pull(skb, sizeof(struct hsr_tag));
239
240
241 skb_pull(skb, sizeof(struct hsr_sup_tag));
242
243 hsr_sp = (struct hsr_sup_payload *)skb->data;
244
245
246 node_db = &port_rcv->hsr->node_db;
247 node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
248 if (!node_real)
249
250 node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
251 HSR_SEQNR_START - 1);
252 if (!node_real)
253 goto done;
254 if (node_real == node_curr)
255
256 goto done;
257
258 ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
259 for (i = 0; i < HSR_PT_PORTS; i++) {
260 if (!node_curr->time_in_stale[i] &&
261 time_after(node_curr->time_in[i], node_real->time_in[i])) {
262 node_real->time_in[i] = node_curr->time_in[i];
263 node_real->time_in_stale[i] =
264 node_curr->time_in_stale[i];
265 }
266 if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
267 node_real->seq_out[i] = node_curr->seq_out[i];
268 }
269 node_real->addr_B_port = port_rcv->type;
270
271 spin_lock_bh(&hsr->list_lock);
272 list_del_rcu(&node_curr->mac_list);
273 spin_unlock_bh(&hsr->list_lock);
274 kfree_rcu(node_curr, rcu_head);
275
276 done:
277 skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
278 }
279
280
281
282
283
284
285
286 void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
287 {
288 if (!skb_mac_header_was_set(skb)) {
289 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
290 return;
291 }
292
293 memcpy(ð_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
294 }
295
296
297
298
299
300
301
302
303
304
305 void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
306 struct hsr_port *port)
307 {
308 struct hsr_node *node_dst;
309
310 if (!skb_mac_header_was_set(skb)) {
311 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
312 return;
313 }
314
315 if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
316 return;
317
318 node_dst = find_node_by_addr_A(&port->hsr->node_db,
319 eth_hdr(skb)->h_dest);
320 if (!node_dst) {
321 WARN_ONCE(1, "%s: Unknown node\n", __func__);
322 return;
323 }
324 if (port->type != node_dst->addr_B_port)
325 return;
326
327 ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
328 }
329
330 void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
331 u16 sequence_nr)
332 {
333
334
335
336
337 if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
338 return;
339
340 node->time_in[port->type] = jiffies;
341 node->time_in_stale[port->type] = false;
342 }
343
344
345
346
347
348
349
350
351
352 int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
353 u16 sequence_nr)
354 {
355 if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
356 return 1;
357
358 node->seq_out[port->type] = sequence_nr;
359 return 0;
360 }
361
362 static struct hsr_port *get_late_port(struct hsr_priv *hsr,
363 struct hsr_node *node)
364 {
365 if (node->time_in_stale[HSR_PT_SLAVE_A])
366 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
367 if (node->time_in_stale[HSR_PT_SLAVE_B])
368 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
369
370 if (time_after(node->time_in[HSR_PT_SLAVE_B],
371 node->time_in[HSR_PT_SLAVE_A] +
372 msecs_to_jiffies(MAX_SLAVE_DIFF)))
373 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
374 if (time_after(node->time_in[HSR_PT_SLAVE_A],
375 node->time_in[HSR_PT_SLAVE_B] +
376 msecs_to_jiffies(MAX_SLAVE_DIFF)))
377 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
378
379 return NULL;
380 }
381
382
383
384
385 void hsr_prune_nodes(struct timer_list *t)
386 {
387 struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
388 struct hsr_node *node;
389 struct hsr_node *tmp;
390 struct hsr_port *port;
391 unsigned long timestamp;
392 unsigned long time_a, time_b;
393
394 spin_lock_bh(&hsr->list_lock);
395 list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
396
397
398
399
400
401 if (hsr_addr_is_self(hsr, node->macaddress_A))
402 continue;
403
404
405 time_a = node->time_in[HSR_PT_SLAVE_A];
406 time_b = node->time_in[HSR_PT_SLAVE_B];
407
408
409 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
410 node->time_in_stale[HSR_PT_SLAVE_A] = true;
411 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
412 node->time_in_stale[HSR_PT_SLAVE_B] = true;
413
414
415
416
417
418 timestamp = time_a;
419 if (node->time_in_stale[HSR_PT_SLAVE_A] ||
420 (!node->time_in_stale[HSR_PT_SLAVE_B] &&
421 time_after(time_b, time_a)))
422 timestamp = time_b;
423
424
425 if (time_is_after_jiffies(timestamp +
426 msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
427 rcu_read_lock();
428 port = get_late_port(hsr, node);
429 if (port)
430 hsr_nl_ringerror(hsr, node->macaddress_A, port);
431 rcu_read_unlock();
432 }
433
434
435 if (time_is_before_jiffies(timestamp +
436 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
437 hsr_nl_nodedown(hsr, node->macaddress_A);
438 list_del_rcu(&node->mac_list);
439
440 kfree_rcu(node, rcu_head);
441 }
442 }
443 spin_unlock_bh(&hsr->list_lock);
444
445
446 mod_timer(&hsr->prune_timer,
447 jiffies + msecs_to_jiffies(PRUNE_PERIOD));
448 }
449
450 void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
451 unsigned char addr[ETH_ALEN])
452 {
453 struct hsr_node *node;
454
455 if (!_pos) {
456 node = list_first_or_null_rcu(&hsr->node_db,
457 struct hsr_node, mac_list);
458 if (node)
459 ether_addr_copy(addr, node->macaddress_A);
460 return node;
461 }
462
463 node = _pos;
464 list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
465 ether_addr_copy(addr, node->macaddress_A);
466 return node;
467 }
468
469 return NULL;
470 }
471
472 int hsr_get_node_data(struct hsr_priv *hsr,
473 const unsigned char *addr,
474 unsigned char addr_b[ETH_ALEN],
475 unsigned int *addr_b_ifindex,
476 int *if1_age,
477 u16 *if1_seq,
478 int *if2_age,
479 u16 *if2_seq)
480 {
481 struct hsr_node *node;
482 struct hsr_port *port;
483 unsigned long tdiff;
484
485 node = find_node_by_addr_A(&hsr->node_db, addr);
486 if (!node)
487 return -ENOENT;
488
489 ether_addr_copy(addr_b, node->macaddress_B);
490
491 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
492 if (node->time_in_stale[HSR_PT_SLAVE_A])
493 *if1_age = INT_MAX;
494 #if HZ <= MSEC_PER_SEC
495 else if (tdiff > msecs_to_jiffies(INT_MAX))
496 *if1_age = INT_MAX;
497 #endif
498 else
499 *if1_age = jiffies_to_msecs(tdiff);
500
501 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
502 if (node->time_in_stale[HSR_PT_SLAVE_B])
503 *if2_age = INT_MAX;
504 #if HZ <= MSEC_PER_SEC
505 else if (tdiff > msecs_to_jiffies(INT_MAX))
506 *if2_age = INT_MAX;
507 #endif
508 else
509 *if2_age = jiffies_to_msecs(tdiff);
510
511
512 *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
513 *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
514
515 if (node->addr_B_port != HSR_PT_NONE) {
516 port = hsr_port_get_hsr(hsr, node->addr_B_port);
517 *addr_b_ifindex = port->dev->ifindex;
518 } else {
519 *addr_b_ifindex = -1;
520 }
521
522 return 0;
523 }