xref: /openbmc/linux/net/hsr/hsr_slave.c (revision d9fd5a71)
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 
19 bool hsr_invalid_dan_ingress_frame(__be16 protocol)
20 {
21 	return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR));
22 }
23 
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 	    hsr->proto_ops->invalid_dan_ingress_frame &&
59 	    hsr->proto_ops->invalid_dan_ingress_frame(protocol))
60 		goto finish_pass;
61 
62 	skb_push(skb, ETH_HLEN);
63 
64 	if (skb_mac_header(skb) != skb->data) {
65 		WARN_ONCE(1, "%s:%d: Malformed frame at source port %s)\n",
66 			  __func__, __LINE__, port->dev->name);
67 		goto finish_consume;
68 	}
69 
70 	hsr_forward_skb(skb, port);
71 
72 finish_consume:
73 	return RX_HANDLER_CONSUMED;
74 
75 finish_pass:
76 	return RX_HANDLER_PASS;
77 }
78 
79 bool hsr_port_exists(const struct net_device *dev)
80 {
81 	return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
82 }
83 
84 static 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. */
126 static 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 	res = dev_set_promiscuity(dev, 1);
136 	if (res)
137 		return res;
138 
139 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
140 	hsr_dev = master->dev;
141 
142 	res = netdev_upper_dev_link(dev, hsr_dev, extack);
143 	if (res)
144 		goto fail_upper_dev_link;
145 
146 	res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
147 	if (res)
148 		goto fail_rx_handler;
149 	dev_disable_lro(dev);
150 
151 	return 0;
152 
153 fail_rx_handler:
154 	netdev_upper_dev_unlink(dev, hsr_dev);
155 fail_upper_dev_link:
156 	dev_set_promiscuity(dev, -1);
157 	return res;
158 }
159 
160 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
161 		 enum hsr_port_type type, struct netlink_ext_ack *extack)
162 {
163 	struct hsr_port *port, *master;
164 	int res;
165 
166 	if (type != HSR_PT_MASTER) {
167 		res = hsr_check_dev_ok(dev, extack);
168 		if (res)
169 			return res;
170 	}
171 
172 	port = hsr_port_get_hsr(hsr, type);
173 	if (port)
174 		return -EBUSY;	/* This port already exists */
175 
176 	port = kzalloc(sizeof(*port), GFP_KERNEL);
177 	if (!port)
178 		return -ENOMEM;
179 
180 	port->hsr = hsr;
181 	port->dev = dev;
182 	port->type = type;
183 
184 	if (type != HSR_PT_MASTER) {
185 		res = hsr_portdev_setup(hsr, dev, port, extack);
186 		if (res)
187 			goto fail_dev_setup;
188 	}
189 
190 	list_add_tail_rcu(&port->port_list, &hsr->ports);
191 	synchronize_rcu();
192 
193 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
194 	netdev_update_features(master->dev);
195 	dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
196 
197 	return 0;
198 
199 fail_dev_setup:
200 	kfree(port);
201 	return res;
202 }
203 
204 void hsr_del_port(struct hsr_port *port)
205 {
206 	struct hsr_priv *hsr;
207 	struct hsr_port *master;
208 
209 	hsr = port->hsr;
210 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
211 	list_del_rcu(&port->port_list);
212 
213 	if (port != master) {
214 		netdev_update_features(master->dev);
215 		dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
216 		netdev_rx_handler_unregister(port->dev);
217 		dev_set_promiscuity(port->dev, -1);
218 		netdev_upper_dev_unlink(port->dev, master->dev);
219 	}
220 
221 	synchronize_rcu();
222 
223 	kfree(port);
224 }
225