Blame view

net/hsr/hsr_framereg.c 14.4 KB
0e7623bdf   Murali Karicheri   net: hsr: convert...
1
  // SPDX-License-Identifier: GPL-2.0
70ebe4a47   Arvid Brodin   net/hsr: Better v...
2
  /* Copyright 2011-2014 Autronica Fire and Security AS
f421436a5   Arvid Brodin   net/hsr: Add supp...
3
   *
f421436a5   Arvid Brodin   net/hsr: Add supp...
4
   * Author(s):
70ebe4a47   Arvid Brodin   net/hsr: Better v...
5
   *	2011-2014 Arvid Brodin, arvid.brodin@alten.se
f421436a5   Arvid Brodin   net/hsr: Add supp...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   *
   * 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"
f266a683a   Arvid Brodin   net/hsr: Better f...
20
  /*	TODO: use hash lists for mac addresses (linux/jhash.h)?    */
f421436a5   Arvid Brodin   net/hsr: Add supp...
21

f266a683a   Arvid Brodin   net/hsr: Better f...
22
23
  /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
   * false otherwise.
f421436a5   Arvid Brodin   net/hsr: Add supp...
24
   */
f266a683a   Arvid Brodin   net/hsr: Better f...
25
  static bool seq_nr_after(u16 a, u16 b)
f421436a5   Arvid Brodin   net/hsr: Add supp...
26
  {
f266a683a   Arvid Brodin   net/hsr: Better f...
27
28
29
  	/* Remove inconsistency where
  	 * seq_nr_after(a, b) == seq_nr_before(a, b)
  	 */
5fa967780   Murali Karicheri   net: hsr: remove ...
30
  	if ((int)b - a == 32768)
f266a683a   Arvid Brodin   net/hsr: Better f...
31
  		return false;
f421436a5   Arvid Brodin   net/hsr: Add supp...
32

5fa967780   Murali Karicheri   net: hsr: remove ...
33
  	return (((s16)(b - a)) < 0);
f421436a5   Arvid Brodin   net/hsr: Add supp...
34
  }
9f73c2bb4   Murali Karicheri   net: hsr: add bla...
35

f266a683a   Arvid Brodin   net/hsr: Better f...
36
37
38
  #define seq_nr_before(a, b)		seq_nr_after((b), (a))
  #define seq_nr_after_or_eq(a, b)	(!seq_nr_before((a), (b)))
  #define seq_nr_before_or_eq(a, b)	(!seq_nr_after((a), (b)))
f421436a5   Arvid Brodin   net/hsr: Add supp...
39

f266a683a   Arvid Brodin   net/hsr: Better f...
40
  bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
f421436a5   Arvid Brodin   net/hsr: Add supp...
41
  {
70ebe4a47   Arvid Brodin   net/hsr: Better v...
42
  	struct hsr_node *node;
f421436a5   Arvid Brodin   net/hsr: Add supp...
43

f266a683a   Arvid Brodin   net/hsr: Better f...
44
45
46
47
48
49
  	node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node,
  				      mac_list);
  	if (!node) {
  		WARN_ONCE(1, "HSR: No self node
  ");
  		return false;
f421436a5   Arvid Brodin   net/hsr: Add supp...
50
  	}
b1b4aa914   Murali Karicheri   net: hsr: remove ...
51
  	if (ether_addr_equal(addr, node->macaddress_A))
f266a683a   Arvid Brodin   net/hsr: Better f...
52
  		return true;
b1b4aa914   Murali Karicheri   net: hsr: remove ...
53
  	if (ether_addr_equal(addr, node->macaddress_B))
f266a683a   Arvid Brodin   net/hsr: Better f...
54
  		return true;
f421436a5   Arvid Brodin   net/hsr: Add supp...
55

f266a683a   Arvid Brodin   net/hsr: Better f...
56
57
  	return false;
  }
f421436a5   Arvid Brodin   net/hsr: Add supp...
58
59
60
  
  /* Search for mac entry. Caller must hold rcu read lock.
   */
b1b4aa914   Murali Karicheri   net: hsr: remove ...
61
62
  static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
  					    const unsigned char addr[ETH_ALEN])
f421436a5   Arvid Brodin   net/hsr: Add supp...
63
  {
70ebe4a47   Arvid Brodin   net/hsr: Better v...
64
  	struct hsr_node *node;
f421436a5   Arvid Brodin   net/hsr: Add supp...
65
66
  
  	list_for_each_entry_rcu(node, node_db, mac_list) {
b1b4aa914   Murali Karicheri   net: hsr: remove ...
67
  		if (ether_addr_equal(node->macaddress_A, addr))
f421436a5   Arvid Brodin   net/hsr: Add supp...
68
69
70
71
72
  			return node;
  	}
  
  	return NULL;
  }
