hsr_slave.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, but for PRP
  44. * there could be non tagged frames as well from Single
  45. * attached nodes (SANs).
  46. */
  47. protocol = eth_hdr(skb)->h_proto;
  48. if (hsr->proto_ops->invalid_dan_ingress_frame &&
  49. hsr->proto_ops->invalid_dan_ingress_frame(protocol))
  50. goto finish_pass;
  51. skb_push(skb, ETH_HLEN);
  52. skb_reset_mac_header(skb);
  53. if ((!hsr->prot_version && protocol == htons(ETH_P_PRP)) ||
  54. protocol == htons(ETH_P_HSR))
  55. skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
  56. skb_reset_mac_len(skb);
  57. hsr_forward_skb(skb, port);
  58. finish_consume:
  59. return RX_HANDLER_CONSUMED;
  60. finish_pass:
  61. return RX_HANDLER_PASS;
  62. }
  63. bool hsr_port_exists(const struct net_device *dev)
  64. {
  65. return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
  66. }
  67. static int hsr_check_dev_ok(struct net_device *dev,
  68. struct netlink_ext_ack *extack)
  69. {
  70. /* Don't allow HSR on non-ethernet like devices */
  71. if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
  72. dev->addr_len != ETH_ALEN) {
  73. NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
  74. return -EINVAL;
  75. }
  76. /* Don't allow enslaving hsr devices */
  77. if (is_hsr_master(dev)) {
  78. NL_SET_ERR_MSG_MOD(extack,
  79. "Cannot create trees of HSR devices.");
  80. return -EINVAL;
  81. }
  82. if (hsr_port_exists(dev)) {
  83. NL_SET_ERR_MSG_MOD(extack,
  84. "This device is already a HSR slave.");
  85. return -EINVAL;
  86. }
  87. if (is_vlan_dev(dev)) {
  88. NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
  89. return -EINVAL;
  90. }
  91. if (dev->priv_flags & IFF_DONT_BRIDGE) {
  92. NL_SET_ERR_MSG_MOD(extack,
  93. "This device does not support bridging.");
  94. return -EOPNOTSUPP;
  95. }
  96. /* HSR over bonded devices has not been tested, but I'm not sure it
  97. * won't work...
  98. */
  99. return 0;
  100. }
  101. /* Setup device to be added to the HSR bridge. */
  102. static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
  103. struct hsr_port *port,
  104. struct netlink_ext_ack *extack)
  105. {
  106. struct net_device *hsr_dev;
  107. struct hsr_port *master;
  108. int res;
  109. res = dev_set_promiscuity(dev, 1);
  110. if (res)
  111. return res;
  112. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  113. hsr_dev = master->dev;
  114. res = netdev_upper_dev_link(dev, hsr_dev, extack);
  115. if (res)
  116. goto fail_upper_dev_link;
  117. res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
  118. if (res)
  119. goto fail_rx_handler;
  120. dev_disable_lro(dev);
  121. return 0;
  122. fail_rx_handler:
  123. netdev_upper_dev_unlink(dev, hsr_dev);
  124. fail_upper_dev_link:
  125. dev_set_promiscuity(dev, -1);
  126. return res;
  127. }
  128. int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
  129. enum hsr_port_type type, struct netlink_ext_ack *extack)
  130. {
  131. struct hsr_port *port, *master;
  132. int res;
  133. if (type != HSR_PT_MASTER) {
  134. res = hsr_check_dev_ok(dev, extack);
  135. if (res)
  136. return res;
  137. }
  138. port = hsr_port_get_hsr(hsr, type);
  139. if (port)
  140. return -EBUSY; /* This port already exists */
  141. port = kzalloc(sizeof(*port), GFP_KERNEL);
  142. if (!port)
  143. return -ENOMEM;
  144. port->hsr = hsr;
  145. port->dev = dev;
  146. port->type = type;
  147. if (type != HSR_PT_MASTER) {
  148. res = hsr_portdev_setup(hsr, dev, port, extack);
  149. if (res)
  150. goto fail_dev_setup;
  151. }
  152. list_add_tail_rcu(&port->port_list, &hsr->ports);
  153. synchronize_rcu();
  154. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  155. netdev_update_features(master->dev);
  156. dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
  157. return 0;
  158. fail_dev_setup:
  159. kfree(port);
  160. return res;
  161. }
  162. void hsr_del_port(struct hsr_port *port)
  163. {
  164. struct hsr_priv *hsr;
  165. struct hsr_port *master;
  166. hsr = port->hsr;
  167. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  168. list_del_rcu(&port->port_list);
  169. if (port != master) {
  170. netdev_update_features(master->dev);
  171. dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
  172. netdev_rx_handler_unregister(port->dev);
  173. dev_set_promiscuity(port->dev, -1);
  174. netdev_upper_dev_unlink(port->dev, master->dev);
  175. }
  176. synchronize_rcu();
  177. kfree(port);
  178. }