hsr_slave.c 5.1 KB

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