hsr_framereg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. if ((int) b - a == 32768)
  245. return false;
  246. return (((s16) (b - a)) < 0);
  247. }
  248. #define seq_nr_before(a, b) seq_nr_after((b), (a))
  249. #define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
  250. #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
  251. void hsr_register_frame_in(struct node_entry *node, enum hsr_dev_idx dev_idx)
  252. {
  253. if ((dev_idx < 0) || (dev_idx >= HSR_MAX_DEV)) {
  254. WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx);
  255. return;
  256. }
  257. node->time_in[dev_idx] = jiffies;
  258. node->time_in_stale[dev_idx] = false;
  259. }
  260. /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
  261. * ethhdr->h_source address and skb->mac_header set.
  262. *
  263. * Return:
  264. * 1 if frame can be shown to have been sent recently on this interface,
  265. * 0 otherwise, or
  266. * negative error code on error
  267. */
  268. int hsr_register_frame_out(struct node_entry *node, enum hsr_dev_idx dev_idx,
  269. struct sk_buff *skb)
  270. {
  271. struct hsr_ethhdr *hsr_ethhdr;
  272. u16 sequence_nr;
  273. if ((dev_idx < 0) || (dev_idx >= HSR_MAX_DEV)) {
  274. WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx);
  275. return -EINVAL;
  276. }
  277. if (!skb_mac_header_was_set(skb)) {
  278. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  279. return -EINVAL;
  280. }
  281. hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb);
  282. sequence_nr = ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
  283. if (seq_nr_before_or_eq(sequence_nr, node->seq_out[dev_idx]))
  284. return 1;
  285. node->seq_out[dev_idx] = sequence_nr;
  286. return 0;
  287. }
  288. static bool is_late(struct node_entry *node, enum hsr_dev_idx dev_idx)
  289. {
  290. enum hsr_dev_idx other;
  291. if (node->time_in_stale[dev_idx])
  292. return true;
  293. if (dev_idx == HSR_DEV_SLAVE_A)
  294. other = HSR_DEV_SLAVE_B;
  295. else
  296. other = HSR_DEV_SLAVE_A;
  297. if (node->time_in_stale[other])
  298. return false;
  299. if (time_after(node->time_in[other], node->time_in[dev_idx] +
  300. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  301. return true;
  302. return false;
  303. }
  304. /* Remove stale sequence_nr records. Called by timer every
  305. * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
  306. */
  307. void hsr_prune_nodes(struct hsr_priv *hsr_priv)
  308. {
  309. struct node_entry *node;
  310. unsigned long timestamp;
  311. unsigned long time_a, time_b;
  312. rcu_read_lock();
  313. list_for_each_entry_rcu(node, &hsr_priv->node_db, mac_list) {
  314. /* Shorthand */
  315. time_a = node->time_in[HSR_DEV_SLAVE_A];
  316. time_b = node->time_in[HSR_DEV_SLAVE_B];
  317. /* Check for timestamps old enough to risk wrap-around */
  318. if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2))
  319. node->time_in_stale[HSR_DEV_SLAVE_A] = true;
  320. if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2))
  321. node->time_in_stale[HSR_DEV_SLAVE_B] = true;
  322. /* Get age of newest frame from node.
  323. * At least one time_in is OK here; nodes get pruned long
  324. * before both time_ins can get stale
  325. */
  326. timestamp = time_a;
  327. if (node->time_in_stale[HSR_DEV_SLAVE_A] ||
  328. (!node->time_in_stale[HSR_DEV_SLAVE_B] &&
  329. time_after(time_b, time_a)))
  330. timestamp = time_b;
  331. /* Warn of ring error only as long as we get frames at all */
  332. if (time_is_after_jiffies(timestamp +
  333. msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) {
  334. if (is_late(node, HSR_DEV_SLAVE_A))
  335. hsr_nl_ringerror(hsr_priv, node->MacAddressA,
  336. HSR_DEV_SLAVE_A);
  337. else if (is_late(node, HSR_DEV_SLAVE_B))
  338. hsr_nl_ringerror(hsr_priv, node->MacAddressA,
  339. HSR_DEV_SLAVE_B);
  340. }
  341. /* Prune old entries */
  342. if (time_is_before_jiffies(timestamp +
  343. msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
  344. hsr_nl_nodedown(hsr_priv, node->MacAddressA);
  345. list_del_rcu(&node->mac_list);
  346. /* Note that we need to free this entry later: */
  347. call_rcu(&node->rcu_head, node_entry_reclaim);
  348. }
  349. }
  350. rcu_read_unlock();
  351. }
  352. void *hsr_get_next_node(struct hsr_priv *hsr_priv, void *_pos,
  353. unsigned char addr[ETH_ALEN])
  354. {
  355. struct node_entry *node;
  356. if (!_pos) {
  357. node = list_first_or_null_rcu(&hsr_priv->node_db,
  358. struct node_entry, mac_list);
  359. if (node)
  360. memcpy(addr, node->MacAddressA, ETH_ALEN);
  361. return node;
  362. }
  363. node = _pos;
  364. list_for_each_entry_continue_rcu(node, &hsr_priv->node_db, mac_list) {
  365. memcpy(addr, node->MacAddressA, ETH_ALEN);
  366. return node;
  367. }
  368. return NULL;
  369. }
  370. int hsr_get_node_data(struct hsr_priv *hsr_priv,
  371. const unsigned char *addr,
  372. unsigned char addr_b[ETH_ALEN],
  373. unsigned int *addr_b_ifindex,
  374. int *if1_age,
  375. u16 *if1_seq,
  376. int *if2_age,
  377. u16 *if2_seq)
  378. {
  379. struct node_entry *node;
  380. unsigned long tdiff;
  381. rcu_read_lock();
  382. node = find_node_by_AddrA(&hsr_priv->node_db, addr);
  383. if (!node) {
  384. rcu_read_unlock();
  385. return -ENOENT; /* No such entry */
  386. }
  387. memcpy(addr_b, node->MacAddressB, ETH_ALEN);
  388. tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_A];
  389. if (node->time_in_stale[HSR_DEV_SLAVE_A])
  390. *if1_age = INT_MAX;
  391. #if HZ <= MSEC_PER_SEC
  392. else if (tdiff > msecs_to_jiffies(INT_MAX))
  393. *if1_age = INT_MAX;
  394. #endif
  395. else
  396. *if1_age = jiffies_to_msecs(tdiff);
  397. tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_B];
  398. if (node->time_in_stale[HSR_DEV_SLAVE_B])
  399. *if2_age = INT_MAX;
  400. #if HZ <= MSEC_PER_SEC
  401. else if (tdiff > msecs_to_jiffies(INT_MAX))
  402. *if2_age = INT_MAX;
  403. #endif
  404. else
  405. *if2_age = jiffies_to_msecs(tdiff);
  406. /* Present sequence numbers as if they were incoming on interface */
  407. *if1_seq = node->seq_out[HSR_DEV_SLAVE_B];
  408. *if2_seq = node->seq_out[HSR_DEV_SLAVE_A];
  409. if ((node->AddrB_if != HSR_DEV_NONE) && hsr_priv->slave[node->AddrB_if])
  410. *addr_b_ifindex = hsr_priv->slave[node->AddrB_if]->ifindex;
  411. else
  412. *addr_b_ifindex = -1;
  413. rcu_read_unlock();
  414. return 0;
  415. }