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
hsr_invalid_dan_ingress_frame(__be16 protocol)19 bool hsr_invalid_dan_ingress_frame(__be16 protocol)
20 {
21 return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR));
22 }
23
hsr_handle_frame(struct sk_buff ** pskb)24 static 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 if (!pskb_may_pull(skb, ETH_HLEN + HSR_HLEN)) {
68 kfree_skb(skb);
69 goto finish_consume;
70 }
71
72 skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
73 }
74 skb_reset_mac_len(skb);
75
76 /* Only the frames received over the interlink port will assign a
77 * sequence number and require synchronisation vs other sender.
78 */
79 if (port->type == HSR_PT_INTERLINK) {
80 spin_lock_bh(&hsr->seqnr_lock);
81 hsr_forward_skb(skb, port);
82 spin_unlock_bh(&hsr->seqnr_lock);
83 } else {
84 hsr_forward_skb(skb, port);
85 }
86
87 finish_consume:
88 return RX_HANDLER_CONSUMED;
89
90 finish_pass:
91 return RX_HANDLER_PASS;
92 }
93
hsr_port_exists(const struct net_device * dev)94 bool hsr_port_exists(const struct net_device *dev)
95 {
96 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
97 }
98
hsr_check_dev_ok(struct net_device * dev,struct netlink_ext_ack * extack)99 static int hsr_check_dev_ok(struct net_device *dev,
100 struct netlink_ext_ack *extack)
101 {
102 /* Don't allow HSR on non-ethernet like devices */
103 if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
104 dev->addr_len != ETH_ALEN) {
105 NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
106 return -EINVAL;
107 }
108
109 /* Don't allow enslaving hsr devices */
110 if (is_hsr_master(dev)) {
111 NL_SET_ERR_MSG_MOD(extack,
112 "Cannot create trees of HSR devices.");
113 return -EINVAL;
114 }
115
116 if (hsr_port_exists(dev)) {
117 NL_SET_ERR_MSG_MOD(extack,
118 "This device is already a HSR slave.");
119 return -EINVAL;
120 }
121
122 if (is_vlan_dev(dev)) {
123 NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
124 return -EINVAL;
125 }
126
127 if (dev->priv_flags & IFF_DONT_BRIDGE) {
128 NL_SET_ERR_MSG_MOD(extack,
129 "This device does not support bridging.");
130 return -EOPNOTSUPP;
131 }
132
133 /* HSR over bonded devices has not been tested, but I'm not sure it
134 * won't work...
135 */
136
137 return 0;
138 }
139
140 /* Setup device to be added to the HSR bridge. */
hsr_portdev_setup(struct hsr_priv * hsr,struct net_device * dev,struct hsr_port * port,struct netlink_ext_ack * extack)141 static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
142 struct hsr_port *port,
143 struct netlink_ext_ack *extack)
144
145 {
146 struct netdev_lag_upper_info lag_upper_info;
147 struct net_device *hsr_dev;
148 struct hsr_port *master;
149 int res;
150
151 /* Don't use promiscuous mode for offload since L2 frame forward
152 * happens at the offloaded hardware. The interlink port never
153 * gets forwarding offload (RedBox forwarding to/from it is done
154 * by this driver), so it still needs promiscuous mode to receive
155 * frames addressed to hsr_dev's MAC rather than its own.
156 */
157 if (!port->hsr->fwd_offloaded || port->type == HSR_PT_INTERLINK) {
158 res = dev_set_promiscuity(dev, 1);
159 if (res)
160 return res;
161 }
162
163 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
164 hsr_dev = master->dev;
165
166 lag_upper_info.tx_type = NETDEV_LAG_TX_TYPE_BROADCAST;
167 lag_upper_info.hash_type = NETDEV_LAG_HASH_UNKNOWN;
168 res = netdev_master_upper_dev_link(dev, hsr_dev, NULL, &lag_upper_info, extack);
169 if (res)
170 goto fail_upper_dev_link;
171
172 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
173 if (res)
174 goto fail_rx_handler;
175 dev_disable_lro(dev);
176
177 return 0;
178
179 fail_rx_handler:
180 netdev_upper_dev_unlink(dev, hsr_dev);
181 fail_upper_dev_link:
182 if (!port->hsr->fwd_offloaded || port->type == HSR_PT_INTERLINK)
183 dev_set_promiscuity(dev, -1);
184
185 return res;
186 }
187
hsr_add_port(struct hsr_priv * hsr,struct net_device * dev,enum hsr_port_type type,struct netlink_ext_ack * extack)188 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
189 enum hsr_port_type type, struct netlink_ext_ack *extack)
190 {
191 struct hsr_port *port, *master;
192 int res;
193
194 if (type != HSR_PT_MASTER) {
195 res = hsr_check_dev_ok(dev, extack);
196 if (res)
197 return res;
198 }
199
200 port = hsr_port_get_hsr(hsr, type);
201 if (port)
202 return -EBUSY; /* This port already exists */
203
204 port = kzalloc_obj(*port);
205 if (!port)
206 return -ENOMEM;
207
208 port->hsr = hsr;
209 port->dev = dev;
210 port->type = type;
211 ether_addr_copy(port->original_macaddress, dev->dev_addr);
212
213 list_add_tail_rcu(&port->port_list, &hsr->ports);
214
215 if (type != HSR_PT_MASTER) {
216 res = hsr_portdev_setup(hsr, dev, port, extack);
217 if (res)
218 goto fail_dev_setup;
219 }
220
221 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
222 netdev_update_features(master->dev);
223 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
224
225 return 0;
226
227 fail_dev_setup:
228 list_del_rcu(&port->port_list);
229 kfree_rcu(port, rcu);
230 return res;
231 }
232
hsr_del_port(struct hsr_port * port)233 void hsr_del_port(struct hsr_port *port)
234 {
235 struct hsr_priv *hsr;
236 struct hsr_port *master;
237
238 hsr = port->hsr;
239 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
240 list_del_rcu(&port->port_list);
241
242 if (port != master) {
243 netdev_update_features(master->dev);
244 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
245 netdev_rx_handler_unregister(port->dev);
246 if (!port->hsr->fwd_offloaded || port->type == HSR_PT_INTERLINK)
247 dev_set_promiscuity(port->dev, -1);
248 if (port->type == HSR_PT_SLAVE_A || port->type == HSR_PT_SLAVE_B)
249 vlan_vids_del_by_dev(port->dev, master->dev);
250 netdev_upper_dev_unlink(port->dev, master->dev);
251 if (hsr->prot_version == PRP_V1 &&
252 port->type == HSR_PT_SLAVE_B) {
253 eth_hw_addr_set(port->dev, port->original_macaddress);
254 call_netdevice_notifiers(NETDEV_CHANGEADDR, port->dev);
255 }
256 }
257
258 kfree_rcu(port, rcu);
259 }
260