1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * net/dsa/tag_edsa.c - Ethertype DSA tagging 4 * Copyright (c) 2008-2009 Marvell Semiconductor 5 */ 6 7 #include <linux/etherdevice.h> 8 #include <linux/list.h> 9 #include <linux/slab.h> 10 11 #include "dsa_priv.h" 12 13 #define DSA_HLEN 4 14 #define EDSA_HLEN 8 15 16 static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev) 17 { 18 struct dsa_port *dp = dsa_slave_to_port(dev); 19 u8 *edsa_header; 20 21 /* 22 * Convert the outermost 802.1q tag to a DSA tag and prepend 23 * a DSA ethertype field is the packet is tagged, or insert 24 * a DSA ethertype plus DSA tag between the addresses and the 25 * current ethertype field if the packet is untagged. 26 */ 27 if (skb->protocol == htons(ETH_P_8021Q)) { 28 if (skb_cow_head(skb, DSA_HLEN) < 0) 29 return NULL; 30 skb_push(skb, DSA_HLEN); 31 32 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN); 33 34 /* 35 * Construct tagged FROM_CPU DSA tag from 802.1q tag. 36 */ 37 edsa_header = skb->data + 2 * ETH_ALEN; 38 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff; 39 edsa_header[1] = ETH_P_EDSA & 0xff; 40 edsa_header[2] = 0x00; 41 edsa_header[3] = 0x00; 42 edsa_header[4] = 0x60 | dp->ds->index; 43 edsa_header[5] = dp->index << 3; 44 45 /* 46 * Move CFI field from byte 6 to byte 5. 47 */ 48 if (edsa_header[6] & 0x10) { 49 edsa_header[5] |= 0x01; 50 edsa_header[6] &= ~0x10; 51 } 52 } else { 53 if (skb_cow_head(skb, EDSA_HLEN) < 0) 54 return NULL; 55 skb_push(skb, EDSA_HLEN); 56 57 memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN); 58 59 /* 60 * Construct untagged FROM_CPU DSA tag. 61 */ 62 edsa_header = skb->data + 2 * ETH_ALEN; 63 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff; 64 edsa_header[1] = ETH_P_EDSA & 0xff; 65 edsa_header[2] = 0x00; 66 edsa_header[3] = 0x00; 67 edsa_header[4] = 0x40 | dp->ds->index; 68 edsa_header[5] = dp->index << 3; 69 edsa_header[6] = 0x00; 70 edsa_header[7] = 0x00; 71 } 72 73 return skb; 74 } 75 76 static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev, 77 struct packet_type *pt) 78 { 79 u8 *edsa_header; 80 int source_device; 81 int source_port; 82 83 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN))) 84 return NULL; 85 86 /* 87 * Skip the two null bytes after the ethertype. 88 */ 89 edsa_header = skb->data + 2; 90 91 /* 92 * Check that frame type is either TO_CPU or FORWARD. 93 */ 94 if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0) 95 return NULL; 96 97 /* 98 * Determine source device and port. 99 */ 100 source_device = edsa_header[0] & 0x1f; 101 source_port = (edsa_header[1] >> 3) & 0x1f; 102 103 skb->dev = dsa_master_find_slave(dev, source_device, source_port); 104 if (!skb->dev) 105 return NULL; 106 107 /* 108 * If the 'tagged' bit is set, convert the DSA tag to a 802.1q 109 * tag and delete the ethertype part. If the 'tagged' bit is 110 * clear, delete the ethertype and the DSA tag parts. 111 */ 112 if (edsa_header[0] & 0x20) { 113 u8 new_header[4]; 114 115 /* 116 * Insert 802.1q ethertype and copy the VLAN-related 117 * fields, but clear the bit that will hold CFI (since 118 * DSA uses that bit location for another purpose). 119 */ 120 new_header[0] = (ETH_P_8021Q >> 8) & 0xff; 121 new_header[1] = ETH_P_8021Q & 0xff; 122 new_header[2] = edsa_header[2] & ~0x10; 123 new_header[3] = edsa_header[3]; 124 125 /* 126 * Move CFI bit from its place in the DSA header to 127 * its 802.1q-designated place. 128 */ 129 if (edsa_header[1] & 0x01) 130 new_header[2] |= 0x10; 131 132 skb_pull_rcsum(skb, DSA_HLEN); 133 134 /* 135 * Update packet checksum if skb is CHECKSUM_COMPLETE. 136 */ 137 if (skb->ip_summed == CHECKSUM_COMPLETE) { 138 __wsum c = skb->csum; 139 c = csum_add(c, csum_partial(new_header + 2, 2, 0)); 140 c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0)); 141 skb->csum = c; 142 } 143 144 memcpy(edsa_header, new_header, DSA_HLEN); 145 146 memmove(skb->data - ETH_HLEN, 147 skb->data - ETH_HLEN - DSA_HLEN, 148 2 * ETH_ALEN); 149 } else { 150 /* 151 * Remove DSA tag and update checksum. 152 */ 153 skb_pull_rcsum(skb, EDSA_HLEN); 154 memmove(skb->data - ETH_HLEN, 155 skb->data - ETH_HLEN - EDSA_HLEN, 156 2 * ETH_ALEN); 157 } 158 159 skb->offload_fwd_mark = 1; 160 161 return skb; 162 } 163 164 static int edsa_tag_flow_dissect(const struct sk_buff *skb, __be16 *proto, 165 int *offset) 166 { 167 *offset = 8; 168 *proto = ((__be16 *)skb->data)[3]; 169 return 0; 170 } 171 172 static const struct dsa_device_ops edsa_netdev_ops = { 173 .name = "edsa", 174 .proto = DSA_TAG_PROTO_EDSA, 175 .xmit = edsa_xmit, 176 .rcv = edsa_rcv, 177 .flow_dissect = edsa_tag_flow_dissect, 178 .overhead = EDSA_HLEN, 179 }; 180 181 MODULE_LICENSE("GPL"); 182 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_EDSA); 183 184 module_dsa_tag_driver(edsa_netdev_ops); 185
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.