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