Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux/fpc-iii.git] / net / dsa / tag_edsa.c
blob6c1720e88537af73a93a744c1132db4c5f2b1873
1 /*
2 * net/dsa/tag_edsa.c - Ethertype DSA tagging
3 * Copyright (c) 2008-2009 Marvell Semiconductor
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 */
11 #include <linux/etherdevice.h>
12 #include <linux/list.h>
13 #include <linux/slab.h>
14 #include "dsa_priv.h"
16 #define DSA_HLEN 4
17 #define EDSA_HLEN 8
19 static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
21 struct dsa_slave_priv *p = netdev_priv(dev);
22 u8 *edsa_header;
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.
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);
35 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
38 * Construct tagged FROM_CPU DSA tag from 802.1q tag.
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;
45 edsa_header[4] = 0x60 | p->parent->index;
46 edsa_header[5] = p->port << 3;
49 * Move CFI field from byte 6 to byte 5.
51 if (edsa_header[6] & 0x10) {
52 edsa_header[5] |= 0x01;
53 edsa_header[6] &= ~0x10;
55 } else {
56 if (skb_cow_head(skb, EDSA_HLEN) < 0)
57 goto out_free;
58 skb_push(skb, EDSA_HLEN);
60 memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
63 * Construct untagged FROM_CPU DSA tag.
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;
70 edsa_header[4] = 0x40 | p->parent->index;
71 edsa_header[5] = p->port << 3;
72 edsa_header[6] = 0x00;
73 edsa_header[7] = 0x00;
76 return skb;
78 out_free:
79 kfree_skb(skb);
80 return NULL;
83 static int edsa_rcv(struct sk_buff *skb, struct net_device *dev,
84 struct packet_type *pt, struct net_device *orig_dev)
86 struct dsa_switch_tree *dst = dev->dsa_ptr;
87 struct dsa_switch *ds;
88 u8 *edsa_header;
89 int source_device;
90 int source_port;
92 if (unlikely(dst == NULL))
93 goto out_drop;
95 skb = skb_unshare(skb, GFP_ATOMIC);
96 if (skb == NULL)
97 goto out;
99 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
100 goto out_drop;
103 * Skip the two null bytes after the ethertype.
105 edsa_header = skb->data + 2;
108 * Check that frame type is either TO_CPU or FORWARD.
110 if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0)
111 goto out_drop;
114 * Determine source device and port.
116 source_device = edsa_header[0] & 0x1f;
117 source_port = (edsa_header[1] >> 3) & 0x1f;
120 * Check that the source device exists and that the source
121 * port is a registered DSA port.
123 if (source_device >= DSA_MAX_SWITCHES)
124 goto out_drop;
126 ds = dst->ds[source_device];
127 if (!ds)
128 goto out_drop;
130 if (source_port >= DSA_MAX_PORTS || !ds->ports[source_port].netdev)
131 goto out_drop;
134 * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
135 * tag and delete the ethertype part. If the 'tagged' bit is
136 * clear, delete the ethertype and the DSA tag parts.
138 if (edsa_header[0] & 0x20) {
139 u8 new_header[4];
142 * Insert 802.1q ethertype and copy the VLAN-related
143 * fields, but clear the bit that will hold CFI (since
144 * DSA uses that bit location for another purpose).
146 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
147 new_header[1] = ETH_P_8021Q & 0xff;
148 new_header[2] = edsa_header[2] & ~0x10;
149 new_header[3] = edsa_header[3];
152 * Move CFI bit from its place in the DSA header to
153 * its 802.1q-designated place.
155 if (edsa_header[1] & 0x01)
156 new_header[2] |= 0x10;
158 skb_pull_rcsum(skb, DSA_HLEN);
161 * Update packet checksum if skb is CHECKSUM_COMPLETE.
163 if (skb->ip_summed == CHECKSUM_COMPLETE) {
164 __wsum c = skb->csum;
165 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
166 c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0));
167 skb->csum = c;
170 memcpy(edsa_header, new_header, DSA_HLEN);
172 memmove(skb->data - ETH_HLEN,
173 skb->data - ETH_HLEN - DSA_HLEN,
174 2 * ETH_ALEN);
175 } else {
177 * Remove DSA tag and update checksum.
179 skb_pull_rcsum(skb, EDSA_HLEN);
180 memmove(skb->data - ETH_HLEN,
181 skb->data - ETH_HLEN - EDSA_HLEN,
182 2 * ETH_ALEN);
185 skb->dev = ds->ports[source_port].netdev;
186 skb_push(skb, ETH_HLEN);
187 skb->pkt_type = PACKET_HOST;
188 skb->protocol = eth_type_trans(skb, skb->dev);
190 skb->dev->stats.rx_packets++;
191 skb->dev->stats.rx_bytes += skb->len;
193 netif_receive_skb(skb);
195 return 0;
197 out_drop:
198 kfree_skb(skb);
199 out:
200 return 0;
203 const struct dsa_device_ops edsa_netdev_ops = {
204 .xmit = edsa_xmit,
205 .rcv = edsa_rcv,