Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Regular and Ethertype DSA tagging
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 *
6 * Regular DSA
7 * -----------
8
9 * For untagged (in 802.1Q terms) packets, the switch will splice in
10 * the tag between the SA and the ethertype of the original
11 * packet. Tagged frames will instead have their outermost .1Q tag
12 * converted to a DSA tag. It expects the same layout when receiving
13 * packets from the CPU.
14 *
15 * Example:
16 *
17 * .----.----.----.---------
18 * Pu: | DA | SA | ET | Payload ...
19 * '----'----'----'---------
20 * 6 6 2 N
21 * .----.----.--------.-----.----.---------
22 * Pt: | DA | SA | 0x8100 | TCI | ET | Payload ...
23 * '----'----'--------'-----'----'---------
24 * 6 6 2 2 2 N
25 * .----.----.-----.----.---------
26 * Pd: | DA | SA | DSA | ET | Payload ...
27 * '----'----'-----'----'---------
28 * 6 6 4 2 N
29 *
30 * No matter if a packet is received untagged (Pu) or tagged (Pt),
31 * they will both have the same layout (Pd) when they are sent to the
32 * CPU. This is done by ignoring 802.3, replacing the ethertype field
33 * with more metadata, among which is a bit to signal if the original
34 * packet was tagged or not.
35 *
36 * Ethertype DSA
37 * -------------
38 * Uses the exact same tag format as regular DSA, but also includes a
39 * proper ethertype field (which the mv88e6xxx driver sets to
40 * ETH_P_EDSA/0xdada) followed by two zero bytes:
41 *
42 * .----.----.--------.--------.-----.----.---------
43 * | DA | SA | 0xdada | 0x0000 | DSA | ET | Payload ...
44 * '----'----'--------'--------'-----'----'---------
45 * 6 6 2 2 4 2 N
46 */
47
48#include <linux/etherdevice.h>
49#include <linux/list.h>
50#include <linux/slab.h>
51
52#include "dsa_priv.h"
53
54#define DSA_HLEN 4
55
56/**
57 * enum dsa_cmd - DSA Command
58 * @DSA_CMD_TO_CPU: Set on packets that were trapped or mirrored to
59 * the CPU port. This is needed to implement control protocols,
60 * e.g. STP and LLDP, that must not allow those control packets to
61 * be switched according to the normal rules.
62 * @DSA_CMD_FROM_CPU: Used by the CPU to send a packet to a specific
63 * port, ignoring all the barriers that the switch normally
64 * enforces (VLANs, STP port states etc.). No source address
65 * learning takes place. "sudo send packet"
66 * @DSA_CMD_TO_SNIFFER: Set on the copies of packets that matched some
67 * user configured ingress or egress monitor criteria. These are
68 * forwarded by the switch tree to the user configured ingress or
69 * egress monitor port, which can be set to the CPU port or a
70 * regular port. If the destination is a regular port, the tag
71 * will be removed before egressing the port. If the destination
72 * is the CPU port, the tag will not be removed.
73 * @DSA_CMD_FORWARD: This tag is used on all bulk traffic passing
74 * through the switch tree, including the flows that are directed
75 * towards the CPU. Its device/port tuple encodes the original
76 * source port on which the packet ingressed. It can also be used
77 * on transmit by the CPU to defer the forwarding decision to the
78 * hardware, based on the current config of PVT/VTU/ATU
79 * etc. Source address learning takes places if enabled on the
80 * receiving DSA/CPU port.
81 */
82enum dsa_cmd {
83 DSA_CMD_TO_CPU = 0,
84 DSA_CMD_FROM_CPU = 1,
85 DSA_CMD_TO_SNIFFER = 2,
86 DSA_CMD_FORWARD = 3
87};
88
89/**
90 * enum dsa_code - TO_CPU Code
91 *
92 * @DSA_CODE_MGMT_TRAP: DA was classified as a management
93 * address. Typical examples include STP BPDUs and LLDP.
94 * @DSA_CODE_FRAME2REG: Response to a "remote management" request.
95 * @DSA_CODE_IGMP_MLD_TRAP: IGMP/MLD signaling.
96 * @DSA_CODE_POLICY_TRAP: Frame matched some policy configuration on
97 * the device. Typical examples are matching on DA/SA/VID and DHCP
98 * snooping.
99 * @DSA_CODE_ARP_MIRROR: The name says it all really.
100 * @DSA_CODE_POLICY_MIRROR: Same as @DSA_CODE_POLICY_TRAP, but the
101 * particular policy was set to trigger a mirror instead of a
102 * trap.
103 * @DSA_CODE_RESERVED_6: Unused on all devices up to at least 6393X.
104 * @DSA_CODE_RESERVED_7: Unused on all devices up to at least 6393X.
105 *
106 * A 3-bit code is used to relay why a particular frame was sent to
107 * the CPU. We only use this to determine if the packet was mirrored
108 * or trapped, i.e. whether the packet has been forwarded by hardware
109 * or not.
110 *
111 * This is the superset of all possible codes. Any particular device
112 * may only implement a subset.
113 */
114enum dsa_code {
115 DSA_CODE_MGMT_TRAP = 0,
116 DSA_CODE_FRAME2REG = 1,
117 DSA_CODE_IGMP_MLD_TRAP = 2,
118 DSA_CODE_POLICY_TRAP = 3,
119 DSA_CODE_ARP_MIRROR = 4,
120 DSA_CODE_POLICY_MIRROR = 5,
121 DSA_CODE_RESERVED_6 = 6,
122 DSA_CODE_RESERVED_7 = 7
123};
124
125static struct sk_buff *dsa_xmit_ll(struct sk_buff *skb, struct net_device *dev,
126 u8 extra)
127{
128 struct dsa_port *dp = dsa_slave_to_port(dev);
129 u8 *dsa_header;
130
131 if (skb->protocol == htons(ETH_P_8021Q)) {
132 if (extra) {
133 skb_push(skb, extra);
134 memmove(skb->data, skb->data + extra, 2 * ETH_ALEN);
135 }
136
137 /* Construct tagged FROM_CPU DSA tag from 802.1Q tag. */
138 dsa_header = skb->data + 2 * ETH_ALEN + extra;
139 dsa_header[0] = (DSA_CMD_FROM_CPU << 6) | 0x20 | dp->ds->index;
140 dsa_header[1] = dp->index << 3;
141
142 /* Move CFI field from byte 2 to byte 1. */
143 if (dsa_header[2] & 0x10) {
144 dsa_header[1] |= 0x01;
145 dsa_header[2] &= ~0x10;
146 }
147 } else {
148 skb_push(skb, DSA_HLEN + extra);
149 memmove(skb->data, skb->data + DSA_HLEN + extra, 2 * ETH_ALEN);
150
151 /* Construct untagged FROM_CPU DSA tag. */
152 dsa_header = skb->data + 2 * ETH_ALEN + extra;
153 dsa_header[0] = (DSA_CMD_FROM_CPU << 6) | dp->ds->index;
154 dsa_header[1] = dp->index << 3;
155 dsa_header[2] = 0x00;
156 dsa_header[3] = 0x00;
157 }
158
159 return skb;
160}
161
162static struct sk_buff *dsa_rcv_ll(struct sk_buff *skb, struct net_device *dev,
163 u8 extra)
164{
165 int source_device, source_port;
166 bool trunk = false;
167 enum dsa_code code;
168 enum dsa_cmd cmd;
169 u8 *dsa_header;
170
171 /* The ethertype field is part of the DSA header. */
172 dsa_header = skb->data - 2;
173
174 cmd = dsa_header[0] >> 6;
175 switch (cmd) {
176 case DSA_CMD_FORWARD:
177 skb->offload_fwd_mark = 1;
178
179 trunk = !!(dsa_header[1] & 4);
180 break;
181
182 case DSA_CMD_TO_CPU:
183 code = (dsa_header[1] & 0x6) | ((dsa_header[2] >> 4) & 1);
184
185 switch (code) {
186 case DSA_CODE_FRAME2REG:
187 /* Remote management is not implemented yet,
188 * drop.
189 */
190 return NULL;
191 case DSA_CODE_ARP_MIRROR:
192 case DSA_CODE_POLICY_MIRROR:
193 /* Mark mirrored packets to notify any upper
194 * device (like a bridge) that forwarding has
195 * already been done by hardware.
196 */
197 skb->offload_fwd_mark = 1;
198 break;
199 case DSA_CODE_MGMT_TRAP:
200 case DSA_CODE_IGMP_MLD_TRAP:
201 case DSA_CODE_POLICY_TRAP:
202 /* Traps have, by definition, not been
203 * forwarded by hardware, so don't mark them.
204 */
205 break;
206 default:
207 /* Reserved code, this could be anything. Drop
208 * seems like the safest option.
209 */
210 return NULL;
211 }
212
213 break;
214
215 default:
216 return NULL;
217 }
218
219 source_device = dsa_header[0] & 0x1f;
220 source_port = (dsa_header[1] >> 3) & 0x1f;
221
222 if (trunk) {
223 struct dsa_port *cpu_dp = dev->dsa_ptr;
224
225 /* The exact source port is not available in the tag,
226 * so we inject the frame directly on the upper
227 * team/bond.
228 */
229 skb->dev = dsa_lag_dev(cpu_dp->dst, source_port);
230 } else {
231 skb->dev = dsa_master_find_slave(dev, source_device,
232 source_port);
233 }
234
235 if (!skb->dev)
236 return NULL;
237
238 /* If the 'tagged' bit is set; convert the DSA tag to a 802.1Q
239 * tag, and delete the ethertype (extra) if applicable. If the
240 * 'tagged' bit is cleared; delete the DSA tag, and ethertype
241 * if applicable.
242 */
243 if (dsa_header[0] & 0x20) {
244 u8 new_header[4];
245
246 /* Insert 802.1Q ethertype and copy the VLAN-related
247 * fields, but clear the bit that will hold CFI (since
248 * DSA uses that bit location for another purpose).
249 */
250 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
251 new_header[1] = ETH_P_8021Q & 0xff;
252 new_header[2] = dsa_header[2] & ~0x10;
253 new_header[3] = dsa_header[3];
254
255 /* Move CFI bit from its place in the DSA header to
256 * its 802.1Q-designated place.
257 */
258 if (dsa_header[1] & 0x01)
259 new_header[2] |= 0x10;
260
261 /* Update packet checksum if skb is CHECKSUM_COMPLETE. */
262 if (skb->ip_summed == CHECKSUM_COMPLETE) {
263 __wsum c = skb->csum;
264 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
265 c = csum_sub(c, csum_partial(dsa_header + 2, 2, 0));
266 skb->csum = c;
267 }
268
269 memcpy(dsa_header, new_header, DSA_HLEN);
270
271 if (extra)
272 memmove(skb->data - ETH_HLEN,
273 skb->data - ETH_HLEN - extra,
274 2 * ETH_ALEN);
275 } else {
276 skb_pull_rcsum(skb, DSA_HLEN);
277 memmove(skb->data - ETH_HLEN,
278 skb->data - ETH_HLEN - DSA_HLEN - extra,
279 2 * ETH_ALEN);
280 }
281
282 return skb;
283}
284
285#if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
286
287static struct sk_buff *dsa_xmit(struct sk_buff *skb, struct net_device *dev)
288{
289 return dsa_xmit_ll(skb, dev, 0);
290}
291
292static struct sk_buff *dsa_rcv(struct sk_buff *skb, struct net_device *dev,
293 struct packet_type *pt)
294{
295 if (unlikely(!pskb_may_pull(skb, DSA_HLEN)))
296 return NULL;
297
298 return dsa_rcv_ll(skb, dev, 0);
299}
300
301static const struct dsa_device_ops dsa_netdev_ops = {
302 .name = "dsa",
303 .proto = DSA_TAG_PROTO_DSA,
304 .xmit = dsa_xmit,
305 .rcv = dsa_rcv,
306 .needed_headroom = DSA_HLEN,
307};
308
309DSA_TAG_DRIVER(dsa_netdev_ops);
310MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_DSA);
311#endif /* CONFIG_NET_DSA_TAG_DSA */
312
313#if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
314
315#define EDSA_HLEN 8
316
317static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
318{
319 u8 *edsa_header;
320
321 skb = dsa_xmit_ll(skb, dev, EDSA_HLEN - DSA_HLEN);
322 if (!skb)
323 return NULL;
324
325 edsa_header = skb->data + 2 * ETH_ALEN;
326 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
327 edsa_header[1] = ETH_P_EDSA & 0xff;
328 edsa_header[2] = 0x00;
329 edsa_header[3] = 0x00;
330 return skb;
331}
332
333static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev,
334 struct packet_type *pt)
335{
336 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
337 return NULL;
338
339 skb_pull_rcsum(skb, EDSA_HLEN - DSA_HLEN);
340
341 return dsa_rcv_ll(skb, dev, EDSA_HLEN - DSA_HLEN);
342}
343
344static const struct dsa_device_ops edsa_netdev_ops = {
345 .name = "edsa",
346 .proto = DSA_TAG_PROTO_EDSA,
347 .xmit = edsa_xmit,
348 .rcv = edsa_rcv,
349 .needed_headroom = EDSA_HLEN,
350};
351
352DSA_TAG_DRIVER(edsa_netdev_ops);
353MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_EDSA);
354#endif /* CONFIG_NET_DSA_TAG_EDSA */
355
356static struct dsa_tag_driver *dsa_tag_drivers[] = {
357#if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
358 &DSA_TAG_DRIVER_NAME(dsa_netdev_ops),
359#endif
360#if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
361 &DSA_TAG_DRIVER_NAME(edsa_netdev_ops),
362#endif
363};
364
365module_dsa_tag_drivers(dsa_tag_drivers);
366
367MODULE_LICENSE("GPL");
1/*
2 * net/dsa/tag_dsa.c - (Non-ethertype) DSA tagging
3 * Copyright (c) 2008-2009 Marvell Semiconductor
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#include <linux/etherdevice.h>
12#include <linux/list.h>
13#include <linux/slab.h>
14#include "dsa_priv.h"
15
16#define DSA_HLEN 4
17
18static struct sk_buff *dsa_xmit(struct sk_buff *skb, struct net_device *dev)
19{
20 struct dsa_slave_priv *p = netdev_priv(dev);
21 u8 *dsa_header;
22
23 /*
24 * Convert the outermost 802.1q tag to a DSA tag for tagged
25 * packets, or insert a DSA tag between the addresses and
26 * the ethertype field for untagged packets.
27 */
28 if (skb->protocol == htons(ETH_P_8021Q)) {
29 if (skb_cow_head(skb, 0) < 0)
30 goto out_free;
31
32 /*
33 * Construct tagged FROM_CPU DSA tag from 802.1q tag.
34 */
35 dsa_header = skb->data + 2 * ETH_ALEN;
36 dsa_header[0] = 0x60 | p->parent->index;
37 dsa_header[1] = p->port << 3;
38
39 /*
40 * Move CFI field from byte 2 to byte 1.
41 */
42 if (dsa_header[2] & 0x10) {
43 dsa_header[1] |= 0x01;
44 dsa_header[2] &= ~0x10;
45 }
46 } else {
47 if (skb_cow_head(skb, DSA_HLEN) < 0)
48 goto out_free;
49 skb_push(skb, DSA_HLEN);
50
51 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
52
53 /*
54 * Construct untagged FROM_CPU DSA tag.
55 */
56 dsa_header = skb->data + 2 * ETH_ALEN;
57 dsa_header[0] = 0x40 | p->parent->index;
58 dsa_header[1] = p->port << 3;
59 dsa_header[2] = 0x00;
60 dsa_header[3] = 0x00;
61 }
62
63 return skb;
64
65out_free:
66 kfree_skb(skb);
67 return NULL;
68}
69
70static int dsa_rcv(struct sk_buff *skb, struct net_device *dev,
71 struct packet_type *pt, struct net_device *orig_dev)
72{
73 struct dsa_switch_tree *dst = dev->dsa_ptr;
74 struct dsa_switch *ds;
75 u8 *dsa_header;
76 int source_device;
77 int source_port;
78
79 if (unlikely(dst == NULL))
80 goto out_drop;
81
82 skb = skb_unshare(skb, GFP_ATOMIC);
83 if (skb == NULL)
84 goto out;
85
86 if (unlikely(!pskb_may_pull(skb, DSA_HLEN)))
87 goto out_drop;
88
89 /*
90 * The ethertype field is part of the DSA header.
91 */
92 dsa_header = skb->data - 2;
93
94 /*
95 * Check that frame type is either TO_CPU or FORWARD.
96 */
97 if ((dsa_header[0] & 0xc0) != 0x00 && (dsa_header[0] & 0xc0) != 0xc0)
98 goto out_drop;
99
100 /*
101 * Determine source device and port.
102 */
103 source_device = dsa_header[0] & 0x1f;
104 source_port = (dsa_header[1] >> 3) & 0x1f;
105
106 /*
107 * Check that the source device exists and that the source
108 * port is a registered DSA port.
109 */
110 if (source_device >= DSA_MAX_SWITCHES)
111 goto out_drop;
112
113 ds = dst->ds[source_device];
114 if (!ds)
115 goto out_drop;
116
117 if (source_port >= DSA_MAX_PORTS || !ds->ports[source_port].netdev)
118 goto out_drop;
119
120 /*
121 * Convert the DSA header to an 802.1q header if the 'tagged'
122 * bit in the DSA header is set. If the 'tagged' bit is clear,
123 * delete the DSA header entirely.
124 */
125 if (dsa_header[0] & 0x20) {
126 u8 new_header[4];
127
128 /*
129 * Insert 802.1q ethertype and copy the VLAN-related
130 * fields, but clear the bit that will hold CFI (since
131 * DSA uses that bit location for another purpose).
132 */
133 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
134 new_header[1] = ETH_P_8021Q & 0xff;
135 new_header[2] = dsa_header[2] & ~0x10;
136 new_header[3] = dsa_header[3];
137
138 /*
139 * Move CFI bit from its place in the DSA header to
140 * its 802.1q-designated place.
141 */
142 if (dsa_header[1] & 0x01)
143 new_header[2] |= 0x10;
144
145 /*
146 * Update packet checksum if skb is CHECKSUM_COMPLETE.
147 */
148 if (skb->ip_summed == CHECKSUM_COMPLETE) {
149 __wsum c = skb->csum;
150 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
151 c = csum_sub(c, csum_partial(dsa_header + 2, 2, 0));
152 skb->csum = c;
153 }
154
155 memcpy(dsa_header, new_header, DSA_HLEN);
156 } else {
157 /*
158 * Remove DSA tag and update checksum.
159 */
160 skb_pull_rcsum(skb, DSA_HLEN);
161 memmove(skb->data - ETH_HLEN,
162 skb->data - ETH_HLEN - DSA_HLEN,
163 2 * ETH_ALEN);
164 }
165
166 skb->dev = ds->ports[source_port].netdev;
167 skb_push(skb, ETH_HLEN);
168 skb->pkt_type = PACKET_HOST;
169 skb->protocol = eth_type_trans(skb, skb->dev);
170
171 skb->dev->stats.rx_packets++;
172 skb->dev->stats.rx_bytes += skb->len;
173
174 netif_receive_skb(skb);
175
176 return 0;
177
178out_drop:
179 kfree_skb(skb);
180out:
181 return 0;
182}
183
184const struct dsa_device_ops dsa_netdev_ops = {
185 .xmit = dsa_xmit,
186 .rcv = dsa_rcv,
187};