hsr_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /* Copyright 2011-2013 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-2013 Arvid Brodin, arvid.brodin@xdin.com
  10. *
  11. * In addition to routines for registering and unregistering HSR support, this
  12. * file also contains the receive routine that handles all incoming frames with
  13. * Ethertype (protocol) ETH_P_PRP (HSRv0), and network device event handling.
  14. */
  15. #include <linux/netdevice.h>
  16. #include <linux/rculist.h>
  17. #include <linux/timer.h>
  18. #include <linux/etherdevice.h>
  19. #include "hsr_main.h"
  20. #include "hsr_device.h"
  21. #include "hsr_netlink.h"
  22. #include "hsr_framereg.h"
  23. /* List of all registered virtual HSR devices */
  24. static LIST_HEAD(hsr_list);
  25. void register_hsr_master(struct hsr_priv *hsr_priv)
  26. {
  27. list_add_tail_rcu(&hsr_priv->hsr_list, &hsr_list);
  28. }
  29. void unregister_hsr_master(struct hsr_priv *hsr_priv)
  30. {
  31. struct hsr_priv *hsr_priv_it;
  32. list_for_each_entry(hsr_priv_it, &hsr_list, hsr_list)
  33. if (hsr_priv_it == hsr_priv) {
  34. list_del_rcu(&hsr_priv_it->hsr_list);
  35. return;
  36. }
  37. }
  38. bool is_hsr_slave(struct net_device *dev)
  39. {
  40. struct hsr_priv *hsr_priv_it;
  41. list_for_each_entry_rcu(hsr_priv_it, &hsr_list, hsr_list) {
  42. if (dev == hsr_priv_it->slave[0])
  43. return true;
  44. if (dev == hsr_priv_it->slave[1])
  45. return true;
  46. }
  47. return false;
  48. }
  49. /* If dev is a HSR slave device, return the virtual master device. Return NULL
  50. * otherwise.
  51. */
  52. static struct hsr_priv *get_hsr_master(struct net_device *dev)
  53. {
  54. struct hsr_priv *hsr_priv;
  55. rcu_read_lock();
  56. list_for_each_entry_rcu(hsr_priv, &hsr_list, hsr_list)
  57. if ((dev == hsr_priv->slave[0]) ||
  58. (dev == hsr_priv->slave[1])) {
  59. rcu_read_unlock();
  60. return hsr_priv;
  61. }
  62. rcu_read_unlock();
  63. return NULL;
  64. }
  65. /* If dev is a HSR slave device, return the other slave device. Return NULL
  66. * otherwise.
  67. */
  68. static struct net_device *get_other_slave(struct hsr_priv *hsr_priv,
  69. struct net_device *dev)
  70. {
  71. if (dev == hsr_priv->slave[0])
  72. return hsr_priv->slave[1];
  73. if (dev == hsr_priv->slave[1])
  74. return hsr_priv->slave[0];
  75. return NULL;
  76. }
  77. static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
  78. void *ptr)
  79. {
  80. struct net_device *slave, *other_slave;
  81. struct hsr_priv *hsr_priv;
  82. int old_operstate;
  83. int mtu_max;
  84. int res;
  85. struct net_device *dev;
  86. dev = netdev_notifier_info_to_dev(ptr);
  87. hsr_priv = get_hsr_master(dev);
  88. if (hsr_priv) {
  89. /* dev is a slave device */
  90. slave = dev;
  91. other_slave = get_other_slave(hsr_priv, slave);
  92. } else {
  93. if (!is_hsr_master(dev))
  94. return NOTIFY_DONE;
  95. hsr_priv = netdev_priv(dev);
  96. slave = hsr_priv->slave[0];
  97. other_slave = hsr_priv->slave[1];
  98. }
  99. switch (event) {
  100. case NETDEV_UP: /* Administrative state DOWN */
  101. case NETDEV_DOWN: /* Administrative state UP */
  102. case NETDEV_CHANGE: /* Link (carrier) state changes */
  103. old_operstate = hsr_priv->dev->operstate;
  104. hsr_set_carrier(hsr_priv->dev, slave, other_slave);
  105. /* netif_stacked_transfer_operstate() cannot be used here since
  106. * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
  107. */
  108. hsr_set_operstate(hsr_priv->dev, slave, other_slave);
  109. hsr_check_announce(hsr_priv->dev, old_operstate);
  110. break;
  111. case NETDEV_CHANGEADDR:
  112. /* This should not happen since there's no ndo_set_mac_address()
  113. * for HSR devices - i.e. not supported.
  114. */
  115. if (dev == hsr_priv->dev)
  116. break;
  117. if (dev == hsr_priv->slave[0])
  118. memcpy(hsr_priv->dev->dev_addr,
  119. hsr_priv->slave[0]->dev_addr, ETH_ALEN);
  120. /* Make sure we recognize frames from ourselves in hsr_rcv() */
  121. res = hsr_create_self_node(&hsr_priv->self_node_db,
  122. hsr_priv->dev->dev_addr,
  123. hsr_priv->slave[1] ?
  124. hsr_priv->slave[1]->dev_addr :
  125. hsr_priv->dev->dev_addr);
  126. if (res)
  127. netdev_warn(hsr_priv->dev,
  128. "Could not update HSR node address.\n");
  129. if (dev == hsr_priv->slave[0])
  130. call_netdevice_notifiers(NETDEV_CHANGEADDR, hsr_priv->dev);
  131. break;
  132. case NETDEV_CHANGEMTU:
  133. if (dev == hsr_priv->dev)
  134. break; /* Handled in ndo_change_mtu() */
  135. mtu_max = hsr_get_max_mtu(hsr_priv);
  136. if (hsr_priv->dev->mtu > mtu_max)
  137. dev_set_mtu(hsr_priv->dev, mtu_max);
  138. break;
  139. case NETDEV_UNREGISTER:
  140. if (dev == hsr_priv->slave[0])
  141. hsr_priv->slave[0] = NULL;
  142. if (dev == hsr_priv->slave[1])
  143. hsr_priv->slave[1] = NULL;
  144. /* There should really be a way to set a new slave device... */
  145. break;
  146. case NETDEV_PRE_TYPE_CHANGE:
  147. /* HSR works only on Ethernet devices. Refuse slave to change
  148. * its type.
  149. */
  150. return NOTIFY_BAD;
  151. }
  152. return NOTIFY_DONE;
  153. }
  154. static struct timer_list prune_timer;
  155. static void prune_nodes_all(unsigned long data)
  156. {
  157. struct hsr_priv *hsr_priv;
  158. rcu_read_lock();
  159. list_for_each_entry_rcu(hsr_priv, &hsr_list, hsr_list)
  160. hsr_prune_nodes(hsr_priv);
  161. rcu_read_unlock();
  162. prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD);
  163. add_timer(&prune_timer);
  164. }
  165. static struct sk_buff *hsr_pull_tag(struct sk_buff *skb)
  166. {
  167. struct hsr_tag *hsr_tag;
  168. struct sk_buff *skb2;
  169. skb2 = skb_share_check(skb, GFP_ATOMIC);
  170. if (unlikely(!skb2))
  171. goto err_free;
  172. skb = skb2;
  173. if (unlikely(!pskb_may_pull(skb, HSR_TAGLEN)))
  174. goto err_free;
  175. hsr_tag = (struct hsr_tag *) skb->data;
  176. skb->protocol = hsr_tag->encap_proto;
  177. skb_pull(skb, HSR_TAGLEN);
  178. return skb;
  179. err_free:
  180. kfree_skb(skb);
  181. return NULL;
  182. }
  183. /* The uses I can see for these HSR supervision frames are:
  184. * 1) Use the frames that are sent after node initialization ("HSR_TLV.Type =
  185. * 22") to reset any sequence_nr counters belonging to that node. Useful if
  186. * the other node's counter has been reset for some reason.
  187. * --
  188. * Or not - resetting the counter and bridging the frame would create a
  189. * loop, unfortunately.
  190. *
  191. * 2) Use the LifeCheck frames to detect ring breaks. I.e. if no LifeCheck
  192. * frame is received from a particular node, we know something is wrong.
  193. * We just register these (as with normal frames) and throw them away.
  194. *
  195. * 3) Allow different MAC addresses for the two slave interfaces, using the
  196. * MacAddressA field.
  197. */
  198. static bool is_supervision_frame(struct hsr_priv *hsr_priv, struct sk_buff *skb)
  199. {
  200. struct hsr_sup_tag *hsr_stag;
  201. if (!ether_addr_equal(eth_hdr(skb)->h_dest,
  202. hsr_priv->sup_multicast_addr))
  203. return false;
  204. hsr_stag = (struct hsr_sup_tag *) skb->data;
  205. if (get_hsr_stag_path(hsr_stag) != 0x0f)
  206. return false;
  207. if ((hsr_stag->HSR_TLV_Type != HSR_TLV_ANNOUNCE) &&
  208. (hsr_stag->HSR_TLV_Type != HSR_TLV_LIFE_CHECK))
  209. return false;
  210. if (hsr_stag->HSR_TLV_Length != 12)
  211. return false;
  212. return true;
  213. }
  214. /* Implementation somewhat according to IEC-62439-3, p. 43
  215. */
  216. static int hsr_rcv(struct sk_buff *skb, struct net_device *dev,
  217. struct packet_type *pt, struct net_device *orig_dev)
  218. {
  219. struct hsr_priv *hsr_priv;
  220. struct net_device *other_slave;
  221. struct node_entry *node;
  222. bool deliver_to_self;
  223. struct sk_buff *skb_deliver;
  224. enum hsr_dev_idx dev_in_idx, dev_other_idx;
  225. bool dup_out;
  226. int ret;
  227. hsr_priv = get_hsr_master(dev);
  228. if (!hsr_priv) {
  229. /* Non-HSR-slave device 'dev' is connected to a HSR network */
  230. kfree_skb(skb);
  231. dev->stats.rx_errors++;
  232. return NET_RX_SUCCESS;
  233. }
  234. if (dev == hsr_priv->slave[0]) {
  235. dev_in_idx = HSR_DEV_SLAVE_A;
  236. dev_other_idx = HSR_DEV_SLAVE_B;
  237. } else {
  238. dev_in_idx = HSR_DEV_SLAVE_B;
  239. dev_other_idx = HSR_DEV_SLAVE_A;
  240. }
  241. node = hsr_find_node(&hsr_priv->self_node_db, skb);
  242. if (node) {
  243. /* Always kill frames sent by ourselves */
  244. kfree_skb(skb);
  245. return NET_RX_SUCCESS;
  246. }
  247. /* Is this frame a candidate for local reception? */
  248. deliver_to_self = false;
  249. if ((skb->pkt_type == PACKET_HOST) ||
  250. (skb->pkt_type == PACKET_MULTICAST) ||
  251. (skb->pkt_type == PACKET_BROADCAST))
  252. deliver_to_self = true;
  253. else if (ether_addr_equal(eth_hdr(skb)->h_dest,
  254. hsr_priv->dev->dev_addr)) {
  255. skb->pkt_type = PACKET_HOST;
  256. deliver_to_self = true;
  257. }
  258. rcu_read_lock(); /* node_db */
  259. node = hsr_find_node(&hsr_priv->node_db, skb);
  260. if (is_supervision_frame(hsr_priv, skb)) {
  261. skb_pull(skb, sizeof(struct hsr_sup_tag));
  262. node = hsr_merge_node(hsr_priv, node, skb, dev_in_idx);
  263. if (!node) {
  264. rcu_read_unlock(); /* node_db */
  265. kfree_skb(skb);
  266. hsr_priv->dev->stats.rx_dropped++;
  267. return NET_RX_DROP;
  268. }
  269. skb_push(skb, sizeof(struct hsr_sup_tag));
  270. deliver_to_self = false;
  271. }
  272. if (!node) {
  273. /* Source node unknown; this might be a HSR frame from
  274. * another net (different multicast address). Ignore it.
  275. */
  276. rcu_read_unlock(); /* node_db */
  277. kfree_skb(skb);
  278. return NET_RX_SUCCESS;
  279. }
  280. /* Register ALL incoming frames as outgoing through the other interface.
  281. * This allows us to register frames as incoming only if they are valid
  282. * for the receiving interface, without using a specific counter for
  283. * incoming frames.
  284. */
  285. dup_out = hsr_register_frame_out(node, dev_other_idx, skb);
  286. if (!dup_out)
  287. hsr_register_frame_in(node, dev_in_idx);
  288. /* Forward this frame? */
  289. if (!dup_out && (skb->pkt_type != PACKET_HOST))
  290. other_slave = get_other_slave(hsr_priv, dev);
  291. else
  292. other_slave = NULL;
  293. if (hsr_register_frame_out(node, HSR_DEV_MASTER, skb))
  294. deliver_to_self = false;
  295. rcu_read_unlock(); /* node_db */
  296. if (!deliver_to_self && !other_slave) {
  297. kfree_skb(skb);
  298. /* Circulated frame; silently remove it. */
  299. return NET_RX_SUCCESS;
  300. }
  301. skb_deliver = skb;
  302. if (deliver_to_self && other_slave) {
  303. /* skb_clone() is not enough since we will strip the hsr tag
  304. * and do address substitution below
  305. */
  306. skb_deliver = pskb_copy(skb, GFP_ATOMIC);
  307. if (!skb_deliver) {
  308. deliver_to_self = false;
  309. hsr_priv->dev->stats.rx_dropped++;
  310. }
  311. }
  312. if (deliver_to_self) {
  313. bool multicast_frame;
  314. skb_deliver = hsr_pull_tag(skb_deliver);
  315. if (!skb_deliver) {
  316. hsr_priv->dev->stats.rx_dropped++;
  317. goto forward;
  318. }
  319. #if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
  320. /* Move everything in the header that is after the HSR tag,
  321. * to work around alignment problems caused by the 6-byte HSR
  322. * tag. In practice, this removes/overwrites the HSR tag in
  323. * the header and restores a "standard" packet.
  324. */
  325. memmove(skb_deliver->data - HSR_TAGLEN, skb_deliver->data,
  326. skb_headlen(skb_deliver));
  327. /* Adjust skb members so they correspond with the move above.
  328. * This cannot possibly underflow skb->data since hsr_pull_tag()
  329. * above succeeded.
  330. * At this point in the protocol stack, the transport and
  331. * network headers have not been set yet, and we haven't touched
  332. * the mac header nor the head. So we only need to adjust data
  333. * and tail:
  334. */
  335. skb_deliver->data -= HSR_TAGLEN;
  336. skb_deliver->tail -= HSR_TAGLEN;
  337. #endif
  338. skb_deliver->dev = hsr_priv->dev;
  339. hsr_addr_subst_source(hsr_priv, skb_deliver);
  340. multicast_frame = (skb_deliver->pkt_type == PACKET_MULTICAST);
  341. ret = netif_rx(skb_deliver);
  342. if (ret == NET_RX_DROP) {
  343. hsr_priv->dev->stats.rx_dropped++;
  344. } else {
  345. hsr_priv->dev->stats.rx_packets++;
  346. hsr_priv->dev->stats.rx_bytes += skb->len;
  347. if (multicast_frame)
  348. hsr_priv->dev->stats.multicast++;
  349. }
  350. }
  351. forward:
  352. if (other_slave) {
  353. skb_push(skb, ETH_HLEN);
  354. skb->dev = other_slave;
  355. dev_queue_xmit(skb);
  356. }
  357. return NET_RX_SUCCESS;
  358. }
  359. static struct packet_type hsr_pt __read_mostly = {
  360. .type = htons(ETH_P_PRP),
  361. .func = hsr_rcv,
  362. };
  363. static struct notifier_block hsr_nb = {
  364. .notifier_call = hsr_netdev_notify, /* Slave event notifications */
  365. };
  366. static int __init hsr_init(void)
  367. {
  368. int res;
  369. BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_TAGLEN);
  370. dev_add_pack(&hsr_pt);
  371. init_timer(&prune_timer);
  372. prune_timer.function = prune_nodes_all;
  373. prune_timer.data = 0;
  374. prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD);
  375. add_timer(&prune_timer);
  376. register_netdevice_notifier(&hsr_nb);
  377. res = hsr_netlink_init();
  378. return res;
  379. }
  380. static void __exit hsr_exit(void)
  381. {
  382. unregister_netdevice_notifier(&hsr_nb);
  383. del_timer(&prune_timer);
  384. hsr_netlink_exit();
  385. dev_remove_pack(&hsr_pt);
  386. }
  387. module_init(hsr_init);
  388. module_exit(hsr_exit);
  389. MODULE_LICENSE("GPL");