hsr_slave.c 4.4 KB

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