hsr_slave.c 4.3 KB

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