hsr_framereg.c 14 KB

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