• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, but for PRP
52 	 * there could be non tagged frames as well from Single
53 	 * attached nodes (SANs).
54 	 */
55 	protocol = eth_hdr(skb)->h_proto;
56 	if (hsr->proto_ops->invalid_dan_ingress_frame &&
57 	    hsr->proto_ops->invalid_dan_ingress_frame(protocol))
58 		goto finish_pass;
59 
60 	skb_push(skb, ETH_HLEN);
61 	skb_reset_mac_header(skb);
62 	if ((!hsr->prot_version && protocol == htons(ETH_P_PRP)) ||
63 	    protocol == htons(ETH_P_HSR))
64 		skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
65 	skb_reset_mac_len(skb);
66 
67 	hsr_forward_skb(skb, port);
68 
69 finish_consume:
70 	return RX_HANDLER_CONSUMED;
71 
72 finish_pass:
73 	return RX_HANDLER_PASS;
74 }
75 
hsr_port_exists(const struct net_device * dev)76 bool hsr_port_exists(const struct net_device *dev)
77 {
78 	return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
79 }
80 
hsr_check_dev_ok(struct net_device * dev,struct netlink_ext_ack * extack)81 static int hsr_check_dev_ok(struct net_device *dev,
82 			    struct netlink_ext_ack *extack)
83 {
84 	/* Don't allow HSR on non-ethernet like devices */
85 	if ((dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
86 	    dev->addr_len != ETH_ALEN) {
87 		NL_SET_ERR_MSG_MOD(extack, "Cannot use loopback or non-ethernet device as HSR slave.");
88 		return -EINVAL;
89 	}
90 
91 	/* Don't allow enslaving hsr devices */
92 	if (is_hsr_master(dev)) {
93 		NL_SET_ERR_MSG_MOD(extack,
94 				   "Cannot create trees of HSR devices.");
95 		return -EINVAL;
96 	}
97 
98 	if (hsr_port_exists(dev)) {
99 		NL_SET_ERR_MSG_MOD(extack,
100 				   "This device is already a HSR slave.");
101 		return -EINVAL;
102 	}
103 
104 	if (is_vlan_dev(dev)) {
105 		NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
106 		return -EINVAL;
107 	}
108 
109 	if (dev->priv_flags & IFF_DONT_BRIDGE) {
110 		NL_SET_ERR_MSG_MOD(extack,
111 				   "This device does not support bridging.");
112 		return -EOPNOTSUPP;
113 	}
114 
115 	/* HSR over bonded devices has not been tested, but I'm not sure it
116 	 * won't work...
117 	 */
118 
119 	return 0;
120 }
121 
122 /* 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)123 static int hsr_portdev_setup(struct hsr_priv *hsr, struct net_device *dev,
124 			     struct hsr_port *port,
125 			     struct netlink_ext_ack *extack)
126 
127 {
128 	struct net_device *hsr_dev;
129 	struct hsr_port *master;
130 	int res;
131 
132 	res = dev_set_promiscuity(dev, 1);
133 	if (res)
134 		return res;
135 
136 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
137 	hsr_dev = master->dev;
138 
139 	res = netdev_upper_dev_link(dev, hsr_dev, extack);
140 	if (res)
141 		goto fail_upper_dev_link;
142 
143 	res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
144 	if (res)
145 		goto fail_rx_handler;
146 	dev_disable_lro(dev);
147 
148 	return 0;
149 
150 fail_rx_handler:
151 	netdev_upper_dev_unlink(dev, hsr_dev);
152 fail_upper_dev_link:
153 	dev_set_promiscuity(dev, -1);
154 	return res;
155 }
156 
hsr_add_port(struct hsr_priv * hsr,struct net_device * dev,enum hsr_port_type type,struct netlink_ext_ack * extack)157 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
158 		 enum hsr_port_type type, struct netlink_ext_ack *extack)
159 {
160 	struct hsr_port *port, *master;
161 	int res;
162 
163 	if (type != HSR_PT_MASTER) {
164 		res = hsr_check_dev_ok(dev, extack);
165 		if (res)
166 			return res;
167 	}
168 
169 	port = hsr_port_get_hsr(hsr, type);
170 	if (port)
171 		return -EBUSY;	/* This port already exists */
172 
173 	port = kzalloc(sizeof(*port), GFP_KERNEL);
174 	if (!port)
175 		return -ENOMEM;
176 
177 	port->hsr = hsr;
178 	port->dev = dev;
179 	port->type = type;
180 
181 	if (type != HSR_PT_MASTER) {
182 		res = hsr_portdev_setup(hsr, dev, port, extack);
183 		if (res)
184 			goto fail_dev_setup;
185 	}
186 
187 	list_add_tail_rcu(&port->port_list, &hsr->ports);
188 	synchronize_rcu();
189 
190 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
191 	netdev_update_features(master->dev);
192 	dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
193 
194 	return 0;
195 
196 fail_dev_setup:
197 	kfree(port);
198 	return res;
199 }
200 
hsr_del_port(struct hsr_port * port)201 void hsr_del_port(struct hsr_port *port)
202 {
203 	struct hsr_priv *hsr;
204 	struct hsr_port *master;
205 
206 	hsr = port->hsr;
207 	master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
208 	list_del_rcu(&port->port_list);
209 
210 	if (port != master) {
211 		netdev_update_features(master->dev);
212 		dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
213 		netdev_rx_handler_unregister(port->dev);
214 		dev_set_promiscuity(port->dev, -1);
215 		netdev_upper_dev_unlink(port->dev, master->dev);
216 	}
217 
218 	synchronize_rcu();
219 
220 	kfree(port);
221 }
222