1 /* Copyright 2011-2014 Autronica Fire and Security AS
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)
9 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
12 #include <linux/netdevice.h>
13 #include <linux/rculist.h>
14 #include <linux/timer.h>
15 #include <linux/etherdevice.h>
17 #include "hsr_device.h"
18 #include "hsr_netlink.h"
19 #include "hsr_framereg.h"
20 #include "hsr_slave.h"
23 /* List of all registered virtual HSR devices */
24 static LIST_HEAD(hsr_list);
26 void register_hsr_master(struct hsr_priv *hsr)
28 list_add_tail_rcu(&hsr->hsr_list, &hsr_list);
31 void unregister_hsr_master(struct hsr_priv *hsr)
33 struct hsr_priv *hsr_it;
35 list_for_each_entry(hsr_it, &hsr_list, hsr_list)
37 list_del_rcu(&hsr_it->hsr_list);
42 bool is_hsr_slave(struct net_device *dev)
44 struct hsr_priv *hsr_it;
46 list_for_each_entry_rcu(hsr_it, &hsr_list, hsr_list) {
47 if (dev == hsr_it->slave[0])
49 if (dev == hsr_it->slave[1])
56 /* If dev is a HSR slave device, return the virtual master device. Return NULL
59 struct hsr_priv *get_hsr_master(struct net_device *dev)
64 list_for_each_entry_rcu(hsr, &hsr_list, hsr_list)
65 if ((dev == hsr->slave[0]) ||
66 (dev == hsr->slave[1])) {
75 /* If dev is a HSR slave device, return the other slave device. Return NULL
78 struct net_device *get_other_slave(struct hsr_priv *hsr,
79 struct net_device *dev)
81 if (dev == hsr->slave[0])
83 if (dev == hsr->slave[1])
90 static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
93 struct net_device *slave, *other_slave;
97 struct net_device *dev;
99 dev = netdev_notifier_info_to_dev(ptr);
101 hsr = get_hsr_master(dev);
103 /* dev is a slave device */
105 other_slave = get_other_slave(hsr, slave);
107 if (!is_hsr_master(dev))
109 hsr = netdev_priv(dev);
110 slave = hsr->slave[0];
111 other_slave = hsr->slave[1];
115 case NETDEV_UP: /* Administrative state DOWN */
116 case NETDEV_DOWN: /* Administrative state UP */
117 case NETDEV_CHANGE: /* Link (carrier) state changes */
118 hsr_check_carrier_and_operstate(hsr);
120 case NETDEV_CHANGEADDR:
122 /* This should not happen since there's no ndo_set_mac_address()
123 * for HSR devices - i.e. not supported.
128 if (dev == hsr->slave[0]) {
129 ether_addr_copy(hsr->dev->dev_addr, dev->dev_addr);
130 call_netdevice_notifiers(NETDEV_CHANGEADDR, hsr->dev);
133 /* Make sure we recognize frames from ourselves in hsr_rcv() */
134 other_slave = hsr->slave[1];
135 res = hsr_create_self_node(&hsr->self_node_db,
138 other_slave->dev_addr :
141 netdev_warn(hsr->dev,
142 "Could not update HSR node address.\n");
144 case NETDEV_CHANGEMTU:
146 break; /* Handled in ndo_change_mtu() */
147 mtu_max = hsr_get_max_mtu(hsr);
148 if (hsr->dev->mtu > mtu_max)
149 dev_set_mtu(hsr->dev, mtu_max);
151 case NETDEV_UNREGISTER:
152 if (dev == hsr->slave[0]) {
153 hsr->slave[0] = NULL;
154 hsr_del_slave(hsr, 0);
156 if (dev == hsr->slave[1]) {
157 hsr->slave[1] = NULL;
158 hsr_del_slave(hsr, 1);
161 /* There should really be a way to set a new slave device... */
164 case NETDEV_PRE_TYPE_CHANGE:
165 /* HSR works only on Ethernet devices. Refuse slave to change
175 static struct notifier_block hsr_nb = {
176 .notifier_call = hsr_netdev_notify, /* Slave event notifications */
180 static int __init hsr_init(void)
184 BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
186 register_netdevice_notifier(&hsr_nb);
187 res = hsr_netlink_init();
192 static void __exit hsr_exit(void)
194 unregister_netdevice_notifier(&hsr_nb);
198 module_init(hsr_init);
199 module_exit(hsr_exit);
200 MODULE_LICENSE("GPL");