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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 14 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 15 | #include "dsa_priv.h" |
| 16 | |
| 17 | #define DSA_HLEN 4 |
| 18 | #define EDSA_HLEN 8 |
| 19 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 20 | static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 21 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 22 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 23 | u8 *edsa_header; |
| 24 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 25 | /* |
| 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) |
Vivien Didelot | fe47d56 | 2017-06-01 16:07:15 -0400 | [diff] [blame] | 33 | return NULL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 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 Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 46 | edsa_header[4] = 0x60 | dp->ds->index; |
| 47 | edsa_header[5] = dp->index << 3; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 48 | |
| 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) |
Vivien Didelot | fe47d56 | 2017-06-01 16:07:15 -0400 | [diff] [blame] | 58 | return NULL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 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 Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 71 | edsa_header[4] = 0x40 | dp->ds->index; |
| 72 | edsa_header[5] = dp->index << 3; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 73 | edsa_header[6] = 0x00; |
| 74 | edsa_header[7] = 0x00; |
| 75 | } |
| 76 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 77 | return skb; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Florian Fainelli | a86d8be | 2017-04-08 08:55:23 -0700 | [diff] [blame] | 80 | static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev, |
Florian Westphal | 89e4950 | 2017-08-17 16:47:00 +0200 | [diff] [blame] | 81 | struct packet_type *pt) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 82 | { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 83 | u8 *edsa_header; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 84 | int source_device; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 85 | int source_port; |
| 86 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 87 | if (unlikely(!pskb_may_pull(skb, EDSA_HLEN))) |
Vivien Didelot | 5470979 | 2017-06-01 16:07:14 -0400 | [diff] [blame] | 88 | return NULL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Skip the two null bytes after the ethertype. |
| 92 | */ |
| 93 | edsa_header = skb->data + 2; |
| 94 | |
| 95 | /* |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 96 | * Check that frame type is either TO_CPU or FORWARD. |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 97 | */ |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 98 | if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0) |
Vivien Didelot | 5470979 | 2017-06-01 16:07:14 -0400 | [diff] [blame] | 99 | return NULL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 100 | |
| 101 | /* |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 102 | * Determine source device and port. |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 103 | */ |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 104 | source_device = edsa_header[0] & 0x1f; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 105 | source_port = (edsa_header[1] >> 3) & 0x1f; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 106 | |
Vivien Didelot | 2231c43 | 2017-10-16 11:12:17 -0400 | [diff] [blame] | 107 | skb->dev = dsa_master_find_slave(dev, source_device, source_port); |
Vivien Didelot | 3775b1b | 2017-09-29 17:19:15 -0400 | [diff] [blame] | 108 | if (!skb->dev) |
Vivien Didelot | 5470979 | 2017-06-01 16:07:14 -0400 | [diff] [blame] | 109 | return NULL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * If the 'tagged' bit is set, convert the DSA tag to a 802.1q |
| 113 | * tag and delete the ethertype part. If the 'tagged' bit is |
| 114 | * clear, delete the ethertype and the DSA tag parts. |
| 115 | */ |
| 116 | if (edsa_header[0] & 0x20) { |
| 117 | u8 new_header[4]; |
| 118 | |
| 119 | /* |
| 120 | * Insert 802.1q ethertype and copy the VLAN-related |
| 121 | * fields, but clear the bit that will hold CFI (since |
| 122 | * DSA uses that bit location for another purpose). |
| 123 | */ |
| 124 | new_header[0] = (ETH_P_8021Q >> 8) & 0xff; |
| 125 | new_header[1] = ETH_P_8021Q & 0xff; |
| 126 | new_header[2] = edsa_header[2] & ~0x10; |
| 127 | new_header[3] = edsa_header[3]; |
| 128 | |
| 129 | /* |
| 130 | * Move CFI bit from its place in the DSA header to |
| 131 | * its 802.1q-designated place. |
| 132 | */ |
| 133 | if (edsa_header[1] & 0x01) |
| 134 | new_header[2] |= 0x10; |
| 135 | |
| 136 | skb_pull_rcsum(skb, DSA_HLEN); |
| 137 | |
| 138 | /* |
| 139 | * Update packet checksum if skb is CHECKSUM_COMPLETE. |
| 140 | */ |
| 141 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 142 | __wsum c = skb->csum; |
| 143 | c = csum_add(c, csum_partial(new_header + 2, 2, 0)); |
| 144 | c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0)); |
| 145 | skb->csum = c; |
| 146 | } |
| 147 | |
| 148 | memcpy(edsa_header, new_header, DSA_HLEN); |
| 149 | |
| 150 | memmove(skb->data - ETH_HLEN, |
| 151 | skb->data - ETH_HLEN - DSA_HLEN, |
| 152 | 2 * ETH_ALEN); |
| 153 | } else { |
| 154 | /* |
| 155 | * Remove DSA tag and update checksum. |
| 156 | */ |
| 157 | skb_pull_rcsum(skb, EDSA_HLEN); |
| 158 | memmove(skb->data - ETH_HLEN, |
| 159 | skb->data - ETH_HLEN - EDSA_HLEN, |
| 160 | 2 * ETH_ALEN); |
| 161 | } |
| 162 | |
Andrew Lunn | 13edbdb | 2017-11-09 22:29:52 +0100 | [diff] [blame] | 163 | skb->offload_fwd_mark = 1; |
| 164 | |
Florian Fainelli | a86d8be | 2017-04-08 08:55:23 -0700 | [diff] [blame] | 165 | return skb; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Rundong Ge | 57fd967 | 2019-02-16 08:35:24 +0000 | [diff] [blame] | 168 | static int edsa_tag_flow_dissect(const struct sk_buff *skb, __be16 *proto, |
| 169 | int *offset) |
| 170 | { |
| 171 | *offset = 8; |
| 172 | *proto = ((__be16 *)skb->data)[3]; |
| 173 | return 0; |
| 174 | } |
| 175 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 176 | const struct dsa_device_ops edsa_netdev_ops = { |
| 177 | .xmit = edsa_xmit, |
| 178 | .rcv = edsa_rcv, |
Rundong Ge | 57fd967 | 2019-02-16 08:35:24 +0000 | [diff] [blame] | 179 | .flow_dissect = edsa_tag_flow_dissect, |
Andrew Lunn | a5dd308 | 2018-12-06 11:36:04 +0100 | [diff] [blame] | 180 | .overhead = EDSA_HLEN, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 181 | }; |