Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/tag_edsa.c - Ethertype DSA tagging |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 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/netdevice.h> |
| 14 | #include "dsa_priv.h" |
| 15 | |
| 16 | #define DSA_HLEN 4 |
| 17 | #define EDSA_HLEN 8 |
| 18 | |
Stephen Hemminger | 6fef4c0 | 2009-08-31 19:50:41 +0000 | [diff] [blame] | 19 | netdev_tx_t edsa_xmit(struct sk_buff *skb, struct net_device *dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 20 | { |
| 21 | struct dsa_slave_priv *p = netdev_priv(dev); |
| 22 | u8 *edsa_header; |
| 23 | |
| 24 | dev->stats.tx_packets++; |
| 25 | dev->stats.tx_bytes += skb->len; |
| 26 | |
| 27 | /* |
| 28 | * Convert the outermost 802.1q tag to a DSA tag and prepend |
| 29 | * a DSA ethertype field is the packet is tagged, or insert |
| 30 | * a DSA ethertype plus DSA tag between the addresses and the |
| 31 | * current ethertype field if the packet is untagged. |
| 32 | */ |
| 33 | if (skb->protocol == htons(ETH_P_8021Q)) { |
| 34 | if (skb_cow_head(skb, DSA_HLEN) < 0) |
| 35 | goto out_free; |
| 36 | skb_push(skb, DSA_HLEN); |
| 37 | |
| 38 | memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN); |
| 39 | |
| 40 | /* |
| 41 | * Construct tagged FROM_CPU DSA tag from 802.1q tag. |
| 42 | */ |
| 43 | edsa_header = skb->data + 2 * ETH_ALEN; |
| 44 | edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff; |
| 45 | edsa_header[1] = ETH_P_EDSA & 0xff; |
| 46 | edsa_header[2] = 0x00; |
| 47 | edsa_header[3] = 0x00; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 48 | edsa_header[4] = 0x60 | p->parent->index; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 49 | edsa_header[5] = p->port << 3; |
| 50 | |
| 51 | /* |
| 52 | * Move CFI field from byte 6 to byte 5. |
| 53 | */ |
| 54 | if (edsa_header[6] & 0x10) { |
| 55 | edsa_header[5] |= 0x01; |
| 56 | edsa_header[6] &= ~0x10; |
| 57 | } |
| 58 | } else { |
| 59 | if (skb_cow_head(skb, EDSA_HLEN) < 0) |
| 60 | goto out_free; |
| 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; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 73 | edsa_header[4] = 0x40 | p->parent->index; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 74 | edsa_header[5] = p->port << 3; |
| 75 | edsa_header[6] = 0x00; |
| 76 | edsa_header[7] = 0x00; |
| 77 | } |
| 78 | |
| 79 | skb->protocol = htons(ETH_P_EDSA); |
| 80 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 81 | skb->dev = p->parent->dst->master_netdev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 82 | dev_queue_xmit(skb); |
| 83 | |
| 84 | return NETDEV_TX_OK; |
| 85 | |
| 86 | out_free: |
| 87 | kfree_skb(skb); |
| 88 | return NETDEV_TX_OK; |
| 89 | } |
| 90 | |
| 91 | static int edsa_rcv(struct sk_buff *skb, struct net_device *dev, |
| 92 | struct packet_type *pt, struct net_device *orig_dev) |
| 93 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 94 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
| 95 | struct dsa_switch *ds; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 96 | u8 *edsa_header; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 97 | int source_device; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 98 | int source_port; |
| 99 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 100 | if (unlikely(dst == NULL)) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 101 | goto out_drop; |
| 102 | |
| 103 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 104 | if (skb == NULL) |
| 105 | goto out; |
| 106 | |
| 107 | if (unlikely(!pskb_may_pull(skb, EDSA_HLEN))) |
| 108 | goto out_drop; |
| 109 | |
| 110 | /* |
| 111 | * Skip the two null bytes after the ethertype. |
| 112 | */ |
| 113 | edsa_header = skb->data + 2; |
| 114 | |
| 115 | /* |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 116 | * Check that frame type is either TO_CPU or FORWARD. |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 117 | */ |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 118 | if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 119 | goto out_drop; |
| 120 | |
| 121 | /* |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 122 | * Determine source device and port. |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 123 | */ |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 124 | source_device = edsa_header[0] & 0x1f; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 125 | source_port = (edsa_header[1] >> 3) & 0x1f; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * Check that the source device exists and that the source |
| 129 | * port is a registered DSA port. |
| 130 | */ |
| 131 | if (source_device >= dst->pd->nr_chips) |
| 132 | goto out_drop; |
| 133 | ds = dst->ds[source_device]; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 134 | if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL) |
| 135 | goto out_drop; |
| 136 | |
| 137 | /* |
| 138 | * If the 'tagged' bit is set, convert the DSA tag to a 802.1q |
| 139 | * tag and delete the ethertype part. If the 'tagged' bit is |
| 140 | * clear, delete the ethertype and the DSA tag parts. |
| 141 | */ |
| 142 | if (edsa_header[0] & 0x20) { |
| 143 | u8 new_header[4]; |
| 144 | |
| 145 | /* |
| 146 | * Insert 802.1q ethertype and copy the VLAN-related |
| 147 | * fields, but clear the bit that will hold CFI (since |
| 148 | * DSA uses that bit location for another purpose). |
| 149 | */ |
| 150 | new_header[0] = (ETH_P_8021Q >> 8) & 0xff; |
| 151 | new_header[1] = ETH_P_8021Q & 0xff; |
| 152 | new_header[2] = edsa_header[2] & ~0x10; |
| 153 | new_header[3] = edsa_header[3]; |
| 154 | |
| 155 | /* |
| 156 | * Move CFI bit from its place in the DSA header to |
| 157 | * its 802.1q-designated place. |
| 158 | */ |
| 159 | if (edsa_header[1] & 0x01) |
| 160 | new_header[2] |= 0x10; |
| 161 | |
| 162 | skb_pull_rcsum(skb, DSA_HLEN); |
| 163 | |
| 164 | /* |
| 165 | * Update packet checksum if skb is CHECKSUM_COMPLETE. |
| 166 | */ |
| 167 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 168 | __wsum c = skb->csum; |
| 169 | c = csum_add(c, csum_partial(new_header + 2, 2, 0)); |
| 170 | c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0)); |
| 171 | skb->csum = c; |
| 172 | } |
| 173 | |
| 174 | memcpy(edsa_header, new_header, DSA_HLEN); |
| 175 | |
| 176 | memmove(skb->data - ETH_HLEN, |
| 177 | skb->data - ETH_HLEN - DSA_HLEN, |
| 178 | 2 * ETH_ALEN); |
| 179 | } else { |
| 180 | /* |
| 181 | * Remove DSA tag and update checksum. |
| 182 | */ |
| 183 | skb_pull_rcsum(skb, EDSA_HLEN); |
| 184 | memmove(skb->data - ETH_HLEN, |
| 185 | skb->data - ETH_HLEN - EDSA_HLEN, |
| 186 | 2 * ETH_ALEN); |
| 187 | } |
| 188 | |
| 189 | skb->dev = ds->ports[source_port]; |
| 190 | skb_push(skb, ETH_HLEN); |
Lennert Buytenhek | 14ee674 | 2008-11-10 21:52:42 -0800 | [diff] [blame] | 191 | skb->pkt_type = PACKET_HOST; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 192 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 193 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 194 | skb->dev->stats.rx_packets++; |
| 195 | skb->dev->stats.rx_bytes += skb->len; |
| 196 | |
| 197 | netif_receive_skb(skb); |
| 198 | |
| 199 | return 0; |
| 200 | |
| 201 | out_drop: |
| 202 | kfree_skb(skb); |
| 203 | out: |
| 204 | return 0; |
| 205 | } |
| 206 | |
Stephen Hemminger | 7546dd9 | 2009-03-09 08:18:29 +0000 | [diff] [blame] | 207 | static struct packet_type edsa_packet_type __read_mostly = { |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 208 | .type = cpu_to_be16(ETH_P_EDSA), |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 209 | .func = edsa_rcv, |
| 210 | }; |
| 211 | |
| 212 | static int __init edsa_init_module(void) |
| 213 | { |
| 214 | dev_add_pack(&edsa_packet_type); |
| 215 | return 0; |
| 216 | } |
| 217 | module_init(edsa_init_module); |
| 218 | |
| 219 | static void __exit edsa_cleanup_module(void) |
| 220 | { |
| 221 | dev_remove_pack(&edsa_packet_type); |
| 222 | } |
| 223 | module_exit(edsa_cleanup_module); |