f421436a5   Arvid Brodin   net/hsr: Add supp...
73
74
75
  /* 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.
   */
0fc906e60   Taehee Yoo   hsr: fix a race c...
76
  int hsr_create_self_node(struct hsr_priv *hsr,
f421436a5   Arvid Brodin   net/hsr: Add supp...
77
78
79
  			 unsigned char addr_a[ETH_ALEN],
  			 unsigned char addr_b[ETH_ALEN])
  {
0fc906e60   Taehee Yoo   hsr: fix a race c...
80
  	struct list_head *self_node_db = &hsr->self_node_db;
70ebe4a47   Arvid Brodin   net/hsr: Better v...
81
  	struct hsr_node *node, *oldnode;
f421436a5   Arvid Brodin   net/hsr: Add supp...
82
83
84
85
  
  	node = kmalloc(sizeof(*node), GFP_KERNEL);
  	if (!node)
  		return -ENOMEM;
b1b4aa914   Murali Karicheri   net: hsr: remove ...
86
87
  	ether_addr_copy(node->macaddress_A, addr_a);
  	ether_addr_copy(node->macaddress_B, addr_b);
f421436a5   Arvid Brodin   net/hsr: Add supp...
88

0fc906e60   Taehee Yoo   hsr: fix a race c...
89
  	spin_lock_bh(&hsr->list_lock);
f421436a5   Arvid Brodin   net/hsr: Add supp...
90
  	oldnode = list_first_or_null_rcu(self_node_db,
4fe25bd8c   Murali Karicheri   net: hsr: fix ali...
91
  					 struct hsr_node, mac_list);
f421436a5   Arvid Brodin   net/hsr: Add supp...
92
93
  	if (oldnode) {
  		list_replace_rcu(&oldnode->mac_list, &node->mac_list);
0fc906e60   Taehee Yoo   hsr: fix a race c...
94
95
  		spin_unlock_bh(&hsr->list_lock);
  		kfree_rcu(oldnode, rcu_head);
f421436a5   Arvid Brodin   net/hsr: Add supp...
96
  	} else {
f421436a5   Arvid Brodin   net/hsr: Add supp...
97
  		list_add_tail_rcu(&node->mac_list, self_node_db);
0fc906e60   Taehee Yoo   hsr: fix a race c...
98
  		spin_unlock_bh(&hsr->list_lock);
f421436a5   Arvid Brodin   net/hsr: Add supp...
99
100
101
102
  	}
  
  	return 0;
  }
0fc906e60   Taehee Yoo   hsr: fix a race c...
103
  void hsr_del_self_node(struct hsr_priv *hsr)
6caabe7f1   Mao Wenan   net: hsr: fix mem...
104
  {
0fc906e60   Taehee Yoo   hsr: fix a race c...
105
  	struct list_head *self_node_db = &hsr->self_node_db;
6caabe7f1   Mao Wenan   net: hsr: fix mem...
106
  	struct hsr_node *node;
0fc906e60   Taehee Yoo   hsr: fix a race c...
107
  	spin_lock_bh(&hsr->list_lock);
6caabe7f1   Mao Wenan   net: hsr: fix mem...
108
  	node = list_first_or_null_rcu(self_node_db, struct hsr_node, mac_list);
6caabe7f1   Mao Wenan   net: hsr: fix mem...
109
110
  	if (node) {
  		list_del_rcu(&node->mac_list);
0fc906e60   Taehee Yoo   hsr: fix a race c...
111
  		kfree_rcu(node, rcu_head);
6caabe7f1   Mao Wenan   net: hsr: fix mem...
112
  	}
0fc906e60   Taehee Yoo   hsr: fix a race c...
113
  	spin_unlock_bh(&hsr->list_lock);
6caabe7f1   Mao Wenan   net: hsr: fix mem...
114
  }
f421436a5   Arvid Brodin   net/hsr: Add supp...
115

b9a1e6274   Cong Wang   hsr: implement de...
116
117
118
119
120
121
122
123
  void hsr_del_nodes(struct list_head *node_db)
  {
  	struct hsr_node *node;
  	struct hsr_node *tmp;
  
  	list_for_each_entry_safe(node, tmp, node_db, mac_list)
  		kfree(node);
  }
b1b4aa914   Murali Karicheri   net: hsr: remove ...
124
  /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A;
