hsr_main.c 3.2 KB

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