hsr_framereg.c 14 KB

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