blob: 431b528c2447247123d2821e9695fc6629b432d9 [file] [log] [blame]
Arvid Brodin70ebe4a2014-07-04 23:34:38 +02001/* Copyright 2011-2014 Autronica Fire and Security AS
Arvid Brodinf4214362013-10-30 21:10:47 +01002 *
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):
Arvid Brodin70ebe4a2014-07-04 23:34:38 +02009 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
Arvid Brodinf4214362013-10-30 21:10:47 +010010 */
11
12#include <linux/netdevice.h>
13#include <linux/rculist.h>
14#include <linux/timer.h>
15#include <linux/etherdevice.h>
16#include "hsr_main.h"
17#include "hsr_device.h"
18#include "hsr_netlink.h"
19#include "hsr_framereg.h"
20
21
22/* List of all registered virtual HSR devices */
23static LIST_HEAD(hsr_list);
24
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020025void register_hsr_master(struct hsr_priv *hsr)
Arvid Brodinf4214362013-10-30 21:10:47 +010026{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020027 list_add_tail_rcu(&hsr->hsr_list, &hsr_list);
Arvid Brodinf4214362013-10-30 21:10:47 +010028}
29
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020030void unregister_hsr_master(struct hsr_priv *hsr)
Arvid Brodinf4214362013-10-30 21:10:47 +010031{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020032 struct hsr_priv *hsr_it;
Arvid Brodinf4214362013-10-30 21:10:47 +010033
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020034 list_for_each_entry(hsr_it, &hsr_list, hsr_list)
35 if (hsr_it == hsr) {
36 list_del_rcu(&hsr_it->hsr_list);
Arvid Brodinf4214362013-10-30 21:10:47 +010037 return;
38 }
39}
40
41bool is_hsr_slave(struct net_device *dev)
42{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020043 struct hsr_priv *hsr_it;
Arvid Brodinf4214362013-10-30 21:10:47 +010044
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020045 list_for_each_entry_rcu(hsr_it, &hsr_list, hsr_list) {
46 if (dev == hsr_it->slave[0])
Arvid Brodinf4214362013-10-30 21:10:47 +010047 return true;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020048 if (dev == hsr_it->slave[1])
Arvid Brodinf4214362013-10-30 21:10:47 +010049 return true;
50 }
51
52 return false;
53}
54
Arvid Brodinf4214362013-10-30 21:10:47 +010055/* If dev is a HSR slave device, return the virtual master device. Return NULL
56 * otherwise.
57 */
Arvid Brodin81ba6af2014-07-04 23:35:24 +020058struct hsr_priv *get_hsr_master(struct net_device *dev)
Arvid Brodinf4214362013-10-30 21:10:47 +010059{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020060 struct hsr_priv *hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +010061
62 rcu_read_lock();
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020063 list_for_each_entry_rcu(hsr, &hsr_list, hsr_list)
64 if ((dev == hsr->slave[0]) ||
65 (dev == hsr->slave[1])) {
Arvid Brodinf4214362013-10-30 21:10:47 +010066 rcu_read_unlock();
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020067 return hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +010068 }
69
70 rcu_read_unlock();
71 return NULL;
72}
73
Arvid Brodinf4214362013-10-30 21:10:47 +010074/* If dev is a HSR slave device, return the other slave device. Return NULL
75 * otherwise.
76 */
Arvid Brodin81ba6af2014-07-04 23:35:24 +020077struct net_device *get_other_slave(struct hsr_priv *hsr,
78 struct net_device *dev)
Arvid Brodinf4214362013-10-30 21:10:47 +010079{
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020080 if (dev == hsr->slave[0])
81 return hsr->slave[1];
82 if (dev == hsr->slave[1])
83 return hsr->slave[0];
Arvid Brodinf4214362013-10-30 21:10:47 +010084
85 return NULL;
86}
87
88
89static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
90 void *ptr)
91{
92 struct net_device *slave, *other_slave;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +020093 struct hsr_priv *hsr;
Arvid Brodinf4214362013-10-30 21:10:47 +010094 int mtu_max;
95 int res;
96 struct net_device *dev;
97
98 dev = netdev_notifier_info_to_dev(ptr);
99
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200100 hsr = get_hsr_master(dev);
101 if (hsr) {
Arvid Brodinf4214362013-10-30 21:10:47 +0100102 /* dev is a slave device */
103 slave = dev;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200104 other_slave = get_other_slave(hsr, slave);
Arvid Brodinf4214362013-10-30 21:10:47 +0100105 } else {
106 if (!is_hsr_master(dev))
107 return NOTIFY_DONE;
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200108 hsr = netdev_priv(dev);
109 slave = hsr->slave[0];
110 other_slave = hsr->slave[1];
Arvid Brodinf4214362013-10-30 21:10:47 +0100111 }
112
113 switch (event) {
114 case NETDEV_UP: /* Administrative state DOWN */
115 case NETDEV_DOWN: /* Administrative state UP */
116 case NETDEV_CHANGE: /* Link (carrier) state changes */
Arvid Brodine9aae562014-07-04 23:36:40 +0200117 hsr_check_carrier_and_operstate(hsr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100118 break;
119 case NETDEV_CHANGEADDR:
120
121 /* This should not happen since there's no ndo_set_mac_address()
122 * for HSR devices - i.e. not supported.
123 */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200124 if (dev == hsr->dev)
Arvid Brodinf4214362013-10-30 21:10:47 +0100125 break;
126
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200127 if (dev == hsr->slave[0])
128 ether_addr_copy(hsr->dev->dev_addr,
129 hsr->slave[0]->dev_addr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100130
131 /* Make sure we recognize frames from ourselves in hsr_rcv() */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200132 res = hsr_create_self_node(&hsr->self_node_db,
133 hsr->dev->dev_addr,
134 hsr->slave[1] ?
135 hsr->slave[1]->dev_addr :
136 hsr->dev->dev_addr);
Arvid Brodinf4214362013-10-30 21:10:47 +0100137 if (res)
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200138 netdev_warn(hsr->dev,
Arvid Brodinf4214362013-10-30 21:10:47 +0100139 "Could not update HSR node address.\n");
140
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200141 if (dev == hsr->slave[0])
142 call_netdevice_notifiers(NETDEV_CHANGEADDR, hsr->dev);
Arvid Brodinf4214362013-10-30 21:10:47 +0100143 break;
144 case NETDEV_CHANGEMTU:
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200145 if (dev == hsr->dev)
Arvid Brodinf4214362013-10-30 21:10:47 +0100146 break; /* Handled in ndo_change_mtu() */
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200147 mtu_max = hsr_get_max_mtu(hsr);
148 if (hsr->dev->mtu > mtu_max)
149 dev_set_mtu(hsr->dev, mtu_max);
Arvid Brodinf4214362013-10-30 21:10:47 +0100150 break;
151 case NETDEV_UNREGISTER:
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200152 if (dev == hsr->slave[0])
153 hsr->slave[0] = NULL;
154 if (dev == hsr->slave[1])
155 hsr->slave[1] = NULL;
Arvid Brodinf4214362013-10-30 21:10:47 +0100156
157 /* There should really be a way to set a new slave device... */
158
159 break;
160 case NETDEV_PRE_TYPE_CHANGE:
161 /* HSR works only on Ethernet devices. Refuse slave to change
162 * its type.
163 */
164 return NOTIFY_BAD;
165 }
166
167 return NOTIFY_DONE;
168}
169
170
Arvid Brodinf4214362013-10-30 21:10:47 +0100171static struct notifier_block hsr_nb = {
172 .notifier_call = hsr_netdev_notify, /* Slave event notifications */
173};
174
175
176static int __init hsr_init(void)
177{
178 int res;
179
Arvid Brodin70ebe4a2014-07-04 23:34:38 +0200180 BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
Arvid Brodinf4214362013-10-30 21:10:47 +0100181
Arvid Brodinf4214362013-10-30 21:10:47 +0100182 register_netdevice_notifier(&hsr_nb);
Arvid Brodinf4214362013-10-30 21:10:47 +0100183 res = hsr_netlink_init();
184
185 return res;
186}
187
188static void __exit hsr_exit(void)
189{
190 unregister_netdevice_notifier(&hsr_nb);
Arvid Brodinf4214362013-10-30 21:10:47 +0100191 hsr_netlink_exit();
Arvid Brodinf4214362013-10-30 21:10:47 +0100192}
193
194module_init(hsr_init);
195module_exit(hsr_exit);
196MODULE_LICENSE("GPL");