hsr_framereg.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright 2011-2014 Autronica Fire and Security AS
  3. *
  4. * Author(s):
  5. * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
  6. *
  7. * The HSR spec says never to forward the same frame twice on the same
  8. * interface. A frame is identified by its source MAC address and its HSR
  9. * sequence number. This code keeps track of senders and their sequence numbers
  10. * to allow filtering of duplicate frames, and to detect HSR ring errors.
  11. * Same code handles filtering of duplicates for PRP as well.
  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. /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
  21. * false otherwise.
  22. */
  23. static bool seq_nr_after(u16 a, u16 b)
  24. {
  25. /* Remove inconsistency where
  26. * seq_nr_after(a, b) == seq_nr_before(a, b)
  27. */
  28. if ((int)b - a == 32768)
  29. return false;
  30. return (((s16)(b - a)) < 0);
  31. }
  32. #define seq_nr_before(a, b) seq_nr_after((b), (a))
  33. #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
  34. #define PRP_DROP_WINDOW_LEN 32768
  35. bool hsr_addr_is_redbox(struct hsr_priv *hsr, unsigned char *addr)
  36. {
  37. if (!hsr->redbox || !is_valid_ether_addr(hsr->macaddress_redbox))
  38. return false;
  39. return ether_addr_equal(addr, hsr->macaddress_redbox);
  40. }
  41. bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
  42. {
  43. struct hsr_self_node *sn;
  44. bool ret = false;
  45. rcu_read_lock();
  46. sn = rcu_dereference(hsr->self_node);
  47. if (!sn) {
  48. WARN_ONCE(1, "HSR: No self node\n");
  49. goto out;
  50. }
  51. if (ether_addr_equal(addr, sn->macaddress_A) ||
  52. ether_addr_equal(addr, sn->macaddress_B))
  53. ret = true;
  54. out:
  55. rcu_read_unlock();
  56. return ret;
  57. }
  58. /* Search for mac entry. Caller must hold rcu read lock.
  59. */
  60. static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
  61. const unsigned char addr[ETH_ALEN])
  62. {
  63. struct hsr_node *node;
  64. list_for_each_entry_rcu(node, node_db, mac_list) {
  65. if (ether_addr_equal(node->macaddress_A, addr))
  66. return node;
  67. }
  68. return NULL;
  69. }
  70. /* Check if node for a given MAC address is already present in data base
  71. */
  72. bool hsr_is_node_in_db(struct list_head *node_db,
  73. const unsigned char addr[ETH_ALEN])
  74. {
  75. return !!find_node_by_addr_A(node_db, addr);
  76. }
  77. /* Helper for device init; the self_node is used in hsr_rcv() to recognize
  78. * frames from self that's been looped over the HSR ring.
  79. */
  80. int hsr_create_self_node(struct hsr_priv *hsr,
  81. const unsigned char addr_a[ETH_ALEN],
  82. const unsigned char addr_b[ETH_ALEN])
  83. {
  84. struct hsr_self_node *sn, *old;
  85. sn = kmalloc(sizeof(*sn), GFP_KERNEL);
  86. if (!sn)
  87. return -ENOMEM;
  88. ether_addr_copy(sn->macaddress_A, addr_a);
  89. ether_addr_copy(sn->macaddress_B, addr_b);
  90. spin_lock_bh(&hsr->list_lock);
  91. old = rcu_replace_pointer(hsr->self_node, sn,
  92. lockdep_is_held(&hsr->list_lock));
  93. spin_unlock_bh(&hsr->list_lock);
  94. if (old)
  95. kfree_rcu(old, rcu_head);
  96. return 0;
  97. }
  98. void hsr_del_self_node(struct hsr_priv *hsr)
  99. {
  100. struct hsr_self_node *old;
  101. spin_lock_bh(&hsr->list_lock);
  102. old = rcu_replace_pointer(hsr->self_node, NULL,
  103. lockdep_is_held(&hsr->list_lock));
  104. spin_unlock_bh(&hsr->list_lock);
  105. if (old)
  106. kfree_rcu(old, rcu_head);
  107. }
  108. void hsr_del_nodes(struct list_head *node_db)
  109. {
  110. struct hsr_node *node;
  111. struct hsr_node *tmp;
  112. list_for_each_entry_safe(node, tmp, node_db, mac_list)
  113. kfree(node);
  114. }
  115. void prp_handle_san_frame(bool san, enum hsr_port_type port,
  116. struct hsr_node *node)
  117. {
  118. /* Mark if the SAN node is over LAN_A or LAN_B */
  119. if (port == HSR_PT_SLAVE_A) {
  120. node->san_a = true;
  121. return;
  122. }
  123. if (port == HSR_PT_SLAVE_B)
  124. node->san_b = true;
  125. }
  126. /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A;
  127. * seq_out is used to initialize filtering of outgoing duplicate frames
  128. * originating from the newly added node.
  129. */
  130. static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
  131. struct list_head *node_db,
  132. unsigned char addr[],
  133. u16 seq_out, bool san,
  134. enum hsr_port_type rx_port)
  135. {
  136. struct hsr_node *new_node, *node;
  137. unsigned long now;
  138. int i;
  139. new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
  140. if (!new_node)
  141. return NULL;
  142. ether_addr_copy(new_node->macaddress_A, addr);
  143. spin_lock_init(&new_node->seq_out_lock);
  144. /* We are only interested in time diffs here, so use current jiffies
  145. * as initialization. (0 could trigger an spurious ring error warning).
  146. */
  147. now = jiffies;
  148. for (i = 0; i < HSR_PT_PORTS; i++) {
  149. new_node->time_in[i] = now;
  150. new_node->time_out[i] = now;
  151. }
  152. for (i = 0; i < HSR_PT_PORTS; i++) {
  153. new_node->seq_out[i] = seq_out;
  154. new_node->seq_expected[i] = seq_out + 1;
  155. new_node->seq_start[i] = seq_out + 1;
  156. }
  157. if (san && hsr->proto_ops->handle_san_frame)
  158. hsr->proto_ops->handle_san_frame(san, rx_port, new_node);
  159. spin_lock_bh(&hsr->list_lock);
  160. list_for_each_entry_rcu(node, node_db, mac_list,
  161. lockdep_is_held(&hsr->list_lock)) {
  162. if (ether_addr_equal(node->macaddress_A, addr))
  163. goto out;
  164. if (ether_addr_equal(node->macaddress_B, addr))
  165. goto out;
  166. }
  167. list_add_tail_rcu(&new_node->mac_list, node_db);
  168. spin_unlock_bh(&hsr->list_lock);
  169. return new_node;
  170. out:
  171. spin_unlock_bh(&hsr->list_lock);
  172. kfree(new_node);
  173. return node;
  174. }
  175. void prp_update_san_info(struct hsr_node *node, bool is_sup)
  176. {
  177. if (!is_sup)
  178. return;
  179. node->san_a = false;
  180. node->san_b = false;
  181. }
  182. /* Get the hsr_node from which 'skb' was sent.
  183. */
  184. struct hsr_node *hsr_get_node(struct hsr_port *port, struct list_head *node_db,
  185. struct sk_buff *skb, bool is_sup,
  186. enum hsr_port_type rx_port)
  187. {
  188. struct hsr_priv *hsr = port->hsr;
  189. struct hsr_node *node;
  190. struct ethhdr *ethhdr;
  191. struct prp_rct *rct;
  192. bool san = false;
  193. u16 seq_out;
  194. if (!skb_mac_header_was_set(skb))
  195. return NULL;
  196. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  197. list_for_each_entry_rcu(node, node_db, mac_list) {
  198. if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
  199. if (hsr->proto_ops->update_san_info)
  200. hsr->proto_ops->update_san_info(node, is_sup);
  201. return node;
  202. }
  203. if (ether_addr_equal(node->macaddress_B, ethhdr->h_source)) {
  204. if (hsr->proto_ops->update_san_info)
  205. hsr->proto_ops->update_san_info(node, is_sup);
  206. return node;
  207. }
  208. }
  209. /* Check if required node is not in proxy nodes table */
  210. list_for_each_entry_rcu(node, &hsr->proxy_node_db, mac_list) {
  211. if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
  212. if (hsr->proto_ops->update_san_info)
  213. hsr->proto_ops->update_san_info(node, is_sup);
  214. return node;
  215. }
  216. }
  217. /* Everyone may create a node entry, connected node to a HSR/PRP
  218. * device.
  219. */
  220. if (ethhdr->h_proto == htons(ETH_P_PRP) ||
  221. ethhdr->h_proto == htons(ETH_P_HSR)) {
  222. /* Check if skb contains hsr_ethhdr */
  223. if (skb->mac_len < sizeof(struct hsr_ethhdr))
  224. return NULL;
  225. /* Use the existing sequence_nr from the tag as starting point
  226. * for filtering duplicate frames.
  227. */
  228. seq_out = hsr_get_skb_sequence_nr(skb) - 1;
  229. } else {
  230. rct = skb_get_PRP_rct(skb);
  231. if (rct && prp_check_lsdu_size(skb, rct, is_sup)) {
  232. seq_out = prp_get_skb_sequence_nr(rct);
  233. } else {
  234. if (rx_port != HSR_PT_MASTER)
  235. san = true;
  236. seq_out = HSR_SEQNR_START;
  237. }
  238. }
  239. return hsr_add_node(hsr, node_db, ethhdr->h_source, seq_out,
  240. san, rx_port);
  241. }
  242. /* Use the Supervision frame's info about an eventual macaddress_B for merging
  243. * nodes that has previously had their macaddress_B registered as a separate
  244. * node.
  245. */
  246. void hsr_handle_sup_frame(struct hsr_frame_info *frame)
  247. {
  248. struct hsr_node *node_curr = frame->node_src;
  249. struct hsr_port *port_rcv = frame->port_rcv;
  250. struct hsr_priv *hsr = port_rcv->hsr;
  251. struct hsr_sup_payload *hsr_sp;
  252. struct hsr_sup_tlv *hsr_sup_tlv;
  253. struct hsr_node *node_real;
  254. struct sk_buff *skb = NULL;
  255. struct list_head *node_db;
  256. struct ethhdr *ethhdr;
  257. int i;
  258. unsigned int pull_size = 0;
  259. unsigned int total_pull_size = 0;
  260. /* Here either frame->skb_hsr or frame->skb_prp should be
  261. * valid as supervision frame always will have protocol
  262. * header info.
  263. */
  264. if (frame->skb_hsr)
  265. skb = frame->skb_hsr;
  266. else if (frame->skb_prp)
  267. skb = frame->skb_prp;
  268. else if (frame->skb_std)
  269. skb = frame->skb_std;
  270. if (!skb)
  271. return;
  272. /* Leave the ethernet header. */
  273. pull_size = sizeof(struct ethhdr);
  274. skb_pull(skb, pull_size);
  275. total_pull_size += pull_size;
  276. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  277. /* And leave the HSR tag. */
  278. if (ethhdr->h_proto == htons(ETH_P_HSR)) {
  279. pull_size = sizeof(struct hsr_tag);
  280. skb_pull(skb, pull_size);
  281. total_pull_size += pull_size;
  282. }
  283. /* And leave the HSR sup tag. */
  284. pull_size = sizeof(struct hsr_sup_tag);
  285. skb_pull(skb, pull_size);
  286. total_pull_size += pull_size;
  287. /* get HSR sup payload */
  288. hsr_sp = (struct hsr_sup_payload *)skb->data;
  289. /* Merge node_curr (registered on macaddress_B) into node_real */
  290. node_db = &port_rcv->hsr->node_db;
  291. node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
  292. if (!node_real)
  293. /* No frame received from AddrA of this node yet */
  294. node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
  295. HSR_SEQNR_START - 1, true,
  296. port_rcv->type);
  297. if (!node_real)
  298. goto done; /* No mem */
  299. if (node_real == node_curr)
  300. /* Node has already been merged */
  301. goto done;
  302. /* Leave the first HSR sup payload. */
  303. pull_size = sizeof(struct hsr_sup_payload);
  304. skb_pull(skb, pull_size);
  305. total_pull_size += pull_size;
  306. /* Get second supervision tlv */
  307. hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data;
  308. /* And check if it is a redbox mac TLV */
  309. if (hsr_sup_tlv->HSR_TLV_type == PRP_TLV_REDBOX_MAC) {
  310. /* We could stop here after pushing hsr_sup_payload,
  311. * or proceed and allow macaddress_B and for redboxes.
  312. */
  313. /* Sanity check length */
  314. if (hsr_sup_tlv->HSR_TLV_length != 6)
  315. goto done;
  316. /* Leave the second HSR sup tlv. */
  317. pull_size = sizeof(struct hsr_sup_tlv);
  318. skb_pull(skb, pull_size);
  319. total_pull_size += pull_size;
  320. /* Get redbox mac address. */
  321. hsr_sp = (struct hsr_sup_payload *)skb->data;
  322. /* Check if redbox mac and node mac are equal. */
  323. if (!ether_addr_equal(node_real->macaddress_A, hsr_sp->macaddress_A)) {
  324. /* This is a redbox supervision frame for a VDAN! */
  325. goto done;
  326. }
  327. }
  328. ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
  329. spin_lock_bh(&node_real->seq_out_lock);
  330. for (i = 0; i < HSR_PT_PORTS; i++) {
  331. if (!node_curr->time_in_stale[i] &&
  332. time_after(node_curr->time_in[i], node_real->time_in[i])) {
  333. node_real->time_in[i] = node_curr->time_in[i];
  334. node_real->time_in_stale[i] =
  335. node_curr->time_in_stale[i];
  336. }
  337. if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
  338. node_real->seq_out[i] = node_curr->seq_out[i];
  339. }
  340. spin_unlock_bh(&node_real->seq_out_lock);
  341. node_real->addr_B_port = port_rcv->type;
  342. spin_lock_bh(&hsr->list_lock);
  343. if (!node_curr->removed) {
  344. list_del_rcu(&node_curr->mac_list);
  345. node_curr->removed = true;
  346. kfree_rcu(node_curr, rcu_head);
  347. }
  348. spin_unlock_bh(&hsr->list_lock);
  349. done:
  350. /* Push back here */
  351. skb_push(skb, total_pull_size);
  352. }
  353. /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
  354. *
  355. * If the frame was sent by a node's B interface, replace the source
  356. * address with that node's "official" address (macaddress_A) so that upper
  357. * layers recognize where it came from.
  358. */
  359. void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
  360. {
  361. if (!skb_mac_header_was_set(skb)) {
  362. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  363. return;
  364. }
  365. memcpy(&eth_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
  366. }
  367. /* 'skb' is a frame meant for another host.
  368. * 'port' is the outgoing interface
  369. *
  370. * Substitute the target (dest) MAC address if necessary, so the it matches the
  371. * recipient interface MAC address, regardless of whether that is the
  372. * recipient's A or B interface.
  373. * This is needed to keep the packets flowing through switches that learn on
  374. * which "side" the different interfaces are.
  375. */
  376. void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
  377. struct hsr_port *port)
  378. {
  379. struct hsr_node *node_dst;
  380. if (!skb_mac_header_was_set(skb)) {
  381. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  382. return;
  383. }
  384. if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
  385. return;
  386. node_dst = find_node_by_addr_A(&port->hsr->node_db,
  387. eth_hdr(skb)->h_dest);
  388. if (!node_dst && port->hsr->redbox)
  389. node_dst = find_node_by_addr_A(&port->hsr->proxy_node_db,
  390. eth_hdr(skb)->h_dest);
  391. if (!node_dst) {
  392. if (port->hsr->prot_version != PRP_V1 && net_ratelimit())
  393. netdev_err(skb->dev, "%s: Unknown node\n", __func__);
  394. return;
  395. }
  396. if (port->type != node_dst->addr_B_port)
  397. return;
  398. if (is_valid_ether_addr(node_dst->macaddress_B))
  399. ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
  400. }
  401. void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
  402. u16 sequence_nr)
  403. {
  404. /* Don't register incoming frames without a valid sequence number. This
  405. * ensures entries of restarted nodes gets pruned so that they can
  406. * re-register and resume communications.
  407. */
  408. if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) &&
  409. seq_nr_before(sequence_nr, node->seq_out[port->type]))
  410. return;
  411. node->time_in[port->type] = jiffies;
  412. node->time_in_stale[port->type] = false;
  413. }
  414. /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
  415. * ethhdr->h_source address and skb->mac_header set.
  416. *
  417. * Return:
  418. * 1 if frame can be shown to have been sent recently on this interface,
  419. * 0 otherwise, or
  420. * negative error code on error
  421. */
  422. int hsr_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame)
  423. {
  424. struct hsr_node *node = frame->node_src;
  425. u16 sequence_nr = frame->sequence_nr;
  426. spin_lock_bh(&node->seq_out_lock);
  427. if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]) &&
  428. time_is_after_jiffies(node->time_out[port->type] +
  429. msecs_to_jiffies(HSR_ENTRY_FORGET_TIME))) {
  430. spin_unlock_bh(&node->seq_out_lock);
  431. return 1;
  432. }
  433. node->time_out[port->type] = jiffies;
  434. node->seq_out[port->type] = sequence_nr;
  435. spin_unlock_bh(&node->seq_out_lock);
  436. return 0;
  437. }
  438. /* Adaptation of the PRP duplicate discard algorithm described in wireshark
  439. * wiki (https://wiki.wireshark.org/PRP)
  440. *
  441. * A drop window is maintained for both LANs with start sequence set to the
  442. * first sequence accepted on the LAN that has not been seen on the other LAN,
  443. * and expected sequence set to the latest received sequence number plus one.
  444. *
  445. * When a frame is received on either LAN it is compared against the received
  446. * frames on the other LAN. If it is outside the drop window of the other LAN
  447. * the frame is accepted and the drop window is updated.
  448. * The drop window for the other LAN is reset.
  449. *
  450. * 'port' is the outgoing interface
  451. * 'frame' is the frame to be sent
  452. *
  453. * Return:
  454. * 1 if frame can be shown to have been sent recently on this interface,
  455. * 0 otherwise
  456. */
  457. int prp_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame)
  458. {
  459. enum hsr_port_type other_port;
  460. enum hsr_port_type rcv_port;
  461. struct hsr_node *node;
  462. u16 sequence_diff;
  463. u16 sequence_exp;
  464. u16 sequence_nr;
  465. /* out-going frames are always in order
  466. * and can be checked the same way as for HSR
  467. */
  468. if (frame->port_rcv->type == HSR_PT_MASTER)
  469. return hsr_register_frame_out(port, frame);
  470. /* for PRP we should only forward frames from the slave ports
  471. * to the master port
  472. */
  473. if (port->type != HSR_PT_MASTER)
  474. return 1;
  475. node = frame->node_src;
  476. sequence_nr = frame->sequence_nr;
  477. sequence_exp = sequence_nr + 1;
  478. rcv_port = frame->port_rcv->type;
  479. other_port = rcv_port == HSR_PT_SLAVE_A ? HSR_PT_SLAVE_B :
  480. HSR_PT_SLAVE_A;
  481. spin_lock_bh(&node->seq_out_lock);
  482. if (time_is_before_jiffies(node->time_out[port->type] +
  483. msecs_to_jiffies(HSR_ENTRY_FORGET_TIME)) ||
  484. (node->seq_start[rcv_port] == node->seq_expected[rcv_port] &&
  485. node->seq_start[other_port] == node->seq_expected[other_port])) {
  486. /* the node hasn't been sending for a while
  487. * or both drop windows are empty, forward the frame
  488. */
  489. node->seq_start[rcv_port] = sequence_nr;
  490. } else if (seq_nr_before(sequence_nr, node->seq_expected[other_port]) &&
  491. seq_nr_before_or_eq(node->seq_start[other_port], sequence_nr)) {
  492. /* drop the frame, update the drop window for the other port
  493. * and reset our drop window
  494. */
  495. node->seq_start[other_port] = sequence_exp;
  496. node->seq_expected[rcv_port] = sequence_exp;
  497. node->seq_start[rcv_port] = node->seq_expected[rcv_port];
  498. spin_unlock_bh(&node->seq_out_lock);
  499. return 1;
  500. }
  501. /* update the drop window for the port where this frame was received
  502. * and clear the drop window for the other port
  503. */
  504. node->seq_start[other_port] = node->seq_expected[other_port];
  505. node->seq_expected[rcv_port] = sequence_exp;
  506. sequence_diff = sequence_exp - node->seq_start[rcv_port];
  507. if (sequence_diff > PRP_DROP_WINDOW_LEN)
  508. node->seq_start[rcv_port] = sequence_exp - PRP_DROP_WINDOW_LEN;
  509. node->time_out[port->type] = jiffies;
  510. node->seq_out[port->type] = sequence_nr;
  511. spin_unlock_bh(&node->seq_out_lock);
  512. return 0;
  513. }
  514. static struct hsr_port *get_late_port(struct hsr_priv *hsr,
  515. struct hsr_node *node)
  516. {
  517. if (node->time_in_stale[HSR_PT_SLAVE_A])
  518. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  519. if (node->time_in_stale[HSR_PT_SLAVE_B])
  520. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  521. if (time_after(node->time_in[HSR_PT_SLAVE_B],
  522. node->time_in[HSR_PT_SLAVE_A] +
  523. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  524. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  525. if (time_after(node->time_in[HSR_PT_SLAVE_A],
  526. node->time_in[HSR_PT_SLAVE_B] +
  527. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  528. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  529. return NULL;
  530. }
  531. /* Remove stale sequence_nr records. Called by timer every
  532. * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
  533. */
  534. void hsr_prune_nodes(struct timer_list *t)
  535. {
  536. struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
  537. struct hsr_node *node;
  538. struct hsr_node *tmp;
  539. struct hsr_port *port;
  540. unsigned long timestamp;
  541. unsigned long time_a, time_b;
  542. spin_lock_bh(&hsr->list_lock);
  543. list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
  544. /* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A]
  545. * nor time_in[HSR_PT_SLAVE_B], will ever be updated for
  546. * the master port. Thus the master node will be repeatedly
  547. * pruned leading to packet loss.
  548. */
  549. if (hsr_addr_is_self(hsr, node->macaddress_A))
  550. continue;
  551. /* Shorthand */
  552. time_a = node->time_in[HSR_PT_SLAVE_A];
  553. time_b = node->time_in[HSR_PT_SLAVE_B];
  554. /* Check for timestamps old enough to risk wrap-around */
  555. if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
  556. node->time_in_stale[HSR_PT_SLAVE_A] = true;
  557. if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
  558. node->time_in_stale[HSR_PT_SLAVE_B] = true;
  559. /* Get age of newest frame from node.
  560. * At least one time_in is OK here; nodes get pruned long
  561. * before both time_ins can get stale
  562. */
  563. timestamp = time_a;
  564. if (node->time_in_stale[HSR_PT_SLAVE_A] ||
  565. (!node->time_in_stale[HSR_PT_SLAVE_B] &&
  566. time_after(time_b, time_a)))
  567. timestamp = time_b;
  568. /* Warn of ring error only as long as we get frames at all */
  569. if (time_is_after_jiffies(timestamp +
  570. msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
  571. rcu_read_lock();
  572. port = get_late_port(hsr, node);
  573. if (port)
  574. hsr_nl_ringerror(hsr, node->macaddress_A, port);
  575. rcu_read_unlock();
  576. }
  577. /* Prune old entries */
  578. if (time_is_before_jiffies(timestamp +
  579. msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
  580. hsr_nl_nodedown(hsr, node->macaddress_A);
  581. if (!node->removed) {
  582. list_del_rcu(&node->mac_list);
  583. node->removed = true;
  584. /* Note that we need to free this entry later: */
  585. kfree_rcu(node, rcu_head);
  586. }
  587. }
  588. }
  589. spin_unlock_bh(&hsr->list_lock);
  590. /* Restart timer */
  591. mod_timer(&hsr->prune_timer,
  592. jiffies + msecs_to_jiffies(PRUNE_PERIOD));
  593. }
  594. void hsr_prune_proxy_nodes(struct timer_list *t)
  595. {
  596. struct hsr_priv *hsr = from_timer(hsr, t, prune_proxy_timer);
  597. unsigned long timestamp;
  598. struct hsr_node *node;
  599. struct hsr_node *tmp;
  600. spin_lock_bh(&hsr->list_lock);
  601. list_for_each_entry_safe(node, tmp, &hsr->proxy_node_db, mac_list) {
  602. /* Don't prune RedBox node. */
  603. if (hsr_addr_is_redbox(hsr, node->macaddress_A))
  604. continue;
  605. timestamp = node->time_in[HSR_PT_INTERLINK];
  606. /* Prune old entries */
  607. if (time_is_before_jiffies(timestamp +
  608. msecs_to_jiffies(HSR_PROXY_NODE_FORGET_TIME))) {
  609. hsr_nl_nodedown(hsr, node->macaddress_A);
  610. if (!node->removed) {
  611. list_del_rcu(&node->mac_list);
  612. node->removed = true;
  613. /* Note that we need to free this entry later: */
  614. kfree_rcu(node, rcu_head);
  615. }
  616. }
  617. }
  618. spin_unlock_bh(&hsr->list_lock);
  619. /* Restart timer */
  620. mod_timer(&hsr->prune_proxy_timer,
  621. jiffies + msecs_to_jiffies(PRUNE_PROXY_PERIOD));
  622. }
  623. void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
  624. unsigned char addr[ETH_ALEN])
  625. {
  626. struct hsr_node *node;
  627. if (!_pos) {
  628. node = list_first_or_null_rcu(&hsr->node_db,
  629. struct hsr_node, mac_list);
  630. if (node)
  631. ether_addr_copy(addr, node->macaddress_A);
  632. return node;
  633. }
  634. node = _pos;
  635. list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
  636. ether_addr_copy(addr, node->macaddress_A);
  637. return node;
  638. }
  639. return NULL;
  640. }
  641. int hsr_get_node_data(struct hsr_priv *hsr,
  642. const unsigned char *addr,
  643. unsigned char addr_b[ETH_ALEN],
  644. unsigned int *addr_b_ifindex,
  645. int *if1_age,
  646. u16 *if1_seq,
  647. int *if2_age,
  648. u16 *if2_seq)
  649. {
  650. struct hsr_node *node;
  651. struct hsr_port *port;
  652. unsigned long tdiff;
  653. node = find_node_by_addr_A(&hsr->node_db, addr);
  654. if (!node)
  655. return -ENOENT;
  656. ether_addr_copy(addr_b, node->macaddress_B);
  657. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
  658. if (node->time_in_stale[HSR_PT_SLAVE_A])
  659. *if1_age = INT_MAX;
  660. #if HZ <= MSEC_PER_SEC
  661. else if (tdiff > msecs_to_jiffies(INT_MAX))
  662. *if1_age = INT_MAX;
  663. #endif
  664. else
  665. *if1_age = jiffies_to_msecs(tdiff);
  666. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
  667. if (node->time_in_stale[HSR_PT_SLAVE_B])
  668. *if2_age = INT_MAX;
  669. #if HZ <= MSEC_PER_SEC
  670. else if (tdiff > msecs_to_jiffies(INT_MAX))
  671. *if2_age = INT_MAX;
  672. #endif
  673. else
  674. *if2_age = jiffies_to_msecs(tdiff);
  675. /* Present sequence numbers as if they were incoming on interface */
  676. *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
  677. *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
  678. if (node->addr_B_port != HSR_PT_NONE) {
  679. port = hsr_port_get_hsr(hsr, node->addr_B_port);
  680. *addr_b_ifindex = port->dev->ifindex;
  681. } else {
  682. *addr_b_ifindex = -1;
  683. }
  684. return 0;
  685. }