hsr_prp_framereg.c 15 KB

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