blob: c1b0e62af0f109e54f1bde5786faec2efcc9898a [file] [log] [blame]
Arvid Brodin70ebe4a2014-07-04 23:34:38 +02001/* Copyright 2011-2014 Autronica Fire and Security AS
Arvid Brodinf4214362013-10-30 21:10:47 +01002 *
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):
Arvid Brodin70ebe4a2014-07-04 23:34:38 +02009 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
Arvid Brodinf4214362013-10-30 21:10:47 +010010 *
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
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020025struct hsr_node {
26 struct list_head mac_list;
27 unsigned char MacAddressA[ETH_ALEN];
28 unsigned char MacAddressB[ETH_ALEN];
Arvid Brodinc5a75912014-07-04 23:38:05 +020029 /* Local slave through which AddrB frames are received from this node */
30 enum hsr_port_type AddrB_port;
31 unsigned long time_in[HSR_PT_PORTS];
32 bool time_in_stale[HSR_PT_PORTS];
33 u16 seq_out[HSR_PT_PORTS];
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020034 struct rcu_head rcu_head;
Arvid Brodinf4214362013-10-30 21:10:47 +010035};
36
37/* TODO: use hash lists for mac addresses (linux/jhash.h)? */
38
Arvid Brodinf266a682014-07-04 23:41:03 +020039/* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
40 * false otherwise.
41 */
42static bool seq_nr_after(u16 a, u16 b)
43{
44 /* Remove inconsistency where
45 * seq_nr_after(a, b) == seq_nr_before(a, b)
46 */
47 if ((int) b - a == 32768)
48 return false;
49
50 return (((s16) (b - a)) < 0);
51}
52#define seq_nr_before(a, b) seq_nr_after((b), (a))
53#define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
54#define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
55
Arvid Brodinf266a682014-07-04 23:41:03 +020056bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
57{
58 struct hsr_node *node;
59
60 node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node,
61 mac_list);
62 if (!node) {
63 WARN_ONCE(1, "HSR: No self node\n");
64 return false;
65 }
66
67 if (ether_addr_equal(addr, node->MacAddressA))
68 return true;
69 if (ether_addr_equal(addr, node->MacAddressB))
70 return true;
71
72 return false;
73}
Arvid Brodinf4214362013-10-30 21:10:47 +010074
75/* Search for mac entry. Caller must hold rcu read lock.
76 */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020077static struct hsr_node *find_node_by_AddrA(struct list_head *node_db,
78 const unsigned char addr[ETH_ALEN])
Arvid Brodinf4214362013-10-30 21:10:47 +010079{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020080 struct hsr_node *node;
Arvid Brodinf4214362013-10-30 21:10:47 +010081
82 list_for_each_entry_rcu(node, node_db, mac_list) {
83 if (ether_addr_equal(node->MacAddressA, addr))
84 return node;
85 }
86
87 return NULL;
88}
89
Arvid Brodinf4214362013-10-30 21:10:47 +010090/* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
91 * frames from self that's been looped over the HSR ring.
92 */
93int hsr_create_self_node(struct list_head *self_node_db,
94 unsigned char addr_a[ETH_ALEN],
95 unsigned char addr_b[ETH_ALEN])
96{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020097 struct hsr_node *node, *oldnode;
Arvid Brodinf4214362013-10-30 21:10:47 +010098
99 node = kmalloc(sizeof(*node), GFP_KERNEL);
100 if (!node)
101 return -ENOMEM;
102
Joe Perchese83abe32014-02-18 10:37:20 -0800103 ether_addr_copy(node->MacAddressA, addr_a);
104 ether_addr_copy(node->MacAddressB, addr_b);
Arvid Brodinf4214362013-10-30 21:10:47 +0100105
106 rcu_read_lock();
107 oldnode = list_first_or_null_rcu(self_node_db,
Murali Karicheri4fe25bd2019-04-05 13:31:26 -0400108 struct hsr_node, mac_list);
Arvid Brodinf4214362013-10-30 21:10:47 +0100109 if (oldnode) {
110 list_replace_rcu(&oldnode->mac_list, &node->mac_list);
111 rcu_read_unlock();
112 synchronize_rcu();
113 kfree(oldnode);
114 } else {
115 rcu_read_unlock();
116 list_add_tail_rcu(&node->mac_list, self_node_db);
117 }
118
119 return 0;
120}
121
Mao Wenan6caabe72019-03-06 22:45:01 +0800122void hsr_del_node(struct list_head *self_node_db)
123{
124 struct hsr_node *node;
125
126 rcu_read_lock();
127 node = list_first_or_null_rcu(self_node_db, struct hsr_node, mac_list);
128 rcu_read_unlock();
129 if (node) {
130 list_del_rcu(&node->mac_list);
131 kfree(node);
132 }
133}
Arvid Brodinf4214362013-10-30 21:10:47 +0100134
Arvid Brodinf266a682014-07-04 23:41:03 +0200135/* Allocate an hsr_node and add it to node_db. 'addr' is the node's AddressA;
136 * seq_out is used to initialize filtering of outgoing duplicate frames
137 * originating from the newly added node.
Arvid Brodinf4214362013-10-30 21:10:47 +0100138 */
Arvid Brodinf266a682014-07-04 23:41:03 +0200139struct hsr_node *hsr_add_node(struct list_head *node_db, unsigned char addr[],
140 u16 seq_out)
Arvid Brodinf4214362013-10-30 21:10:47 +0100141{
Arvid Brodinf266a682014-07-04 23:41:03 +0200142 struct hsr_node *node;
Arvid Brodinf4214362013-10-30 21:10:47 +0100143 unsigned long now;
Arvid Brodinf266a682014-07-04 23:41:03 +0200144 int i;
Arvid Brodinf4214362013-10-30 21:10:47 +0100145
146 node = kzalloc(sizeof(*node), GFP_ATOMIC);
147 if (!node)
148 return NULL;
149
Arvid Brodinf266a682014-07-04 23:41:03 +0200150 ether_addr_copy(node->MacAddressA, addr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100151
152 /* We are only interested in time diffs here, so use current jiffies
153 * as initialization. (0 could trigger an spurious ring error warning).
154 */
155 now = jiffies;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200156 for (i = 0; i < HSR_PT_PORTS; i++)
Arvid Brodinf4214362013-10-30 21:10:47 +0100157 node->time_in[i] = now;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200158 for (i = 0; i < HSR_PT_PORTS; i++)
Arvid Brodinf266a682014-07-04 23:41:03 +0200159 node->seq_out[i] = seq_out;
Arvid Brodinf4214362013-10-30 21:10:47 +0100160
Arvid Brodinf266a682014-07-04 23:41:03 +0200161 list_add_tail_rcu(&node->mac_list, node_db);
Arvid Brodinf4214362013-10-30 21:10:47 +0100162
163 return node;
164}
165
Arvid Brodinf266a682014-07-04 23:41:03 +0200166/* Get the hsr_node from which 'skb' was sent.
167 */
Karicheri, Muralidharan675c8da2017-06-12 15:06:26 -0400168struct hsr_node *hsr_get_node(struct hsr_port *port, struct sk_buff *skb,
Arvid Brodinf266a682014-07-04 23:41:03 +0200169 bool is_sup)
170{
Karicheri, Muralidharan675c8da2017-06-12 15:06:26 -0400171 struct list_head *node_db = &port->hsr->node_db;
Arvid Brodinf266a682014-07-04 23:41:03 +0200172 struct hsr_node *node;
173 struct ethhdr *ethhdr;
174 u16 seq_out;
175
176 if (!skb_mac_header_was_set(skb))
177 return NULL;
178
179 ethhdr = (struct ethhdr *) skb_mac_header(skb);
180
181 list_for_each_entry_rcu(node, node_db, mac_list) {
182 if (ether_addr_equal(node->MacAddressA, ethhdr->h_source))
183 return node;
184 if (ether_addr_equal(node->MacAddressB, ethhdr->h_source))
185 return node;
186 }
187
Peter Heiseee1c2792016-04-13 13:52:22 +0200188 /* Everyone may create a node entry, connected node to a HSR device. */
Arvid Brodinf266a682014-07-04 23:41:03 +0200189
Peter Heiseee1c2792016-04-13 13:52:22 +0200190 if (ethhdr->h_proto == htons(ETH_P_PRP)
191 || ethhdr->h_proto == htons(ETH_P_HSR)) {
Arvid Brodinf266a682014-07-04 23:41:03 +0200192 /* Use the existing sequence_nr from the tag as starting point
193 * for filtering duplicate frames.
194 */
195 seq_out = hsr_get_skb_sequence_nr(skb) - 1;
196 } else {
Karicheri, Muralidharan675c8da2017-06-12 15:06:26 -0400197 /* this is called also for frames from master port and
198 * so warn only for non master ports
199 */
200 if (port->type != HSR_PT_MASTER)
201 WARN_ONCE(1, "%s: Non-HSR frame\n", __func__);
Peter Heiseee1c2792016-04-13 13:52:22 +0200202 seq_out = HSR_SEQNR_START;
Arvid Brodinf266a682014-07-04 23:41:03 +0200203 }
204
205 return hsr_add_node(node_db, ethhdr->h_source, seq_out);
206}
207
208/* Use the Supervision frame's info about an eventual MacAddressB for merging
209 * nodes that has previously had their MacAddressB registered as a separate
210 * node.
211 */
212void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr,
213 struct hsr_port *port_rcv)
214{
Peter Heiseee1c2792016-04-13 13:52:22 +0200215 struct ethhdr *ethhdr;
Arvid Brodinf266a682014-07-04 23:41:03 +0200216 struct hsr_node *node_real;
217 struct hsr_sup_payload *hsr_sp;
218 struct list_head *node_db;
219 int i;
220
Peter Heiseee1c2792016-04-13 13:52:22 +0200221 ethhdr = (struct ethhdr *) skb_mac_header(skb);
Arvid Brodinf266a682014-07-04 23:41:03 +0200222
Peter Heiseee1c2792016-04-13 13:52:22 +0200223 /* Leave the ethernet header. */
224 skb_pull(skb, sizeof(struct ethhdr));
225
226 /* And leave the HSR tag. */
227 if (ethhdr->h_proto == htons(ETH_P_HSR))
228 skb_pull(skb, sizeof(struct hsr_tag));
229
230 /* And leave the HSR sup tag. */
231 skb_pull(skb, sizeof(struct hsr_sup_tag));
232
233 hsr_sp = (struct hsr_sup_payload *) skb->data;
Arvid Brodinf266a682014-07-04 23:41:03 +0200234
235 /* Merge node_curr (registered on MacAddressB) into node_real */
236 node_db = &port_rcv->hsr->node_db;
237 node_real = find_node_by_AddrA(node_db, hsr_sp->MacAddressA);
238 if (!node_real)
239 /* No frame received from AddrA of this node yet */
240 node_real = hsr_add_node(node_db, hsr_sp->MacAddressA,
241 HSR_SEQNR_START - 1);
242 if (!node_real)
243 goto done; /* No mem */
244 if (node_real == node_curr)
245 /* Node has already been merged */
246 goto done;
247
Peter Heiseee1c2792016-04-13 13:52:22 +0200248 ether_addr_copy(node_real->MacAddressB, ethhdr->h_source);
Arvid Brodinf266a682014-07-04 23:41:03 +0200249 for (i = 0; i < HSR_PT_PORTS; i++) {
250 if (!node_curr->time_in_stale[i] &&
251 time_after(node_curr->time_in[i], node_real->time_in[i])) {
252 node_real->time_in[i] = node_curr->time_in[i];
Murali Karicherid595b852019-04-05 13:31:23 -0400253 node_real->time_in_stale[i] =
254 node_curr->time_in_stale[i];
Arvid Brodinf266a682014-07-04 23:41:03 +0200255 }
256 if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
257 node_real->seq_out[i] = node_curr->seq_out[i];
258 }
259 node_real->AddrB_port = port_rcv->type;
260
261 list_del_rcu(&node_curr->mac_list);
262 kfree_rcu(node_curr, rcu_head);
263
264done:
Peter Heiseee1c2792016-04-13 13:52:22 +0200265 skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
Arvid Brodinf266a682014-07-04 23:41:03 +0200266}
267
Arvid Brodinf4214362013-10-30 21:10:47 +0100268/* 'skb' is a frame meant for this host, that is to be passed to upper layers.
269 *
Arvid Brodinf266a682014-07-04 23:41:03 +0200270 * If the frame was sent by a node's B interface, replace the source
Arvid Brodinf4214362013-10-30 21:10:47 +0100271 * address with that node's "official" address (MacAddressA) so that upper
272 * layers recognize where it came from.
273 */
Arvid Brodinf266a682014-07-04 23:41:03 +0200274void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
Arvid Brodinf4214362013-10-30 21:10:47 +0100275{
Arvid Brodinf4214362013-10-30 21:10:47 +0100276 if (!skb_mac_header_was_set(skb)) {
277 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
278 return;
279 }
Arvid Brodinf4214362013-10-30 21:10:47 +0100280
Arvid Brodinf266a682014-07-04 23:41:03 +0200281 memcpy(&eth_hdr(skb)->h_source, node->MacAddressA, ETH_ALEN);
Arvid Brodinf4214362013-10-30 21:10:47 +0100282}
283
Arvid Brodinf4214362013-10-30 21:10:47 +0100284/* 'skb' is a frame meant for another host.
Arvid Brodinf266a682014-07-04 23:41:03 +0200285 * 'port' is the outgoing interface
Arvid Brodinf4214362013-10-30 21:10:47 +0100286 *
287 * Substitute the target (dest) MAC address if necessary, so the it matches the
288 * recipient interface MAC address, regardless of whether that is the
289 * recipient's A or B interface.
290 * This is needed to keep the packets flowing through switches that learn on
291 * which "side" the different interfaces are.
292 */
Arvid Brodinf266a682014-07-04 23:41:03 +0200293void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
Arvid Brodinc5a75912014-07-04 23:38:05 +0200294 struct hsr_port *port)
Arvid Brodinf4214362013-10-30 21:10:47 +0100295{
Arvid Brodinf266a682014-07-04 23:41:03 +0200296 struct hsr_node *node_dst;
Arvid Brodinf4214362013-10-30 21:10:47 +0100297
Arvid Brodinf266a682014-07-04 23:41:03 +0200298 if (!skb_mac_header_was_set(skb)) {
299 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
300 return;
301 }
302
303 if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
304 return;
305
Murali Karicherid595b852019-04-05 13:31:23 -0400306 node_dst = find_node_by_AddrA(&port->hsr->node_db,
307 eth_hdr(skb)->h_dest);
Arvid Brodinf266a682014-07-04 23:41:03 +0200308 if (!node_dst) {
309 WARN_ONCE(1, "%s: Unknown node\n", __func__);
310 return;
311 }
312 if (port->type != node_dst->AddrB_port)
313 return;
Arvid Brodinf266a682014-07-04 23:41:03 +0200314
315 ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->MacAddressB);
Arvid Brodinf4214362013-10-30 21:10:47 +0100316}
317
Arvid Brodinf266a682014-07-04 23:41:03 +0200318void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
319 u16 sequence_nr)
Arvid Brodinf4214362013-10-30 21:10:47 +0100320{
Arvid Brodinf266a682014-07-04 23:41:03 +0200321 /* Don't register incoming frames without a valid sequence number. This
322 * ensures entries of restarted nodes gets pruned so that they can
323 * re-register and resume communications.
Arvid Brodin213e3bc2013-11-29 23:37:07 +0100324 */
Arvid Brodinf266a682014-07-04 23:41:03 +0200325 if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
326 return;
Arvid Brodinf4214362013-10-30 21:10:47 +0100327
Arvid Brodinc5a75912014-07-04 23:38:05 +0200328 node->time_in[port->type] = jiffies;
329 node->time_in_stale[port->type] = false;
Arvid Brodinf4214362013-10-30 21:10:47 +0100330}
331
Arvid Brodinf4214362013-10-30 21:10:47 +0100332/* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
333 * ethhdr->h_source address and skb->mac_header set.
334 *
335 * Return:
336 * 1 if frame can be shown to have been sent recently on this interface,
337 * 0 otherwise, or
338 * negative error code on error
339 */
Arvid Brodinf266a682014-07-04 23:41:03 +0200340int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
341 u16 sequence_nr)
Arvid Brodinf4214362013-10-30 21:10:47 +0100342{
Arvid Brodinc5a75912014-07-04 23:38:05 +0200343 if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
Arvid Brodinf4214362013-10-30 21:10:47 +0100344 return 1;
345
Arvid Brodinc5a75912014-07-04 23:38:05 +0200346 node->seq_out[port->type] = sequence_nr;
Arvid Brodinf4214362013-10-30 21:10:47 +0100347 return 0;
348}
349
Arvid Brodinc5a75912014-07-04 23:38:05 +0200350static struct hsr_port *get_late_port(struct hsr_priv *hsr,
351 struct hsr_node *node)
Arvid Brodinf4214362013-10-30 21:10:47 +0100352{
Arvid Brodinc5a75912014-07-04 23:38:05 +0200353 if (node->time_in_stale[HSR_PT_SLAVE_A])
354 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
355 if (node->time_in_stale[HSR_PT_SLAVE_B])
356 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
Arvid Brodinf4214362013-10-30 21:10:47 +0100357
Arvid Brodinc5a75912014-07-04 23:38:05 +0200358 if (time_after(node->time_in[HSR_PT_SLAVE_B],
359 node->time_in[HSR_PT_SLAVE_A] +
360 msecs_to_jiffies(MAX_SLAVE_DIFF)))
361 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
362 if (time_after(node->time_in[HSR_PT_SLAVE_A],
363 node->time_in[HSR_PT_SLAVE_B] +
364 msecs_to_jiffies(MAX_SLAVE_DIFF)))
365 return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
Arvid Brodinf4214362013-10-30 21:10:47 +0100366
Arvid Brodinc5a75912014-07-04 23:38:05 +0200367 return NULL;
Arvid Brodinf4214362013-10-30 21:10:47 +0100368}
369
Arvid Brodinf4214362013-10-30 21:10:47 +0100370/* Remove stale sequence_nr records. Called by timer every
371 * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
372 */
Kees Cookdda436b2017-10-24 01:46:16 -0700373void hsr_prune_nodes(struct timer_list *t)
Arvid Brodinf4214362013-10-30 21:10:47 +0100374{
Kees Cookdda436b2017-10-24 01:46:16 -0700375 struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200376 struct hsr_node *node;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200377 struct hsr_port *port;
Arvid Brodinf4214362013-10-30 21:10:47 +0100378 unsigned long timestamp;
379 unsigned long time_a, time_b;
380
381 rcu_read_lock();
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200382 list_for_each_entry_rcu(node, &hsr->node_db, mac_list) {
Arvid Brodinf4214362013-10-30 21:10:47 +0100383 /* Shorthand */
Arvid Brodinc5a75912014-07-04 23:38:05 +0200384 time_a = node->time_in[HSR_PT_SLAVE_A];
385 time_b = node->time_in[HSR_PT_SLAVE_B];
Arvid Brodinf4214362013-10-30 21:10:47 +0100386
387 /* Check for timestamps old enough to risk wrap-around */
388 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2))
Arvid Brodinc5a75912014-07-04 23:38:05 +0200389 node->time_in_stale[HSR_PT_SLAVE_A] = true;
Arvid Brodinf4214362013-10-30 21:10:47 +0100390 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2))
Arvid Brodinc5a75912014-07-04 23:38:05 +0200391 node->time_in_stale[HSR_PT_SLAVE_B] = true;
Arvid Brodinf4214362013-10-30 21:10:47 +0100392
393 /* Get age of newest frame from node.
394 * At least one time_in is OK here; nodes get pruned long
395 * before both time_ins can get stale
396 */
397 timestamp = time_a;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200398 if (node->time_in_stale[HSR_PT_SLAVE_A] ||
399 (!node->time_in_stale[HSR_PT_SLAVE_B] &&
Arvid Brodinf4214362013-10-30 21:10:47 +0100400 time_after(time_b, time_a)))
401 timestamp = time_b;
402
403 /* Warn of ring error only as long as we get frames at all */
404 if (time_is_after_jiffies(timestamp +
405 msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) {
Arvid Brodinc5a75912014-07-04 23:38:05 +0200406 rcu_read_lock();
407 port = get_late_port(hsr, node);
Murali Karicheri05ca6e62019-04-05 13:31:28 -0400408 if (port)
Arvid Brodinc5a75912014-07-04 23:38:05 +0200409 hsr_nl_ringerror(hsr, node->MacAddressA, port);
410 rcu_read_unlock();
Arvid Brodinf4214362013-10-30 21:10:47 +0100411 }
412
413 /* Prune old entries */
414 if (time_is_before_jiffies(timestamp +
Murali Karicherid595b852019-04-05 13:31:23 -0400415 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200416 hsr_nl_nodedown(hsr, node->MacAddressA);
Arvid Brodinf4214362013-10-30 21:10:47 +0100417 list_del_rcu(&node->mac_list);
418 /* Note that we need to free this entry later: */
Wei Yongjun1aee6cc2013-12-16 14:05:50 +0800419 kfree_rcu(node, rcu_head);
Arvid Brodinf4214362013-10-30 21:10:47 +0100420 }
421 }
422 rcu_read_unlock();
423}
424
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200425void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
Arvid Brodinf4214362013-10-30 21:10:47 +0100426 unsigned char addr[ETH_ALEN])
427{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200428 struct hsr_node *node;
Arvid Brodinf4214362013-10-30 21:10:47 +0100429
430 if (!_pos) {
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200431 node = list_first_or_null_rcu(&hsr->node_db,
432 struct hsr_node, mac_list);
Arvid Brodinf4214362013-10-30 21:10:47 +0100433 if (node)
Joe Perchese83abe32014-02-18 10:37:20 -0800434 ether_addr_copy(addr, node->MacAddressA);
Arvid Brodinf4214362013-10-30 21:10:47 +0100435 return node;
436 }
437
438 node = _pos;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200439 list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
Joe Perchese83abe32014-02-18 10:37:20 -0800440 ether_addr_copy(addr, node->MacAddressA);
Arvid Brodinf4214362013-10-30 21:10:47 +0100441 return node;
442 }
443
444 return NULL;
445}
446
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200447int hsr_get_node_data(struct hsr_priv *hsr,
Arvid Brodinf4214362013-10-30 21:10:47 +0100448 const unsigned char *addr,
449 unsigned char addr_b[ETH_ALEN],
450 unsigned int *addr_b_ifindex,
451 int *if1_age,
452 u16 *if1_seq,
453 int *if2_age,
454 u16 *if2_seq)
455{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200456 struct hsr_node *node;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200457 struct hsr_port *port;
Arvid Brodinf4214362013-10-30 21:10:47 +0100458 unsigned long tdiff;
459
Arvid Brodinf4214362013-10-30 21:10:47 +0100460 rcu_read_lock();
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200461 node = find_node_by_AddrA(&hsr->node_db, addr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100462 if (!node) {
463 rcu_read_unlock();
464 return -ENOENT; /* No such entry */
465 }
466
Joe Perchese83abe32014-02-18 10:37:20 -0800467 ether_addr_copy(addr_b, node->MacAddressB);
Arvid Brodinf4214362013-10-30 21:10:47 +0100468
Arvid Brodinc5a75912014-07-04 23:38:05 +0200469 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
470 if (node->time_in_stale[HSR_PT_SLAVE_A])
Arvid Brodinf4214362013-10-30 21:10:47 +0100471 *if1_age = INT_MAX;
472#if HZ <= MSEC_PER_SEC
473 else if (tdiff > msecs_to_jiffies(INT_MAX))
474 *if1_age = INT_MAX;
475#endif
476 else
477 *if1_age = jiffies_to_msecs(tdiff);
478
Arvid Brodinc5a75912014-07-04 23:38:05 +0200479 tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
480 if (node->time_in_stale[HSR_PT_SLAVE_B])
Arvid Brodinf4214362013-10-30 21:10:47 +0100481 *if2_age = INT_MAX;
482#if HZ <= MSEC_PER_SEC
483 else if (tdiff > msecs_to_jiffies(INT_MAX))
484 *if2_age = INT_MAX;
485#endif
486 else
487 *if2_age = jiffies_to_msecs(tdiff);
488
489 /* Present sequence numbers as if they were incoming on interface */
Arvid Brodinc5a75912014-07-04 23:38:05 +0200490 *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
491 *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
Arvid Brodinf4214362013-10-30 21:10:47 +0100492
Arvid Brodinc5a75912014-07-04 23:38:05 +0200493 if (node->AddrB_port != HSR_PT_NONE) {
494 port = hsr_port_get_hsr(hsr, node->AddrB_port);
495 *addr_b_ifindex = port->dev->ifindex;
496 } else {
Arvid Brodinf4214362013-10-30 21:10:47 +0100497 *addr_b_ifindex = -1;
Arvid Brodinc5a75912014-07-04 23:38:05 +0200498 }
Arvid Brodinf4214362013-10-30 21:10:47 +0100499
500 rcu_read_unlock();
501
502 return 0;
503}