hsr_slave.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "hsr_slave.h"
  12. #include <linux/etherdevice.h>
  13. #include <linux/if_arp.h>
  14. #include <linux/if_vlan.h>
  15. #include "hsr_main.h"
  16. #include "hsr_device.h"
  17. #include "hsr_forward.h"
  18. #include "hsr_framereg.h"
  19. static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
  20. {
  21. struct sk_buff *skb = *pskb;
  22. struct hsr_port *port;
  23. u16 protocol;
  24. if (!skb_mac_header_was_set(skb)) {
  25. WARN_ONCE(1, "%s: skb invalid", __func__);
  26. return RX_HANDLER_PASS;
  27. }
  28. rcu_read_lock(); /* hsr->node_db, hsr->ports */
  29. port = hsr_port_get_rcu(skb->dev);
  30. if (!port)
  31. goto finish_pass;
  32. if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
  33. /* Directly kill frames sent by ourselves */
  34. kfree_skb(skb);
  35. goto finish_consume;
  36. }
  37. protocol = eth_hdr(skb)->h_proto;
  38. if (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR))
  39. goto finish_pass;
  40. skb_push(skb, ETH_HLEN);
  41. hsr_forward_skb(skb, port);
  42. finish_consume:
  43. rcu_read_unlock(); /* hsr->node_db, hsr->ports */
  44. return RX_HANDLER_CONSUMED;
  45. finish_pass:
  46. rcu_read_unlock(); /* hsr->node_db, hsr->ports */
  47. return RX_HANDLER_PASS;
  48. }
  49. bool hsr_port_exists(const struct net_device *dev)
  50. {
  51. return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
  52. }
  53. static int hsr_check_dev_ok(struct net_device *dev)
  54. {
  55. /* Don't allow HSR on non-ethernet like devices */
  56. if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) ||
  57. (dev->addr_len != ETH_ALEN)) {
  58. netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n");
  59. return -EINVAL;
  60. }
  61. /* Don't allow enslaving hsr devices */
  62. if (is_hsr_master(dev)) {
  63. netdev_info(dev, "Cannot create trees of HSR devices.\n");
  64. return -EINVAL;
  65. }
  66. if (hsr_port_exists(dev)) {
  67. netdev_info(dev, "This device is already a HSR slave.\n");
  68. return -EINVAL;
  69. }
  70. if (is_vlan_dev(dev)) {
  71. netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n");
  72. return -EINVAL;
  73. }
  74. if (dev->priv_flags & IFF_DONT_BRIDGE) {
  75. netdev_info(dev, "This device does not support bridging.\n");
  76. return -EOPNOTSUPP;
  77. }
  78. /* HSR over bonded devices has not been tested, but I'm not sure it
  79. * won't work...
  80. */
  81. return 0;
  82. }
  83. /* Setup device to be added to the HSR bridge. */
  84. static int hsr_portdev_setup(struct net_device *dev, struct hsr_port *port)
  85. {
  86. int res;
  87. dev_hold(dev);
  88. res = dev_set_promiscuity(dev, 1);
  89. if (res)
  90. goto fail_promiscuity;
  91. /* FIXME:
  92. * What does net device "adjacency" mean? Should we do
  93. * res = netdev_master_upper_dev_link(port->dev, port->hsr->dev); ?
  94. */
  95. res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
  96. if (res)
  97. goto fail_rx_handler;
  98. dev_disable_lro(dev);
  99. return 0;
  100. fail_rx_handler:
  101. dev_set_promiscuity(dev, -1);
  102. fail_promiscuity:
  103. dev_put(dev);
  104. return res;
  105. }
  106. int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
  107. enum hsr_port_type type)
  108. {
  109. struct hsr_port *port, *master;
  110. int res;
  111. if (type != HSR_PT_MASTER) {
  112. res = hsr_check_dev_ok(dev);
  113. if (res)
  114. return res;
  115. }
  116. port = hsr_port_get_hsr(hsr, type);
  117. if (port != NULL)
  118. return -EBUSY; /* This port already exists */
  119. port = kzalloc(sizeof(*port), GFP_KERNEL);
  120. if (port == NULL)
  121. return -ENOMEM;
  122. port->hsr = hsr;
  123. port->dev = dev;
  124. port->type = type;
  125. if (type != HSR_PT_MASTER) {
  126. res = hsr_portdev_setup(dev, port);
  127. if (res)
  128. goto fail_dev_setup;
  129. }
  130. list_add_tail_rcu(&port->port_list, &hsr->ports);
  131. synchronize_rcu();
  132. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  133. netdev_update_features(master->dev);
  134. dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
  135. return 0;
  136. fail_dev_setup:
  137. kfree(port);
  138. return res;
  139. }
  140. void hsr_del_port(struct hsr_port *port)
  141. {
  142. struct hsr_priv *hsr;
  143. struct hsr_port *master;
  144. hsr = port->hsr;
  145. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  146. list_del_rcu(&port->port_list);
  147. if (port != master) {
  148. if (master != NULL) {
  149. netdev_update_features(master->dev);
  150. dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
  151. }
  152. netdev_rx_handler_unregister(port->dev);
  153. dev_set_promiscuity(port->dev, -1);
  154. }
  155. /* FIXME?
  156. * netdev_upper_dev_unlink(port->dev, port->hsr->dev);
  157. */
  158. synchronize_rcu();
  159. if (port != master)
  160. dev_put(port->dev);
  161. }