hsr_prp_main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <linux/netdevice.h>
  12. #include <linux/rculist.h>
  13. #include <linux/timer.h>
  14. #include <linux/etherdevice.h>
  15. #include "hsr_prp_main.h"
  16. #include "hsr_prp_device.h"
  17. #include "hsr_netlink.h"
  18. #include "hsr_prp_framereg.h"
  19. #include "hsr_prp_slave.h"
  20. static struct notifier_block hsr_nb = {
  21. .notifier_call = hsr_prp_netdev_notify, /* Slave event notifications */
  22. };
  23. static struct notifier_block prp_nb = {
  24. .notifier_call = hsr_prp_netdev_notify, /* Slave event notifications */
  25. };
  26. int hsr_prp_netdev_notify(struct notifier_block *nb, unsigned long event,
  27. void *ptr)
  28. {
  29. struct net_device *dev;
  30. struct hsr_prp_port *port, *master;
  31. struct hsr_prp_priv *priv;
  32. int mtu_max;
  33. int res;
  34. dev = netdev_notifier_info_to_dev(ptr);
  35. port = hsr_prp_port_get_rtnl(dev);
  36. if (!port) {
  37. if (!is_hsr_prp_master(dev))
  38. return NOTIFY_DONE; /* Not an HSR device */
  39. priv = netdev_priv(dev);
  40. port = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
  41. if (!port) {
  42. /* Resend of notification concerning removed device? */
  43. return NOTIFY_DONE;
  44. }
  45. } else {
  46. priv = port->priv;
  47. }
  48. if ((priv->prot_version <= HSR_V1) &&
  49. (nb != &hsr_nb))
  50. return NOTIFY_DONE;
  51. else if ((priv->prot_version == PRP_V1) &&
  52. (nb != &prp_nb))
  53. return NOTIFY_DONE;
  54. switch (event) {
  55. case NETDEV_UP: /* Administrative state DOWN */
  56. case NETDEV_DOWN: /* Administrative state UP */
  57. case NETDEV_CHANGE: /* Link (carrier) state changes */
  58. hsr_prp_check_carrier_and_operstate(priv);
  59. break;
  60. case NETDEV_CHANGEADDR:
  61. if (port->type == HSR_PRP_PT_MASTER) {
  62. /* This should not happen since there's no
  63. * ndo_set_mac_address() for HSR devices - i.e. not
  64. * supported.
  65. */
  66. break;
  67. }
  68. master = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
  69. if (port->type == HSR_PRP_PT_SLAVE_A) {
  70. ether_addr_copy(master->dev->dev_addr, dev->dev_addr);
  71. call_netdevice_notifiers(NETDEV_CHANGEADDR,
  72. master->dev);
  73. }
  74. /* Make sure we recognize frames from ourselves in hsr_rcv() */
  75. port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_B);
  76. res = hsr_prp_create_self_node(&priv->self_node_db,
  77. master->dev->dev_addr,
  78. port ?
  79. port->dev->dev_addr :
  80. master->dev->dev_addr);
  81. if (res)
  82. netdev_warn(master->dev,
  83. "Could not update HSR node address.\n");
  84. break;
  85. case NETDEV_CHANGEMTU:
  86. if (port->type == HSR_PRP_PT_MASTER)
  87. break; /* Handled in ndo_change_mtu() */
  88. mtu_max = hsr_prp_get_max_mtu(port->priv);
  89. master = hsr_prp_get_port(port->priv, HSR_PRP_PT_MASTER);
  90. master->dev->mtu = mtu_max;
  91. break;
  92. case NETDEV_UNREGISTER:
  93. hsr_prp_del_port(port);
  94. break;
  95. case NETDEV_PRE_TYPE_CHANGE:
  96. /* HSR works only on Ethernet devices. Refuse slave to change
  97. * its type.
  98. */
  99. return NOTIFY_BAD;
  100. }
  101. return NOTIFY_DONE;
  102. }
  103. struct hsr_prp_port *hsr_prp_get_port(struct hsr_prp_priv *priv,
  104. enum hsr_prp_port_type pt)
  105. {
  106. struct hsr_prp_port *port;
  107. hsr_prp_for_each_port(priv, port)
  108. if (port->type == pt)
  109. return port;
  110. return NULL;
  111. }
  112. int hsr_prp_register_notifier(u8 proto)
  113. {
  114. if (proto == PRP)
  115. return register_netdevice_notifier(&prp_nb);
  116. return register_netdevice_notifier(&hsr_nb);
  117. }
  118. void hsr_prp_unregister_notifier(u8 proto)
  119. {
  120. if (proto == PRP)
  121. unregister_netdevice_notifier(&prp_nb);
  122. unregister_netdevice_notifier(&hsr_nb);
  123. }