blob: 2288c8098c42800c6477068c0334d7e1874ba608 [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>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include "dsa_priv.h"
15
16#define DSA_HLEN 4
17#define EDSA_HLEN 8
18
Florian Fainelli4ed70ce2015-07-31 11:42:56 -070019static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000020{
21 struct dsa_slave_priv *p = netdev_priv(dev);
22 u8 *edsa_header;
23
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000024 /*
25 * Convert the outermost 802.1q tag to a DSA tag and prepend
26 * a DSA ethertype field is the packet is tagged, or insert
27 * a DSA ethertype plus DSA tag between the addresses and the
28 * current ethertype field if the packet is untagged.
29 */
30 if (skb->protocol == htons(ETH_P_8021Q)) {
31 if (skb_cow_head(skb, DSA_HLEN) < 0)
32 goto out_free;
33 skb_push(skb, DSA_HLEN);
34
35 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
36
37 /*
38 * Construct tagged FROM_CPU DSA tag from 802.1q tag.
39 */
40 edsa_header = skb->data + 2 * ETH_ALEN;
41 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
42 edsa_header[1] = ETH_P_EDSA & 0xff;
43 edsa_header[2] = 0x00;
44 edsa_header[3] = 0x00;
Lennert Buytenheke84665c2009-03-20 09:52:09 +000045 edsa_header[4] = 0x60 | p->parent->index;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000046 edsa_header[5] = p->port << 3;
47
48 /*
49 * Move CFI field from byte 6 to byte 5.
50 */
51 if (edsa_header[6] & 0x10) {
52 edsa_header[5] |= 0x01;
53 edsa_header[6] &= ~0x10;
54 }
55 } else {
56 if (skb_cow_head(skb, EDSA_HLEN) < 0)
57 goto out_free;
58 skb_push(skb, EDSA_HLEN);
59
60 memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
61
62 /*
63 * Construct untagged FROM_CPU DSA tag.
64 */
65 edsa_header = skb->data + 2 * ETH_ALEN;
66 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
67 edsa_header[1] = ETH_P_EDSA & 0xff;
68 edsa_header[2] = 0x00;
69 edsa_header[3] = 0x00;
Lennert Buytenheke84665c2009-03-20 09:52:09 +000070 edsa_header[4] = 0x40 | p->parent->index;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071 edsa_header[5] = p->port << 3;
72 edsa_header[6] = 0x00;
73 edsa_header[7] = 0x00;
74 }
75
Florian Fainelli4ed70ce2015-07-31 11:42:56 -070076 return skb;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000077
78out_free:
79 kfree_skb(skb);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -070080 return NULL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000081}
82
83static int edsa_rcv(struct sk_buff *skb, struct net_device *dev,
84 struct packet_type *pt, struct net_device *orig_dev)
85{
Lennert Buytenheke84665c2009-03-20 09:52:09 +000086 struct dsa_switch_tree *dst = dev->dsa_ptr;
87 struct dsa_switch *ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000088 u8 *edsa_header;
Lennert Buytenheke84665c2009-03-20 09:52:09 +000089 int source_device;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000090 int source_port;
91
Lennert Buytenheke84665c2009-03-20 09:52:09 +000092 if (unlikely(dst == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000093 goto out_drop;
94
95 skb = skb_unshare(skb, GFP_ATOMIC);
96 if (skb == NULL)
97 goto out;
98
99 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
100 goto out_drop;
101
102 /*
103 * Skip the two null bytes after the ethertype.
104 */
105 edsa_header = skb->data + 2;
106
107 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000108 * Check that frame type is either TO_CPU or FORWARD.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000109 */
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000110 if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000111 goto out_drop;
112
113 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000114 * Determine source device and port.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000115 */
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000116 source_device = edsa_header[0] & 0x1f;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000117 source_port = (edsa_header[1] >> 3) & 0x1f;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000118
119 /*
120 * Check that the source device exists and that the source
121 * port is a registered DSA port.
122 */
123 if (source_device >= dst->pd->nr_chips)
124 goto out_drop;
125 ds = dst->ds[source_device];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000126 if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL)
127 goto out_drop;
128
129 /*
130 * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
131 * tag and delete the ethertype part. If the 'tagged' bit is
132 * clear, delete the ethertype and the DSA tag parts.
133 */
134 if (edsa_header[0] & 0x20) {
135 u8 new_header[4];
136
137 /*
138 * Insert 802.1q ethertype and copy the VLAN-related
139 * fields, but clear the bit that will hold CFI (since
140 * DSA uses that bit location for another purpose).
141 */
142 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
143 new_header[1] = ETH_P_8021Q & 0xff;
144 new_header[2] = edsa_header[2] & ~0x10;
145 new_header[3] = edsa_header[3];
146
147 /*
148 * Move CFI bit from its place in the DSA header to
149 * its 802.1q-designated place.
150 */
151 if (edsa_header[1] & 0x01)
152 new_header[2] |= 0x10;
153
154 skb_pull_rcsum(skb, DSA_HLEN);
155
156 /*
157 * Update packet checksum if skb is CHECKSUM_COMPLETE.
158 */
159 if (skb->ip_summed == CHECKSUM_COMPLETE) {
160 __wsum c = skb->csum;
161 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
162 c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0));
163 skb->csum = c;
164 }
165
166 memcpy(edsa_header, new_header, DSA_HLEN);
167
168 memmove(skb->data - ETH_HLEN,
169 skb->data - ETH_HLEN - DSA_HLEN,
170 2 * ETH_ALEN);
171 } else {
172 /*
173 * Remove DSA tag and update checksum.
174 */
175 skb_pull_rcsum(skb, EDSA_HLEN);
176 memmove(skb->data - ETH_HLEN,
177 skb->data - ETH_HLEN - EDSA_HLEN,
178 2 * ETH_ALEN);
179 }
180
181 skb->dev = ds->ports[source_port];
182 skb_push(skb, ETH_HLEN);
Lennert Buytenhek14ee6742008-11-10 21:52:42 -0800183 skb->pkt_type = PACKET_HOST;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000184 skb->protocol = eth_type_trans(skb, skb->dev);
185
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000186 skb->dev->stats.rx_packets++;
187 skb->dev->stats.rx_bytes += skb->len;
188
189 netif_receive_skb(skb);
190
191 return 0;
192
193out_drop:
194 kfree_skb(skb);
195out:
196 return 0;
197}
198
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700199const struct dsa_device_ops edsa_netdev_ops = {
200 .xmit = edsa_xmit,
201 .rcv = edsa_rcv,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000202};