summaryrefslogtreecommitdiffstats
path: root/net/hsr/hsr_main.c
blob: b5abe26f7b4c45751309a3874d3fd9f8a0f2f324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* Copyright 2011-2014 Autronica Fire and Security AS
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * Author(s):
 *	2011-2014 Arvid Brodin, arvid.brodin@alten.se
 */

#include <linux/netdevice.h>
#include <linux/rculist.h>
#include <linux/timer.h>
#include <linux/etherdevice.h>
#include "hsr_main.h"
#include "hsr_device.h"
#include "hsr_netlink.h"
#include "hsr_framereg.h"
#include "hsr_slave.h"


/* List of all registered virtual HSR devices */
static LIST_HEAD(hsr_list);

void register_hsr_master(struct hsr_priv *hsr)
{
	list_add_tail_rcu(&hsr->hsr_list, &hsr_list);
}

void unregister_hsr_master(struct hsr_priv *hsr)
{
	struct hsr_priv *hsr_it;

	list_for_each_entry(hsr_it, &hsr_list, hsr_list)
		if (hsr_it == hsr) {
			list_del_rcu(&hsr_it->hsr_list);
			return;
		}
}

bool is_hsr_slave(struct net_device *dev)
{
	struct hsr_priv *hsr_it;

	list_for_each_entry_rcu(hsr_it, &hsr_list, hsr_list) {
		if (dev == hsr_it->slave[0])
			return true;
		if (dev == hsr_it->slave[1])
			return true;
	}

	return false;
}

/* If dev is a HSR slave device, return the virtual master device. Return NULL
 * otherwise.
 */
struct hsr_priv *get_hsr_master(struct net_device *dev)
{
	struct hsr_priv *hsr;

	rcu_read_lock();
	list_for_each_entry_rcu(hsr, &hsr_list, hsr_list)
		if ((dev == hsr->slave[0]) ||
		    (dev == hsr->slave[1])) {
			rcu_read_unlock();
			return hsr;
		}

	rcu_read_unlock();
	return NULL;
}

/* If dev is a HSR slave device, return the other slave device. Return NULL
 * otherwise.
 */
struct net_device *get_other_slave(struct hsr_priv *hsr,
				   struct net_device *dev)
{
	if (dev == hsr->slave[0])
		return hsr->slave[1];
	if (dev == hsr->slave[1])
		return hsr->slave[0];

	return NULL;
}


static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
			     void *ptr)
{
	struct net_device *slave, *other_slave;
	struct hsr_priv *hsr;
	int mtu_max;
	int res;
	struct net_device *dev;

	dev = netdev_notifier_info_to_dev(ptr);

	hsr = get_hsr_master(dev);
	if (hsr) {
		/* dev is a slave device */
		slave = dev;
		other_slave = get_other_slave(hsr, slave);
	} else {
		if (!is_hsr_master(dev))
			return NOTIFY_DONE;
		hsr = netdev_priv(dev);
		slave = hsr->slave[0];
		other_slave = hsr->slave[1];
	}

	switch (event) {
	case NETDEV_UP:		/* Administrative state DOWN */
	case NETDEV_DOWN:	/* Administrative state UP */
	case NETDEV_CHANGE:	/* Link (carrier) state changes */
		hsr_check_carrier_and_operstate(hsr);
		break;
	case NETDEV_CHANGEADDR:

		/* This should not happen since there's no ndo_set_mac_address()
		 * for HSR devices - i.e. not supported.
		 */
		if (dev == hsr->dev)
			break;

		if (dev == hsr->slave[0]) {
			ether_addr_copy(hsr->dev->dev_addr, dev->dev_addr);
			call_netdevice_notifiers(NETDEV_CHANGEADDR, hsr->dev);
		}

		/* Make sure we recognize frames from ourselves in hsr_rcv() */
		other_slave = hsr->slave[1];
		res = hsr_create_self_node(&hsr->self_node_db,
					   hsr->dev->dev_addr,
					   other_slave ?
						other_slave->dev_addr :
						hsr->dev->dev_addr);
		if (res)
			netdev_warn(hsr->dev,
				    "Could not update HSR node address.\n");
		break;
	case NETDEV_CHANGEMTU:
		if (dev == hsr->dev)
			break; /* Handled in ndo_change_mtu() */
		mtu_max = hsr_get_max_mtu(hsr);
		if (hsr->dev->mtu > mtu_max)
			dev_set_mtu(hsr->dev, mtu_max);
		break;
	case NETDEV_UNREGISTER:
		if (dev == hsr->slave[0]) {
			hsr->slave[0] = NULL;
			hsr_del_slave(hsr, 0);
		}
		if (dev == hsr->slave[1]) {
			hsr->slave[1] = NULL;
			hsr_del_slave(hsr, 1);
		}

		/* There should really be a way to set a new slave device... */

		break;
	case NETDEV_PRE_TYPE_CHANGE:
		/* HSR works only on Ethernet devices. Refuse slave to change
		 * its type.
		 */
		return NOTIFY_BAD;
	}

	return NOTIFY_DONE;
}


static struct notifier_block hsr_nb = {
	.notifier_call = hsr_netdev_notify,	/* Slave event notifications */
};


static int __init hsr_init(void)
{
	int res;

	BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);

	register_netdevice_notifier(&hsr_nb);
	res = hsr_netlink_init();

	return res;
}

static void __exit hsr_exit(void)
{
	unregister_netdevice_notifier(&hsr_nb);
	hsr_netlink_exit();
}

module_init(hsr_init);
module_exit(hsr_exit);
MODULE_LICENSE("GPL");