1 /* Copyright 2011-2014 Autronica Fire and Security AS
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)
9 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
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.
17 #include <linux/if_ether.h>
18 #include <linux/etherdevice.h>
19 #include <linux/slab.h>
20 #include <linux/rculist.h>
22 #include "hsr_framereg.h"
23 #include "hsr_netlink.h"
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
;
38 /* TODO: use hash lists for mac addresses (linux/jhash.h)? */
42 /* Search for mac entry. Caller must hold rcu read lock.
44 static struct hsr_node
*find_node_by_AddrA(struct list_head
*node_db
,
45 const unsigned char addr
[ETH_ALEN
])
47 struct hsr_node
*node
;
49 list_for_each_entry_rcu(node
, node_db
, mac_list
) {
50 if (ether_addr_equal(node
->MacAddressA
, addr
))
58 /* Search for mac entry. Caller must hold rcu read lock.
60 static struct hsr_node
*find_node_by_AddrB(struct list_head
*node_db
,
61 const unsigned char addr
[ETH_ALEN
])
63 struct hsr_node
*node
;
65 list_for_each_entry_rcu(node
, node_db
, mac_list
) {
66 if (ether_addr_equal(node
->MacAddressB
, addr
))
74 /* Search for mac entry. Caller must hold rcu read lock.
76 struct hsr_node
*hsr_find_node(struct list_head
*node_db
, struct sk_buff
*skb
)
78 struct hsr_node
*node
;
79 struct ethhdr
*ethhdr
;
81 if (!skb_mac_header_was_set(skb
))
84 ethhdr
= (struct ethhdr
*) skb_mac_header(skb
);
86 list_for_each_entry_rcu(node
, node_db
, mac_list
) {
87 if (ether_addr_equal(node
->MacAddressA
, ethhdr
->h_source
))
89 if (ether_addr_equal(node
->MacAddressB
, ethhdr
->h_source
))
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.
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
])
104 struct hsr_node
*node
, *oldnode
;
106 node
= kmalloc(sizeof(*node
), GFP_KERNEL
);
110 ether_addr_copy(node
->MacAddressA
, addr_a
);
111 ether_addr_copy(node
->MacAddressB
, addr_b
);
114 oldnode
= list_first_or_null_rcu(self_node_db
,
115 struct hsr_node
, mac_list
);
117 list_replace_rcu(&oldnode
->mac_list
, &node
->mac_list
);
123 list_add_tail_rcu(&node
->mac_list
, self_node_db
);
130 /* Add/merge node to the database of nodes. 'skb' must contain an HSR
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.
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:
143 * - (AddrB, SlaveB): The new AddrB will be recorded by PICS_SUBS code since
145 * - (AddrB, SlaveA): Will work as usual (the AddrB change won't be detected
148 * - (AddrA, SlaveB): The old node will be found. We need to detect this and
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.
153 * We also need to detect if the sender's SlaveA and SlaveB cables have been
156 struct hsr_node
*hsr_merge_node(struct hsr_node
*node
, struct sk_buff
*skb
,
157 struct hsr_port
*port
)
159 struct hsr_priv
*hsr
;
160 struct hsr_sup_payload
*hsr_sp
;
161 struct hsr_ethhdr_sp
*hsr_ethsup
;
165 hsr_ethsup
= (struct hsr_ethhdr_sp
*) skb_mac_header(skb
);
166 hsr_sp
= (struct hsr_sup_payload
*) skb
->data
;
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
);
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
);
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
);
196 node
= find_node_by_AddrA(&hsr
->node_db
, hsr_sp
->MacAddressA
);
198 /* Node is known, but frame was received from an unknown
199 * address. Node is PICS_SUBS capable; merge its AddrB.
201 ether_addr_copy(node
->MacAddressB
, hsr_ethsup
->ethhdr
.h_source
);
202 node
->AddrB_port
= port
->type
;
206 node
= kzalloc(sizeof(*node
), GFP_ATOMIC
);
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
;
215 /* We are only interested in time diffs here, so use current jiffies
216 * as initialization. (0 could trigger an spurious ring error warning).
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;
224 list_add_tail_rcu(&node
->mac_list
, &hsr
->node_db
);
230 /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
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.
236 void hsr_addr_subst_source(struct hsr_priv
*hsr
, struct sk_buff
*skb
)
238 struct ethhdr
*ethhdr
;
239 struct hsr_node
*node
;
241 if (!skb_mac_header_was_set(skb
)) {
242 WARN_ONCE(1, "%s: Mac header not set\n", __func__
);
245 ethhdr
= (struct ethhdr
*) skb_mac_header(skb
);
248 node
= find_node_by_AddrB(&hsr
->node_db
, ethhdr
->h_source
);
250 ether_addr_copy(ethhdr
->h_source
, node
->MacAddressA
);
255 /* 'skb' is a frame meant for another host.
256 * 'hsr_dev_idx' is the HSR index of the outgoing device
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.
264 void hsr_addr_subst_dest(struct hsr_priv
*hsr
, struct ethhdr
*ethhdr
,
265 struct hsr_port
*port
)
267 struct hsr_node
*node
;
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
);
277 /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
280 static bool seq_nr_after(u16 a
, u16 b
)
282 /* Remove inconsistency where
283 * seq_nr_after(a, b) == seq_nr_before(a, b)
285 if ((int) b
- a
== 32768)
288 return (((s16
) (b
- a
)) < 0);
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)))
295 void hsr_register_frame_in(struct hsr_node
*node
, struct hsr_port
*port
)
297 node
->time_in
[port
->type
] = jiffies
;
298 node
->time_in_stale
[port
->type
] = false;
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.
306 * 1 if frame can be shown to have been sent recently on this interface,
308 * negative error code on error
310 int hsr_register_frame_out(struct hsr_node
*node
, struct hsr_port
*port
,
313 struct hsr_ethhdr
*hsr_ethhdr
;
316 if (!skb_mac_header_was_set(skb
)) {
317 WARN_ONCE(1, "%s: Mac header not set\n", __func__
);
320 hsr_ethhdr
= (struct hsr_ethhdr
*) skb_mac_header(skb
);
322 sequence_nr
= ntohs(hsr_ethhdr
->hsr_tag
.sequence_nr
);
323 if (seq_nr_before_or_eq(sequence_nr
, node
->seq_out
[port
->type
]))
326 node
->seq_out
[port
->type
] = sequence_nr
;
331 static struct hsr_port
*get_late_port(struct hsr_priv
*hsr
,
332 struct hsr_node
*node
)
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
);
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
);
352 /* Remove stale sequence_nr records. Called by timer every
353 * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
355 void hsr_prune_nodes(unsigned long data
)
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
;
363 hsr
= (struct hsr_priv
*) data
;
366 list_for_each_entry_rcu(node
, &hsr
->node_db
, mac_list
) {
368 time_a
= node
->time_in
[HSR_PT_SLAVE_A
];
369 time_b
= node
->time_in
[HSR_PT_SLAVE_B
];
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;
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
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
)))
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
))) {
391 port
= get_late_port(hsr
, node
);
393 hsr_nl_ringerror(hsr
, node
->MacAddressA
, port
);
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
);
410 void *hsr_get_next_node(struct hsr_priv
*hsr
, void *_pos
,
411 unsigned char addr
[ETH_ALEN
])
413 struct hsr_node
*node
;
416 node
= list_first_or_null_rcu(&hsr
->node_db
,
417 struct hsr_node
, mac_list
);
419 ether_addr_copy(addr
, node
->MacAddressA
);
424 list_for_each_entry_continue_rcu(node
, &hsr
->node_db
, mac_list
) {
425 ether_addr_copy(addr
, node
->MacAddressA
);
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
,
442 struct hsr_node
*node
;
443 struct hsr_port
*port
;
448 node
= find_node_by_AddrA(&hsr
->node_db
, addr
);
451 return -ENOENT
; /* No such entry */
454 ether_addr_copy(addr_b
, node
->MacAddressB
);
456 tdiff
= jiffies
- node
->time_in
[HSR_PT_SLAVE_A
];
457 if (node
->time_in_stale
[HSR_PT_SLAVE_A
])
459 #if HZ <= MSEC_PER_SEC
460 else if (tdiff
> msecs_to_jiffies(INT_MAX
))
464 *if1_age
= jiffies_to_msecs(tdiff
);
466 tdiff
= jiffies
- node
->time_in
[HSR_PT_SLAVE_B
];
467 if (node
->time_in_stale
[HSR_PT_SLAVE_B
])
469 #if HZ <= MSEC_PER_SEC
470 else if (tdiff
> msecs_to_jiffies(INT_MAX
))
474 *if2_age
= jiffies_to_msecs(tdiff
);
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
];
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
;
484 *addr_b_ifindex
= -1;
This page took 0.053398 seconds and 5 git commands to generate.