hsr_main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_main.h"
  16. #include "hsr_device.h"
  17. #include "hsr_netlink.h"
  18. #include "hsr_framereg.h"
  19. #include "hsr_slave.h"
  20. static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
  21. void *ptr)
  22. {
  23. struct net_device *dev;
  24. struct hsr_port *port, *master;
  25. struct hsr_priv *hsr;
  26. int mtu_max;
  27. int res;
  28. dev = netdev_notifier_info_to_dev(ptr);
  29. port = hsr_port_get_rtnl(dev);
  30. if (port == NULL) {
  31. if (!is_hsr_master(dev))
  32. return NOTIFY_DONE; /* Not an HSR device */
  33. hsr = netdev_priv(dev);
  34. port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  35. } else {
  36. hsr = port->hsr;
  37. }
  38. switch (event) {
  39. case NETDEV_UP: /* Administrative state DOWN */
  40. case NETDEV_DOWN: /* Administrative state UP */
  41. case NETDEV_CHANGE: /* Link (carrier) state changes */
  42. hsr_check_carrier_and_operstate(hsr);
  43. break;
  44. case NETDEV_CHANGEADDR:
  45. if (port->type == HSR_PT_MASTER) {
  46. /* This should not happen since there's no
  47. * ndo_set_mac_address() for HSR devices - i.e. not
  48. * supported.
  49. */
  50. break;
  51. }
  52. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  53. if (port->type == HSR_PT_SLAVE_A) {
  54. ether_addr_copy(master->dev->dev_addr, dev->dev_addr);
  55. call_netdevice_notifiers(NETDEV_CHANGEADDR, master->dev);
  56. }
  57. /* Make sure we recognize frames from ourselves in hsr_rcv() */
  58. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  59. res = hsr_create_self_node(&hsr->self_node_db,
  60. master->dev->dev_addr,
  61. port ?
  62. port->dev->dev_addr :
  63. master->dev->dev_addr);
  64. if (res)
  65. netdev_warn(master->dev,
  66. "Could not update HSR node address.\n");
  67. break;
  68. case NETDEV_CHANGEMTU:
  69. if (port->type == HSR_PT_MASTER)
  70. break; /* Handled in ndo_change_mtu() */
  71. mtu_max = hsr_get_max_mtu(port->hsr);
  72. master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
  73. master->dev->mtu = mtu_max;
  74. break;
  75. case NETDEV_UNREGISTER:
  76. hsr_del_port(port);
  77. break;
  78. case NETDEV_PRE_TYPE_CHANGE:
  79. /* HSR works only on Ethernet devices. Refuse slave to change
  80. * its type.
  81. */
  82. return NOTIFY_BAD;
  83. }
  84. return NOTIFY_DONE;
  85. }
  86. struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt)
  87. {
  88. struct hsr_port *port;
  89. hsr_for_each_port(hsr, port)
  90. if (port->type == pt)
  91. return port;
  92. return NULL;
  93. }
  94. static struct notifier_block hsr_nb = {
  95. .notifier_call = hsr_netdev_notify, /* Slave event notifications */
  96. };
  97. static int __init hsr_init(void)
  98. {
  99. int res;
  100. BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
  101. register_netdevice_notifier(&hsr_nb);
  102. res = hsr_netlink_init();
  103. return res;
  104. }
  105. static void __exit hsr_exit(void)
  106. {
  107. unregister_netdevice_notifier(&hsr_nb);
  108. hsr_netlink_exit();
  109. }
  110. module_init(hsr_init);
  111. module_exit(hsr_exit);
  112. MODULE_LICENSE("GPL");