Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 hsr_forward_skb(skb, port);
71
72finish_consume:
73 return RX_HANDLER_CONSUMED;
74
75finish_pass:
76 return RX_HANDLER_PASS;
77}
78
79bool hsr_port_exists(const struct net_device *dev)
80{
81 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
82}
83
84static int hsr_check_dev_ok(struct net_device *dev,
85 struct netlink_ext_ack *extack)
86{
87 /* Don't allow HSR on non-ethernet like devices */
88 if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
89 dev->addr_len != ETH_ALEN) {
90 NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
91 return -EINVAL;
92 }
93
94 /* Don't allow enslaving hsr devices */
95 if (is_hsr_master(dev)) {
96 NL_SET_ERR_MSG_MOD(extack,
97 "Cannot create trees of HSR devices.");
98 return -EINVAL;
99 }
100
101 if (hsr_port_exists(dev)) {
102 NL_SET_ERR_MSG_MOD(extack,
103 "This device is already a HSR slave.");
104 return -EINVAL;
105 }
106
107 if (is_vlan_dev(dev)) {
108 NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
109 return -EINVAL;
110 }
111
112 if (dev->priv_flags & IFF_DONT_BRIDGE) {
113 NL_SET_ERR_MSG_MOD(extack,
114 "This device does not support bridging.");
115 return -EOPNOTSUPP;
116 }
117
118 /* HSR over bonded devices has not been tested, but I'm not sure it
119 * won't work...
120 */
121
122 return 0;
123}
124
125/* Setup device to be added to the HSR bridge. */
126static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
127 struct hsr_port *port,
128 struct netlink_ext_ack *extack)
129
130{
131 struct net_device *hsr_dev;
132 struct hsr_port *master;
133 int res;
134
135 /* Don't use promiscuous mode for offload since L2 frame forward
136 * happens at the offloaded hardware.
137 */
138 if (!port->hsr->fwd_offloaded) {
139 res = dev_set_promiscuity(dev, 1);
140 if (res)
141 return res;
142 }
143
144 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
145 hsr_dev = master->dev;
146
147 res = netdev_upper_dev_link(dev, hsr_dev, extack);
148 if (res)
149 goto fail_upper_dev_link;
150
151 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
152 if (res)
153 goto fail_rx_handler;
154 dev_disable_lro(dev);
155
156 return 0;
157
158fail_rx_handler:
159 netdev_upper_dev_unlink(dev, hsr_dev);
160fail_upper_dev_link:
161 if (!port->hsr->fwd_offloaded)
162 dev_set_promiscuity(dev, -1);
163
164 return res;
165}
166
167int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
168 enum hsr_port_type type, struct netlink_ext_ack *extack)
169{
170 struct hsr_port *port, *master;
171 int res;
172
173 if (type != HSR_PT_MASTER) {
174 res = hsr_check_dev_ok(dev, extack);
175 if (res)
176 return res;
177 }
178
179 port = hsr_port_get_hsr(hsr, type);
180 if (port)
181 return -EBUSY; /* This port already exists */
182
183 port = kzalloc(sizeof(*port), GFP_KERNEL);
184 if (!port)
185 return -ENOMEM;
186
187 port->hsr = hsr;
188 port->dev = dev;
189 port->type = type;
190
191 if (type != HSR_PT_MASTER) {
192 res = hsr_portdev_setup(hsr, dev, port, extack);
193 if (res)
194 goto fail_dev_setup;
195 }
196
197 list_add_tail_rcu(&port->port_list, &hsr->ports);
198 synchronize_rcu();
199
200 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
201 netdev_update_features(master->dev);
202 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
203
204 return 0;
205
206fail_dev_setup:
207 kfree(port);
208 return res;
209}
210
211void hsr_del_port(struct hsr_port *port)
212{
213 struct hsr_priv *hsr;
214 struct hsr_port *master;
215
216 hsr = port->hsr;
217 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
218 list_del_rcu(&port->port_list);
219
220 if (port != master) {
221 netdev_update_features(master->dev);
222 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
223 netdev_rx_handler_unregister(port->dev);
224 if (!port->hsr->fwd_offloaded)
225 dev_set_promiscuity(port->dev, -1);
226 netdev_upper_dev_unlink(port->dev, master->dev);
227 }
228
229 synchronize_rcu();
230
231 kfree(port);
232}