f266a683a   Arvid Brodin   net/hsr: Better f...
125
126
   * seq_out is used to initialize filtering of outgoing duplicate frames
   * originating from the newly added node.
f421436a5   Arvid Brodin   net/hsr: Add supp...
127
   */
0fc906e60   Taehee Yoo   hsr: fix a race c...
128
129
130
131
  static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
  				     struct list_head *node_db,
  				     unsigned char addr[],
  				     u16 seq_out)
f421436a5   Arvid Brodin   net/hsr: Add supp...
132
  {
0fc906e60   Taehee Yoo   hsr: fix a race c...
133
  	struct hsr_node *new_node, *node;
f421436a5   Arvid Brodin   net/hsr: Add supp...
134
  	unsigned long now;
f266a683a   Arvid Brodin   net/hsr: Better f...
135
  	int i;
f421436a5   Arvid Brodin   net/hsr: Add supp...
136

0fc906e60   Taehee Yoo   hsr: fix a race c...
137
138
  	new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
  	if (!new_node)
f421436a5   Arvid Brodin   net/hsr: Add supp...
139
  		return NULL;
0fc906e60   Taehee Yoo   hsr: fix a race c...
140
  	ether_addr_copy(new_node->macaddress_A, addr);
f421436a5   Arvid Brodin   net/hsr: Add supp...
141
142
143
144
145
  
  	/* We are only interested in time diffs here, so use current jiffies
  	 * as initialization. (0 could trigger an spurious ring error warning).
  	 */
  	now = jiffies;
c5a759117   Arvid Brodin   net/hsr: Use list...
146
  	for (i = 0; i < HSR_PT_PORTS; i++)
0fc906e60   Taehee Yoo   hsr: fix a race c...
147
  		new_node->time_in[i] = now;
c5a759117   Arvid Brodin   net/hsr: Use list...
148
  	for (i = 0; i < HSR_PT_PORTS; i++)
0fc906e60   Taehee Yoo   hsr: fix a race c...
149
  		new_node->seq_out[i] = seq_out;
f421436a5   Arvid Brodin   net/hsr: Add supp...
150

0fc906e60   Taehee Yoo   hsr: fix a race c...
151
152
153
154
155
156
157
158
159
160
161
162
163
  	spin_lock_bh(&hsr->list_lock);
  	list_for_each_entry_rcu(node, node_db, mac_list) {
  		if (ether_addr_equal(node->macaddress_A, addr))
  			goto out;
  		if (ether_addr_equal(node->macaddress_B, addr))
  			goto out;
  	}
  	list_add_tail_rcu(&new_node->mac_list, node_db);
  	spin_unlock_bh(&hsr->list_lock);
  	return new_node;
  out:
  	spin_unlock_bh(&hsr->list_lock);
  	kfree(new_node);
f421436a5   Arvid Brodin   net/hsr: Add supp...
164
165
  	return node;
  }
f266a683a   Arvid Brodin   net/hsr: Better f...
166
167
  /* Get the hsr_node from which 'skb' was sent.
   */
