hsr_prp_framereg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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_prp_main.h"
  21. #include "hsr_prp_framereg.h"
  22. #include "hsr_netlink.h"
  23. #include "prp_netlink.h"
  24. /* TODO: use hash lists for mac addresses (linux/jhash.h)? */
  25. /* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
  26. * false otherwise.
  27. */
  28. static bool seq_nr_after(u16 a, u16 b)
  29. {
  30. /* Remove inconsistency where
  31. * seq_nr_after(a, b) == seq_nr_before(a, b)
  32. */
  33. if ((int)b - a == 32768)
  34. return false;
  35. return (((s16)(b - a)) < 0);
  36. }
  37. #define seq_nr_before(a, b) seq_nr_after((b), (a))
  38. #define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
  39. #define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
  40. bool hsr_prp_addr_is_self(struct hsr_prp_priv *priv, unsigned char *addr)
  41. {
  42. struct hsr_prp_node *node;
  43. node = list_first_or_null_rcu(&priv->self_node_db, struct hsr_prp_node,
  44. mac_list);
  45. if (!node) {
  46. WARN_ONCE(1, "HSR: No self node\n");
  47. return false;
  48. }
  49. if (ether_addr_equal(addr, node->mac_address_a))
  50. return true;
  51. if (ether_addr_equal(addr, node->mac_address_b))
  52. return true;
  53. return false;
  54. }
  55. /* Search for mac entry. Caller must hold rcu read lock.
  56. */
  57. static struct hsr_prp_node *
  58. find_node_by_addr_a(struct list_head *node_db,
  59. const unsigned char addr[ETH_ALEN])
  60. {
  61. struct hsr_prp_node *node;
  62. list_for_each_entry_rcu(node, node_db, mac_list) {
  63. if (ether_addr_equal(node->mac_address_a, addr))
  64. return node;
  65. }
  66. return NULL;
  67. }
  68. /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
  69. * frames from self that's been looped over the HSR ring.
  70. */
  71. int hsr_prp_create_self_node(struct list_head *self_node_db,
  72. unsigned char addr_a[ETH_ALEN],
  73. unsigned char addr_b[ETH_ALEN])
  74. {
  75. struct hsr_prp_node *node, *oldnode;
  76. node = kmalloc(sizeof(*node), GFP_KERNEL);
  77. if (!node)
  78. return -ENOMEM;
  79. ether_addr_copy(node->mac_address_a, addr_a);
  80. ether_addr_copy(node->mac_address_b, addr_b);
  81. rcu_read_lock();
  82. oldnode = list_first_or_null_rcu(self_node_db,
  83. struct hsr_prp_node, mac_list);
  84. if (oldnode) {
  85. list_replace_rcu(&oldnode->mac_list, &node->mac_list);
  86. rcu_read_unlock();
  87. synchronize_rcu();
  88. kfree(oldnode);
  89. } else {
  90. rcu_read_unlock();
  91. list_add_tail_rcu(&node->mac_list, self_node_db);
  92. }
  93. return 0;
  94. }
  95. /* Allocate an hsr_node and add it to node_db. 'addr' is the node's AddressA;
  96. * seq_out is used to initialize filtering of outgoing duplicate frames
  97. * originating from the newly added node.
  98. */
  99. struct hsr_prp_node *hsr_prp_add_node(struct list_head *node_db,
  100. unsigned char addr[],
  101. u16 seq_out, bool san,
  102. enum hsr_prp_port_type rx_port)
  103. {
  104. struct hsr_prp_node *node;
  105. unsigned long now;
  106. int i;
  107. node = kzalloc(sizeof(*node), GFP_ATOMIC);
  108. if (!node)
  109. return NULL;
  110. ether_addr_copy(node->mac_address_a, addr);
  111. /* We are only interested in time diffs here, so use current jiffies
  112. * as initialization. (0 could trigger an spurious ring error warning).
  113. */
  114. now = jiffies;
  115. for (i = 0; i < HSR_PRP_PT_PORTS; i++)
  116. node->time_in[i] = now;
  117. for (i = 0; i < HSR_PRP_PT_PORTS; i++)
  118. node->seq_out[i] = seq_out;
  119. if (san) {
  120. /* Mark if the SAN node is over LAN_A or LAN_B */
  121. if (rx_port == HSR_PRP_PT_SLAVE_A)
  122. node->san_a = true;
  123. else if (rx_port == HSR_PRP_PT_SLAVE_B)
  124. node->san_b = true;
  125. } else {
  126. node->san_a = false;
  127. node->san_b = false;
  128. }
  129. list_add_tail_rcu(&node->mac_list, node_db);
  130. return node;
  131. }
  132. /* Get the hsr_node from which 'skb' was sent.
  133. */
  134. struct hsr_prp_node *hsr_prp_get_node(struct list_head *node_db,
  135. struct sk_buff *skb,
  136. bool is_sup,
  137. enum hsr_prp_port_type rx_port)
  138. {
  139. struct hsr_prp_node *node;
  140. struct ethhdr *ethhdr;
  141. struct prp_rct *rct;
  142. bool san = false;
  143. u16 seq_out;
  144. if (!skb_mac_header_was_set(skb))
  145. return NULL;
  146. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  147. list_for_each_entry_rcu(node, node_db, mac_list) {
  148. if (ether_addr_equal(node->mac_address_a, ethhdr->h_source))
  149. return node;
  150. if (ether_addr_equal(node->mac_address_b, ethhdr->h_source))
  151. return node;
  152. }
  153. /* Everyone may create a node entry, connected node to a HSR device. */
  154. if (ethhdr->h_proto == htons(ETH_P_PRP) ||
  155. ethhdr->h_proto == htons(ETH_P_HSR)) {
  156. /* Use the existing sequence_nr from the tag as starting point
  157. * for filtering duplicate frames.
  158. */
  159. seq_out = hsr_get_skb_sequence_nr(skb) - 1;
  160. } else {
  161. rct = skb_get_PRP_rct(skb);
  162. if (rct && prp_check_lsdu_size(skb, rct, is_sup)) {
  163. seq_out = prp_get_skb_sequence_nr(rct);
  164. } else {
  165. if (rx_port != HSR_PRP_PT_MASTER)
  166. san = true;
  167. seq_out = HSR_PRP_SEQNR_START;
  168. }
  169. }
  170. return hsr_prp_add_node(node_db, ethhdr->h_source, seq_out,
  171. san, rx_port);
  172. }
  173. /* Use the Supervision frame's info about an eventual mac_address_b for merging
  174. * nodes that has previously had their mac_address_b registered as a separate
  175. * node.
  176. */
  177. void hsr_prp_handle_sup_frame(struct sk_buff *skb,
  178. struct hsr_prp_node *node_curr,
  179. struct hsr_prp_port *port_rcv)
  180. {
  181. struct ethhdr *ethhdr;
  182. struct hsr_prp_node *node_real;
  183. struct hsr_prp_sup_payload *hsr_sp;
  184. struct list_head *node_db;
  185. int i;
  186. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  187. /* Leave the ethernet header. */
  188. skb_pull(skb, sizeof(struct ethhdr));
  189. /* And leave the HSR tag. */
  190. if (ethhdr->h_proto == htons(ETH_P_HSR))
  191. skb_pull(skb, sizeof(struct hsr_tag));
  192. /* And leave the HSR sup tag. */
  193. skb_pull(skb, sizeof(struct hsr_prp_sup_tag));
  194. hsr_sp = (struct hsr_prp_sup_payload *)skb->data;
  195. /* Merge node_curr (registered on mac_address_b) into node_real */
  196. node_db = &port_rcv->priv->node_db;
  197. node_real = find_node_by_addr_a(node_db, hsr_sp->mac_address_a);
  198. if (!node_real)
  199. /* No frame received from AddrA of this node yet */
  200. node_real = hsr_prp_add_node(node_db, hsr_sp->mac_address_a,
  201. HSR_PRP_SEQNR_START - 1, true,
  202. port_rcv->type);
  203. if (!node_real)
  204. goto done; /* No mem */
  205. if (node_real == node_curr)
  206. /* Node has already been merged */
  207. goto done;
  208. ether_addr_copy(node_real->mac_address_b, ethhdr->h_source);
  209. for (i = 0; i < HSR_PRP_PT_PORTS; i++) {
  210. if (!node_curr->time_in_stale[i] &&
  211. time_after(node_curr->time_in[i], node_real->time_in[i])) {
  212. node_real->time_in[i] = node_curr->time_in[i];
  213. node_real->time_in_stale[i] =
  214. node_curr->time_in_stale[i];
  215. }
  216. if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
  217. node_real->seq_out[i] = node_curr->seq_out[i];
  218. }
  219. node_real->addr_b_port = port_rcv->type;
  220. list_del_rcu(&node_curr->mac_list);
  221. kfree_rcu(node_curr, rcu_head);
  222. done:
  223. skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
  224. }
  225. /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
  226. *
  227. * If the frame was sent by a node's B interface, replace the source
  228. * address with that node's "official" address (mac_address_a) so that upper
  229. * layers recognize where it came from.
  230. */
  231. void hsr_addr_subst_source(struct hsr_prp_node *node, struct sk_buff *skb)
  232. {
  233. if (!skb_mac_header_was_set(skb)) {
  234. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  235. return;
  236. }
  237. memcpy(&eth_hdr(skb)->h_source, node->mac_address_a, ETH_ALEN);
  238. }
  239. /* 'skb' is a frame meant for another host.
  240. * 'port' is the outgoing interface
  241. *
  242. * Substitute the target (dest) MAC address if necessary, so the it matches the
  243. * recipient interface MAC address, regardless of whether that is the
  244. * recipient's A or B interface.
  245. * This is needed to keep the packets flowing through switches that learn on
  246. * which "side" the different interfaces are.
  247. */
  248. void hsr_addr_subst_dest(struct hsr_prp_node *node_src, struct sk_buff *skb,
  249. struct hsr_prp_port *port)
  250. {
  251. struct hsr_prp_node *node_dst;
  252. if (!skb_mac_header_was_set(skb)) {
  253. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  254. return;
  255. }
  256. if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
  257. return;
  258. node_dst = find_node_by_addr_a(&port->priv->node_db,
  259. eth_hdr(skb)->h_dest);
  260. if (!node_dst) {
  261. WARN_ONCE(1, "%s: Unknown node\n", __func__);
  262. return;
  263. }
  264. if (port->type != node_dst->addr_b_port)
  265. return;
  266. if (is_valid_ether_addr(node_dst->mac_address_b))
  267. ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->mac_address_b);
  268. else
  269. WARN_ONCE(1, "%s: mac address B not valid\n", __func__);
  270. }
  271. void hsr_register_frame_in(struct hsr_prp_node *node,
  272. struct hsr_prp_port *port,
  273. u16 sequence_nr)
  274. {
  275. /* Don't register incoming frames without a valid sequence number. This
  276. * ensures entries of restarted nodes gets pruned so that they can
  277. * re-register and resume communications.
  278. */
  279. if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
  280. return;
  281. node->time_in[port->type] = jiffies;
  282. node->time_in_stale[port->type] = false;
  283. }
  284. /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
  285. * ethhdr->h_source address and skb->mac_header set.
  286. *
  287. * Return:
  288. * 1 if frame can be shown to have been sent recently on this interface,
  289. * 0 otherwise, or
  290. * negative error code on error
  291. */
  292. int hsr_register_frame_out(struct hsr_prp_port *port,
  293. struct hsr_prp_node *node,
  294. u16 sequence_nr)
  295. {
  296. if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]))
  297. return 1;
  298. node->seq_out[port->type] = sequence_nr;
  299. return 0;
  300. }
  301. static struct hsr_prp_port *get_late_port(struct hsr_prp_priv *priv,
  302. struct hsr_prp_node *node)
  303. {
  304. if (node->time_in_stale[HSR_PRP_PT_SLAVE_A])
  305. return hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_A);
  306. if (node->time_in_stale[HSR_PRP_PT_SLAVE_B])
  307. return hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_B);
  308. if (time_after(node->time_in[HSR_PRP_PT_SLAVE_B],
  309. node->time_in[HSR_PRP_PT_SLAVE_A] +
  310. msecs_to_jiffies(HSR_PRP_MAX_SLAVE_DIFF)))
  311. return hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_A);
  312. if (time_after(node->time_in[HSR_PRP_PT_SLAVE_A],
  313. node->time_in[HSR_PRP_PT_SLAVE_B] +
  314. msecs_to_jiffies(HSR_PRP_MAX_SLAVE_DIFF)))
  315. return hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_B);
  316. return NULL;
  317. }
  318. /* Remove stale sequence_nr records. Called by timer every
  319. * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
  320. */
  321. void hsr_prp_prune_nodes(unsigned long data)
  322. {
  323. struct hsr_prp_priv *priv;
  324. struct hsr_prp_node *node;
  325. struct hsr_prp_port *port;
  326. unsigned long timestamp;
  327. unsigned long time_a, time_b;
  328. priv = (struct hsr_prp_priv *)data;
  329. rcu_read_lock();
  330. list_for_each_entry_rcu(node, &priv->node_db, mac_list) {
  331. /* Shorthand */
  332. time_a = node->time_in[HSR_PRP_PT_SLAVE_A];
  333. time_b = node->time_in[HSR_PRP_PT_SLAVE_B];
  334. /* Check for timestamps old enough to risk wrap-around */
  335. if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
  336. node->time_in_stale[HSR_PRP_PT_SLAVE_A] = true;
  337. if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
  338. node->time_in_stale[HSR_PRP_PT_SLAVE_B] = true;
  339. /* Get age of newest frame from node.
  340. * At least one time_in is OK here; nodes get pruned long
  341. * before both time_ins can get stale
  342. */
  343. timestamp = time_a;
  344. if (node->time_in_stale[HSR_PRP_PT_SLAVE_A] ||
  345. (!node->time_in_stale[HSR_PRP_PT_SLAVE_B] &&
  346. time_after(time_b, time_a)))
  347. timestamp = time_b;
  348. /* Warn of ring error only as long as we get frames at all */
  349. if (time_is_after_jiffies(timestamp +
  350. msecs_to_jiffies(1.5 * HSR_PRP_MAX_SLAVE_DIFF))) {
  351. rcu_read_lock();
  352. port = get_late_port(priv, node);
  353. if (port)
  354. hsr_nl_ringerror(priv,
  355. node->mac_address_a, port);
  356. rcu_read_unlock();
  357. }
  358. /* Prune old entries */
  359. if (time_is_before_jiffies(timestamp +
  360. msecs_to_jiffies(HSR_PRP_NODE_FORGET_TIME))) {
  361. if (priv->prot_version <= HSR_V1)
  362. hsr_nl_nodedown(priv, node->mac_address_a);
  363. else
  364. prp_nl_nodedown(priv, node->mac_address_a);
  365. list_del_rcu(&node->mac_list);
  366. /* Note that we need to free this entry later: */
  367. kfree_rcu(node, rcu_head);
  368. }
  369. }
  370. rcu_read_unlock();
  371. }
  372. void *hsr_prp_get_next_node(struct hsr_prp_priv *priv, void *_pos,
  373. unsigned char addr[ETH_ALEN])
  374. {
  375. struct hsr_prp_node *node;
  376. if (!_pos) {
  377. node = list_first_or_null_rcu(&priv->node_db,
  378. struct hsr_prp_node, mac_list);
  379. if (node)
  380. ether_addr_copy(addr, node->mac_address_a);
  381. return node;
  382. }
  383. node = _pos;
  384. list_for_each_entry_continue_rcu(node, &priv->node_db, mac_list) {
  385. ether_addr_copy(addr, node->mac_address_a);
  386. return node;
  387. }
  388. return NULL;
  389. }
  390. int hsr_prp_get_node_data(struct hsr_prp_priv *priv,
  391. const unsigned char *addr,
  392. unsigned char addr_b[ETH_ALEN],
  393. unsigned int *addr_b_ifindex,
  394. int *if1_age, u16 *if1_seq,
  395. int *if2_age, u16 *if2_seq)
  396. {
  397. struct hsr_prp_node *node;
  398. struct hsr_prp_port *port;
  399. unsigned long tdiff;
  400. rcu_read_lock();
  401. node = find_node_by_addr_a(&priv->node_db, addr);
  402. if (!node) {
  403. rcu_read_unlock();
  404. return -ENOENT; /* No such entry */
  405. }
  406. ether_addr_copy(addr_b, node->mac_address_b);
  407. tdiff = jiffies - node->time_in[HSR_PRP_PT_SLAVE_A];
  408. if (node->time_in_stale[HSR_PRP_PT_SLAVE_A])
  409. *if1_age = INT_MAX;
  410. #if HZ <= MSEC_PER_SEC
  411. else if (tdiff > msecs_to_jiffies(INT_MAX))
  412. *if1_age = INT_MAX;
  413. #endif
  414. else
  415. *if1_age = jiffies_to_msecs(tdiff);
  416. tdiff = jiffies - node->time_in[HSR_PRP_PT_SLAVE_B];
  417. if (node->time_in_stale[HSR_PRP_PT_SLAVE_B])
  418. *if2_age = INT_MAX;
  419. #if HZ <= MSEC_PER_SEC
  420. else if (tdiff > msecs_to_jiffies(INT_MAX))
  421. *if2_age = INT_MAX;
  422. #endif
  423. else
  424. *if2_age = jiffies_to_msecs(tdiff);
  425. /* Present sequence numbers as if they were incoming on interface */
  426. *if1_seq = node->seq_out[HSR_PRP_PT_SLAVE_B];
  427. *if2_seq = node->seq_out[HSR_PRP_PT_SLAVE_A];
  428. if (node->addr_b_port != HSR_PRP_PT_NONE) {
  429. port = hsr_prp_get_port(priv, node->addr_b_port);
  430. *addr_b_ifindex = port->dev->ifindex;
  431. } else {
  432. *addr_b_ifindex = -1;
  433. }
  434. rcu_read_unlock();
  435. return 0;
  436. }