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
9 #include <linux/etherdevice.h>
10 #include <linux/if_arp.h>
11 #include <linux/if_vlan.h>
13 #include "hsr_device.h"
14 #include "hsr_forward.h"
15 #include "hsr_framereg.h"
17 static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
19 struct sk_buff *skb = *pskb;
20 struct hsr_port *port;
23 if (!skb_mac_header_was_set(skb)) {
24 WARN_ONCE(1, "%s: skb invalid", __func__);
25 return RX_HANDLER_PASS;
28 port = hsr_port_get_rcu(skb->dev);
32 if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
33 /* Directly kill frames sent by ourselves */
38 protocol = eth_hdr(skb)->h_proto;
39 if (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR))
42 skb_push(skb, ETH_HLEN);
44 hsr_forward_skb(skb, port);
47 return RX_HANDLER_CONSUMED;
50 return RX_HANDLER_PASS;
53 bool hsr_port_exists(const struct net_device *dev)
55 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
58 static int hsr_check_dev_ok(struct net_device *dev,
59 struct netlink_ext_ack *extack)
61 /* Don't allow HSR on non-ethernet like devices */
62 if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
63 dev->addr_len != ETH_ALEN) {
64 NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
68 /* Don't allow enslaving hsr devices */
69 if (is_hsr_master(dev)) {
70 NL_SET_ERR_MSG_MOD(extack,
71 "Cannot create trees of HSR devices.");
75 if (hsr_port_exists(dev)) {
76 NL_SET_ERR_MSG_MOD(extack,
77 "This device is already a HSR slave.");
81 if (is_vlan_dev(dev)) {
82 NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
86 if (dev->priv_flags & IFF_DONT_BRIDGE) {
87 NL_SET_ERR_MSG_MOD(extack,
88 "This device does not support bridging.");
92 /* HSR over bonded devices has not been tested, but I'm not sure it
99 /* Setup device to be added to the HSR bridge. */
100 static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
101 struct hsr_port *port,
102 struct netlink_ext_ack *extack)
105 struct net_device *hsr_dev;
106 struct hsr_port *master;
109 res = dev_set_promiscuity(dev, 1);
113 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
114 hsr_dev = master->dev;
116 res = netdev_upper_dev_link(dev, hsr_dev, extack);
118 goto fail_upper_dev_link;
120 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
122 goto fail_rx_handler;
123 dev_disable_lro(dev);
128 netdev_upper_dev_unlink(dev, hsr_dev);
130 dev_set_promiscuity(dev, -1);
134 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
135 enum hsr_port_type type, struct netlink_ext_ack *extack)
137 struct hsr_port *port, *master;
140 if (type != HSR_PT_MASTER) {
141 res = hsr_check_dev_ok(dev, extack);
146 port = hsr_port_get_hsr(hsr, type);
148 return -EBUSY; /* This port already exists */
150 port = kzalloc(sizeof(*port), GFP_KERNEL);
158 if (type != HSR_PT_MASTER) {
159 res = hsr_portdev_setup(hsr, dev, port, extack);
164 list_add_tail_rcu(&port->port_list, &hsr->ports);
167 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
168 netdev_update_features(master->dev);
169 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
178 void hsr_del_port(struct hsr_port *port)
180 struct hsr_priv *hsr;
181 struct hsr_port *master;
184 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
185 list_del_rcu(&port->port_list);
187 if (port != master) {
188 netdev_update_features(master->dev);
189 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
190 netdev_rx_handler_unregister(port->dev);
191 dev_set_promiscuity(port->dev, -1);
192 netdev_upper_dev_unlink(port->dev, master->dev);