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, but for PRP
52 * there could be non tagged frames as well from Single
53 * attached nodes (SANs).
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))
60 skb_push(skb, ETH_HLEN);
61 skb_reset_mac_header(skb);
62 if ((!hsr->prot_version && protocol == htons(ETH_P_PRP)) ||
63 protocol == htons(ETH_P_HSR))
64 skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
65 skb_reset_mac_len(skb);
67 hsr_forward_skb(skb, port);
70 return RX_HANDLER_CONSUMED;
73 return RX_HANDLER_PASS;
76 bool hsr_port_exists(const struct net_device *dev)
78 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
81 static int hsr_check_dev_ok(struct net_device *dev,
82 struct netlink_ext_ack *extack)
84 /* Don't allow HSR on non-ethernet like devices */
85 if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
86 dev->addr_len != ETH_ALEN) {
87 NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
91 /* Don't allow enslaving hsr devices */
92 if (is_hsr_master(dev)) {
93 NL_SET_ERR_MSG_MOD(extack,
94 "Cannot create trees of HSR devices.");
98 if (hsr_port_exists(dev)) {
99 NL_SET_ERR_MSG_MOD(extack,
100 "This device is already a HSR slave.");
104 if (is_vlan_dev(dev)) {
105 NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
109 if (dev->priv_flags & IFF_DONT_BRIDGE) {
110 NL_SET_ERR_MSG_MOD(extack,
111 "This device does not support bridging.");
115 /* HSR over bonded devices has not been tested, but I'm not sure it
122 /* Setup device to be added to the HSR bridge. */
123 static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
124 struct hsr_port *port,
125 struct netlink_ext_ack *extack)
128 struct net_device *hsr_dev;
129 struct hsr_port *master;
132 res = dev_set_promiscuity(dev, 1);
136 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
137 hsr_dev = master->dev;
139 res = netdev_upper_dev_link(dev, hsr_dev, extack);
141 goto fail_upper_dev_link;
143 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
145 goto fail_rx_handler;
146 dev_disable_lro(dev);
151 netdev_upper_dev_unlink(dev, hsr_dev);
153 dev_set_promiscuity(dev, -1);
157 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
158 enum hsr_port_type type, struct netlink_ext_ack *extack)
160 struct hsr_port *port, *master;
163 if (type != HSR_PT_MASTER) {
164 res = hsr_check_dev_ok(dev, extack);
169 port = hsr_port_get_hsr(hsr, type);
171 return -EBUSY; /* This port already exists */
173 port = kzalloc(sizeof(*port), GFP_KERNEL);
181 if (type != HSR_PT_MASTER) {
182 res = hsr_portdev_setup(hsr, dev, port, extack);
187 list_add_tail_rcu(&port->port_list, &hsr->ports);
190 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
191 netdev_update_features(master->dev);
192 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
201 void hsr_del_port(struct hsr_port *port)
203 struct hsr_priv *hsr;
204 struct hsr_port *master;
207 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
208 list_del_rcu(&port->port_list);
210 if (port != master) {
211 netdev_update_features(master->dev);
212 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
213 netdev_rx_handler_unregister(port->dev);
214 dev_set_promiscuity(port->dev, -1);
215 netdev_upper_dev_unlink(port->dev, master->dev);