hsr_framereg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* Copyright 2011-2014 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-2014 Arvid Brodin, arvid.brodin@alten.se
  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 hsr_node {
  24. struct list_head mac_list;
  25. unsigned char MacAddressA[ETH_ALEN];
  26. unsigned char MacAddressB[ETH_ALEN];
  27. /* Local slave through which AddrB frames are received from this node */
  28. enum hsr_port_type AddrB_port;
  29. unsigned long time_in[HSR_PT_PORTS];
  30. bool time_in_stale[HSR_PT_PORTS];
  31. u16 seq_out[HSR_PT_PORTS];
  32. struct rcu_head rcu_head;
  33. };
  34. /* TODO: use hash lists for mac addresses (linux/jhash.h)? */
  35. /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
  36. * false otherwise.
  37. */
  38. static bool seq_nr_after(u16 a, u16 b)
  39. {
  40. /* Remove inconsistency where
  41. * seq_nr_after(a, b) == seq_nr_before(a, b)
  42. */
  43. if ((int) b - a == 32768)
  44. return false;
  45. return (((s16) (b - a)) < 0);
  46. }
  47. #define seq_nr_before(a, b) seq_nr_after((b), (a))
  48. #define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
  49. #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
  50. bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
  51. {
  52. struct hsr_node *node;
  53. node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node,
  54. mac_list);
  55. if (!node) {
  56. WARN_ONCE(1, "HSR: No self node\n");
  57. return false;
  58. }
  59. if (ether_addr_equal(addr, node->MacAddressA))
  60. return true;
  61. if (ether_addr_equal(addr, node->MacAddressB))
  62. return true;
  63. return false;
  64. }
  65. /* Search for mac entry. Caller must hold rcu read lock.
  66. */
  67. static struct hsr_node *find_node_by_AddrA(struct list_head *node_db,
  68. const unsigned char addr[ETH_ALEN])
  69. {
  70. struct hsr_node *node;
  71. list_for_each_entry_rcu(node, node_db, mac_list) {
  72. if (ether_addr_equal(node->MacAddressA, addr))
  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 hsr_node *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 hsr_node, 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. /* Allocate an hsr_node and add it to node_db. 'addr' is the node's AddressA;
  105. * seq_out is used to initialize filtering of outgoing duplicate frames
  106. * originating from the newly added node.
  107. */
  108. struct hsr_node *hsr_add_node(struct list_head *node_db, unsigned char addr[],
  109. u16 seq_out)
  110. {
  111. struct hsr_node *node;
  112. unsigned long now;
  113. int i;
  114. node = kzalloc(sizeof(*node), GFP_ATOMIC);
  115. if (!node)
  116. return NULL;
  117. ether_addr_copy(node->MacAddressA, addr);
  118. /* We are only interested in time diffs here, so use current jiffies
  119. * as initialization. (0 could trigger an spurious ring error warning).
  120. */
  121. now = jiffies;
  122. for (i = 0; i < HSR_PT_PORTS; i++)
  123. node->time_in[i] = now;
  124. for (i = 0; i < HSR_PT_PORTS; i++)
  125. node->seq_out[i] = seq_out;
  126. list_add_tail_rcu(&node->mac_list, node_db);
  127. return node;
  128. }
  129. /* Get the hsr_node from which 'skb' was sent.
  130. */
  131. struct hsr_node *hsr_get_node(struct list_head *node_db, struct sk_buff *skb,
  132. bool is_sup)
  133. {
  134. struct hsr_node *node;
  135. struct ethhdr *ethhdr;
  136. u16 seq_out;
  137. if (!skb_mac_header_was_set(skb))
  138. return NULL;
  139. ethhdr = (struct ethhdr *) skb_mac_header(skb);
  140. list_for_each_entry_rcu(node, node_db, mac_list) {
  141. if (ether_addr_equal(node->MacAddressA, ethhdr->h_source))
  142. return node;
  143. if (ether_addr_equal(node->MacAddressB, ethhdr->h_source))
  144. return node;
  145. }
  146. if (!is_sup)
  147. return NULL; /* Only supervision frame may create node entry */
  148. if (ethhdr->h_proto == htons(ETH_P_PRP)) {
  149. /* Use the existing sequence_nr from the tag as starting point
  150. * for filtering duplicate frames.
  151. */
  152. seq_out = hsr_get_skb_sequence_nr(skb) - 1;
  153. } else {
  154. WARN_ONCE(1, "%s: Non-HSR frame\n", __func__);
  155. seq_out = 0;
  156. }
  157. return hsr_add_node(node_db, ethhdr->h_source, seq_out);
  158. }
  159. /* Use the Supervision frame's info about an eventual MacAddressB for merging
  160. * nodes that has previously had their MacAddressB registered as a separate
  161. * node.
  162. */
  163. void hsr_handle_sup_frame(struct sk_buff *skb, struct hsr_node *node_curr,
  164. struct hsr_port *port_rcv)
  165. {
  166. struct hsr_node *node_real;
  167. struct hsr_sup_payload *hsr_sp;
  168. struct list_head *node_db;
  169. int i;
  170. skb_pull(skb, sizeof(struct hsr_ethhdr_sp));
  171. hsr_sp = (struct hsr_sup_payload *) skb->data;
  172. if (ether_addr_equal(eth_hdr(skb)->h_source, hsr_sp->MacAddressA))
  173. /* Not sent from MacAddressB of a PICS_SUBS capable node */
  174. goto done;
  175. /* Merge node_curr (registered on MacAddressB) into node_real */
  176. node_db = &port_rcv->hsr->node_db;
  177. node_real = find_node_by_AddrA(node_db, hsr_sp->MacAddressA);
  178. if (!node_real)
  179. /* No frame received from AddrA of this node yet */
  180. node_real = hsr_add_node(node_db, hsr_sp->MacAddressA,
  181. HSR_SEQNR_START - 1);
  182. if (!node_real)
  183. goto done; /* No mem */
  184. if (node_real == node_curr)
  185. /* Node has already been merged */
  186. goto done;
  187. ether_addr_copy(node_real->MacAddressB, eth_hdr(skb)->h_source);
  188. for (i = 0; i < HSR_PT_PORTS; i++) {
  189. if (!node_curr->time_in_stale[i] &&
  190. time_after(node_curr->time_in[i], node_real->time_in[i])) {
  191. node_real->time_in[i] = node_curr->time_in[i];
  192. node_real->time_in_stale[i] = node_curr->time_in_stale[i];
  193. }
  194. if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
  195. node_real->seq_out[i] = node_curr->seq_out[i];
  196. }
  197. node_real->AddrB_port = port_rcv->type;
  198. list_del_rcu(&node_curr->mac_list);
  199. kfree_rcu(node_curr, rcu_head);
  200. done:
  201. skb_push(skb, sizeof(struct hsr_ethhdr_sp));
  202. }
  203. /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
  204. *
  205. * If the frame was sent by a node's B interface, replace the source
  206. * address with that node's "official" address (MacAddressA) so that upper
  207. * layers recognize where it came from.
  208. */
  209. void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
  210. {
  211. if (!skb_mac_header_was_set(skb)) {
  212. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  213. return;
  214. }
  215. memcpy(&eth_hdr(skb)->h_source, node->MacAddressA, ETH_ALEN);
  216. }
  217. /* 'skb' is a frame meant for another host.
  218. * 'port' is the outgoing interface
  219. *
  220. * Substitute the target (dest) MAC address if necessary, so the it matches the
  221. * recipient interface MAC address, regardless of whether that is the
  222. * recipient's A or B interface.
  223. * This is needed to keep the packets flowing through switches that learn on
  224. * which "side" the different interfaces are.
  225. */
  226. void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
  227. struct hsr_port *port)
  228. {
  229. struct hsr_node *node_dst;
  230. if (!skb_mac_header_was_set(skb)) {
  231. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  232. return;
  233. }
  234. if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
  235. return;
  236. node_dst = find_node_by_AddrA(&port->hsr->node_db, eth_hdr(skb)->h_dest);
  237. if (!node_dst) {
  238. WARN_ONCE(1, "%s: Unknown node\n", __func__);
  239. return;
  240. }
  241. if (port->type != node_dst->AddrB_port)
  242. return;
  243. ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->MacAddressB);
  244. }
  245. void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
  246. u16 sequence_nr)
  247. {
  248. /* Don't register incoming frames without a valid sequence number. This
  249. * ensures entries of restarted nodes gets pruned so that they can
  250. * re-register and resume communications.
  251. */
  252. if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
  253. return;
  254. node->time_in[port->type] = jiffies;
  255. node->time_in_stale[port->type] = 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 hsr_port *port, struct hsr_node *node,
  266. u16 sequence_nr)
  267. {
  268. if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
  269. return 1;
  270. node->seq_out[port->type] = sequence_nr;
  271. return 0;
  272. }
  273. static struct hsr_port *get_late_port(struct hsr_priv *hsr,
  274. struct hsr_node *node)
  275. {
  276. if (node->time_in_stale[HSR_PT_SLAVE_A])
  277. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  278. if (node->time_in_stale[HSR_PT_SLAVE_B])
  279. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  280. if (time_after(node->time_in[HSR_PT_SLAVE_B],
  281. node->time_in[HSR_PT_SLAVE_A] +
  282. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  283. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  284. if (time_after(node->time_in[HSR_PT_SLAVE_A],
  285. node->time_in[HSR_PT_SLAVE_B] +
  286. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  287. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  288. return NULL;
  289. }
  290. /* Remove stale sequence_nr records. Called by timer every
  291. * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
  292. */
  293. void hsr_prune_nodes(unsigned long data)
  294. {
  295. struct hsr_priv *hsr;
  296. struct hsr_node *node;
  297. struct hsr_port *port;
  298. unsigned long timestamp;
  299. unsigned long time_a, time_b;
  300. hsr = (struct hsr_priv *) data;
  301. rcu_read_lock();
  302. list_for_each_entry_rcu(node, &hsr->node_db, mac_list) {
  303. /* Shorthand */
  304. time_a = node->time_in[HSR_PT_SLAVE_A];
  305. time_b = node->time_in[HSR_PT_SLAVE_B];
  306. /* Check for timestamps old enough to risk wrap-around */
  307. if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2))
  308. node->time_in_stale[HSR_PT_SLAVE_A] = true;
  309. if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2))
  310. node->time_in_stale[HSR_PT_SLAVE_B] = true;
  311. /* Get age of newest frame from node.
  312. * At least one time_in is OK here; nodes get pruned long
  313. * before both time_ins can get stale
  314. */
  315. timestamp = time_a;
  316. if (node->time_in_stale[HSR_PT_SLAVE_A] ||
  317. (!node->time_in_stale[HSR_PT_SLAVE_B] &&
  318. time_after(time_b, time_a)))
  319. timestamp = time_b;
  320. /* Warn of ring error only as long as we get frames at all */
  321. if (time_is_after_jiffies(timestamp +
  322. msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) {
  323. rcu_read_lock();
  324. port = get_late_port(hsr, node);
  325. if (port != NULL)
  326. hsr_nl_ringerror(hsr, node->MacAddressA, port);
  327. rcu_read_unlock();
  328. }
  329. /* Prune old entries */
  330. if (time_is_before_jiffies(timestamp +
  331. msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
  332. hsr_nl_nodedown(hsr, node->MacAddressA);
  333. list_del_rcu(&node->mac_list);
  334. /* Note that we need to free this entry later: */
  335. kfree_rcu(node, rcu_head);
  336. }
  337. }
  338. rcu_read_unlock();
  339. }
  340. void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
  341. unsigned char addr[ETH_ALEN])
  342. {
  343. struct hsr_node *node;
  344. if (!_pos) {
  345. node = list_first_or_null_rcu(&hsr->node_db,
  346. struct hsr_node, mac_list);
  347. if (node)
  348. ether_addr_copy(addr, node->MacAddressA);
  349. return node;
  350. }
  351. node = _pos;
  352. list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
  353. ether_addr_copy(addr, node->MacAddressA);
  354. return node;
  355. }
  356. return NULL;
  357. }
  358. int hsr_get_node_data(struct hsr_priv *hsr,
  359. const unsigned char *addr,
  360. unsigned char addr_b[ETH_ALEN],
  361. unsigned int *addr_b_ifindex,
  362. int *if1_age,
  363. u16 *if1_seq,
  364. int *if2_age,
  365. u16 *if2_seq)
  366. {
  367. struct hsr_node *node;
  368. struct hsr_port *port;
  369. unsigned long tdiff;
  370. rcu_read_lock();
  371. node = find_node_by_AddrA(&hsr->node_db, addr);
  372. if (!node) {
  373. rcu_read_unlock();
  374. return -ENOENT; /* No such entry */
  375. }
  376. ether_addr_copy(addr_b, node->MacAddressB);
  377. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
  378. if (node->time_in_stale[HSR_PT_SLAVE_A])
  379. *if1_age = INT_MAX;
  380. #if HZ <= MSEC_PER_SEC
  381. else if (tdiff > msecs_to_jiffies(INT_MAX))
  382. *if1_age = INT_MAX;
  383. #endif
  384. else
  385. *if1_age = jiffies_to_msecs(tdiff);
  386. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
  387. if (node->time_in_stale[HSR_PT_SLAVE_B])
  388. *if2_age = INT_MAX;
  389. #if HZ <= MSEC_PER_SEC
  390. else if (tdiff > msecs_to_jiffies(INT_MAX))
  391. *if2_age = INT_MAX;
  392. #endif
  393. else
  394. *if2_age = jiffies_to_msecs(tdiff);
  395. /* Present sequence numbers as if they were incoming on interface */
  396. *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
  397. *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
  398. if (node->AddrB_port != HSR_PT_NONE) {
  399. port = hsr_port_get_hsr(hsr, node->AddrB_port);
  400. *addr_b_ifindex = port->dev->ifindex;
  401. } else {
  402. *addr_b_ifindex = -1;
  403. }
  404. rcu_read_unlock();
  405. return 0;
  406. }