675c8da04   Karicheri, Muralidharan   hsr: fix incorrec...
168
  struct hsr_node *hsr_get_node(struct hsr_port *port, struct sk_buff *skb,
f266a683a   Arvid Brodin   net/hsr: Better f...
169
170
  			      bool is_sup)
  {
675c8da04   Karicheri, Muralidharan   hsr: fix incorrec...
171
  	struct list_head *node_db = &port->hsr->node_db;
0fc906e60   Taehee Yoo   hsr: fix a race c...
172
  	struct hsr_priv *hsr = port->hsr;
f266a683a   Arvid Brodin   net/hsr: Better f...
173
174
175
176
177
178
  	struct hsr_node *node;
  	struct ethhdr *ethhdr;
  	u16 seq_out;
  
  	if (!skb_mac_header_was_set(skb))
  		return NULL;
5fa967780   Murali Karicheri   net: hsr: remove ...
179
  	ethhdr = (struct ethhdr *)skb_mac_header(skb);
f266a683a   Arvid Brodin   net/hsr: Better f...
180
181
  
  	list_for_each_entry_rcu(node, node_db, mac_list) {
b1b4aa914   Murali Karicheri   net: hsr: remove ...
182
  		if (ether_addr_equal(node->macaddress_A, ethhdr->h_source))
f266a683a   Arvid Brodin   net/hsr: Better f...
183
  			return node;
b1b4aa914   Murali Karicheri   net: hsr: remove ...
184
  		if (ether_addr_equal(node->macaddress_B, ethhdr->h_source))
f266a683a   Arvid Brodin   net/hsr: Better f...
185
186
  			return node;
  	}
ee1c27977   Peter Heise   net/hsr: Added su...
187
  	/* Everyone may create a node entry, connected node to a HSR device. */
f266a683a   Arvid Brodin   net/hsr: Better f...
188

059477830   Murali Karicheri   net: hsr: fix pla...
189
190
  	if (ethhdr->h_proto == htons(ETH_P_PRP) ||
  	    ethhdr->h_proto == htons(ETH_P_HSR)) {
f266a683a   Arvid Brodin   net/hsr: Better f...
191
192
193
194
195
  		/* Use the existing sequence_nr from the tag as starting point
  		 * for filtering duplicate frames.
  		 */
  		seq_out = hsr_get_skb_sequence_nr(skb) - 1;
  	} else {
675c8da04   Karicheri, Muralidharan   hsr: fix incorrec...
196
197
198
199
200
201
  		/* this is called also for frames from master port and
  		 * so warn only for non master ports
  		 */
  		if (port->type != HSR_PT_MASTER)
  			WARN_ONCE(1, "%s: Non-HSR frame
  ", __func__);
ee1c27977   Peter Heise   net/hsr: Added su...
202
  		seq_out = HSR_SEQNR_START;
f266a683a   Arvid Brodin   net/hsr: Better f...
203
  	}
0fc906e60   Taehee Yoo   hsr: fix a race c...
204
  	return hsr_add_node(hsr, node_db, ethhdr->h_source, seq_out);
f266a683a   Arvid Brodin   net/hsr: Better f...
205
  }
b1b4aa914   Murali Karicheri   net: hsr: remove ...
206
207
  /* Use the Supervision frame's info about an eventual macaddress_B for merging
   * nodes that has previously had their macaddress_B registered as a separate
f266a683a   Arvid Brodin   net/hsr: Better f...
208
209
210
211
212
   * node.
   */
  void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr,
  			  struct hsr_port *port_rcv)
  {
0fc906e60   Taehee Yoo   hsr: fix a race c...
213
  	struct hsr_priv *hsr = port_rcv->hsr;
f266a683a   Arvid Brodin   net/hsr: Better f...
214
  	struct hsr_sup_payload *hsr_sp;
0fc906e60   Taehee Yoo   hsr: fix a race c...
215
  	struct hsr_node *node_real;
f266a683a   Arvid Brodin   net/hsr: Better f...
216
  	struct list_head *node_db;
0fc906e60   Taehee Yoo   hsr: fix a race c...
217
  	struct ethhdr *ethhdr;
f266a683a   Arvid Brodin   net/hsr: Better f...
218
  	int i;
5fa967780   Murali Karicheri   net: hsr: remove ...
219
  	ethhdr = (struct ethhdr *)skb_mac_header(skb);
f266a683a   Arvid Brodin   net/hsr: Better f...
220

ee1c27977   Peter Heise   net/hsr: Added su...
221
222
223
224
225
226
227
228
229
  	/* Leave the ethernet header. */
  	skb_pull(skb, sizeof(struct ethhdr));
  
  	/* And leave the HSR tag. */
  	if (ethhdr->h_proto == htons(ETH_P_HSR))
  		skb_pull(skb, sizeof(struct hsr_tag));
  
  	/* And leave the HSR sup tag. */
  	skb_pull(skb, sizeof(struct hsr_sup_tag));
5fa967780   Murali Karicheri   net: hsr: remove ...
230
  	hsr_sp = (struct hsr_sup_payload *)skb->data;
f266a683a   Arvid Brodin   net/hsr: Better f...
231

b1b4aa914   Murali Karicheri   net: hsr: remove ...
232
  	/* Merge node_curr (registered on macaddress_B) into node_real */
f266a683a   Arvid Brodin   net/hsr: Better f...
233
  	node_db = &port_rcv->hsr->node_db;
b1b4aa914   Murali Karicheri   net: hsr: remove ...
234
  	node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
f266a683a   Arvid Brodin   net/hsr: Better f...
235
236
  	if (!node_real)
  		/* No frame received from AddrA of this node yet */
0fc906e60   Taehee Yoo   hsr: fix a race c...
237
  		node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
f266a683a   Arvid Brodin   net/hsr: Better f...
238
239
240
241
242
243
  					 HSR_SEQNR_START - 1);
  	if (!node_real)
  		goto done; /* No mem */
  	if (node_real == node_curr)
  		/* Node has already been merged */
  		goto done;
