]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - net/hsr/hsr_slave.c
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
[mirror_ubuntu-kernels.git] / net / hsr / hsr_slave.c
CommitLineData
0e7623bd 1// SPDX-License-Identifier: GPL-2.0
81ba6afd 2/* Copyright 2011-2014 Autronica Fire and Security AS
81ba6afd
AB
3 *
4 * Author(s):
5 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
8f4c0e01
MK
6 *
7 * Frame handler other utility functions for HSR and PRP.
81ba6afd
AB
8 */
9
10#include "hsr_slave.h"
11#include <linux/etherdevice.h>
51f3c605 12#include <linux/if_arp.h>
d0d7b10b 13#include <linux/if_vlan.h>
81ba6afd 14#include "hsr_main.h"
51f3c605 15#include "hsr_device.h"
f266a683 16#include "hsr_forward.h"
81ba6afd
AB
17#include "hsr_framereg.h"
18
451d8123
MK
19bool hsr_invalid_dan_ingress_frame(__be16 protocol)
20{
21 return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR));
22}
23
f266a683
AB
24static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
25{
26 struct sk_buff *skb = *pskb;
27 struct hsr_port *port;
451d8123 28 struct hsr_priv *hsr;
f5dda315 29 __be16 protocol;
f266a683 30
451d8123
MK
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
f266a683
AB
35 if (!skb_mac_header_was_set(skb)) {
36 WARN_ONCE(1, "%s: skb invalid", __func__);
37 return RX_HANDLER_PASS;
38 }
39
f266a683 40 port = hsr_port_get_rcu(skb->dev);
2b5b8251
ED
41 if (!port)
42 goto finish_pass;
451d8123 43 hsr = port->hsr;
f266a683
AB
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
dcf0cd1c
GM
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).
451d8123 54 */
ee1c2797 55 protocol = eth_hdr(skb)->h_proto;
dcf0cd1c
GM
56
57 if (!(port->dev->features & NETIF_F_HW_HSR_TAG_RM) &&
58 hsr->proto_ops->invalid_dan_ingress_frame &&
451d8123 59 hsr->proto_ops->invalid_dan_ingress_frame(protocol))
f266a683
AB
60 goto finish_pass;
61
62 skb_push(skb, ETH_HLEN);
48b491a5
GM
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);
451d8123 68
f266a683
AB
69 hsr_forward_skb(skb, port);
70
71finish_consume:
f266a683
AB
72 return RX_HANDLER_CONSUMED;
73
74finish_pass:
f266a683
AB
75 return RX_HANDLER_PASS;
76}
77
78bool hsr_port_exists(const struct net_device *dev)
79{
80 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
81}
82
13eeb5fe
TY
83static int hsr_check_dev_ok(struct net_device *dev,
84 struct netlink_ext_ack *extack)
51f3c605
AB
85{
86 /* Don't allow HSR on non-ethernet like devices */
5670342c
MK
87 if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
88 dev->addr_len != ETH_ALEN) {
13eeb5fe 89 NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
51f3c605
AB
90 return -EINVAL;
91 }
92
93 /* Don't allow enslaving hsr devices */
94 if (is_hsr_master(dev)) {
13eeb5fe
TY
95 NL_SET_ERR_MSG_MOD(extack,
96 "Cannot create trees of HSR devices.");
51f3c605
AB
97 return -EINVAL;
98 }
99
c5a75911 100 if (hsr_port_exists(dev)) {
13eeb5fe
TY
101 NL_SET_ERR_MSG_MOD(extack,
102 "This device is already a HSR slave.");
51f3c605
AB
103 return -EINVAL;
104 }
105
d0d7b10b 106 if (is_vlan_dev(dev)) {
13eeb5fe 107 NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
51f3c605
AB
108 return -EINVAL;
109 }
110
f266a683 111 if (dev->priv_flags & IFF_DONT_BRIDGE) {
13eeb5fe
TY
112 NL_SET_ERR_MSG_MOD(extack,
113 "This device does not support bridging.");
f266a683
AB
114 return -EOPNOTSUPP;
115 }
116
51f3c605
AB
117 /* HSR over bonded devices has not been tested, but I'm not sure it
118 * won't work...
119 */
120
121 return 0;
122}
123
c5a75911 124/* Setup device to be added to the HSR bridge. */
e0a4b997
TY
125static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
126 struct hsr_port *port,
127 struct netlink_ext_ack *extack)
128
51f3c605 129{
e0a4b997
TY
130 struct net_device *hsr_dev;
131 struct hsr_port *master;
51f3c605
AB
132 int res;
133
51f3c605
AB
134 res = dev_set_promiscuity(dev, 1);
135 if (res)
56dc0a0e 136 return res;
51f3c605 137
e0a4b997
TY
138 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
139 hsr_dev = master->dev;
140
141 res = netdev_upper_dev_link(dev, hsr_dev, extack);
142 if (res)
143 goto fail_upper_dev_link;
51f3c605 144
f266a683
AB
145 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
146 if (res)
147 goto fail_rx_handler;
148 dev_disable_lro(dev);
149
51f3c605
AB
150 return 0;
151
152fail_rx_handler:
e0a4b997
TY
153 netdev_upper_dev_unlink(dev, hsr_dev);
154fail_upper_dev_link:
51f3c605 155 dev_set_promiscuity(dev, -1);
51f3c605
AB
156 return res;
157}
158
c5a75911 159int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
13eeb5fe 160 enum hsr_port_type type, struct netlink_ext_ack *extack)
51f3c605 161{
c5a75911
AB
162 struct hsr_port *port, *master;
163 int res;
51f3c605 164
c5a75911 165 if (type != HSR_PT_MASTER) {
13eeb5fe 166 res = hsr_check_dev_ok(dev, extack);
c5a75911
AB
167 if (res)
168 return res;
169 }
170
171 port = hsr_port_get_hsr(hsr, type);
05ca6e64 172 if (port)
c5a75911
AB
173 return -EBUSY; /* This port already exists */
174
175 port = kzalloc(sizeof(*port), GFP_KERNEL);
05ca6e64 176 if (!port)
c5a75911
AB
177 return -ENOMEM;
178
3a303cfd
TY
179 port->hsr = hsr;
180 port->dev = dev;
181 port->type = type;
182
c5a75911 183 if (type != HSR_PT_MASTER) {
e0a4b997 184 res = hsr_portdev_setup(hsr, dev, port, extack);
c5a75911
AB
185 if (res)
186 goto fail_dev_setup;
187 }
188
c5a75911
AB
189 list_add_tail_rcu(&port->port_list, &hsr->ports);
190 synchronize_rcu();
191
192 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
1cc1eb52 193 netdev_update_features(master->dev);
c5a75911 194 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
51f3c605 195
c5a75911
AB
196 return 0;
197
198fail_dev_setup:
199 kfree(port);
200 return res;
201}
202
203void hsr_del_port(struct hsr_port *port)
204{
205 struct hsr_priv *hsr;
206 struct hsr_port *master;
207
208 hsr = port->hsr;
209 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
210 list_del_rcu(&port->port_list);
211
212 if (port != master) {
e0a4b997
TY
213 netdev_update_features(master->dev);
214 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
c5a75911
AB
215 netdev_rx_handler_unregister(port->dev);
216 dev_set_promiscuity(port->dev, -1);
e0a4b997 217 netdev_upper_dev_unlink(port->dev, master->dev);
51f3c605
AB
218 }
219
220 synchronize_rcu();
56b08fdc 221
619afef0 222 kfree(port);
51f3c605 223}