1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011-2014 Autronica Fire and Security AS
5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
7 * Frame handler other utility functions for HSR and PRP.
10 #include "hsr_slave.h"
11 #include <linux/etherdevice.h>
12 #include <linux/if_arp.h>
13 #include <linux/if_vlan.h>
15 #include "hsr_device.h"
16 #include "hsr_forward.h"
17 #include "hsr_framereg.h"
19 bool hsr_invalid_dan_ingress_frame(__be16 protocol)
21 return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR));
24 static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
26 struct sk_buff *skb = *pskb;
27 struct hsr_port *port;
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;
35 if (!skb_mac_header_was_set(skb)) {
36 WARN_ONCE(1, "%s: skb invalid", __func__);
37 return RX_HANDLER_PASS;
40 port = hsr_port_get_rcu(skb->dev);
45 if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
46 /* Directly kill frames sent by ourselves */
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).
55 protocol = eth_hdr(skb)->h_proto;
57 if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) &&
58 hsr->proto_ops->invalid_dan_ingress_frame &&
59 hsr->proto_ops->invalid_dan_ingress_frame(protocol))
62 skb_push(skb, ETH_HLEN);
63 skb_reset_mac_header(skb);
64 if ((!hsr->prot_version && protocol == htons(ETH_P_PRP)) ||
65 protocol == htons(ETH_P_HSR))
66 skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
67 skb_reset_mac_len(skb);
69 hsr_forward_skb(skb, port);
72 return RX_HANDLER_CONSUMED;
75 return RX_HANDLER_PASS;
78 bool hsr_port_exists(const struct net_device *dev)
80 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
83 static int hsr_check_dev_ok(struct net_device *dev,
84 struct netlink_ext_ack *extack)
86 /* Don't allow HSR on non-ethernet like devices */
87 if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
88 dev->addr_len != ETH_ALEN) {
89 NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
93 /* Don't allow enslaving hsr devices */
94 if (is_hsr_master(dev)) {
95 NL_SET_ERR_MSG_MOD(extack,
96 "Cannot create trees of HSR devices.");
100 if (hsr_port_exists(dev)) {
101 NL_SET_ERR_MSG_MOD(extack,
102 "This device is already a HSR slave.");
106 if (is_vlan_dev(dev)) {
107 NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
111 if (dev->priv_flags & IFF_DONT_BRIDGE) {
112 NL_SET_ERR_MSG_MOD(extack,
113 "This device does not support bridging.");
117 /* HSR over bonded devices has not been tested, but I'm not sure it
124 /* Setup device to be added to the HSR bridge. */
125 static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
126 struct hsr_port *port,
127 struct netlink_ext_ack *extack)
130 struct net_device *hsr_dev;
131 struct hsr_port *master;
134 /* Don't use promiscuous mode for offload since L2 frame forward
135 * happens at the offloaded hardware.
137 if (!port->hsr->fwd_offloaded) {
138 res = dev_set_promiscuity(dev, 1);
143 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
144 hsr_dev = master->dev;
146 res = netdev_upper_dev_link(dev, hsr_dev, extack);
148 goto fail_upper_dev_link;
150 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
152 goto fail_rx_handler;
153 dev_disable_lro(dev);
158 netdev_upper_dev_unlink(dev, hsr_dev);
160 if (!port->hsr->fwd_offloaded)
161 dev_set_promiscuity(dev, -1);
166 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
167 enum hsr_port_type type, struct netlink_ext_ack *extack)
169 struct hsr_port *port, *master;
172 if (type != HSR_PT_MASTER) {
173 res = hsr_check_dev_ok(dev, extack);
178 port = hsr_port_get_hsr(hsr, type);
180 return -EBUSY; /* This port already exists */
182 port = kzalloc(sizeof(*port), GFP_KERNEL);
190 if (type != HSR_PT_MASTER) {
191 res = hsr_portdev_setup(hsr, dev, port, extack);
196 list_add_tail_rcu(&port->port_list, &hsr->ports);
199 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
200 netdev_update_features(master->dev);
201 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
210 void hsr_del_port(struct hsr_port *port)
212 struct hsr_priv *hsr;
213 struct hsr_port *master;
216 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
217 list_del_rcu(&port->port_list);
219 if (port != master) {
220 netdev_update_features(master->dev);
221 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
222 netdev_rx_handler_unregister(port->dev);
223 dev_set_promiscuity(port->dev, -1);
224 netdev_upper_dev_unlink(port->dev, master->dev);