hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/hsr/hsr_slave.c
....@@ -1,12 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /* Copyright 2011-2014 Autronica Fire and Security AS
2
- *
3
- * This program is free software; you can redistribute it and/or modify it
4
- * under the terms of the GNU General Public License as published by the Free
5
- * Software Foundation; either version 2 of the License, or (at your option)
6
- * any later version.
73 *
84 * Author(s):
95 * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
6
+ *
7
+ * Frame handler other utility functions for HSR and PRP.
108 */
119
1210 #include "hsr_slave.h"
....@@ -18,22 +16,31 @@
1816 #include "hsr_forward.h"
1917 #include "hsr_framereg.h"
2018
19
+bool hsr_invalid_dan_ingress_frame(__be16 protocol)
20
+{
21
+ return (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR));
22
+}
2123
2224 static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
2325 {
2426 struct sk_buff *skb = *pskb;
2527 struct hsr_port *port;
26
- u16 protocol;
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;
2734
2835 if (!skb_mac_header_was_set(skb)) {
2936 WARN_ONCE(1, "%s: skb invalid", __func__);
3037 return RX_HANDLER_PASS;
3138 }
3239
33
- rcu_read_lock(); /* hsr->node_db, hsr->ports */
3440 port = hsr_port_get_rcu(skb->dev);
3541 if (!port)
3642 goto finish_pass;
43
+ hsr = port->hsr;
3744
3845 if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
3946 /* Directly kill frames sent by ourselves */
....@@ -41,20 +48,28 @@
4148 goto finish_consume;
4249 }
4350
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
+ */
4455 protocol = eth_hdr(skb)->h_proto;
45
- if (protocol != htons(ETH_P_PRP) && protocol != htons(ETH_P_HSR))
56
+ if (hsr->proto_ops->invalid_dan_ingress_frame &&
57
+ hsr->proto_ops->invalid_dan_ingress_frame(protocol))
4658 goto finish_pass;
4759
4860 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);
4966
5067 hsr_forward_skb(skb, port);
5168
5269 finish_consume:
53
- rcu_read_unlock(); /* hsr->node_db, hsr->ports */
5470 return RX_HANDLER_CONSUMED;
5571
5672 finish_pass:
57
- rcu_read_unlock(); /* hsr->node_db, hsr->ports */
5873 return RX_HANDLER_PASS;
5974 }
6075
....@@ -63,34 +78,37 @@
6378 return rcu_access_pointer(dev->rx_handler) == hsr_handle_frame;
6479 }
6580
66
-
67
-static int hsr_check_dev_ok(struct net_device *dev)
81
+static int hsr_check_dev_ok(struct net_device *dev,
82
+ struct netlink_ext_ack *extack)
6883 {
6984 /* Don't allow HSR on non-ethernet like devices */
70
- if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) ||
71
- (dev->addr_len != ETH_ALEN)) {
72
- netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n");
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.");
7388 return -EINVAL;
7489 }
7590
7691 /* Don't allow enslaving hsr devices */
7792 if (is_hsr_master(dev)) {
78
- netdev_info(dev, "Cannot create trees of HSR devices.\n");
93
+ NL_SET_ERR_MSG_MOD(extack,
94
+ "Cannot create trees of HSR devices.");
7995 return -EINVAL;
8096 }
8197
8298 if (hsr_port_exists(dev)) {
83
- netdev_info(dev, "This device is already a HSR slave.\n");
99
+ NL_SET_ERR_MSG_MOD(extack,
100
+ "This device is already a HSR slave.");
84101 return -EINVAL;
85102 }
86103
87104 if (is_vlan_dev(dev)) {
88
- netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n");
105
+ NL_SET_ERR_MSG_MOD(extack, "HSR on top of VLAN is not yet supported in this driver.");
89106 return -EINVAL;
90107 }
91108
92109 if (dev->priv_flags & IFF_DONT_BRIDGE) {
93
- netdev_info(dev, "This device does not support bridging.\n");
110
+ NL_SET_ERR_MSG_MOD(extack,
111
+ "This device does not support bridging.");
94112 return -EOPNOTSUPP;
95113 }
96114
....@@ -101,21 +119,26 @@
101119 return 0;
102120 }
103121
104
-
105122 /* Setup device to be added to the HSR bridge. */
106
-static int hsr_portdev_setup(struct net_device *dev, struct hsr_port *port)
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
+
107127 {
128
+ struct net_device *hsr_dev;
129
+ struct hsr_port *master;
108130 int res;
109131
110
- dev_hold(dev);
111132 res = dev_set_promiscuity(dev, 1);
112133 if (res)
113
- goto fail_promiscuity;
134
+ return res;
114135
115
- /* FIXME:
116
- * What does net device "adjacency" mean? Should we do
117
- * res = netdev_master_upper_dev_link(port->dev, port->hsr->dev); ?
118
- */
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;
119142
120143 res = netdev_rx_handler_register(dev, hsr_handle_frame, port);
121144 if (res)
....@@ -125,31 +148,30 @@
125148 return 0;
126149
127150 fail_rx_handler:
151
+ netdev_upper_dev_unlink(dev, hsr_dev);
152
+fail_upper_dev_link:
128153 dev_set_promiscuity(dev, -1);
129
-fail_promiscuity:
130
- dev_put(dev);
131
-
132154 return res;
133155 }
134156
135157 int hsr_add_port(struct hsr_priv *hsr, struct net_device *dev,
136
- enum hsr_port_type type)
158
+ enum hsr_port_type type, struct netlink_ext_ack *extack)
137159 {
138160 struct hsr_port *port, *master;
139161 int res;
140162
141163 if (type != HSR_PT_MASTER) {
142
- res = hsr_check_dev_ok(dev);
164
+ res = hsr_check_dev_ok(dev, extack);
143165 if (res)
144166 return res;
145167 }
146168
147169 port = hsr_port_get_hsr(hsr, type);
148
- if (port != NULL)
170
+ if (port)
149171 return -EBUSY; /* This port already exists */
150172
151173 port = kzalloc(sizeof(*port), GFP_KERNEL);
152
- if (port == NULL)
174
+ if (!port)
153175 return -ENOMEM;
154176
155177 port->hsr = hsr;
....@@ -157,7 +179,7 @@
157179 port->type = type;
158180
159181 if (type != HSR_PT_MASTER) {
160
- res = hsr_portdev_setup(dev, port);
182
+ res = hsr_portdev_setup(hsr, dev, port, extack);
161183 if (res)
162184 goto fail_dev_setup;
163185 }
....@@ -186,20 +208,14 @@
186208 list_del_rcu(&port->port_list);
187209
188210 if (port != master) {
189
- if (master != NULL) {
190
- netdev_update_features(master->dev);
191
- dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
192
- }
211
+ netdev_update_features(master->dev);
212
+ dev_set_mtu(master->dev, hsr_get_max_mtu(hsr));
193213 netdev_rx_handler_unregister(port->dev);
194214 dev_set_promiscuity(port->dev, -1);
215
+ netdev_upper_dev_unlink(port->dev, master->dev);
195216 }
196
-
197
- /* FIXME?
198
- * netdev_upper_dev_unlink(port->dev, port->hsr->dev);
199
- */
200217
201218 synchronize_rcu();
202219
203
- if (port != master)
204
- dev_put(port->dev);
220
+ kfree(port);
205221 }