hsr_framereg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /* Copyright 2011-2013 Autronica Fire and Security AS
  2. *
  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):
  9. * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
  10. *
  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. #include <linux/if_ether.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/slab.h>
  19. #include <linux/rculist.h>
  20. #include "hsr_main.h"
  21. #include "hsr_framereg.h"
  22. #include "hsr_netlink.h"
  23. struct node_entry {
  24. struct list_head mac_list;
  25. unsigned char MacAddressA[ETH_ALEN];
  26. unsigned char MacAddressB[ETH_ALEN];
  27. enum hsr_dev_idx AddrB_if; /* The local slave through which AddrB
  28. * frames are received from this node
  29. */
  30. unsigned long time_in[HSR_MAX_SLAVE];
  31. bool time_in_stale[HSR_MAX_SLAVE];
  32. u16 seq_out[HSR_MAX_DEV];
  33. struct rcu_head rcu_head;
  34. };
  35. /* TODO: use hash lists for mac addresses (linux/jhash.h)? */
  36. /* Search for mac entry. Caller must hold rcu read lock.
  37. */
  38. static struct node_entry *find_node_by_AddrA(struct list_head *node_db,
  39. const unsigned char addr[ETH_ALEN])
  40. {
  41. struct node_entry *node;
  42. list_for_each_entry_rcu(node, node_db, mac_list) {
  43. if (ether_addr_equal(node->MacAddressA, addr))
  44. return node;
  45. }
  46. return NULL;
  47. }
  48. /* Search for mac entry. Caller must hold rcu read lock.
  49. */
  50. static struct node_entry *find_node_by_AddrB(struct list_head *node_db,
  51. const unsigned char addr[ETH_ALEN])
  52. {
  53. struct node_entry *node;
  54. list_for_each_entry_rcu(node, node_db, mac_list) {
  55. if (ether_addr_equal(node->MacAddressB, addr))
  56. return node;
  57. }
  58. return NULL;
  59. }
  60. /* Search for mac entry. Caller must hold rcu read lock.
  61. */
  62. struct node_entry *hsr_find_node(struct list_head *node_db, struct sk_buff *skb)
  63. {
  64. struct node_entry *node;
  65. struct ethhdr *ethhdr;
  66. if (!skb_mac_header_was_set(skb))
  67. return NULL;
  68. ethhdr = (struct ethhdr *) skb_mac_header(skb);
  69. list_for_each_entry_rcu(node, node_db, mac_list) {
  70. if (ether_addr_equal(node->MacAddressA, ethhdr->h_source))
  71. return node;
  72. if (ether_addr_equal(node->MacAddressB, ethhdr->h_source))
  73. return node;
  74. }
  75. return NULL;
  76. }
  77. /* Helper for device init; the self_node_db 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 list_head *self_node_db,
  81. unsigned char addr_a[ETH_ALEN],
  82. unsigned char addr_b[ETH_ALEN])
  83. {
  84. struct node_entry *node, *oldnode;
  85. node = kmalloc(sizeof(*node), GFP_KERNEL);
  86. if (!node)
  87. return -ENOMEM;
  88. ether_addr_copy(node->MacAddressA, addr_a);
  89. ether_addr_copy(node->MacAddressB, addr_b);
  90. rcu_read_lock();
  91. oldnode = list_first_or_null_rcu(self_node_db,
  92. struct node_entry, mac_list);
  93. if (oldnode) {
  94. list_replace_rcu(&oldnode->mac_list, &node->mac_list);
  95. rcu_read_unlock();
  96. synchronize_rcu();
  97. kfree(oldnode);
  98. } else {
  99. rcu_read_unlock();
  100. list_add_tail_rcu(&node->mac_list, self_node_db);
  101. }
  102. return 0;
  103. }
  104. /* Add/merge node to the database of nodes. 'skb' must contain an HSR
  105. * supervision frame.
  106. * - If the supervision header's MacAddressA field is not yet in the database,
  107. * this frame is from an hitherto unknown node - add it to the database.
  108. * - If the sender's MAC address is not the same as its MacAddressA address,
  109. * the node is using PICS_SUBS (address substitution). Record the sender's
  110. * address as the node's MacAddressB.
  111. *
  112. * This function needs to work even if the sender node has changed one of its
  113. * slaves' MAC addresses. In this case, there are four different cases described
  114. * by (Addr-changed, received-from) pairs as follows. Note that changing the
  115. * SlaveA address is equal to changing the node's own address:
  116. *
  117. * - (AddrB, SlaveB): The new AddrB will be recorded by PICS_SUBS code since
  118. * node == NULL.
  119. * - (AddrB, SlaveA): Will work as usual (the AddrB change won't be detected
  120. * from this frame).
  121. *
  122. * - (AddrA, SlaveB): The old node will be found. We need to detect this and
  123. * remove the node.
  124. * - (AddrA, SlaveA): A new node will be registered (non-PICS_SUBS at first).
  125. * The old one will be pruned after HSR_NODE_FORGET_TIME.
  126. *
  127. * We also need to detect if the sender's SlaveA and SlaveB cables have been
  128. * swapped.
  129. */
  130. struct node_entry *hsr_merge_node(struct hsr_priv *hsr_priv,
  131. struct node_entry *node,
  132. struct sk_buff *skb,
  133. enum hsr_dev_idx dev_idx)
  134. {
  135. struct hsr_sup_payload *hsr_sp;
  136. struct hsr_ethhdr_sp *hsr_ethsup;
  137. int i;
  138. unsigned long now;
  139. hsr_ethsup = (struct hsr_ethhdr_sp *) skb_mac_header(skb);
  140. hsr_sp = (struct hsr_sup_payload *) skb->data;
  141. if (node && !ether_addr_equal(node->MacAddressA, hsr_sp->MacAddressA)) {
  142. /* Node has changed its AddrA, frame was received from SlaveB */
  143. list_del_rcu(&node->mac_list);
  144. kfree_rcu(node, rcu_head);
  145. node = NULL;
  146. }
  147. if (node && (dev_idx == node->AddrB_if) &&
  148. !ether_addr_equal(node->MacAddressB, hsr_ethsup->ethhdr.h_source)) {
  149. /* Cables have been swapped */
  150. list_del_rcu(&node->mac_list);
  151. kfree_rcu(node, rcu_head);
  152. node = NULL;
  153. }
  154. if (node && (dev_idx != node->AddrB_if) &&
  155. (node->AddrB_if != HSR_DEV_NONE) &&
  156. !ether_addr_equal(node->MacAddressA, hsr_ethsup->ethhdr.h_source)) {
  157. /* Cables have been swapped */
  158. list_del_rcu(&node->mac_list);
  159. kfree_rcu(node, rcu_head);
  160. node = NULL;
  161. }
  162. if (node)
  163. return node;
  164. node = find_node_by_AddrA(&hsr_priv->node_db, hsr_sp->MacAddressA);
  165. if (node) {
  166. /* Node is known, but frame was received from an unknown
  167. * address. Node is PICS_SUBS capable; merge its AddrB.
  168. */
  169. ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source);
  170. node->AddrB_if = dev_idx;
  171. return node;
  172. }
  173. node = kzalloc(sizeof(*node), GFP_ATOMIC);
  174. if (!node)
  175. return NULL;
  176. ether_addr_copy(node->MacAddressA, hsr_sp->MacAddressA);
  177. ether_addr_copy(node->MacAddressB, hsr_ethsup->ethhdr.h_source);
  178. if (!ether_addr_equal(hsr_sp->MacAddressA, hsr_ethsup->ethhdr.h_source))
  179. node->AddrB_if = dev_idx;
  180. else
  181. node->AddrB_if = HSR_DEV_NONE;
  182. /* We are only interested in time diffs here, so use current jiffies
  183. * as initialization. (0 could trigger an spurious ring error warning).
  184. */
  185. now = jiffies;
  186. for (i = 0; i < HSR_MAX_SLAVE; i++)
  187. node->time_in[i] = now;
  188. for (i = 0; i < HSR_MAX_DEV; i++)
  189. node->seq_out[i] = ntohs(hsr_ethsup->hsr_sup.sequence_nr) - 1;
  190. list_add_tail_rcu(&node->mac_list, &hsr_priv->node_db);
  191. return node;
  192. }
  193. /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
  194. *
  195. * If the frame was sent by a node's B interface, replace the sender
  196. * address with that node's "official" address (MacAddressA) so that upper
  197. * layers recognize where it came from.
  198. */
  199. void hsr_addr_subst_source(struct hsr_priv *hsr_priv, struct sk_buff *skb)
  200. {
  201. struct ethhdr *ethhdr;
  202. struct node_entry *node;
  203. if (!skb_mac_header_was_set(skb)) {
  204. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  205. return;
  206. }
  207. ethhdr = (struct ethhdr *) skb_mac_header(skb);
  208. rcu_read_lock();
  209. node = find_node_by_AddrB(&hsr_priv->node_db, ethhdr->h_source);
  210. if (node)
  211. ether_addr_copy(ethhdr->h_source, node->MacAddressA);
  212. rcu_read_unlock();
  213. }
  214. /* 'skb' is a frame meant for another host.
  215. * 'hsr_dev_idx' is the HSR index of the outgoing device
  216. *
  217. * Substitute the target (dest) MAC address if necessary, so the it matches the
  218. * recipient interface MAC address, regardless of whether that is the
  219. * recipient's A or B interface.
  220. * This is needed to keep the packets flowing through switches that learn on
  221. * which "side" the different interfaces are.
  222. */
  223. void hsr_addr_subst_dest(struct hsr_priv *hsr_priv, struct ethhdr *ethhdr,
  224. enum hsr_dev_idx dev_idx)
  225. {
  226. struct node_entry *node;
  227. rcu_read_lock();
  228. node = find_node_by_AddrA(&hsr_priv->node_db, ethhdr->h_dest);
  229. if (node && (node->AddrB_if == dev_idx))
  230. ether_addr_copy(ethhdr->h_dest, node->MacAddressB);
  231. rcu_read_unlock();
  232. }
  233. /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
  234. * false otherwise.
  235. */
  236. static bool seq_nr_after(u16 a, u16 b)
  237. {
  238. /* Remove inconsistency where
  239. * seq_nr_after(a, b) == seq_nr_before(a, b)
  240. */
  241. if ((int) b - a == 32768)
  242. return false;
  243. return (((s16) (b - a)) < 0);
  244. }
  245. #define seq_nr_before(a, b) seq_nr_after((b), (a))
  246. #define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
  247. #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
  248. void hsr_register_frame_in(struct node_entry *node, enum hsr_dev_idx dev_idx)
  249. {
  250. if ((dev_idx < 0) || (dev_idx >= HSR_MAX_SLAVE)) {
  251. WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx);
  252. return;
  253. }
  254. node->time_in[dev_idx] = jiffies;
  255. node->time_in_stale[dev_idx] = false;
  256. }
  257. /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
  258. * ethhdr->h_source address and skb->mac_header set.
  259. *
  260. * Return:
  261. * 1 if frame can be shown to have been sent recently on this interface,
  262. * 0 otherwise, or
  263. * negative error code on error
  264. */
  265. int hsr_register_frame_out(struct node_entry *node, enum hsr_dev_idx dev_idx,
  266. struct sk_buff *skb)
  267. {
  268. struct hsr_ethhdr *hsr_ethhdr;
  269. u16 sequence_nr;
  270. if ((dev_idx < 0) || (dev_idx >= HSR_MAX_DEV)) {
  271. WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx);
  272. return -EINVAL;
  273. }
  274. if (!skb_mac_header_was_set(skb)) {
  275. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  276. return -EINVAL;
  277. }
  278. hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb);
  279. sequence_nr = ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
  280. if (seq_nr_before_or_eq(sequence_nr, node->seq_out[dev_idx]))
  281. return 1;
  282. node->seq_out[dev_idx] = sequence_nr;
  283. return 0;
  284. }
  285. static bool is_late(struct node_entry *node, enum hsr_dev_idx dev_idx)
  286. {
  287. enum hsr_dev_idx other;
  288. if (node->time_in_stale[dev_idx])
  289. return true;
  290. if (dev_idx == HSR_DEV_SLAVE_A)
  291. other = HSR_DEV_SLAVE_B;
  292. else
  293. other = HSR_DEV_SLAVE_A;
  294. if (node->time_in_stale[other])
  295. return false;
  296. if (time_after(node->time_in[other], node->time_in[dev_idx] +
  297. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  298. return true;
  299. return false;
  300. }
  301. /* Remove stale sequence_nr records. Called by timer every
  302. * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
  303. */
  304. void hsr_prune_nodes(struct hsr_priv *hsr_priv)
  305. {
  306. struct node_entry *node;
  307. unsigned long timestamp;
  308. unsigned long time_a, time_b;
  309. rcu_read_lock();
  310. list_for_each_entry_rcu(node, &hsr_priv->node_db, mac_list) {
  311. /* Shorthand */
  312. time_a = node->time_in[HSR_DEV_SLAVE_A];
  313. time_b = node->time_in[HSR_DEV_SLAVE_B];
  314. /* Check for timestamps old enough to risk wrap-around */
  315. if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2))
  316. node->time_in_stale[HSR_DEV_SLAVE_A] = true;
  317. if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2))
  318. node->time_in_stale[HSR_DEV_SLAVE_B] = true;
  319. /* Get age of newest frame from node.
  320. * At least one time_in is OK here; nodes get pruned long
  321. * before both time_ins can get stale
  322. */
  323. timestamp = time_a;
  324. if (node->time_in_stale[HSR_DEV_SLAVE_A] ||
  325. (!node->time_in_stale[HSR_DEV_SLAVE_B] &&
  326. time_after(time_b, time_a)))
  327. timestamp = time_b;
  328. /* Warn of ring error only as long as we get frames at all */
  329. if (time_is_after_jiffies(timestamp +
  330. msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) {
  331. if (is_late(node, HSR_DEV_SLAVE_A))
  332. hsr_nl_ringerror(hsr_priv, node->MacAddressA,
  333. HSR_DEV_SLAVE_A);
  334. else if (is_late(node, HSR_DEV_SLAVE_B))
  335. hsr_nl_ringerror(hsr_priv, node->MacAddressA,
  336. HSR_DEV_SLAVE_B);
  337. }
  338. /* Prune old entries */
  339. if (time_is_before_jiffies(timestamp +
  340. msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
  341. hsr_nl_nodedown(hsr_priv, node->MacAddressA);
  342. list_del_rcu(&node->mac_list);
  343. /* Note that we need to free this entry later: */
  344. kfree_rcu(node, rcu_head);
  345. }
  346. }
  347. rcu_read_unlock();
  348. }
  349. void *hsr_get_next_node(struct hsr_priv *hsr_priv, void *_pos,
  350. unsigned char addr[ETH_ALEN])
  351. {
  352. struct node_entry *node;
  353. if (!_pos) {
  354. node = list_first_or_null_rcu(&hsr_priv->node_db,
  355. struct node_entry, mac_list);
  356. if (node)
  357. ether_addr_copy(addr, node->MacAddressA);
  358. return node;
  359. }
  360. node = _pos;
  361. list_for_each_entry_continue_rcu(node, &hsr_priv->node_db, mac_list) {
  362. ether_addr_copy(addr, node->MacAddressA);
  363. return node;
  364. }
  365. return NULL;
  366. }
  367. int hsr_get_node_data(struct hsr_priv *hsr_priv,
  368. const unsigned char *addr,
  369. unsigned char addr_b[ETH_ALEN],
  370. unsigned int *addr_b_ifindex,
  371. int *if1_age,
  372. u16 *if1_seq,
  373. int *if2_age,
  374. u16 *if2_seq)
  375. {
  376. struct node_entry *node;
  377. unsigned long tdiff;
  378. rcu_read_lock();
  379. node = find_node_by_AddrA(&hsr_priv->node_db, addr);
  380. if (!node) {
  381. rcu_read_unlock();
  382. return -ENOENT; /* No such entry */
  383. }
  384. ether_addr_copy(addr_b, node->MacAddressB);
  385. tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_A];
  386. if (node->time_in_stale[HSR_DEV_SLAVE_A])
  387. *if1_age = INT_MAX;
  388. #if HZ <= MSEC_PER_SEC
  389. else if (tdiff > msecs_to_jiffies(INT_MAX))
  390. *if1_age = INT_MAX;
  391. #endif
  392. else
  393. *if1_age = jiffies_to_msecs(tdiff);
  394. tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_B];
  395. if (node->time_in_stale[HSR_DEV_SLAVE_B])
  396. *if2_age = INT_MAX;
  397. #if HZ <= MSEC_PER_SEC
  398. else if (tdiff > msecs_to_jiffies(INT_MAX))
  399. *if2_age = INT_MAX;
  400. #endif
  401. else
  402. *if2_age = jiffies_to_msecs(tdiff);
  403. /* Present sequence numbers as if they were incoming on interface */
  404. *if1_seq = node->seq_out[HSR_DEV_SLAVE_B];
  405. *if2_seq = node->seq_out[HSR_DEV_SLAVE_A];
  406. if ((node->AddrB_if != HSR_DEV_NONE) && hsr_priv->slave[node->AddrB_if])
  407. *addr_b_ifindex = hsr_priv->slave[node->AddrB_if]->ifindex;
  408. else
  409. *addr_b_ifindex = -1;
  410. rcu_read_unlock();
  411. return 0;
  412. }