Linux Audio

Check our new training course

Loading...
v5.9
  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 * Frame handler other utility functions for HSR and PRP.
  8 */
  9
 10#include "hsr_slave.h"
 11#include <linux/etherdevice.h>
 12#include <linux/if_arp.h>
 13#include <linux/if_vlan.h>
 14#include "hsr_main.h"
 15#include "hsr_device.h"
 16#include "hsr_forward.h"
 17#include "hsr_framereg.h"
 18
 19bool hsr_invalid_dan_ingress_frame(__be16 protocol)
 20{
 21	return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR));
 22}
 23
 24static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
 25{
 26	struct sk_buff *skb = *pskb;
 27	struct hsr_port *port;
 28	struct hsr_priv *hsr;
 29	__be16 protocol;
 30
 31	/* Packets from dev_loopback_xmit() do not have L2 header, bail out */
 32	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
 33		return RX_HANDLER_PASS;
 34
 35	if (!skb_mac_header_was_set(skb)) {
 36		WARN_ONCE(1, "%s: skb invalid", __func__);
 37		return RX_HANDLER_PASS;
 38	}
 39
 40	port = hsr_port_get_rcu(skb->dev);
 41	if (!port)
 42		goto finish_pass;
 43	hsr = port->hsr;
 44
 45	if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
 46		/* Directly kill frames sent by ourselves */
 47		kfree_skb(skb);
 48		goto finish_consume;
 49	}
 50
 51	/* For HSR, only tagged frames are expected, but for PRP
 52	 * there could be non tagged frames as well from Single
 53	 * attached nodes (SANs).
 54	 */
 55	protocol = eth_hdr(skb)->h_proto;
 56	if (hsr->proto_ops->invalid_dan_ingress_frame &&
 
 
 
 57	    hsr->proto_ops->invalid_dan_ingress_frame(protocol))
 58		goto finish_pass;
 59
 60	skb_push(skb, ETH_HLEN);
 
 
 
 
 
 61
 62	if (skb_mac_header(skb) != skb->data) {
 63		WARN_ONCE(1, "%s:%d: Malformed frame at source port %s)\n",
 64			  __func__, __LINE__, port->dev->name);
 65		goto finish_consume;
 
 
 
 
 
 66	}
 67
 68	hsr_forward_skb(skb, port);
 69
 70finish_consume:
 71	return RX_HANDLER_CONSUMED;
 72
 73finish_pass:
 74	return RX_HANDLER_PASS;
 75}
 76
 77bool hsr_port_exists(const struct net_device *dev)
 78{
 79	return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
 80}
 81
 82static int hsr_check_dev_ok(struct net_device *dev,
 83			    struct netlink_ext_ack *extack)
 84{
 85	/* Don't allow HSR on non-ethernet like devices */
 86	if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
 87	    dev->addr_len != ETH_ALEN) {
 88		NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
 89		return -EINVAL;
 90	}
 91
 92	/* Don't allow enslaving hsr devices */
 93	if (is_hsr_master(dev)) {
 94		NL_SET_ERR_MSG_MOD(extack,
 95				   "Cannot create trees of HSR devices.");
 96		return -EINVAL;
 97	}
 98
 99	if (hsr_port_exists(dev)) {
100		NL_SET_ERR_MSG_MOD(extack,
101				   "This device is already a HSR slave.");
102		return -EINVAL;
103	}
104
105	if (is_vlan_dev(dev)) {
106		NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
107		return -EINVAL;
108	}
109
110	if (dev->priv_flags & IFF_DONT_BRIDGE) {
111		NL_SET_ERR_MSG_MOD(extack,
112				   "This device does not support bridging.");
113		return -EOPNOTSUPP;
114	}
115
116	/* HSR over bonded devices has not been tested, but I'm not sure it
117	 * won't work...
118	 */
119
120	return 0;
121}
122
123/* Setup device to be added to the HSR bridge. */
124static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
125			     struct hsr_port *port,
126			     struct netlink_ext_ack *extack)
127
128{
129	struct net_device *hsr_dev;
130	struct hsr_port *master;
131	int res;
132
133	res = dev_set_promiscuity(dev, 1);
134	if (res)
135		return res;
 
 
 
 
 
136
137	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
138	hsr_dev = master->dev;
139
140	res = netdev_upper_dev_link(dev, hsr_dev, extack);
141	if (res)
142		goto fail_upper_dev_link;
143
144	res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
145	if (res)
146		goto fail_rx_handler;
147	dev_disable_lro(dev);
148
149	return 0;
150
151fail_rx_handler:
152	netdev_upper_dev_unlink(dev, hsr_dev);
153fail_upper_dev_link:
154	dev_set_promiscuity(dev, -1);
 
 
155	return res;
156}
157
158int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
159		 enum hsr_port_type type, struct netlink_ext_ack *extack)
160{
161	struct hsr_port *port, *master;
162	int res;
163
164	if (type != HSR_PT_MASTER) {
165		res = hsr_check_dev_ok(dev, extack);
166		if (res)
167			return res;
168	}
169
170	port = hsr_port_get_hsr(hsr, type);
171	if (port)
172		return -EBUSY;	/* This port already exists */
173
174	port = kzalloc(sizeof(*port), GFP_KERNEL);
175	if (!port)
176		return -ENOMEM;
177
178	port->hsr = hsr;
179	port->dev = dev;
180	port->type = type;
181
182	if (type != HSR_PT_MASTER) {
183		res = hsr_portdev_setup(hsr, dev, port, extack);
184		if (res)
185			goto fail_dev_setup;
186	}
187
188	list_add_tail_rcu(&port->port_list, &hsr->ports);
189	synchronize_rcu();
190
191	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
192	netdev_update_features(master->dev);
193	dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
194
195	return 0;
196
197fail_dev_setup:
198	kfree(port);
199	return res;
200}
201
202void hsr_del_port(struct hsr_port *port)
203{
204	struct hsr_priv *hsr;
205	struct hsr_port *master;
206
207	hsr = port->hsr;
208	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
209	list_del_rcu(&port->port_list);
210
211	if (port != master) {
212		netdev_update_features(master->dev);
213		dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
214		netdev_rx_handler_unregister(port->dev);
215		dev_set_promiscuity(port->dev, -1);
 
216		netdev_upper_dev_unlink(port->dev, master->dev);
217	}
218
219	synchronize_rcu();
220
221	kfree(port);
222}
v6.13.7
  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 * Frame handler other utility functions for HSR and PRP.
  8 */
  9
 10#include "hsr_slave.h"
 11#include <linux/etherdevice.h>
 12#include <linux/if_arp.h>
 13#include <linux/if_vlan.h>
 14#include "hsr_main.h"
 15#include "hsr_device.h"
 16#include "hsr_forward.h"
 17#include "hsr_framereg.h"
 18
 19bool hsr_invalid_dan_ingress_frame(__be16 protocol)
 20{
 21	return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR));
 22}
 23
 24static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
 25{
 26	struct sk_buff *skb = *pskb;
 27	struct hsr_port *port;
 28	struct hsr_priv *hsr;
 29	__be16 protocol;
 30
 31	/* Packets from dev_loopback_xmit() do not have L2 header, bail out */
 32	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
 33		return RX_HANDLER_PASS;
 34
 35	if (!skb_mac_header_was_set(skb)) {
 36		WARN_ONCE(1, "%s: skb invalid", __func__);
 37		return RX_HANDLER_PASS;
 38	}
 39
 40	port = hsr_port_get_rcu(skb->dev);
 41	if (!port)
 42		goto finish_pass;
 43	hsr = port->hsr;
 44
 45	if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
 46		/* Directly kill frames sent by ourselves */
 47		kfree_skb(skb);
 48		goto finish_consume;
 49	}
 50
 51	/* For HSR, only tagged frames are expected (unless the device offloads
 52	 * HSR tag removal), but for PRP there could be non tagged frames as
 53	 * well from Single attached nodes (SANs).
 54	 */
 55	protocol = eth_hdr(skb)->h_proto;
 56
 57	if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) &&
 58	    port->type != HSR_PT_INTERLINK &&
 59	    hsr->proto_ops->invalid_dan_ingress_frame &&
 60	    hsr->proto_ops->invalid_dan_ingress_frame(protocol))
 61		goto finish_pass;
 62
 63	skb_push(skb, ETH_HLEN);
 64	skb_reset_mac_header(skb);
 65	if ((!hsr->prot_version && protocol == htons(ETH_P_PRP)) ||
 66	    protocol == htons(ETH_P_HSR))
 67		skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
 68	skb_reset_mac_len(skb);
 69
 70	/* Only the frames received over the interlink port will assign a
 71	 * sequence number and require synchronisation vs other sender.
 72	 */
 73	if (port->type == HSR_PT_INTERLINK) {
 74		spin_lock_bh(&hsr->seqnr_lock);
 75		hsr_forward_skb(skb, port);
 76		spin_unlock_bh(&hsr->seqnr_lock);
 77	} else {
 78		hsr_forward_skb(skb, port);
 79	}
 80
 
 
 81finish_consume:
 82	return RX_HANDLER_CONSUMED;
 83
 84finish_pass:
 85	return RX_HANDLER_PASS;
 86}
 87
 88bool hsr_port_exists(const struct net_device *dev)
 89{
 90	return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
 91}
 92
 93static int hsr_check_dev_ok(struct net_device *dev,
 94			    struct netlink_ext_ack *extack)
 95{
 96	/* Don't allow HSR on non-ethernet like devices */
 97	if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
 98	    dev->addr_len != ETH_ALEN) {
 99		NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
100		return -EINVAL;
101	}
102
103	/* Don't allow enslaving hsr devices */
104	if (is_hsr_master(dev)) {
105		NL_SET_ERR_MSG_MOD(extack,
106				   "Cannot create trees of HSR devices.");
107		return -EINVAL;
108	}
109
110	if (hsr_port_exists(dev)) {
111		NL_SET_ERR_MSG_MOD(extack,
112				   "This device is already a HSR slave.");
113		return -EINVAL;
114	}
115
116	if (is_vlan_dev(dev)) {
117		NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
118		return -EINVAL;
119	}
120
121	if (dev->priv_flags & IFF_DONT_BRIDGE) {
122		NL_SET_ERR_MSG_MOD(extack,
123				   "This device does not support bridging.");
124		return -EOPNOTSUPP;
125	}
126
127	/* HSR over bonded devices has not been tested, but I'm not sure it
128	 * won't work...
129	 */
130
131	return 0;
132}
133
134/* Setup device to be added to the HSR bridge. */
135static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
136			     struct hsr_port *port,
137			     struct netlink_ext_ack *extack)
138
139{
140	struct net_device *hsr_dev;
141	struct hsr_port *master;
142	int res;
143
144	/* Don't use promiscuous mode for offload since L2 frame forward
145	 * happens at the offloaded hardware.
146	 */
147	if (!port->hsr->fwd_offloaded) {
148		res = dev_set_promiscuity(dev, 1);
149		if (res)
150			return res;
151	}
152
153	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
154	hsr_dev = master->dev;
155
156	res = netdev_upper_dev_link(dev, hsr_dev, extack);
157	if (res)
158		goto fail_upper_dev_link;
159
160	res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
161	if (res)
162		goto fail_rx_handler;
163	dev_disable_lro(dev);
164
165	return 0;
166
167fail_rx_handler:
168	netdev_upper_dev_unlink(dev, hsr_dev);
169fail_upper_dev_link:
170	if (!port->hsr->fwd_offloaded)
171		dev_set_promiscuity(dev, -1);
172
173	return res;
174}
175
176int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
177		 enum hsr_port_type type, struct netlink_ext_ack *extack)
178{
179	struct hsr_port *port, *master;
180	int res;
181
182	if (type != HSR_PT_MASTER) {
183		res = hsr_check_dev_ok(dev, extack);
184		if (res)
185			return res;
186	}
187
188	port = hsr_port_get_hsr(hsr, type);
189	if (port)
190		return -EBUSY;	/* This port already exists */
191
192	port = kzalloc(sizeof(*port), GFP_KERNEL);
193	if (!port)
194		return -ENOMEM;
195
196	port->hsr = hsr;
197	port->dev = dev;
198	port->type = type;
199
200	if (type != HSR_PT_MASTER) {
201		res = hsr_portdev_setup(hsr, dev, port, extack);
202		if (res)
203			goto fail_dev_setup;
204	}
205
206	list_add_tail_rcu(&port->port_list, &hsr->ports);
207	synchronize_rcu();
208
209	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
210	netdev_update_features(master->dev);
211	dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
212
213	return 0;
214
215fail_dev_setup:
216	kfree(port);
217	return res;
218}
219
220void hsr_del_port(struct hsr_port *port)
221{
222	struct hsr_priv *hsr;
223	struct hsr_port *master;
224
225	hsr = port->hsr;
226	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
227	list_del_rcu(&port->port_list);
228
229	if (port != master) {
230		netdev_update_features(master->dev);
231		dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
232		netdev_rx_handler_unregister(port->dev);
233		if (!port->hsr->fwd_offloaded)
234			dev_set_promiscuity(port->dev, -1);
235		netdev_upper_dev_unlink(port->dev, master->dev);
236	}
237
238	synchronize_rcu();
239
240	kfree(port);
241}