1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * net/dsa/tag_edsa.c - Ethertype DSA tagging
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 */
6
7 #include <linux/etherdevice.h>
8 #include <linux/list.h>
9 #include <linux/slab.h>
10
11 #include "dsa_priv.h"
12
13 #define DSA_HLEN 4
14 #define EDSA_HLEN 8
15
16 #define FRAME_TYPE_TO_CPU 0x00
17 #define FRAME_TYPE_FORWARD 0x03
18
19 #define TO_CPU_CODE_MGMT_TRAP 0x00
20 #define TO_CPU_CODE_FRAME2REG 0x01
21 #define TO_CPU_CODE_IGMP_MLD_TRAP 0x02
22 #define TO_CPU_CODE_POLICY_TRAP 0x03
23 #define TO_CPU_CODE_ARP_MIRROR 0x04
24 #define TO_CPU_CODE_POLICY_MIRROR 0x05
25
edsa_xmit(struct sk_buff * skb,struct net_device * dev)26 static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
27 {
28 struct dsa_port *dp = dsa_slave_to_port(dev);
29 u8 *edsa_header;
30
31 /*
32 * Convert the outermost 802.1q tag to a DSA tag and prepend
33 * a DSA ethertype field is the packet is tagged, or insert
34 * a DSA ethertype plus DSA tag between the addresses and the
35 * current ethertype field if the packet is untagged.
36 */
37 if (skb->protocol == htons(ETH_P_8021Q)) {
38 skb_push(skb, DSA_HLEN);
39
40 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
41
42 /*
43 * Construct tagged FROM_CPU DSA tag from 802.1q tag.
44 */
45 edsa_header = skb->data + 2 * ETH_ALEN;
46 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
47 edsa_header[1] = ETH_P_EDSA & 0xff;
48 edsa_header[2] = 0x00;
49 edsa_header[3] = 0x00;
50 edsa_header[4] = 0x60 | dp->ds->index;
51 edsa_header[5] = dp->index << 3;
52
53 /*
54 * Move CFI field from byte 6 to byte 5.
55 */
56 if (edsa_header[6] & 0x10) {
57 edsa_header[5] |= 0x01;
58 edsa_header[6] &= ~0x10;
59 }
60 } else {
61 skb_push(skb, EDSA_HLEN);
62
63 memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
64
65 /*
66 * Construct untagged FROM_CPU DSA tag.
67 */
68 edsa_header = skb->data + 2 * ETH_ALEN;
69 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
70 edsa_header[1] = ETH_P_EDSA & 0xff;
71 edsa_header[2] = 0x00;
72 edsa_header[3] = 0x00;
73 edsa_header[4] = 0x40 | dp->ds->index;
74 edsa_header[5] = dp->index << 3;
75 edsa_header[6] = 0x00;
76 edsa_header[7] = 0x00;
77 }
78
79 return skb;
80 }
81
edsa_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt)82 static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev,
83 struct packet_type *pt)
84 {
85 u8 *edsa_header;
86 int frame_type;
87 int code;
88 int source_device;
89 int source_port;
90
91 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
92 return NULL;
93
94 /*
95 * Skip the two null bytes after the ethertype.
96 */
97 edsa_header = skb->data + 2;
98
99 /*
100 * Check that frame type is either TO_CPU or FORWARD.
101 */
102 frame_type = edsa_header[0] >> 6;
103
104 switch (frame_type) {
105 case FRAME_TYPE_TO_CPU:
106 code = (edsa_header[1] & 0x6) | ((edsa_header[2] >> 4) & 1);
107
108 /*
109 * Mark the frame to never egress on any port of the same switch
110 * unless it's a trapped IGMP/MLD packet, in which case the
111 * bridge might want to forward it.
112 */
113 if (code != TO_CPU_CODE_IGMP_MLD_TRAP)
114 skb->offload_fwd_mark = 1;
115
116 break;
117
118 case FRAME_TYPE_FORWARD:
119 skb->offload_fwd_mark = 1;
120 break;
121
122 default:
123 return NULL;
124 }
125
126 /*
127 * Determine source device and port.
128 */
129 source_device = edsa_header[0] & 0x1f;
130 source_port = (edsa_header[1] >> 3) & 0x1f;
131
132 skb->dev = dsa_master_find_slave(dev, source_device, source_port);
133 if (!skb->dev)
134 return NULL;
135
136 /*
137 * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
138 * tag and delete the ethertype part. If the 'tagged' bit is
139 * clear, delete the ethertype and the DSA tag parts.
140 */
141 if (edsa_header[0] & 0x20) {
142 u8 new_header[4];
143
144 /*
145 * Insert 802.1q ethertype and copy the VLAN-related
146 * fields, but clear the bit that will hold CFI (since
147 * DSA uses that bit location for another purpose).
148 */
149 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
150 new_header[1] = ETH_P_8021Q & 0xff;
151 new_header[2] = edsa_header[2] & ~0x10;
152 new_header[3] = edsa_header[3];
153
154 /*
155 * Move CFI bit from its place in the DSA header to
156 * its 802.1q-designated place.
157 */
158 if (edsa_header[1] & 0x01)
159 new_header[2] |= 0x10;
160
161 skb_pull_rcsum(skb, DSA_HLEN);
162
163 /*
164 * Update packet checksum if skb is CHECKSUM_COMPLETE.
165 */
166 if (skb->ip_summed == CHECKSUM_COMPLETE) {
167 __wsum c = skb->csum;
168 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
169 c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0));
170 skb->csum = c;
171 }
172
173 memcpy(edsa_header, new_header, DSA_HLEN);
174
175 memmove(skb->data - ETH_HLEN,
176 skb->data - ETH_HLEN - DSA_HLEN,
177 2 * ETH_ALEN);
178 } else {
179 /*
180 * Remove DSA tag and update checksum.
181 */
182 skb_pull_rcsum(skb, EDSA_HLEN);
183 memmove(skb->data - ETH_HLEN,
184 skb->data - ETH_HLEN - EDSA_HLEN,
185 2 * ETH_ALEN);
186 }
187
188 return skb;
189 }
190
191 static const struct dsa_device_ops edsa_netdev_ops = {
192 .name = "edsa",
193 .proto = DSA_TAG_PROTO_EDSA,
194 .xmit = edsa_xmit,
195 .rcv = edsa_rcv,
196 .overhead = EDSA_HLEN,
197 };
198
199 MODULE_LICENSE("GPL");
200 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_EDSA);
201
202 module_dsa_tag_driver(edsa_netdev_ops);
203