]> git.ipfire.org Git - thirdparty/linux.git/blame - net/hsr/hsr_slave.c
Merge tag 'io_uring-5.7-2020-05-22' of git://git.kernel.dk/linux-block
[thirdparty/linux.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
6 */
7
8#include "hsr_slave.h"
9#include <linux/etherdevice.h>
51f3c605 10#include <linux/if_arp.h>
d0d7b10b 11#include <linux/if_vlan.h>
81ba6afd 12#include "hsr_main.h"
51f3c605 13#include "hsr_device.h"
f266a683 14#include "hsr_forward.h"
81ba6afd
AB
15#include "hsr_framereg.h"
16
f266a683
AB
17static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
18{
19 struct sk_buff *skb = *pskb;
20 struct hsr_port *port;
f5dda315 21 __be16 protocol;
f266a683
AB
22
23 if (!skb_mac_header_was_set(skb)) {
24 WARN_ONCE(1, "%s: skb invalid", __func__);
25 return RX_HANDLER_PASS;
26 }
27
f266a683 28 port = hsr_port_get_rcu(skb->dev);
2b5b8251
ED
29 if (!port)
30 goto finish_pass;
f266a683
AB
31
32 if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
33 /* Directly kill frames sent by ourselves */
34 kfree_skb(skb);
35 goto finish_consume;
36 }
37
ee1c2797
PH
38 protocol = eth_hdr(skb)->h_proto;
39 if (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR))
f266a683
AB
40 goto finish_pass;
41
42 skb_push(skb, ETH_HLEN);
43
44 hsr_forward_skb(skb, port);
45
46finish_consume:
f266a683
AB
47 return RX_HANDLER_CONSUMED;
48
49finish_pass:
f266a683
AB
50 return RX_HANDLER_PASS;
51}
52
53bool hsr_port_exists(const struct net_device *dev)
54{
55 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
56}
57
13eeb5fe
TY
58static int hsr_check_dev_ok(struct net_device *dev,
59 struct netlink_ext_ack *extack)
51f3c605
AB
60{
61 /* Don't allow HSR on non-ethernet like devices */
5670342c
MK
62 if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
63 dev->addr_len != ETH_ALEN) {
13eeb5fe 64 NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
51f3c605
AB
65 return -EINVAL;
66 }
67
68 /* Don't allow enslaving hsr devices */
69 if (is_hsr_master(dev)) {
13eeb5fe
TY
70 NL_SET_ERR_MSG_MOD(extack,
71 "Cannot create trees of HSR devices.");
51f3c605
AB
72 return -EINVAL;
73 }
74
c5a75911 75 if (hsr_port_exists(dev)) {
13eeb5fe
TY
76 NL_SET_ERR_MSG_MOD(extack,
77 "This device is already a HSR slave.");
51f3c605
AB
78 return -EINVAL;
79 }
80
d0d7b10b 81 if (is_vlan_dev(dev)) {
13eeb5fe 82 NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
51f3c605
AB
83 return -EINVAL;
84 }
85
f266a683 86 if (dev->priv_flags & IFF_DONT_BRIDGE) {
13eeb5fe
TY
87 NL_SET_ERR_MSG_MOD(extack,
88 "This device does not support bridging.");
f266a683
AB
89 return -EOPNOTSUPP;
90 }
91
51f3c605
AB
92 /* HSR over bonded devices has not been tested, but I'm not sure it
93 * won't work...
94 */
95
96 return 0;
97}
98
c5a75911 99/* Setup device to be added to the HSR bridge. */
e0a4b997
TY
100static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
101 struct hsr_port *port,
102 struct netlink_ext_ack *extack)
103
51f3c605 104{
e0a4b997
TY
105 struct net_device *hsr_dev;
106 struct hsr_port *master;
51f3c605
AB
107 int res;
108
51f3c605
AB
109 res = dev_set_promiscuity(dev, 1);
110 if (res)
56dc0a0e 111 return res;
51f3c605 112
e0a4b997
TY
113 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
114 hsr_dev = master->dev;
115
116 res = netdev_upper_dev_link(dev, hsr_dev, extack);
117 if (res)
118 goto fail_upper_dev_link;
51f3c605 119
f266a683
AB
120 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
121 if (res)
122 goto fail_rx_handler;
123 dev_disable_lro(dev);
124
51f3c605
AB
125 return 0;
126
127fail_rx_handler:
e0a4b997
TY
128 netdev_upper_dev_unlink(dev, hsr_dev);
129fail_upper_dev_link:
51f3c605 130 dev_set_promiscuity(dev, -1);
51f3c605
AB
131 return res;
132}
133
c5a75911 134int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
13eeb5fe 135 enum hsr_port_type type, struct netlink_ext_ack *extack)
51f3c605 136{
c5a75911
AB
137 struct hsr_port *port, *master;
138 int res;
51f3c605 139
c5a75911 140 if (type != HSR_PT_MASTER) {
13eeb5fe 141 res = hsr_check_dev_ok(dev, extack);
c5a75911
AB
142 if (res)
143 return res;
144 }
145
146 port = hsr_port_get_hsr(hsr, type);
05ca6e64 147 if (port)
c5a75911
AB
148 return -EBUSY; /* This port already exists */
149
150 port = kzalloc(sizeof(*port), GFP_KERNEL);
05ca6e64 151 if (!port)
c5a75911
AB
152 return -ENOMEM;
153
3a303cfd
TY
154 port->hsr = hsr;
155 port->dev = dev;
156 port->type = type;
157
c5a75911 158 if (type != HSR_PT_MASTER) {
e0a4b997 159 res = hsr_portdev_setup(hsr, dev, port, extack);
c5a75911
AB
160 if (res)
161 goto fail_dev_setup;
162 }
163
c5a75911
AB
164 list_add_tail_rcu(&port->port_list, &hsr->ports);
165 synchronize_rcu();
166
167 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
1cc1eb52 168 netdev_update_features(master->dev);
c5a75911 169 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
51f3c605 170
c5a75911
AB
171 return 0;
172
173fail_dev_setup:
174 kfree(port);
175 return res;
176}
177
178void hsr_del_port(struct hsr_port *port)
179{
180 struct hsr_priv *hsr;
181 struct hsr_port *master;
182
183 hsr = port->hsr;
184 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
185 list_del_rcu(&port->port_list);
186
187 if (port != master) {
e0a4b997
TY
188 netdev_update_features(master->dev);
189 dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
c5a75911
AB
190 netdev_rx_handler_unregister(port->dev);
191 dev_set_promiscuity(port->dev, -1);
e0a4b997 192 netdev_upper_dev_unlink(port->dev, master->dev);
51f3c605
AB
193 }
194
195 synchronize_rcu();
56b08fdc 196
619afef0 197 kfree(port);
51f3c605 198}