1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
8 #include <linux/netdevice.h>
9 #include <linux/rculist.h>
10 #include <linux/timer.h>
11 #include <linux/etherdevice.h>
13 #include "hsr_device.h"
14 #include "hsr_netlink.h"
15 #include "hsr_framereg.h"
16 #include "hsr_slave.h"
18 static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
21 struct net_device *dev;
22 struct hsr_port *port, *master;
27 dev = netdev_notifier_info_to_dev(ptr);
28 port = hsr_port_get_rtnl(dev);
30 if (!is_hsr_master(dev))
31 return NOTIFY_DONE; /* Not an HSR device */
32 hsr = netdev_priv(dev);
33 port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
35 /* Resend of notification concerning removed device? */
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);
48 case NETDEV_CHANGENAME:
49 if (is_hsr_master(dev))
50 hsr_debugfs_rename(dev);
52 case NETDEV_CHANGEADDR:
53 if (port->type == HSR_PT_MASTER) {
54 /* This should not happen since there's no
55 * ndo_set_mac_address() for HSR devices - i.e. not
61 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
63 if (port->type == HSR_PT_SLAVE_A) {
64 ether_addr_copy(master->dev->dev_addr, dev->dev_addr);
65 call_netdevice_notifiers(NETDEV_CHANGEADDR,
69 /* Make sure we recognize frames from ourselves in hsr_rcv() */
70 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
71 res = hsr_create_self_node(hsr,
72 master->dev->dev_addr,
75 master->dev->dev_addr);
77 netdev_warn(master->dev,
78 "Could not update HSR node address.\n");
80 case NETDEV_CHANGEMTU:
81 if (port->type == HSR_PT_MASTER)
82 break; /* Handled in ndo_change_mtu() */
83 mtu_max = hsr_get_max_mtu(port->hsr);
84 master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
85 master->dev->mtu = mtu_max;
87 case NETDEV_UNREGISTER:
90 case NETDEV_PRE_TYPE_CHANGE:
91 /* HSR works only on Ethernet devices. Refuse slave to change
100 struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt)
102 struct hsr_port *port;
104 hsr_for_each_port(hsr, port)
105 if (port->type == pt)
110 static struct notifier_block hsr_nb = {
111 .notifier_call = hsr_netdev_notify, /* Slave event notifications */
114 static int __init hsr_init(void)
118 BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
120 register_netdevice_notifier(&hsr_nb);
121 res = hsr_netlink_init();
126 static void __exit hsr_exit(void)
128 unregister_netdevice_notifier(&hsr_nb);
130 hsr_debugfs_remove_root();
133 module_init(hsr_init);
134 module_exit(hsr_exit);
135 MODULE_LICENSE("GPL");