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