b1b4aa914   Murali Karicheri   net: hsr: remove ...
244
  	ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
f266a683a   Arvid Brodin   net/hsr: Better f...
245
246
247
248
  	for (i = 0; i < HSR_PT_PORTS; i++) {
  		if (!node_curr->time_in_stale[i] &&
  		    time_after(node_curr->time_in[i], node_real->time_in[i])) {
  			node_real->time_in[i] = node_curr->time_in[i];
d595b85a6   Murali Karicheri   net: hsr: fix lin...
249
250
  			node_real->time_in_stale[i] =
  						node_curr->time_in_stale[i];
f266a683a   Arvid Brodin   net/hsr: Better f...
251
252
253
254
  		}
  		if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
  			node_real->seq_out[i] = node_curr->seq_out[i];
  	}
b1b4aa914   Murali Karicheri   net: hsr: remove ...
255
  	node_real->addr_B_port = port_rcv->type;
f266a683a   Arvid Brodin   net/hsr: Better f...
256

0fc906e60   Taehee Yoo   hsr: fix a race c...
257
  	spin_lock_bh(&hsr->list_lock);
f266a683a   Arvid Brodin   net/hsr: Better f...
258
  	list_del_rcu(&node_curr->mac_list);
0fc906e60   Taehee Yoo   hsr: fix a race c...
259
  	spin_unlock_bh(&hsr->list_lock);
f266a683a   Arvid Brodin   net/hsr: Better f...
260
261
262
  	kfree_rcu(node_curr, rcu_head);
  
  done:
ee1c27977   Peter Heise   net/hsr: Added su...
263
  	skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
f266a683a   Arvid Brodin   net/hsr: Better f...
264
  }
f421436a5   Arvid Brodin   net/hsr: Add supp...
265
266
  /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
   *
f266a683a   Arvid Brodin   net/hsr: Better f...
267
   * If the frame was sent by a node's B interface, replace the source
b1b4aa914   Murali Karicheri   net: hsr: remove ...
268
   * address with that node's "official" address (macaddress_A) so that upper
f421436a5   Arvid Brodin   net/hsr: Add supp...
269
270
   * layers recognize where it came from.
   */
f266a683a   Arvid Brodin   net/hsr: Better f...
271
  void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
