hsr_slave.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * Frame handler other utility functions for HSR and PRP.
  8. */
  9. #include "hsr_slave.h"
  10. #include <linux/etherdevice.h>
  11. #include <linux/if_arp.h>
  12. #include <linux/if_vlan.h>
  13. #include "hsr_main.h"
  14. #include "hsr_device.h"
  15. #include "hsr_forward.h"
  16. #include "hsr_framereg.h"
  17. bool hsr_invalid_dan_ingress_frame(__be16 protocol)
  18. {
  19. return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR));
  20. }
  21. static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
  22. {
  23. struct sk_buff *skb = *pskb;
  24. struct hsr_port *port;
  25. struct hsr_priv *hsr;
  26. __be16 protocol;
  27. /* Packets from dev_loopback_xmit() do not have L2 header, bail out */
  28. if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
  29. return RX_HANDLER_PASS;
  30. if (!skb_mac_header_was_set(skb)) {
  31. WARN_ONCE(1, "%s: skb invalid", __func__);
  32. return RX_HANDLER_PASS;
  33. }
  34. port = hsr_port_get_rcu(skb->dev);
  35. if (!port)
  36. goto finish_pass;
  37. hsr = port->hsr;
  38. if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
  39. /* Directly kill frames sent by ourselves */
  40. kfree_skb(skb);
  41. goto finish_consume;
  42. }
  43. /* For HSR, only tagged frames are expected (unless the device offloads
  44. * HSR tag removal), but for PRP there could be non tagged frames as
  45. * well from Single attached nodes (SANs).
  46. */
  47. protocol = eth_hdr(skb)->h_proto;
  48. if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) &&
  49. port->type != HSR_PT_INTERLINK &&
  50. hsr->proto_ops->invalid_dan_ingress_frame &&
  51. hsr->proto_ops->invalid_dan_ingress_frame(protocol))
  52. goto finish_pass;
  53. skb_push(skb, ETH_HLEN);
  54. skb_reset_mac_header(skb);
  55. if ((!hsr->prot_version && protocol == htons(ETH_P_PRP)) ||
  56. protocol == htons(ETH_P_HSR))
  57. skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
  58. skb_reset_mac_len(skb);
  59. /* Only the frames received over the interlink port will assign a
  60. * sequence number and require synchronisation vs other sender.
  61. */
  62. if (port->type == HSR_PT_INTERLINK) {
  63. spin_lock_bh(&hsr->seqnr_lock);
  64. hsr_forward_skb(skb, port);
  65. spin_unlock_bh(&hsr->seqnr_lock);
  66. } else {
  67. hsr_forward_skb(skb, port);
  68. }
  69. finish_consume:
  70. return RX_HANDLER_CONSUMED;
  71. finish_pass:
  72. return RX_HANDLER_PASS;
  73. }
  74. bool hsr_port_exists(const struct net_device *dev)
  75. {
  76. return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
  77. }
  78. static int hsr_check_dev_ok(struct net_device *dev,
  79. struct netlink_ext_ack *extack)
  80. {
  81. /* Don't allow HSR on non-ethernet like devices */
  82. if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
  83. dev->addr_len != ETH_ALEN) {
  84. NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
  85. return -EINVAL;
  86. }
  87. /* Don't allow enslaving hsr devices */
  88. if (is_hsr_master(dev)) {
  89. NL_SET_ERR_MSG_MOD(extack,
  90. "Cannot create trees of HSR devices.");
  91. return -EINVAL;
  92. }
  93. if (hsr_port_exists(dev)) {
  94. NL_SET_ERR_MSG_MOD(extack,
  95. "This device is already a HSR slave.");
  96. return -EINVAL;
  97. }
  98. if (is_vlan_dev(dev)) {
  99. NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
  100. return -EINVAL;
  101. }
  102. if (dev->priv_flags & IFF_DONT_BRIDGE) {
  103. NL_SET_ERR_MSG_MOD(extack,
  104. "This device does not support bridging.");
  105. return -EOPNOTSUPP;
  106. }
  107. /* HSR over bonded devices has not been tested, but I'm not sure it
  108. * won't work...
  109. */
  110. return 0;
  111. }
  112. /* Setup device to be added to the HSR bridge. */
  113. static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
  114. struct hsr_port *port,
  115. struct netlink_ext_ack *extack)
  116. {
  117. struct net_device *hsr_dev;
  118. struct hsr_port *master;
  119. int res;
  120. /* Don't use promiscuous mode for offload since L2 frame forward
  121. * happens at the offloaded hardware.
  122. */
  123. if (!port->hsr->fwd_offloaded) {
  124. res = dev_set_promiscuity(dev, 1);
  125. if (res)
  126. return res;
  127. }
  128. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  129. hsr_dev = master->dev;
  130. res = netdev_upper_dev_link(dev, hsr_dev, extack);
  131. if (res)
  132. goto fail_upper_dev_link;
  133. res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
  134. if (res)
  135. goto fail_rx_handler;
  136. dev_disable_lro(dev);
  137. return 0;
  138. fail_rx_handler:
  139. netdev_upper_dev_unlink(dev, hsr_dev);
  140. fail_upper_dev_link:
  141. if (!port->hsr->fwd_offloaded)
  142. dev_set_promiscuity(dev, -1);
  143. return res;
  144. }
  145. int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
  146. enum hsr_port_type type, struct netlink_ext_ack *extack)
  147. {
  148. struct hsr_port *port, *master;
  149. int res;
  150. if (type != HSR_PT_MASTER) {
  151. res = hsr_check_dev_ok(dev, extack);
  152. if (res)
  153. return res;
  154. }
  155. port = hsr_port_get_hsr(hsr, type);
  156. if (port)
  157. return -EBUSY; /* This port already exists */
  158. port = kzalloc(sizeof(*port), GFP_KERNEL);
  159. if (!port)
  160. return -ENOMEM;
  161. port->hsr = hsr;
  162. port->dev = dev;
  163. port->type = type;
  164. if (type != HSR_PT_MASTER) {
  165. res = hsr_portdev_setup(hsr, dev, port, extack);
  166. if (res)
  167. goto fail_dev_setup;
  168. }
  169. list_add_tail_rcu(&port->port_list, &hsr->ports);
  170. synchronize_rcu();
  171. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  172. netdev_update_features(master->dev);
  173. dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
  174. return 0;
  175. fail_dev_setup:
  176. kfree(port);
  177. return res;
  178. }
  179. void hsr_del_port(struct hsr_port *port)
  180. {
  181. struct hsr_priv *hsr;
  182. struct hsr_port *master;
  183. hsr = port->hsr;
  184. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  185. list_del_rcu(&port->port_list);
  186. if (port != master) {
  187. netdev_update_features(master->dev);
  188. dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
  189. netdev_rx_handler_unregister(port->dev);
  190. if (!port->hsr->fwd_offloaded)
  191. dev_set_promiscuity(port->dev, -1);
  192. netdev_upper_dev_unlink(port->dev, master->dev);
  193. }
  194. synchronize_rcu();
  195. kfree(port);
  196. }