hsr_framereg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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 <kunit/visibility.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/slab.h>
  17. #include <linux/rculist.h>
  18. #include "hsr_main.h"
  19. #include "hsr_framereg.h"
  20. #include "hsr_netlink.h"
  21. bool hsr_addr_is_redbox(struct hsr_priv *hsr, unsigned char *addr)
  22. {
  23. if (!hsr->redbox || !is_valid_ether_addr(hsr->macaddress_redbox))
  24. return false;
  25. return ether_addr_equal(addr, hsr->macaddress_redbox);
  26. }
  27. bool hsr_addr_is_self(struct hsr_priv *hsr, unsigned char *addr)
  28. {
  29. struct hsr_self_node *sn;
  30. bool ret = false;
  31. rcu_read_lock();
  32. sn = rcu_dereference(hsr->self_node);
  33. if (!sn) {
  34. WARN_ONCE(1, "HSR: No self node\n");
  35. goto out;
  36. }
  37. if (ether_addr_equal(addr, sn->macaddress_A) ||
  38. ether_addr_equal(addr, sn->macaddress_B))
  39. ret = true;
  40. out:
  41. rcu_read_unlock();
  42. return ret;
  43. }
  44. /* Search for mac entry. Caller must hold rcu read lock.
  45. */
  46. static struct hsr_node *find_node_by_addr_A(struct list_head *node_db,
  47. const unsigned char addr[ETH_ALEN])
  48. {
  49. struct hsr_node *node;
  50. list_for_each_entry_rcu(node, node_db, mac_list) {
  51. if (ether_addr_equal(node->macaddress_A, addr))
  52. return node;
  53. }
  54. return NULL;
  55. }
  56. /* Check if node for a given MAC address is already present in data base
  57. */
  58. bool hsr_is_node_in_db(struct list_head *node_db,
  59. const unsigned char addr[ETH_ALEN])
  60. {
  61. return !!find_node_by_addr_A(node_db, addr);
  62. }
  63. /* Helper for device init; the self_node is used in hsr_rcv() to recognize
  64. * frames from self that's been looped over the HSR ring.
  65. */
  66. int hsr_create_self_node(struct hsr_priv *hsr,
  67. const unsigned char addr_a[ETH_ALEN],
  68. const unsigned char addr_b[ETH_ALEN])
  69. {
  70. struct hsr_self_node *sn, *old;
  71. sn = kmalloc_obj(*sn);
  72. if (!sn)
  73. return -ENOMEM;
  74. ether_addr_copy(sn->macaddress_A, addr_a);
  75. ether_addr_copy(sn->macaddress_B, addr_b);
  76. spin_lock_bh(&hsr->list_lock);
  77. old = rcu_replace_pointer(hsr->self_node, sn,
  78. lockdep_is_held(&hsr->list_lock));
  79. spin_unlock_bh(&hsr->list_lock);
  80. if (old)
  81. kfree_rcu(old, rcu_head);
  82. return 0;
  83. }
  84. void hsr_del_self_node(struct hsr_priv *hsr)
  85. {
  86. struct hsr_self_node *old;
  87. spin_lock_bh(&hsr->list_lock);
  88. old = rcu_replace_pointer(hsr->self_node, NULL,
  89. lockdep_is_held(&hsr->list_lock));
  90. spin_unlock_bh(&hsr->list_lock);
  91. if (old)
  92. kfree_rcu(old, rcu_head);
  93. }
  94. static void hsr_free_node(struct hsr_node *node)
  95. {
  96. xa_destroy(&node->seq_blocks);
  97. kfree(node->block_buf);
  98. kfree(node);
  99. }
  100. static void hsr_free_node_rcu(struct rcu_head *rn)
  101. {
  102. struct hsr_node *node = container_of(rn, struct hsr_node, rcu_head);
  103. hsr_free_node(node);
  104. }
  105. static void hsr_lock_seq_out_pair(struct hsr_node *node_a,
  106. struct hsr_node *node_b)
  107. {
  108. if (node_a == node_b) {
  109. spin_lock_bh(&node_a->seq_out_lock);
  110. return;
  111. }
  112. if (node_a < node_b) {
  113. spin_lock_bh(&node_a->seq_out_lock);
  114. spin_lock_nested(&node_b->seq_out_lock, SINGLE_DEPTH_NESTING);
  115. } else {
  116. spin_lock_bh(&node_b->seq_out_lock);
  117. spin_lock_nested(&node_a->seq_out_lock, SINGLE_DEPTH_NESTING);
  118. }
  119. }
  120. static void hsr_unlock_seq_out_pair(struct hsr_node *node_a,
  121. struct hsr_node *node_b)
  122. {
  123. if (node_a == node_b) {
  124. spin_unlock_bh(&node_a->seq_out_lock);
  125. return;
  126. }
  127. if (node_a < node_b) {
  128. spin_unlock(&node_b->seq_out_lock);
  129. spin_unlock_bh(&node_a->seq_out_lock);
  130. } else {
  131. spin_unlock(&node_a->seq_out_lock);
  132. spin_unlock_bh(&node_b->seq_out_lock);
  133. }
  134. }
  135. void hsr_del_nodes(struct list_head *node_db)
  136. {
  137. struct hsr_node *node;
  138. struct hsr_node *tmp;
  139. list_for_each_entry_safe(node, tmp, node_db, mac_list) {
  140. list_del(&node->mac_list);
  141. hsr_free_node(node);
  142. }
  143. }
  144. void prp_handle_san_frame(bool san, enum hsr_port_type port,
  145. struct hsr_node *node)
  146. {
  147. /* Mark if the SAN node is over LAN_A or LAN_B */
  148. if (port == HSR_PT_SLAVE_A) {
  149. node->san_a = true;
  150. return;
  151. }
  152. if (port == HSR_PT_SLAVE_B)
  153. node->san_b = true;
  154. }
  155. /* Allocate an hsr_node and add it to node_db. 'addr' is the node's address_A.
  156. */
  157. static struct hsr_node *hsr_add_node(struct hsr_priv *hsr,
  158. struct list_head *node_db,
  159. unsigned char addr[], bool san,
  160. enum hsr_port_type rx_port)
  161. {
  162. struct hsr_node *new_node, *node = NULL;
  163. unsigned long now;
  164. size_t block_sz;
  165. int i;
  166. new_node = kzalloc_obj(*new_node, GFP_ATOMIC);
  167. if (!new_node)
  168. return NULL;
  169. ether_addr_copy(new_node->macaddress_A, addr);
  170. spin_lock_init(&new_node->seq_out_lock);
  171. if (hsr->prot_version == PRP_V1)
  172. new_node->seq_port_cnt = 1;
  173. else
  174. new_node->seq_port_cnt = HSR_PT_PORTS - 1;
  175. block_sz = hsr_seq_block_size(new_node);
  176. new_node->block_buf = kcalloc(HSR_MAX_SEQ_BLOCKS, block_sz, GFP_ATOMIC);
  177. if (!new_node->block_buf)
  178. goto free;
  179. xa_init(&new_node->seq_blocks);
  180. /* We are only interested in time diffs here, so use current jiffies
  181. * as initialization. (0 could trigger an spurious ring error warning).
  182. */
  183. now = jiffies;
  184. for (i = 0; i < HSR_PT_PORTS; i++) {
  185. new_node->time_in[i] = now;
  186. }
  187. if (san && hsr->proto_ops->handle_san_frame)
  188. hsr->proto_ops->handle_san_frame(san, rx_port, new_node);
  189. spin_lock_bh(&hsr->list_lock);
  190. list_for_each_entry_rcu(node, node_db, mac_list,
  191. lockdep_is_held(&hsr->list_lock)) {
  192. if (ether_addr_equal(node->macaddress_A, addr))
  193. goto out;
  194. if (ether_addr_equal(node->macaddress_B, addr))
  195. goto out;
  196. }
  197. list_add_tail_rcu(&new_node->mac_list, node_db);
  198. spin_unlock_bh(&hsr->list_lock);
  199. return new_node;
  200. out:
  201. spin_unlock_bh(&hsr->list_lock);
  202. kfree(new_node->block_buf);
  203. free:
  204. kfree(new_node);
  205. return node;
  206. }
  207. void prp_update_san_info(struct hsr_node *node, bool is_sup)
  208. {
  209. if (!is_sup)
  210. return;
  211. node->san_a = false;
  212. node->san_b = false;
  213. }
  214. /* Get the hsr_node from which 'skb' was sent.
  215. */
  216. struct hsr_node *hsr_get_node(struct hsr_port *port, struct list_head *node_db,
  217. struct sk_buff *skb, bool is_sup,
  218. enum hsr_port_type rx_port)
  219. {
  220. struct hsr_priv *hsr = port->hsr;
  221. struct hsr_node *node;
  222. struct ethhdr *ethhdr;
  223. struct prp_rct *rct;
  224. bool san = false;
  225. if (!skb_mac_header_was_set(skb))
  226. return NULL;
  227. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  228. list_for_each_entry_rcu(node, node_db, mac_list) {
  229. if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
  230. if (hsr->proto_ops->update_san_info)
  231. hsr->proto_ops->update_san_info(node, is_sup);
  232. return node;
  233. }
  234. if (ether_addr_equal(node->macaddress_B, ethhdr->h_source)) {
  235. if (hsr->proto_ops->update_san_info)
  236. hsr->proto_ops->update_san_info(node, is_sup);
  237. return node;
  238. }
  239. }
  240. /* Check if required node is not in proxy nodes table */
  241. list_for_each_entry_rcu(node, &hsr->proxy_node_db, mac_list) {
  242. if (ether_addr_equal(node->macaddress_A, ethhdr->h_source)) {
  243. if (hsr->proto_ops->update_san_info)
  244. hsr->proto_ops->update_san_info(node, is_sup);
  245. return node;
  246. }
  247. }
  248. /* Everyone may create a node entry, connected node to a HSR/PRP
  249. * device.
  250. */
  251. if (ethhdr->h_proto == htons(ETH_P_PRP) ||
  252. ethhdr->h_proto == htons(ETH_P_HSR)) {
  253. /* Check if skb contains hsr_ethhdr */
  254. if (skb->mac_len < sizeof(struct hsr_ethhdr))
  255. return NULL;
  256. } else {
  257. rct = skb_get_PRP_rct(skb);
  258. if (!rct && rx_port != HSR_PT_MASTER)
  259. san = true;
  260. }
  261. return hsr_add_node(hsr, node_db, ethhdr->h_source, san, rx_port);
  262. }
  263. static bool hsr_seq_block_is_old(struct hsr_seq_block *block)
  264. {
  265. unsigned long expiry = msecs_to_jiffies(HSR_ENTRY_FORGET_TIME);
  266. return time_is_before_jiffies(block->time + expiry);
  267. }
  268. static void hsr_forget_seq_block(struct hsr_node *node,
  269. struct hsr_seq_block *block)
  270. {
  271. if (block->time)
  272. xa_erase(&node->seq_blocks, block->block_idx);
  273. block->time = 0;
  274. }
  275. /* Get the currently active sequence number block. If there is no block yet, or
  276. * the existing one is expired, a new block is created. The idea is to maintain
  277. * a "sparse bitmap" where a bitmap for the whole sequence number space is
  278. * split into blocks and not all blocks exist all the time. The blocks can
  279. * expire after time (in low traffic situations) or when they are replaced in
  280. * the backing fixed size buffer (in high traffic situations).
  281. */
  282. VISIBLE_IF_KUNIT struct hsr_seq_block *hsr_get_seq_block(struct hsr_node *node,
  283. u16 block_idx)
  284. {
  285. struct hsr_seq_block *block, *res;
  286. size_t block_sz;
  287. block = xa_load(&node->seq_blocks, block_idx);
  288. if (block && hsr_seq_block_is_old(block)) {
  289. hsr_forget_seq_block(node, block);
  290. block = NULL;
  291. }
  292. if (!block) {
  293. block_sz = hsr_seq_block_size(node);
  294. block = node->block_buf + node->next_block * block_sz;
  295. hsr_forget_seq_block(node, block);
  296. memset(block, 0, block_sz);
  297. block->time = jiffies;
  298. block->block_idx = block_idx;
  299. res = xa_store(&node->seq_blocks, block_idx, block, GFP_ATOMIC);
  300. if (xa_is_err(res)) {
  301. block->time = 0;
  302. return NULL;
  303. }
  304. node->next_block =
  305. (node->next_block + 1) & (HSR_MAX_SEQ_BLOCKS - 1);
  306. }
  307. return block;
  308. }
  309. EXPORT_SYMBOL_IF_KUNIT(hsr_get_seq_block);
  310. /* Use the Supervision frame's info about an eventual macaddress_B for merging
  311. * nodes that has previously had their macaddress_B registered as a separate
  312. * node.
  313. */
  314. void hsr_handle_sup_frame(struct hsr_frame_info *frame)
  315. {
  316. struct hsr_node *node_curr = frame->node_src;
  317. struct hsr_port *port_rcv = frame->port_rcv;
  318. struct hsr_seq_block *src_blk, *merge_blk;
  319. struct hsr_priv *hsr = port_rcv->hsr;
  320. struct hsr_sup_tlv *hsr_sup_tlv;
  321. struct hsr_sup_payload *hsr_sp;
  322. struct hsr_node *node_real;
  323. struct sk_buff *skb = NULL;
  324. struct list_head *node_db;
  325. struct ethhdr *ethhdr;
  326. unsigned int total_pull_size = 0;
  327. unsigned int pull_size = 0;
  328. unsigned long idx;
  329. int i;
  330. /* Here either frame->skb_hsr or frame->skb_prp should be
  331. * valid as supervision frame always will have protocol
  332. * header info.
  333. */
  334. if (frame->skb_hsr)
  335. skb = frame->skb_hsr;
  336. else if (frame->skb_prp)
  337. skb = frame->skb_prp;
  338. else if (frame->skb_std)
  339. skb = frame->skb_std;
  340. if (!skb)
  341. return;
  342. /* Leave the ethernet header. */
  343. pull_size = sizeof(struct ethhdr);
  344. skb_pull(skb, pull_size);
  345. total_pull_size += pull_size;
  346. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  347. /* And leave the HSR tag. */
  348. if (ethhdr->h_proto == htons(ETH_P_HSR)) {
  349. pull_size = sizeof(struct hsr_tag);
  350. skb_pull(skb, pull_size);
  351. total_pull_size += pull_size;
  352. }
  353. /* And leave the HSR sup tag. */
  354. pull_size = sizeof(struct hsr_sup_tag);
  355. skb_pull(skb, pull_size);
  356. total_pull_size += pull_size;
  357. /* get HSR sup payload */
  358. hsr_sp = (struct hsr_sup_payload *)skb->data;
  359. /* Merge node_curr (registered on macaddress_B) into node_real */
  360. node_db = &port_rcv->hsr->node_db;
  361. node_real = find_node_by_addr_A(node_db, hsr_sp->macaddress_A);
  362. if (!node_real)
  363. /* No frame received from AddrA of this node yet */
  364. node_real = hsr_add_node(hsr, node_db, hsr_sp->macaddress_A,
  365. true, port_rcv->type);
  366. if (!node_real)
  367. goto done; /* No mem */
  368. if (node_real == node_curr)
  369. /* Node has already been merged */
  370. goto done;
  371. /* Leave the first HSR sup payload. */
  372. pull_size = sizeof(struct hsr_sup_payload);
  373. skb_pull(skb, pull_size);
  374. total_pull_size += pull_size;
  375. /* Get second supervision tlv */
  376. hsr_sup_tlv = (struct hsr_sup_tlv *)skb->data;
  377. /* And check if it is a redbox mac TLV */
  378. if (hsr_sup_tlv->HSR_TLV_type == PRP_TLV_REDBOX_MAC) {
  379. /* We could stop here after pushing hsr_sup_payload,
  380. * or proceed and allow macaddress_B and for redboxes.
  381. */
  382. /* Sanity check length */
  383. if (hsr_sup_tlv->HSR_TLV_length != 6)
  384. goto done;
  385. /* Leave the second HSR sup tlv. */
  386. pull_size = sizeof(struct hsr_sup_tlv);
  387. skb_pull(skb, pull_size);
  388. total_pull_size += pull_size;
  389. /* Get redbox mac address. */
  390. hsr_sp = (struct hsr_sup_payload *)skb->data;
  391. /* Check if redbox mac and node mac are equal. */
  392. if (!ether_addr_equal(node_real->macaddress_A, hsr_sp->macaddress_A)) {
  393. /* This is a redbox supervision frame for a VDAN! */
  394. goto done;
  395. }
  396. }
  397. ether_addr_copy(node_real->macaddress_B, ethhdr->h_source);
  398. hsr_lock_seq_out_pair(node_real, node_curr);
  399. for (i = 0; i < HSR_PT_PORTS; i++) {
  400. if (!node_curr->time_in_stale[i] &&
  401. time_after(node_curr->time_in[i], node_real->time_in[i])) {
  402. node_real->time_in[i] = node_curr->time_in[i];
  403. node_real->time_in_stale[i] =
  404. node_curr->time_in_stale[i];
  405. }
  406. }
  407. xa_for_each(&node_curr->seq_blocks, idx, src_blk) {
  408. if (hsr_seq_block_is_old(src_blk))
  409. continue;
  410. merge_blk = hsr_get_seq_block(node_real, src_blk->block_idx);
  411. if (!merge_blk)
  412. continue;
  413. merge_blk->time = min(merge_blk->time, src_blk->time);
  414. for (i = 0; i < node_real->seq_port_cnt; i++) {
  415. bitmap_or(merge_blk->seq_nrs[i], merge_blk->seq_nrs[i],
  416. src_blk->seq_nrs[i], HSR_SEQ_BLOCK_SIZE);
  417. }
  418. }
  419. hsr_unlock_seq_out_pair(node_real, node_curr);
  420. node_real->addr_B_port = port_rcv->type;
  421. spin_lock_bh(&hsr->list_lock);
  422. if (!node_curr->removed) {
  423. list_del_rcu(&node_curr->mac_list);
  424. node_curr->removed = true;
  425. call_rcu(&node_curr->rcu_head, hsr_free_node_rcu);
  426. }
  427. spin_unlock_bh(&hsr->list_lock);
  428. done:
  429. /* Push back here */
  430. skb_push(skb, total_pull_size);
  431. }
  432. /* 'skb' is a frame meant for this host, that is to be passed to upper layers.
  433. *
  434. * If the frame was sent by a node's B interface, replace the source
  435. * address with that node's "official" address (macaddress_A) so that upper
  436. * layers recognize where it came from.
  437. */
  438. void hsr_addr_subst_source(struct hsr_node *node, struct sk_buff *skb)
  439. {
  440. if (!skb_mac_header_was_set(skb)) {
  441. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  442. return;
  443. }
  444. memcpy(&eth_hdr(skb)->h_source, node->macaddress_A, ETH_ALEN);
  445. }
  446. /* 'skb' is a frame meant for another host.
  447. * 'port' is the outgoing interface
  448. *
  449. * Substitute the target (dest) MAC address if necessary, so the it matches the
  450. * recipient interface MAC address, regardless of whether that is the
  451. * recipient's A or B interface.
  452. * This is needed to keep the packets flowing through switches that learn on
  453. * which "side" the different interfaces are.
  454. */
  455. void hsr_addr_subst_dest(struct hsr_node *node_src, struct sk_buff *skb,
  456. struct hsr_port *port)
  457. {
  458. struct hsr_node *node_dst;
  459. if (!skb_mac_header_was_set(skb)) {
  460. WARN_ONCE(1, "%s: Mac header not set\n", __func__);
  461. return;
  462. }
  463. if (!is_unicast_ether_addr(eth_hdr(skb)->h_dest))
  464. return;
  465. node_dst = find_node_by_addr_A(&port->hsr->node_db,
  466. eth_hdr(skb)->h_dest);
  467. if (!node_dst && port->hsr->redbox)
  468. node_dst = find_node_by_addr_A(&port->hsr->proxy_node_db,
  469. eth_hdr(skb)->h_dest);
  470. if (!node_dst) {
  471. if (port->hsr->prot_version != PRP_V1 && net_ratelimit())
  472. netdev_err(skb->dev, "%s: Unknown node\n", __func__);
  473. return;
  474. }
  475. if (port->type != node_dst->addr_B_port)
  476. return;
  477. if (is_valid_ether_addr(node_dst->macaddress_B))
  478. ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
  479. }
  480. void hsr_register_frame_in(struct hsr_node *node, struct hsr_port *port,
  481. u16 sequence_nr)
  482. {
  483. node->time_in[port->type] = jiffies;
  484. node->time_in_stale[port->type] = false;
  485. }
  486. /* Duplicate discard algorithm: we maintain a bitmap where we set a bit for
  487. * every seen sequence number. The bitmap is split into blocks and the block
  488. * management is detailed in hsr_get_seq_block(). In any case, we err on the
  489. * side of accepting a packet, as the specification requires the algorithm to
  490. * be "designed such that it never rejects a legitimate frame, while occasional
  491. * acceptance of a duplicate can be tolerated." (IEC 62439-3:2021, 4.1.10.3).
  492. * While this requirement is explicit for PRP, applying it to HSR does no harm
  493. * either.
  494. *
  495. * 'frame' is the frame to be sent
  496. * 'port_type' is the type of the outgoing interface
  497. *
  498. * Return:
  499. * 1 if frame can be shown to have been sent recently on this interface,
  500. * 0 otherwise
  501. */
  502. static int hsr_check_duplicate(struct hsr_frame_info *frame,
  503. unsigned int port_type)
  504. {
  505. u16 sequence_nr, seq_bit, block_idx;
  506. struct hsr_seq_block *block;
  507. struct hsr_node *node;
  508. node = frame->node_src;
  509. sequence_nr = frame->sequence_nr;
  510. if (WARN_ON_ONCE(port_type >= node->seq_port_cnt))
  511. return 0;
  512. spin_lock_bh(&node->seq_out_lock);
  513. block_idx = hsr_seq_block_index(sequence_nr);
  514. block = hsr_get_seq_block(node, block_idx);
  515. if (!block)
  516. goto out_new;
  517. seq_bit = hsr_seq_block_bit(sequence_nr);
  518. if (__test_and_set_bit(seq_bit, block->seq_nrs[port_type]))
  519. goto out_seen;
  520. out_new:
  521. spin_unlock_bh(&node->seq_out_lock);
  522. return 0;
  523. out_seen:
  524. spin_unlock_bh(&node->seq_out_lock);
  525. return 1;
  526. }
  527. /* HSR duplicate discard: we check if the same frame has already been sent on
  528. * this outgoing interface. The check follows the general duplicate discard
  529. * algorithm.
  530. *
  531. * 'port' is the outgoing interface
  532. * 'frame' is the frame to be sent
  533. *
  534. * Return:
  535. * 1 if frame can be shown to have been sent recently on this interface,
  536. * 0 otherwise
  537. */
  538. int hsr_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame)
  539. {
  540. return hsr_check_duplicate(frame, port->type - 1);
  541. }
  542. /* PRP duplicate discard: we only consider frames that are received on port A
  543. * or port B and should go to the master port. For those, we check if they have
  544. * already been received by the host, i.e., master port. The check uses the
  545. * general duplicate discard algorithm, but without tracking multiple ports.
  546. *
  547. * 'port' is the outgoing interface
  548. * 'frame' is the frame to be sent
  549. *
  550. * Return:
  551. * 1 if frame can be shown to have been sent recently on this interface,
  552. * 0 otherwise
  553. */
  554. int prp_register_frame_out(struct hsr_port *port, struct hsr_frame_info *frame)
  555. {
  556. /* out-going frames are always in order */
  557. if (frame->port_rcv->type == HSR_PT_MASTER)
  558. return 0;
  559. /* for PRP we should only forward frames from the slave ports
  560. * to the master port
  561. */
  562. if (port->type != HSR_PT_MASTER)
  563. return 1;
  564. return hsr_check_duplicate(frame, 0);
  565. }
  566. EXPORT_SYMBOL_IF_KUNIT(prp_register_frame_out);
  567. static struct hsr_port *get_late_port(struct hsr_priv *hsr,
  568. struct hsr_node *node)
  569. {
  570. if (node->time_in_stale[HSR_PT_SLAVE_A])
  571. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  572. if (node->time_in_stale[HSR_PT_SLAVE_B])
  573. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  574. if (time_after(node->time_in[HSR_PT_SLAVE_B],
  575. node->time_in[HSR_PT_SLAVE_A] +
  576. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  577. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  578. if (time_after(node->time_in[HSR_PT_SLAVE_A],
  579. node->time_in[HSR_PT_SLAVE_B] +
  580. msecs_to_jiffies(MAX_SLAVE_DIFF)))
  581. return hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  582. return NULL;
  583. }
  584. /* Remove stale sequence_nr records. Called by timer every
  585. * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
  586. */
  587. void hsr_prune_nodes(struct timer_list *t)
  588. {
  589. struct hsr_priv *hsr = timer_container_of(hsr, t, prune_timer);
  590. struct hsr_node *node;
  591. struct hsr_node *tmp;
  592. struct hsr_port *port;
  593. unsigned long timestamp;
  594. unsigned long time_a, time_b;
  595. spin_lock_bh(&hsr->list_lock);
  596. list_for_each_entry_safe(node, tmp, &hsr->node_db, mac_list) {
  597. /* Don't prune own node. Neither time_in[HSR_PT_SLAVE_A]
  598. * nor time_in[HSR_PT_SLAVE_B], will ever be updated for
  599. * the master port. Thus the master node will be repeatedly
  600. * pruned leading to packet loss.
  601. */
  602. if (hsr_addr_is_self(hsr, node->macaddress_A))
  603. continue;
  604. /* Shorthand */
  605. time_a = node->time_in[HSR_PT_SLAVE_A];
  606. time_b = node->time_in[HSR_PT_SLAVE_B];
  607. /* Check for timestamps old enough to risk wrap-around */
  608. if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET / 2))
  609. node->time_in_stale[HSR_PT_SLAVE_A] = true;
  610. if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET / 2))
  611. node->time_in_stale[HSR_PT_SLAVE_B] = true;
  612. /* Get age of newest frame from node.
  613. * At least one time_in is OK here; nodes get pruned long
  614. * before both time_ins can get stale
  615. */
  616. timestamp = time_a;
  617. if (node->time_in_stale[HSR_PT_SLAVE_A] ||
  618. (!node->time_in_stale[HSR_PT_SLAVE_B] &&
  619. time_after(time_b, time_a)))
  620. timestamp = time_b;
  621. /* Warn of ring error only as long as we get frames at all */
  622. if (time_is_after_jiffies(timestamp +
  623. msecs_to_jiffies(1.5 * MAX_SLAVE_DIFF))) {
  624. rcu_read_lock();
  625. port = get_late_port(hsr, node);
  626. if (port)
  627. hsr_nl_ringerror(hsr, node->macaddress_A, port);
  628. rcu_read_unlock();
  629. }
  630. /* Prune old entries */
  631. if (time_is_before_jiffies(timestamp +
  632. msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
  633. hsr_nl_nodedown(hsr, node->macaddress_A);
  634. if (!node->removed) {
  635. list_del_rcu(&node->mac_list);
  636. node->removed = true;
  637. /* Note that we need to free this entry later: */
  638. call_rcu(&node->rcu_head, hsr_free_node_rcu);
  639. }
  640. }
  641. }
  642. spin_unlock_bh(&hsr->list_lock);
  643. /* Restart timer */
  644. mod_timer(&hsr->prune_timer,
  645. jiffies + msecs_to_jiffies(PRUNE_PERIOD));
  646. }
  647. void hsr_prune_proxy_nodes(struct timer_list *t)
  648. {
  649. struct hsr_priv *hsr = timer_container_of(hsr, t, prune_proxy_timer);
  650. unsigned long timestamp;
  651. struct hsr_node *node;
  652. struct hsr_node *tmp;
  653. spin_lock_bh(&hsr->list_lock);
  654. list_for_each_entry_safe(node, tmp, &hsr->proxy_node_db, mac_list) {
  655. /* Don't prune RedBox node. */
  656. if (hsr_addr_is_redbox(hsr, node->macaddress_A))
  657. continue;
  658. timestamp = node->time_in[HSR_PT_INTERLINK];
  659. /* Prune old entries */
  660. if (time_is_before_jiffies(timestamp +
  661. msecs_to_jiffies(HSR_PROXY_NODE_FORGET_TIME))) {
  662. hsr_nl_nodedown(hsr, node->macaddress_A);
  663. if (!node->removed) {
  664. list_del_rcu(&node->mac_list);
  665. node->removed = true;
  666. /* Note that we need to free this entry later: */
  667. call_rcu(&node->rcu_head, hsr_free_node_rcu);
  668. }
  669. }
  670. }
  671. spin_unlock_bh(&hsr->list_lock);
  672. /* Restart timer */
  673. mod_timer(&hsr->prune_proxy_timer,
  674. jiffies + msecs_to_jiffies(PRUNE_PROXY_PERIOD));
  675. }
  676. void *hsr_get_next_node(struct hsr_priv *hsr, void *_pos,
  677. unsigned char addr[ETH_ALEN])
  678. {
  679. struct hsr_node *node;
  680. if (!_pos) {
  681. node = list_first_or_null_rcu(&hsr->node_db,
  682. struct hsr_node, mac_list);
  683. if (node)
  684. ether_addr_copy(addr, node->macaddress_A);
  685. return node;
  686. }
  687. node = _pos;
  688. list_for_each_entry_continue_rcu(node, &hsr->node_db, mac_list) {
  689. ether_addr_copy(addr, node->macaddress_A);
  690. return node;
  691. }
  692. return NULL;
  693. }
  694. /* Fill the last sequence number that has been received from node on if1 by
  695. * finding the last sequence number sent on port B; accordingly get the last
  696. * received sequence number for if2 using sent sequence numbers on port A.
  697. */
  698. static void fill_last_seq_nrs(struct hsr_node *node, u16 *if1_seq, u16 *if2_seq)
  699. {
  700. struct hsr_seq_block *block;
  701. unsigned int block_off;
  702. size_t block_sz;
  703. u16 seq_bit;
  704. spin_lock_bh(&node->seq_out_lock);
  705. /* Get last inserted block */
  706. block_off = (node->next_block - 1) & (HSR_MAX_SEQ_BLOCKS - 1);
  707. block_sz = hsr_seq_block_size(node);
  708. block = node->block_buf + block_off * block_sz;
  709. if (!bitmap_empty(block->seq_nrs[HSR_PT_SLAVE_B - 1],
  710. HSR_SEQ_BLOCK_SIZE)) {
  711. seq_bit = find_last_bit(block->seq_nrs[HSR_PT_SLAVE_B - 1],
  712. HSR_SEQ_BLOCK_SIZE);
  713. *if1_seq = (block->block_idx << HSR_SEQ_BLOCK_SHIFT) | seq_bit;
  714. }
  715. if (!bitmap_empty(block->seq_nrs[HSR_PT_SLAVE_A - 1],
  716. HSR_SEQ_BLOCK_SIZE)) {
  717. seq_bit = find_last_bit(block->seq_nrs[HSR_PT_SLAVE_A - 1],
  718. HSR_SEQ_BLOCK_SIZE);
  719. *if2_seq = (block->block_idx << HSR_SEQ_BLOCK_SHIFT) | seq_bit;
  720. }
  721. spin_unlock_bh(&node->seq_out_lock);
  722. }
  723. int hsr_get_node_data(struct hsr_priv *hsr,
  724. const unsigned char *addr,
  725. unsigned char addr_b[ETH_ALEN],
  726. unsigned int *addr_b_ifindex,
  727. int *if1_age,
  728. u16 *if1_seq,
  729. int *if2_age,
  730. u16 *if2_seq)
  731. {
  732. struct hsr_node *node;
  733. struct hsr_port *port;
  734. unsigned long tdiff;
  735. node = find_node_by_addr_A(&hsr->node_db, addr);
  736. if (!node)
  737. return -ENOENT;
  738. ether_addr_copy(addr_b, node->macaddress_B);
  739. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_A];
  740. if (node->time_in_stale[HSR_PT_SLAVE_A])
  741. *if1_age = INT_MAX;
  742. #if HZ <= MSEC_PER_SEC
  743. else if (tdiff > msecs_to_jiffies(INT_MAX))
  744. *if1_age = INT_MAX;
  745. #endif
  746. else
  747. *if1_age = jiffies_to_msecs(tdiff);
  748. tdiff = jiffies - node->time_in[HSR_PT_SLAVE_B];
  749. if (node->time_in_stale[HSR_PT_SLAVE_B])
  750. *if2_age = INT_MAX;
  751. #if HZ <= MSEC_PER_SEC
  752. else if (tdiff > msecs_to_jiffies(INT_MAX))
  753. *if2_age = INT_MAX;
  754. #endif
  755. else
  756. *if2_age = jiffies_to_msecs(tdiff);
  757. /* Present sequence numbers as if they were incoming on interface */
  758. *if1_seq = 0;
  759. *if2_seq = 0;
  760. if (hsr->prot_version != PRP_V1)
  761. fill_last_seq_nrs(node, if1_seq, if2_seq);
  762. if (node->addr_B_port != HSR_PT_NONE) {
  763. port = hsr_port_get_hsr(hsr, node->addr_B_port);
  764. *addr_b_ifindex = port->dev->ifindex;
  765. } else {
  766. *addr_b_ifindex = -1;
  767. }
  768. return 0;
  769. }