hsr_framereg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. * Same code handles filtering of duplicates for PRP as well.
  12. */
  13. #include <linux/if_ether.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/slab.h>
  16. #include <linux/rculist.h>
  17. #include "hsr_main.h"
  18. #include "hsr_framereg.h"
  19. #include "hsr_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_before_or_eq(a, b) (!seq_nr_after((a), (b)))
  35. bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
  36. {
  37. struct hsr_node *node;
  38. node = list_first_or_null_rcu(&hsr->self_node_db, struct hsr_node,
  39. mac_list);
  40. if (!node) {
  41. WARN_ONCE(1, "HSR: No self node\n");
  42. return false;
  43. }
  44. if (ether_addr_equal(addr, node->macaddress_A))
  45. return true;
  46. if (ether_addr_equal(addr, node->macaddress_B))
  47. return true;
  48. return false;
  49. }
  50. /* Search for mac entry. Caller must hold rcu read lock.
  51. */
  52. static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
  53. const unsigned char addr[ETH_ALEN])
  54. {
  55. struct hsr_node *node;
  56. list_for_each_entry_rcu(node, node_db, mac_list) {
  57. if (ether_addr_equal(node->macaddress_A, addr))
  58. return node;
  59. }
  60. return NULL;
  61. }
  62. /* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
  63. * frames from self that's been looped over the HSR ring.
  64. */
  65. int hsr_create_self_node(struct hsr_priv *hsr,
  66. unsigned char addr_a[ETH_ALEN],
  67. unsigned char addr_b[ETH_ALEN])
  68. {
  69. struct list_head *self_node_db = &hsr->self_node_db;
  70. struct hsr_node *node, *oldnode;
  71. node = kmalloc(sizeof(*node), GFP_KERNEL);
  72. if (!node)
  73. return -ENOMEM;
  74. ether_addr_copy(node->macaddress_A, addr_a);
  75. ether_addr_copy(node->macaddress_B, addr_b);
  76. spin_lock_bh(&hsr->list_lock);
  77. oldnode = list_first_or_null_rcu(self_node_db,
  78. struct hsr_node, mac_list);
  79. if (oldnode) {
  80. list_replace_rcu(&oldnode->mac_list, &node->mac_list);
  81. spin_unlock_bh(&hsr->list_lock);
  82. kfree_rcu(oldnode, rcu_head);
  83. } else {
  84. list_add_tail_rcu(&node->mac_list, self_node_db);
  85. spin_unlock_bh(&hsr->list_lock);
  86. }
  87. return 0;
  88. }
  89. void hsr_del_self_node(struct hsr_priv *hsr)
  90. {
  91. struct list_head *self_node_db = &hsr->self_node_db;
  92. struct hsr_node *node;
  93. spin_lock_bh(&hsr->list_lock);
  94. node = list_first_or_null_rcu(self_node_db, struct hsr_node, mac_list);
  95. if (node) {
  96. list_del_rcu(&node->mac_list);
  97. kfree_rcu(node, rcu_head);
  98. }
  99. spin_unlock_bh(&hsr->list_lock);
  100. }
  101. void hsr_del_nodes(struct list_head *node_db)
  102. {
  103. struct hsr_node *node;
  104. struct hsr_node *tmp;
  105. list_for_each_entry_safe(node, tmp, node_db, mac_list)
  106. kfree(node);
  107. }
  108. void prp_handle_san_frame(bool san, enum hsr_port_type port,
  109. struct hsr_node *node)
  110. {
  111. /* Mark if the SAN node is over LAN_A or LAN_B */
  112. if (port == HSR_PT_SLAVE_A) {
  113. node->san_a = true;
  114. return;
  115. }
  116. if (port == HSR_PT_SLAVE_B)
  117. node->san_b = true;
  118. }
  119. /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A;
  120. * seq_out is used to initialize filtering of outgoing duplicate frames
  121. * originating from the newly added node.
  122. */
  123. static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
  124. struct list_head *node_db,
  125. unsigned char addr[],
  126. u16 seq_out, bool san,
  127. enum hsr_port_type rx_port)
  128. {
  129. struct hsr_node *new_node, *node;
  130. unsigned long now;
  131. int i;
  132. new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
  133. if (!new_node)
  134. return NULL;
  135. ether_addr_copy(new_node->macaddress_A, addr);
  136. /* We are only interested in time diffs here, so use current jiffies
  137. * as initialization. (0 could trigger an spurious ring error warning).
  138. */
  139. now = jiffies;
  140. for (i = 0; i < HSR_PT_PORTS; i++) {
  141. new_node->time_in[i] = now;
  142. new_node->time_out[i] = now;
  143. }
  144. for (i = 0; i < HSR_PT_PORTS; i++)
  145. new_node->seq_out[i] = seq_out;
  146. if (san && hsr->proto_ops->handle_san_frame)
  147. hsr->proto_ops->handle_san_frame(san, rx_port, new_node);
  148. spin_lock_bh(&hsr->list_lock);
  149. list_for_each_entry_rcu(node, node_db, mac_list,
  150. lockdep_is_held(&hsr->list_lock)) {
  151. if (ether_addr_equal(node->macaddress_A, addr))
  152. goto out;
  153. if (ether_addr_equal(node->macaddress_B, addr))
  154. goto out;
  155. }
  156. list_add_tail_rcu(&new_node->mac_list, node_db);
  157. spin_unlock_bh(&hsr->list_lock);
  158. return new_node;
  159. out:
  160. spin_unlock_bh(&hsr->list_lock);
  161. kfree(new_node);
  162. return node;
  163. }
  164. void prp_update_san_info(struct hsr_node *node, bool is_sup)
  165. {
  166. if (!is_sup)
  167. return;
  168. node->san_a = false;
  169. node->san_b = false;
  170. }
  171. /* Get the hsr_node from which 'skb' was sent.
  172. */
  173. struct hsr_node *hsr_get_node(struct hsr_port *port, struct list_head *node_db,
  174. struct sk_buff *skb, bool is_sup,
  175. enum hsr_port_type rx_port)
  176. {
  177. struct hsr_priv *hsr = port->hsr;
  178. struct hsr_node *node;
  179. struct ethhdr *ethhdr;
  180. struct prp_rct *rct;
  181. bool san = false;
  182. u16 seq_out;
  183. if (!skb_mac_header_was_set(skb))
  184. return NULL;
  185. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  186. list_for_each_entry_rcu(node, node_db, mac_list) {
  187. if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
  188. if (hsr->proto_ops->update_san_info)
  189. hsr->proto_ops->update_san_info(node, is_sup);
  190. return node;
  191. }
  192. if (ether_addr_equal(node->macaddress_B, ethhdr->h_source)) {
  193. if (hsr->proto_ops->update_san_info)
  194. hsr->proto_ops->update_san_info(node, is_sup);
  195. return node;
  196. }
  197. }
  198. /* Everyone may create a node entry, connected node to a HSR/PRP
  199. * device.
  200. */
  201. if (ethhdr->h_proto == htons(ETH_P_PRP) ||
  202. ethhdr->h_proto == htons(ETH_P_HSR)) {
  203. /* Use the existing sequence_nr from the tag as starting point
  204. * for filtering duplicate frames.
  205. */
  206. seq_out = hsr_get_skb_sequence_nr(skb) - 1;
  207. } else {
  208. rct = skb_get_PRP_rct(skb);
  209. if (rct && prp_check_lsdu_size(skb, rct, is_sup)) {
  210. seq_out = prp_get_skb_sequence_nr(rct);
  211. } else {
  212. if (rx_port != HSR_PT_MASTER)
  213. san = true;
  214. seq_out = HSR_SEQNR_START;
  215. }
  216. }
  217. return hsr_add_node(hsr, node_db, ethhdr->h_source, seq_out,
  218. san, rx_port);
  219. }
  220. /* Use the Supervision frame's info about an eventual macaddress_B for merging
  221. * nodes that has previously had their macaddress_B registered as a separate
  222. * node.
  223. */
  224. void hsr_handle_sup_frame(struct hsr_frame_info *frame)
  225. {
  226. struct hsr_node *node_curr = frame->node_src;
  227. struct hsr_port *port_rcv = frame->port_rcv;
  228. struct hsr_priv *hsr = port_rcv->hsr;
  229. struct hsr_sup_payload *hsr_sp;
  230. struct hsr_node *node_real;
  231. struct sk_buff *skb = NULL;
  232. struct list_head *node_db;
  233. struct ethhdr *ethhdr;
  234. int i;
  235. /* Here either frame->skb_hsr or frame->skb_prp should be
  236. * valid as supervision frame always will have protocol
  237. * header info.
  238. */
  239. if (frame->skb_hsr)
  240. skb = frame->skb_hsr;
  241. else if (frame->skb_prp)
  242. skb = frame->skb_prp;
  243. if (!skb)
  244. return;
  245. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  246. /* Leave the ethernet header. */
  247. skb_pull(skb, sizeof(struct ethhdr));
  248. /* And leave the HSR tag. */
  249. if (ethhdr->h_proto == htons(ETH_P_HSR))
  250. skb_pull(skb, sizeof(struct hsr_tag));
  251. /* And leave the HSR sup tag. */
  252. skb_pull(skb, sizeof(struct hsr_sup_tag));
  253. hsr_sp = (struct hsr_sup_payload *)skb->data;
  254. /* Merge node_curr (registered on macaddress_B) into node_real */
  255. node_db = &port_rcv->hsr->node_db;
  256. node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
  257. if (!node_real)
  258. /* No frame received from AddrA of this node yet */
  259. node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
  260. HSR_SEQNR_START - 1, true,
  261. port_rcv->type);
  262. if (!node_real)
  263. goto done; /* No mem */
  264. if (node_real == node_curr)
  265. /* Node has already been merged */
  266. goto done;
  267. ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
  268. for (i = 0; i < HSR_PT_PORTS; i++) {
  269. if (!node_curr->time_in_stale[i] &&
  270. time_after(node_curr->time_in[i], node_real->time_in[i])) {
  271. node_real->time_in[i] = node_curr->time_in[i];
  272. node_real->time_in_stale[i] =
  273. node_curr->time_in_stale[i];
  274. }
  275. if (seq_nr_after(node_curr->seq_out[i], node_real->seq_out[i]))
  276. node_real->seq_out[i] = node_curr->seq_out[i];
  277. }
  278. node_real->addr_B_port = port_rcv->type;
  279. spin_lock_bh(&hsr->list_lock);
  280. list_del_rcu(&node_curr->mac_list);
  281. spin_unlock_bh(&hsr->list_lock);
  282. kfree_rcu(node_curr, rcu_head);
  283. done:
  284. /* PRP uses v0 header */
  285. if (ethhdr->h_proto == htons(ETH_P_HSR))
  286. skb_push(skb, sizeof(struct hsrv1_ethhdr_sp));
  287. else
  288. skb_push(skb, sizeof(struct hsrv0_ethhdr_sp));
  289. }
  290. /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
  291. *
  292. * If the frame was sent by a node's B interface, replace the source
  293. * address with that node's "official" address (macaddress_A) so that upper
  294. * layers recognize where it came from.
  295. */
  296. void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
  297. {
  298. if (!skb_mac_header_was_set(skb)) {
  299. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  300. return;
  301. }
  302. memcpy(&eth_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
  303. }
  304. /* 'skb' is a frame meant for another host.
  305. * 'port' is the outgoing interface
  306. *
  307. * Substitute the target (dest) MAC address if necessary, so the it matches the
  308. * recipient interface MAC address, regardless of whether that is the
  309. * recipient's A or B interface.
  310. * This is needed to keep the packets flowing through switches that learn on
  311. * which "side" the different interfaces are.
  312. */
  313. void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
  314. struct hsr_port *port)
  315. {
  316. struct hsr_node *node_dst;
  317. if (!skb_mac_header_was_set(skb)) {
  318. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  319. return;
  320. }
  321. if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
  322. return;
  323. node_dst = find_node_by_addr_A(&port->hsr->node_db,
  324. eth_hdr(skb)->h_dest);
  325. if (!node_dst) {
  326. if (net_ratelimit())
  327. netdev_err(skb->dev, "%s: Unknown node\n", __func__);
  328. return;
  329. }
  330. if (port->type != node_dst->addr_B_port)
  331. return;
  332. if (is_valid_ether_addr(node_dst->macaddress_B))
  333. ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
  334. }
  335. void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
  336. u16 sequence_nr)
  337. {
  338. /* Don't register incoming frames without a valid sequence number. This
  339. * ensures entries of restarted nodes gets pruned so that they can
  340. * re-register and resume communications.
  341. */
  342. if (seq_nr_before(sequence_nr, node->seq_out[port->type]))
  343. return;
  344. node->time_in[port->type] = jiffies;
  345. node->time_in_stale[port->type] = false;
  346. }
  347. /* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
  348. * ethhdr->h_source address and skb->mac_header set.
  349. *
  350. * Return:
  351. * 1 if frame can be shown to have been sent recently on this interface,
  352. * 0 otherwise, or
  353. * negative error code on error
  354. */
  355. int hsr_register_frame_out(struct hsr_port *port, struct hsr_node *node,
  356. u16 sequence_nr)
  357. {
  358. if (seq_nr_before_or_eq(sequence_nr, node->seq_out[port->type]) &&
  359. time_is_after_jiffies(node->time_out[port->type] +
  360. msecs_to_jiffies(HSR_ENTRY_FORGET_TIME)))
  361. return 1;
  362. node->time_out[port->type] = jiffies;
  363. node->seq_out[port->type] = sequence_nr;
  364. return 0;
  365. }
  366. static struct hsr_port *get_late_port(struct hsr_priv *hsr,
  367. struct hsr_node *node)
  368. {
  369. if (node->time_in_stale[HSR_PT_SLAVE_A])
  370. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  371. if (node->time_in_stale[HSR_PT_SLAVE_B])
  372. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  373. if (time_after(node->time_in[HSR_PT_SLAVE_B],
  374. node->time_in[HSR_PT_SLAVE_A] +
  375. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  376. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  377. if (time_after(node->time_in[HSR_PT_SLAVE_A],
  378. node->time_in[HSR_PT_SLAVE_B] +
  379. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  380. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  381. return NULL;
  382. }
  383. /* Remove stale sequence_nr records. Called by timer every
  384. * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
  385. */
  386. void hsr_prune_nodes(struct timer_list *t)
  387. {
  388. struct hsr_priv *hsr = from_timer(hsr, t, prune_timer);
  389. struct hsr_node *node;
  390. struct hsr_node *tmp;
  391. struct hsr_port *port;
  392. unsigned long timestamp;
  393. unsigned long time_a, time_b;
  394. spin_lock_bh(&hsr->list_lock);
  395. list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
  396. /* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A]
  397. * nor time_in[HSR_PT_SLAVE_B], will ever be updated for
  398. * the master port. Thus the master node will be repeatedly
  399. * pruned leading to packet loss.
  400. */
  401. if (hsr_addr_is_self(hsr, node->macaddress_A))
  402. continue;
  403. /* Shorthand */
  404. time_a = node->time_in[HSR_PT_SLAVE_A];
  405. time_b = node->time_in[HSR_PT_SLAVE_B];
  406. /* Check for timestamps old enough to risk wrap-around */
  407. if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
  408. node->time_in_stale[HSR_PT_SLAVE_A] = true;
  409. if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
  410. node->time_in_stale[HSR_PT_SLAVE_B] = true;
  411. /* Get age of newest frame from node.
  412. * At least one time_in is OK here; nodes get pruned long
  413. * before both time_ins can get stale
  414. */
  415. timestamp = time_a;
  416. if (node->time_in_stale[HSR_PT_SLAVE_A] ||
  417. (!node->time_in_stale[HSR_PT_SLAVE_B] &&
  418. time_after(time_b, time_a)))
  419. timestamp = time_b;
  420. /* Warn of ring error only as long as we get frames at all */
  421. if (time_is_after_jiffies(timestamp +
  422. msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
  423. rcu_read_lock();
  424. port = get_late_port(hsr, node);
  425. if (port)
  426. hsr_nl_ringerror(hsr, node->macaddress_A, port);
  427. rcu_read_unlock();
  428. }
  429. /* Prune old entries */
  430. if (time_is_before_jiffies(timestamp +
  431. msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
  432. hsr_nl_nodedown(hsr, node->macaddress_A);
  433. list_del_rcu(&node->mac_list);
  434. /* Note that we need to free this entry later: */
  435. kfree_rcu(node, rcu_head);
  436. }
  437. }
  438. spin_unlock_bh(&hsr->list_lock);
  439. /* Restart timer */
  440. mod_timer(&hsr->prune_timer,
  441. jiffies + msecs_to_jiffies(PRUNE_PERIOD));
  442. }
  443. void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
  444. unsigned char addr[ETH_ALEN])
  445. {
  446. struct hsr_node *node;
  447. if (!_pos) {
  448. node = list_first_or_null_rcu(&hsr->node_db,
  449. struct hsr_node, mac_list);
  450. if (node)
  451. ether_addr_copy(addr, node->macaddress_A);
  452. return node;
  453. }
  454. node = _pos;
  455. list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
  456. ether_addr_copy(addr, node->macaddress_A);
  457. return node;
  458. }
  459. return NULL;
  460. }
  461. int hsr_get_node_data(struct hsr_priv *hsr,
  462. const unsigned char *addr,
  463. unsigned char addr_b[ETH_ALEN],
  464. unsigned int *addr_b_ifindex,
  465. int *if1_age,
  466. u16 *if1_seq,
  467. int *if2_age,
  468. u16 *if2_seq)
  469. {
  470. struct hsr_node *node;
  471. struct hsr_port *port;
  472. unsigned long tdiff;
  473. node = find_node_by_addr_A(&hsr->node_db, addr);
  474. if (!node)
  475. return -ENOENT;
  476. ether_addr_copy(addr_b, node->macaddress_B);
  477. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
  478. if (node->time_in_stale[HSR_PT_SLAVE_A])
  479. *if1_age = INT_MAX;
  480. #if HZ <= MSEC_PER_SEC
  481. else if (tdiff > msecs_to_jiffies(INT_MAX))
  482. *if1_age = INT_MAX;
  483. #endif
  484. else
  485. *if1_age = jiffies_to_msecs(tdiff);
  486. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
  487. if (node->time_in_stale[HSR_PT_SLAVE_B])
  488. *if2_age = INT_MAX;
  489. #if HZ <= MSEC_PER_SEC
  490. else if (tdiff > msecs_to_jiffies(INT_MAX))
  491. *if2_age = INT_MAX;
  492. #endif
  493. else
  494. *if2_age = jiffies_to_msecs(tdiff);
  495. /* Present sequence numbers as if they were incoming on interface */
  496. *if1_seq = node->seq_out[HSR_PT_SLAVE_B];
  497. *if2_seq = node->seq_out[HSR_PT_SLAVE_A];
  498. if (node->addr_B_port != HSR_PT_NONE) {
  499. port = hsr_port_get_hsr(hsr, node->addr_B_port);
  500. *addr_b_ifindex = port->dev->ifindex;
  501. } else {
  502. *addr_b_ifindex = -1;
  503. }
  504. return 0;
  505. }