hsr_framereg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. /* Everyone may create a node entry, connected node to a HSR device. */
  147. if (ethhdr->h_proto == htons(ETH_P_PRP)
  148. || ethhdr->h_proto == htons(ETH_P_HSR)) {
  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 = HSR_SEQNR_START;
  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 ethhdr *ethhdr;
  167. struct hsr_node *node_real;
  168. struct hsr_sup_payload *hsr_sp;
  169. struct list_head *node_db;
  170. int i;
  171. ethhdr = (struct ethhdr *) skb_mac_header(skb);
  172. /* Leave the ethernet header. */
  173. skb_pull(skb, sizeof(struct ethhdr));
  174. /* And leave the HSR tag. */
  175. if (ethhdr->h_proto == htons(ETH_P_HSR))
  176. skb_pull(skb, sizeof(struct hsr_tag));
  177. /* And leave the HSR sup tag. */
  178. skb_pull(skb, sizeof(struct hsr_sup_tag));
  179. hsr_sp = (struct hsr_sup_payload *) skb->data;
  180. /* Merge node_curr (registered on MacAddressB) into node_real */
  181. node_db = &port_rcv->hsr->node_db;
  182. node_real = find_node_by_AddrA(node_db, hsr_sp->MacAddressA);
  183. if (!node_real)
  184. /* No frame received from AddrA of this node yet */
  185. node_real = hsr_add_node(node_db, hsr_sp->MacAddressA,
  186. HSR_SEQNR_START - 1);
  187. if (!node_real)
  188. goto done; /* No mem */
  189. if (node_real == node_curr)
  190. /* Node has already been merged */
  191. goto done;
  192. ether_addr_copy(node_real->MacAddressB, ethhdr->h_source);
  193. for (i = 0; i < HSR_PT_PORTS; i++) {
  194. if (!node_curr->time_in_stale[i] &&
  195. time_after(node_curr->time_in[i], node_real->time_in[i])) {
  196. node_real->time_in[i] = node_curr->time_in[i];
  197. node_real->time_in_stale[i] = node_curr->time_in_stale[i];
  198. }
  199. if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
  200. node_real->seq_out[i] = node_curr->seq_out[i];
  201. }
  202. node_real->AddrB_port = port_rcv->type;
  203. list_del_rcu(&node_curr->mac_list);
  204. kfree_rcu(node_curr, rcu_head);
  205. done:
  206. skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
  207. }
  208. /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
  209. *
  210. * If the frame was sent by a node's B interface, replace the source
  211. * address with that node's "official" address (MacAddressA) so that upper
  212. * layers recognize where it came from.
  213. */
  214. void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
  215. {
  216. if (!skb_mac_header_was_set(skb)) {
  217. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  218. return;
  219. }
  220. memcpy(&eth_hdr(skb)->h_source, node->MacAddressA, ETH_ALEN);
  221. }
  222. /* 'skb' is a frame meant for another host.
  223. * 'port' is the outgoing interface
  224. *
  225. * Substitute the target (dest) MAC address if necessary, so the it matches the
  226. * recipient interface MAC address, regardless of whether that is the
  227. * recipient's A or B interface.
  228. * This is needed to keep the packets flowing through switches that learn on
  229. * which "side" the different interfaces are.
  230. */
  231. void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
  232. struct hsr_port *port)
  233. {
  234. struct hsr_node *node_dst;
  235. if (!skb_mac_header_was_set(skb)) {
  236. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  237. return;
  238. }
  239. if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
  240. return;
  241. node_dst = find_node_by_AddrA(&port->hsr->node_db, eth_hdr(skb)->h_dest);
  242. if (!node_dst) {
  243. WARN_ONCE(1, "%s: Unknown node\n", __func__);
  244. return;
  245. }
  246. if (port->type != node_dst->AddrB_port)
  247. return;
  248. ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->MacAddressB);
  249. }
  250. void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
  251. u16 sequence_nr)
  252. {
  253. /* Don't register incoming frames without a valid sequence number. This
  254. * ensures entries of restarted nodes gets pruned so that they can
  255. * re-register and resume communications.
  256. */
  257. if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
  258. return;
  259. node->time_in[port->type] = jiffies;
  260. node->time_in_stale[port->type] = false;
  261. }
  262. /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
  263. * ethhdr->h_source address and skb->mac_header set.
  264. *
  265. * Return:
  266. * 1 if frame can be shown to have been sent recently on this interface,
  267. * 0 otherwise, or
  268. * negative error code on error
  269. */
  270. int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
  271. u16 sequence_nr)
  272. {
  273. if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
  274. return 1;
  275. node->seq_out[port->type] = sequence_nr;
  276. return 0;
  277. }
  278. static struct hsr_port *get_late_port(struct hsr_priv *hsr,
  279. struct hsr_node *node)
  280. {
  281. if (node->time_in_stale[HSR_PT_SLAVE_A])
  282. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  283. if (node->time_in_stale[HSR_PT_SLAVE_B])
  284. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  285. if (time_after(node->time_in[HSR_PT_SLAVE_B],
  286. node->time_in[HSR_PT_SLAVE_A] +
  287. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  288. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  289. if (time_after(node->time_in[HSR_PT_SLAVE_A],
  290. node->time_in[HSR_PT_SLAVE_B] +
  291. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  292. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  293. return NULL;
  294. }
  295. /* Remove stale sequence_nr records. Called by timer every
  296. * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
  297. */
  298. void hsr_prune_nodes(unsigned long data)
  299. {
  300. struct hsr_priv *hsr;
  301. struct hsr_node *node;
  302. struct hsr_port *port;
  303. unsigned long timestamp;
  304. unsigned long time_a, time_b;
  305. hsr = (struct hsr_priv *) data;
  306. rcu_read_lock();
  307. list_for_each_entry_rcu(node, &hsr->node_db, mac_list) {
  308. /* Shorthand */
  309. time_a = node->time_in[HSR_PT_SLAVE_A];
  310. time_b = node->time_in[HSR_PT_SLAVE_B];
  311. /* Check for timestamps old enough to risk wrap-around */
  312. if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2))
  313. node->time_in_stale[HSR_PT_SLAVE_A] = true;
  314. if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2))
  315. node->time_in_stale[HSR_PT_SLAVE_B] = true;
  316. /* Get age of newest frame from node.
  317. * At least one time_in is OK here; nodes get pruned long
  318. * before both time_ins can get stale
  319. */
  320. timestamp = time_a;
  321. if (node->time_in_stale[HSR_PT_SLAVE_A] ||
  322. (!node->time_in_stale[HSR_PT_SLAVE_B] &&
  323. time_after(time_b, time_a)))
  324. timestamp = time_b;
  325. /* Warn of ring error only as long as we get frames at all */
  326. if (time_is_after_jiffies(timestamp +
  327. msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) {
  328. rcu_read_lock();
  329. port = get_late_port(hsr, node);
  330. if (port != NULL)
  331. hsr_nl_ringerror(hsr, node->MacAddressA, port);
  332. rcu_read_unlock();
  333. }
  334. /* Prune old entries */
  335. if (time_is_before_jiffies(timestamp +
  336. msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
  337. hsr_nl_nodedown(hsr, node->MacAddressA);
  338. list_del_rcu(&node->mac_list);
  339. /* Note that we need to free this entry later: */
  340. kfree_rcu(node, rcu_head);
  341. }
  342. }
  343. rcu_read_unlock();
  344. }
  345. void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
  346. unsigned char addr[ETH_ALEN])
  347. {
  348. struct hsr_node *node;
  349. if (!_pos) {
  350. node = list_first_or_null_rcu(&hsr->node_db,
  351. struct hsr_node, mac_list);
  352. if (node)
  353. ether_addr_copy(addr, node->MacAddressA);
  354. return node;
  355. }
  356. node = _pos;
  357. list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
  358. ether_addr_copy(addr, node->MacAddressA);
  359. return node;
  360. }
  361. return NULL;
  362. }
  363. int hsr_get_node_data(struct hsr_priv *hsr,
  364. const unsigned char *addr,
  365. unsigned char addr_b[ETH_ALEN],
  366. unsigned int *addr_b_ifindex,
  367. int *if1_age,
  368. u16 *if1_seq,
  369. int *if2_age,
  370. u16 *if2_seq)
  371. {
  372. struct hsr_node *node;
  373. struct hsr_port *port;
  374. unsigned long tdiff;
  375. rcu_read_lock();
  376. node = find_node_by_AddrA(&hsr->node_db, addr);
  377. if (!node) {
  378. rcu_read_unlock();
  379. return -ENOENT; /* No such entry */
  380. }
  381. ether_addr_copy(addr_b, node->MacAddressB);
  382. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
  383. if (node->time_in_stale[HSR_PT_SLAVE_A])
  384. *if1_age = INT_MAX;
  385. #if HZ <= MSEC_PER_SEC
  386. else if (tdiff > msecs_to_jiffies(INT_MAX))
  387. *if1_age = INT_MAX;
  388. #endif
  389. else
  390. *if1_age = jiffies_to_msecs(tdiff);
  391. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
  392. if (node->time_in_stale[HSR_PT_SLAVE_B])
  393. *if2_age = INT_MAX;
  394. #if HZ <= MSEC_PER_SEC
  395. else if (tdiff > msecs_to_jiffies(INT_MAX))
  396. *if2_age = INT_MAX;
  397. #endif
  398. else
  399. *if2_age = jiffies_to_msecs(tdiff);
  400. /* Present sequence numbers as if they were incoming on interface */
  401. *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
  402. *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
  403. if (node->AddrB_port != HSR_PT_NONE) {
  404. port = hsr_port_get_hsr(hsr, node->AddrB_port);
  405. *addr_b_ifindex = port->dev->ifindex;
  406. } else {
  407. *addr_b_ifindex = -1;
  408. }
  409. rcu_read_unlock();
  410. return 0;
  411. }