File Index
Files and directories
Sign in
FILE

hsr_main.c

File preview with syntax highlighting.

linux-6.19.14/net/hsr/hsr_main.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright 2011-2014 Autronica Fire and Security AS
  3 *
  4 * Author(s):
  5 *	2011-2014 Arvid Brodin, arvid.brodin@alten.se
  6 *
  7 * Event handling for HSR and PRP devices.
  8 */
  9
 10#include <linux/netdevice.h>
 11#include <net/rtnetlink.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
 21static bool hsr_slave_empty(struct hsr_priv *hsr)
 22{
 23	struct hsr_port *port;
 24
 25	hsr_for_each_port_rtnl(hsr, port)
 26		if (port->type != HSR_PT_MASTER)
 27			return false;
 28	return true;
 29}
 30
 31static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
 32			     void *ptr)
 33{
 34	struct hsr_port *port, *master;
 35	struct net_device *dev;
 36	struct hsr_priv *hsr;
 37	LIST_HEAD(list_kill);
 38	int mtu_max;
 39	int res;
 40
 41	dev = netdev_notifier_info_to_dev(ptr);
 42	port = hsr_port_get_rtnl(dev);
 43	if (!port) {
 44		if (!is_hsr_master(dev))
 45			return NOTIFY_DONE;	/* Not an HSR device */
 46		hsr = netdev_priv(dev);
 47		port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
 48		if (!port) {
 49			/* Resend of notification concerning removed device? */
 50			return NOTIFY_DONE;
 51		}
 52	} else {
 53		hsr = port->hsr;
 54	}
 55
 56	switch (event) {
 57	case NETDEV_UP:		/* Administrative state DOWN */
 58	case NETDEV_DOWN:	/* Administrative state UP */
 59	case NETDEV_CHANGE:	/* Link (carrier) state changes */
 60		hsr_check_carrier_and_operstate(hsr);
 61		break;
 62	case NETDEV_CHANGENAME:
 63		if (is_hsr_master(dev))
 64			hsr_debugfs_rename(dev);
 65		break;
 66	case NETDEV_CHANGEADDR:
 67		if (port->type == HSR_PT_MASTER) {
 68			/* This should not happen since there's no
 69			 * ndo_set_mac_address() for HSR devices - i.e. not
 70			 * supported.
 71			 */
 72			break;
 73		}
 74
 75		master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
 76
 77		if (port->type == HSR_PT_SLAVE_A) {
 78			eth_hw_addr_set(master->dev, dev->dev_addr);
 79			call_netdevice_notifiers(NETDEV_CHANGEADDR,
 80						 master->dev);
 81
 82			if (hsr->prot_version == PRP_V1) {
 83				port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
 84				if (port) {
 85					eth_hw_addr_set(port->dev, dev->dev_addr);
 86					call_netdevice_notifiers(NETDEV_CHANGEADDR,
 87								 port->dev);
 88				}
 89			}
 90		}
 91
 92		/* Make sure we recognize frames from ourselves in hsr_rcv() */
 93		port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
 94		res = hsr_create_self_node(hsr,
 95					   master->dev->dev_addr,
 96					   port ?
 97						port->dev->dev_addr :
 98						master->dev->dev_addr);
 99		if (res)
100			netdev_warn(master->dev,
101				    "Could not update HSR node address.\n");
102		break;
103	case NETDEV_CHANGEMTU:
104		if (port->type == HSR_PT_MASTER)
105			break; /* Handled in ndo_change_mtu() */
106		mtu_max = hsr_get_max_mtu(port->hsr);
107		master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
108		WRITE_ONCE(master->dev->mtu, mtu_max);
109		break;
110	case NETDEV_UNREGISTER:
111		if (!is_hsr_master(dev)) {
112			master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER);
113			hsr_del_port(port);
114			if (hsr_slave_empty(master->hsr)) {
115				const struct rtnl_link_ops *ops;
116
117				ops = master->dev->rtnl_link_ops;
118				ops->dellink(master->dev, &list_kill);
119				unregister_netdevice_many(&list_kill);
120			}
121		}
122		break;
123	case NETDEV_PRE_TYPE_CHANGE:
124		/* HSR works only on Ethernet devices. Refuse slave to change
125		 * its type.
126		 */
127		return NOTIFY_BAD;
128	}
129
130	return NOTIFY_DONE;
131}
132
133struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt)
134{
135	struct hsr_port *port;
136
137	hsr_for_each_port_rtnl(hsr, port)
138		if (port->type == pt)
139			return port;
140	return NULL;
141}
142
143int hsr_get_version(struct net_device *dev, enum hsr_version *ver)
144{
145	struct hsr_priv *hsr;
146
147	hsr = netdev_priv(dev);
148	*ver = hsr->prot_version;
149
150	return 0;
151}
152EXPORT_SYMBOL(hsr_get_version);
153
154static struct notifier_block hsr_nb = {
155	.notifier_call = hsr_netdev_notify,	/* Slave event notifications */
156};
157
158static int __init hsr_init(void)
159{
160	int err;
161
162	BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN);
163
164	err = register_netdevice_notifier(&hsr_nb);
165	if (err)
166		return err;
167
168	err = hsr_netlink_init();
169	if (err) {
170		unregister_netdevice_notifier(&hsr_nb);
171		return err;
172	}
173
174	return 0;
175}
176
177static void __exit hsr_exit(void)
178{
179	hsr_netlink_exit();
180	hsr_debugfs_remove_root();
181	unregister_netdevice_notifier(&hsr_nb);
182}
183
184module_init(hsr_init);
185module_exit(hsr_exit);
186MODULE_DESCRIPTION("High-availability Seamless Redundancy (HSR) driver");
187MODULE_LICENSE("GPL");
~/ Read-only file preview.