hsr_framereg.c 13 KB

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