f421436a5   Arvid Brodin   net/hsr: Add supp...
272
  {
f421436a5   Arvid Brodin   net/hsr: Add supp...
273
274
275
276
277
  	if (!skb_mac_header_was_set(skb)) {
  		WARN_ONCE(1, "%s: Mac header not set
  ", __func__);
  		return;
  	}
f421436a5   Arvid Brodin   net/hsr: Add supp...
278

b1b4aa914   Murali Karicheri   net: hsr: remove ...
279
  	memcpy(&eth_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
f421436a5   Arvid Brodin   net/hsr: Add supp...
280
  }
f421436a5   Arvid Brodin   net/hsr: Add supp...
281
  /* 'skb' is a frame meant for another host.
f266a683a   Arvid Brodin   net/hsr: Better f...
282
   * 'port' is the outgoing interface
f421436a5   Arvid Brodin   net/hsr: Add supp...
283
284
285
286
287
288
289
   *
   * 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.
   */
f266a683a   Arvid Brodin   net/hsr: Better f...
290
  void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
c5a759117   Arvid Brodin   net/hsr: Use list...
291
  			 struct hsr_port *port)
f421436a5   Arvid Brodin   net/hsr: Add supp...
292
  {
f266a683a   Arvid Brodin   net/hsr: Better f...
293
  	struct hsr_node *node_dst;
f421436a5   Arvid Brodin   net/hsr: Add supp...
294

f266a683a   Arvid Brodin   net/hsr: Better f...
295
296
297
298
299
  	if (!skb_mac_header_was_set(skb)) {
  		WARN_ONCE(1, "%s: Mac header not set
  ", __func__);
  		return;
  	}
f421436a5   Arvid Brodin   net/hsr: Add supp...
300

f266a683a   Arvid Brodin   net/hsr: Better f...
301
302
  	if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
  		return;
f421436a5   Arvid Brodin   net/hsr: Add supp...
303

b1b4aa914   Murali Karicheri   net: hsr: remove ...
304
305
  	node_dst = find_node_by_addr_A(&port->hsr->node_db,
  				       eth_hdr(skb)->h_dest);
f266a683a   Arvid Brodin   net/hsr: Better f...
306
307
308
309
310
  	if (!node_dst) {
  		WARN_ONCE(1, "%s: Unknown node
  ", __func__);
  		return;
  	}
b1b4aa914   Murali Karicheri   net: hsr: remove ...
311
  	if (port->type != node_dst->addr_B_port)
f266a683a   Arvid Brodin   net/hsr: Better f...
312
  		return;
f421436a5   Arvid Brodin   net/hsr: Add supp...
313

b1b4aa914   Murali Karicheri   net: hsr: remove ...
314
  	ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
f421436a5   Arvid Brodin   net/hsr: Add supp...
315
  }
f421436a5   Arvid Brodin   net/hsr: Add supp...
316

f266a683a   Arvid Brodin   net/hsr: Better f...
317
318
  void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
  			   u16 sequence_nr)
f421436a5   Arvid Brodin   net/hsr: Add supp...
319
  {
f266a683a   Arvid Brodin   net/hsr: Better f...
320
321
322
323
324
325
  	/* Don't register incoming frames without a valid sequence number. This
  	 * ensures entries of restarted nodes gets pruned so that they can
  	 * re-register and resume communications.
  	 */
  	if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
  		return;
c5a759117   Arvid Brodin   net/hsr: Use list...
326
327
  	node->time_in[port->type] = jiffies;
  	node->time_in_stale[port->type] = false;
f421436a5   Arvid Brodin   net/hsr: Add supp...
328
  }
f421436a5   Arvid Brodin   net/hsr: Add supp...
329
330
331
332
333
334
335
336
  /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
   * ethhdr->h_source address and skb->mac_header set.
   *
   * Return:
   *	 1 if frame can be shown to have been sent recently on this interface,
   *	 0 otherwise, or
   *	 negative error code on error
   */
f266a683a   Arvid Brodin   net/hsr: Better f...
337
338
  int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
  			   u16 sequence_nr)
f421436a5   Arvid Brodin   net/hsr: Add supp...
339
  {
c5a759117   Arvid Brodin   net/hsr: Use list...
340
  	if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
f421436a5   Arvid Brodin   net/hsr: Add supp...
341
  		return 1;
c5a759117   Arvid Brodin   net/hsr: Use list...
342
  	node->seq_out[port->type] = sequence_nr;
f421436a5   Arvid Brodin   net/hsr: Add supp...
343
344
  	return 0;
  }
c5a759117   Arvid Brodin   net/hsr: Use list...
345
346
  static struct hsr_port *get_late_port(struct hsr_priv *hsr,
  				      struct hsr_node *node)
f421436a5   Arvid Brodin   net/hsr: Add supp...
347
  {
c5a759117   Arvid Brodin   net/hsr: Use list...
348
349
350
351
352
353
354
355
356
357
358
359
360
  	if (node->time_in_stale[HSR_PT_SLAVE_A])
  		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  	if (node->time_in_stale[HSR_PT_SLAVE_B])
  		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  
  	if (time_after(node->time_in[HSR_PT_SLAVE_B],
  		       node->time_in[HSR_PT_SLAVE_A] +
  					msecs_to_jiffies(MAX_SLAVE_DIFF)))
  		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  	if (time_after(node->time_in[HSR_PT_SLAVE_A],
  		       node->time_in[HSR_PT_SLAVE_B] +
  					msecs_to_jiffies(MAX_SLAVE_DIFF)))
  		return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
f421436a5   Arvid Brodin   net/hsr: Add supp...
361

c5a759117   Arvid Brodin   net/hsr: Use list...
362
  	return NULL;
f421436a5   Arvid Brodin   net/hsr: Add supp...
363
  }
f421436a5   Arvid Brodin   net/hsr: Add supp...
364
365
366
  /* Remove stale sequence_nr records. Called by timer every
   * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
   */
dda436b7a   Kees Cook   net: hsr: Convert...
367
  void hsr_prune_nodes(struct timer_list *t)
f421436a5   Arvid Brodin   net/hsr: Add supp...
368
  {
dda436b7a   Kees Cook   net: hsr: Convert...
369
  	struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
70ebe4a47   Arvid Brodin   net/hsr: Better v...
370
  	struct hsr_node *node;
0fc906e60   Taehee Yoo   hsr: fix a race c...
371
  	struct hsr_node *tmp;
c5a759117   Arvid Brodin   net/hsr: Use list...
372
  	struct hsr_port *port;
f421436a5   Arvid Brodin   net/hsr: Add supp...
373
374
  	unsigned long timestamp;
  	unsigned long time_a, time_b;
0fc906e60   Taehee Yoo   hsr: fix a race c...
375
376
  	spin_lock_bh(&hsr->list_lock);
  	list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
d2daa127e   Andreas Oetken   hsr: fix don't pr...
377
378
379
380
381
382
383
  		/* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A]
  		 * nor time_in[HSR_PT_SLAVE_B], will ever be updated for
  		 * the master port. Thus the master node will be repeatedly
  		 * pruned leading to packet loss.
  		 */
  		if (hsr_addr_is_self(hsr, node->macaddress_A))
  			continue;
f421436a5   Arvid Brodin   net/hsr: Add supp...
384
  		/* Shorthand */
c5a759117   Arvid Brodin   net/hsr: Use list...
385
386
  		time_a = node->time_in[HSR_PT_SLAVE_A];
  		time_b = node->time_in[HSR_PT_SLAVE_B];
f421436a5   Arvid Brodin   net/hsr: Add supp...
387
388
  
  		/* Check for timestamps old enough to risk wrap-around */
d131fcc69   Murali Karicheri   net: hsr: add mis...
389
  		if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
c5a759117   Arvid Brodin   net/hsr: Use list...
390
  			node->time_in_stale[HSR_PT_SLAVE_A] = true;
d131fcc69   Murali Karicheri   net: hsr: add mis...
391
  		if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
c5a759117   Arvid Brodin   net/hsr: Use list...
392
  			node->time_in_stale[HSR_PT_SLAVE_B] = true;
f421436a5   Arvid Brodin   net/hsr: Add supp...
393
394
395
396
397
398
  
  		/* Get age of newest frame from node.
  		 * At least one time_in is OK here; nodes get pruned long
  		 * before both time_ins can get stale
  		 */
  		timestamp = time_a;
c5a759117   Arvid Brodin   net/hsr: Use list...
399
400
  		if (node->time_in_stale[HSR_PT_SLAVE_A] ||
  		    (!node->time_in_stale[HSR_PT_SLAVE_B] &&
f421436a5   Arvid Brodin   net/hsr: Add supp...
401
402
403
404
405
  		    time_after(time_b, time_a)))
  			timestamp = time_b;
  
  		/* Warn of ring error only as long as we get frames at all */
  		if (time_is_after_jiffies(timestamp +
d131fcc69   Murali Karicheri   net: hsr: add mis...
406
  				msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
c5a759117   Arvid Brodin   net/hsr: Use list...
407
408
  			rcu_read_lock();
  			port = get_late_port(hsr, node);
05ca6e644   Murali Karicheri   net: hsr: fix NUL...
409
  			if (port)
b1b4aa914   Murali Karicheri   net: hsr: remove ...
410
  				hsr_nl_ringerror(hsr, node->macaddress_A, port);
c5a759117   Arvid Brodin   net/hsr: Use list...
411
  			rcu_read_unlock();
f421436a5   Arvid Brodin   net/hsr: Add supp...
412
413
414
415
  		}
  
  		/* Prune old entries */
  		if (time_is_before_jiffies(timestamp +
d595b85a6   Murali Karicheri   net: hsr: fix lin...
416
  				msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
b1b4aa914   Murali Karicheri   net: hsr: remove ...
417
  			hsr_nl_nodedown(hsr, node->macaddress_A);
f421436a5   Arvid Brodin   net/hsr: Add supp...
418
419
  			list_del_rcu(&node->mac_list);
  			/* Note that we need to free this entry later: */
1aee6cc2a   Wei Yongjun   net/hsr: using kf...
420
  			kfree_rcu(node, rcu_head);
f421436a5   Arvid Brodin   net/hsr: Add supp...
421
422
  		}
  	}
0fc906e60   Taehee Yoo   hsr: fix a race c...
423
  	spin_unlock_bh(&hsr->list_lock);
5150b45fd   Aaron Kramer   net: hsr: Fix nod...
424
425
426
427
  
  	/* Restart timer */
  	mod_timer(&hsr->prune_timer,
  		  jiffies + msecs_to_jiffies(PRUNE_PERIOD));
f421436a5   Arvid Brodin   net/hsr: Add supp...
428
  }
70ebe4a47   Arvid Brodin   net/hsr: Better v...
429
  void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
f421436a5   Arvid Brodin   net/hsr: Add supp...
430
431
  			unsigned char addr[ETH_ALEN])
  {
70ebe4a47   Arvid Brodin   net/hsr: Better v...
432
  	struct hsr_node *node;
f421436a5   Arvid Brodin   net/hsr: Add supp...
433
434
  
  	if (!_pos) {
70ebe4a47   Arvid Brodin   net/hsr: Better v...
435
436
  		node = list_first_or_null_rcu(&hsr->node_db,
  					      struct hsr_node, mac_list);
f421436a5   Arvid Brodin   net/hsr: Add supp...
437
  		if (node)
b1b4aa914   Murali Karicheri   net: hsr: remove ...
438
  			ether_addr_copy(addr, node->macaddress_A);
f421436a5   Arvid Brodin   net/hsr: Add supp...
439
440
441
442
  		return node;
  	}
  
  	node = _pos;
70ebe4a47   Arvid Brodin   net/hsr: Better v...
443
  	list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
b1b4aa914   Murali Karicheri   net: hsr: remove ...
444
  		ether_addr_copy(addr, node->macaddress_A);
f421436a5   Arvid Brodin   net/hsr: Add supp...
445
446
447
448
449
  		return node;
  	}
  
  	return NULL;
  }
