blob: d9c668aa5e54682914a0e61b3cb6373a71a0ee8b [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/tag_edsa.c - Ethertype DSA tagging
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020014#include <net/dsa.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000015#include "dsa_priv.h"
16
17#define DSA_HLEN 4
18#define EDSA_HLEN 8
19
Florian Fainelli4ed70ce2015-07-31 11:42:56 -070020static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000021{
22 struct dsa_slave_priv *p = netdev_priv(dev);
23 u8 *edsa_header;
24
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000025 /*
26 * Convert the outermost 802.1q tag to a DSA tag and prepend
27 * a DSA ethertype field is the packet is tagged, or insert
28 * a DSA ethertype plus DSA tag between the addresses and the
29 * current ethertype field if the packet is untagged.
30 */
31 if (skb->protocol == htons(ETH_P_8021Q)) {
32 if (skb_cow_head(skb, DSA_HLEN) < 0)
33 goto out_free;
34 skb_push(skb, DSA_HLEN);
35
36 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
37
38 /*
39 * Construct tagged FROM_CPU DSA tag from 802.1q tag.
40 */
41 edsa_header = skb->data + 2 * ETH_ALEN;
42 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
43 edsa_header[1] = ETH_P_EDSA & 0xff;
44 edsa_header[2] = 0x00;
45 edsa_header[3] = 0x00;
Vivien Didelotafdcf152017-01-27 15:29:39 -050046 edsa_header[4] = 0x60 | p->dp->ds->index;
47 edsa_header[5] = p->dp->index << 3;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000048
49 /*
50 * Move CFI field from byte 6 to byte 5.
51 */
52 if (edsa_header[6] & 0x10) {
53 edsa_header[5] |= 0x01;
54 edsa_header[6] &= ~0x10;
55 }
56 } else {
57 if (skb_cow_head(skb, EDSA_HLEN) < 0)
58 goto out_free;
59 skb_push(skb, EDSA_HLEN);
60
61 memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
62
63 /*
64 * Construct untagged FROM_CPU DSA tag.
65 */
66 edsa_header = skb->data + 2 * ETH_ALEN;
67 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
68 edsa_header[1] = ETH_P_EDSA & 0xff;
69 edsa_header[2] = 0x00;
70 edsa_header[3] = 0x00;
Vivien Didelotafdcf152017-01-27 15:29:39 -050071 edsa_header[4] = 0x40 | p->dp->ds->index;
72 edsa_header[5] = p->dp->index << 3;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000073 edsa_header[6] = 0x00;
74 edsa_header[7] = 0x00;
75 }
76
Florian Fainelli4ed70ce2015-07-31 11:42:56 -070077 return skb;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000078
79out_free:
80 kfree_skb(skb);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -070081 return NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000082}
83
Florian Fainellia86d8be2017-04-08 08:55:23 -070084static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev,
85 struct packet_type *pt,
86 struct net_device *orig_dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000087{
Lennert Buytenheke84665c2009-03-20 09:52:09 +000088 struct dsa_switch_tree *dst = dev->dsa_ptr;
89 struct dsa_switch *ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000090 u8 *edsa_header;
Lennert Buytenheke84665c2009-03-20 09:52:09 +000091 int source_device;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000092 int source_port;
93
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000094 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
95 goto out_drop;
96
97 /*
98 * Skip the two null bytes after the ethertype.
99 */
100 edsa_header = skb->data + 2;
101
102 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000103 * Check that frame type is either TO_CPU or FORWARD.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000104 */
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000105 if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000106 goto out_drop;
107
108 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000109 * Determine source device and port.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110 */
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000111 source_device = edsa_header[0] & 0x1f;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112 source_port = (edsa_header[1] >> 3) & 0x1f;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000113
114 /*
115 * Check that the source device exists and that the source
116 * port is a registered DSA port.
117 */
Andrew Lunn149cafd2016-06-04 21:16:56 +0200118 if (source_device >= DSA_MAX_SWITCHES)
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000119 goto out_drop;
Andrew Lunn149cafd2016-06-04 21:16:56 +0200120
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000121 ds = dst->ds[source_device];
Andrew Lunn149cafd2016-06-04 21:16:56 +0200122 if (!ds)
123 goto out_drop;
124
Vivien Didelot26895e22017-01-27 15:29:37 -0500125 if (source_port >= ds->num_ports || !ds->ports[source_port].netdev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000126 goto out_drop;
127
128 /*
129 * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
130 * tag and delete the ethertype part. If the 'tagged' bit is
131 * clear, delete the ethertype and the DSA tag parts.
132 */
133 if (edsa_header[0] & 0x20) {
134 u8 new_header[4];
135
136 /*
137 * Insert 802.1q ethertype and copy the VLAN-related
138 * fields, but clear the bit that will hold CFI (since
139 * DSA uses that bit location for another purpose).
140 */
141 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
142 new_header[1] = ETH_P_8021Q & 0xff;
143 new_header[2] = edsa_header[2] & ~0x10;
144 new_header[3] = edsa_header[3];
145
146 /*
147 * Move CFI bit from its place in the DSA header to
148 * its 802.1q-designated place.
149 */
150 if (edsa_header[1] & 0x01)
151 new_header[2] |= 0x10;
152
153 skb_pull_rcsum(skb, DSA_HLEN);
154
155 /*
156 * Update packet checksum if skb is CHECKSUM_COMPLETE.
157 */
158 if (skb->ip_summed == CHECKSUM_COMPLETE) {
159 __wsum c = skb->csum;
160 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
161 c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0));
162 skb->csum = c;
163 }
164
165 memcpy(edsa_header, new_header, DSA_HLEN);
166
167 memmove(skb->data - ETH_HLEN,
168 skb->data - ETH_HLEN - DSA_HLEN,
169 2 * ETH_ALEN);
170 } else {
171 /*
172 * Remove DSA tag and update checksum.
173 */
174 skb_pull_rcsum(skb, EDSA_HLEN);
175 memmove(skb->data - ETH_HLEN,
176 skb->data - ETH_HLEN - EDSA_HLEN,
177 2 * ETH_ALEN);
178 }
179
Andrew Lunnc8b09802016-06-04 21:16:57 +0200180 skb->dev = ds->ports[source_port].netdev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000181
Florian Fainellia86d8be2017-04-08 08:55:23 -0700182 return skb;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000183
184out_drop:
Florian Fainellia86d8be2017-04-08 08:55:23 -0700185 return NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000186}
187
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700188const struct dsa_device_ops edsa_netdev_ops = {
189 .xmit = edsa_xmit,
190 .rcv = edsa_rcv,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000191};