70ebe4a47   Arvid Brodin   net/hsr: Better v...
450
  int hsr_get_node_data(struct hsr_priv *hsr,
f421436a5   Arvid Brodin   net/hsr: Add supp...
451
452
453
454
455
456
457
458
  		      const unsigned char *addr,
  		      unsigned char addr_b[ETH_ALEN],
  		      unsigned int *addr_b_ifindex,
  		      int *if1_age,
  		      u16 *if1_seq,
  		      int *if2_age,
  		      u16 *if2_seq)
  {
70ebe4a47   Arvid Brodin   net/hsr: Better v...
459
  	struct hsr_node *node;
c5a759117   Arvid Brodin   net/hsr: Use list...
460
  	struct hsr_port *port;
f421436a5   Arvid Brodin   net/hsr: Add supp...
461
  	unsigned long tdiff;
b1b4aa914   Murali Karicheri   net: hsr: remove ...
462
  	node = find_node_by_addr_A(&hsr->node_db, addr);
80aa1e38e   Taehee Yoo   hsr: use rcu_read...
463
464
  	if (!node)
  		return -ENOENT;
f421436a5   Arvid Brodin   net/hsr: Add supp...
465

b1b4aa914   Murali Karicheri   net: hsr: remove ...
466
  	ether_addr_copy(addr_b, node->macaddress_B);
f421436a5   Arvid Brodin   net/hsr: Add supp...
467

c5a759117   Arvid Brodin   net/hsr: Use list...
468
469
  	tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
  	if (node->time_in_stale[HSR_PT_SLAVE_A])
f421436a5   Arvid Brodin   net/hsr: Add supp...
470
471
472
473
474
475
476
  		*if1_age = INT_MAX;
  #if HZ <= MSEC_PER_SEC
  	else if (tdiff > msecs_to_jiffies(INT_MAX))
  		*if1_age = INT_MAX;
  #endif
  	else
  		*if1_age = jiffies_to_msecs(tdiff);
c5a759117   Arvid Brodin   net/hsr: Use list...
477
478
  	tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
  	if (node->time_in_stale[HSR_PT_SLAVE_B])
f421436a5   Arvid Brodin   net/hsr: Add supp...
479
480
481
482
483
484
485
486
487
  		*if2_age = INT_MAX;
  #if HZ <= MSEC_PER_SEC
  	else if (tdiff > msecs_to_jiffies(INT_MAX))
  		*if2_age = INT_MAX;
  #endif
  	else
  		*if2_age = jiffies_to_msecs(tdiff);
  
  	/* Present sequence numbers as if they were incoming on interface */
c5a759117   Arvid Brodin   net/hsr: Use list...
488
489
  	*if1_seq = node->seq_out[HSR_PT_SLAVE_B];
  	*if2_seq = node->seq_out[HSR_PT_SLAVE_A];
f421436a5   Arvid Brodin   net/hsr: Add supp...
490

b1b4aa914   Murali Karicheri   net: hsr: remove ...
491
492
  	if (node->addr_B_port != HSR_PT_NONE) {
  		port = hsr_port_get_hsr(hsr, node->addr_B_port);
c5a759117   Arvid Brodin   net/hsr: Use list...
493
494
  		*addr_b_ifindex = port->dev->ifindex;
  	} else {
f421436a5   Arvid Brodin   net/hsr: Add supp...
495
  		*addr_b_ifindex = -1;
c5a759117   Arvid Brodin   net/hsr: Use list...
496
  	}
f421436a5   Arvid Brodin   net/hsr: Add supp...
497

f421436a5   Arvid Brodin   net/hsr: Add supp...
498
499
  	return 0;
  }