tag_edsa.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * net/dsa/tag_edsa.c - Ethertype DSA tagging
  3. * Copyright (c) 2008-2009 Marvell Semiconductor
  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. #include <linux/etherdevice.h>
  11. #include <linux/list.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/slab.h>
  14. #include "dsa_priv.h"
  15. #define DSA_HLEN 4
  16. #define EDSA_HLEN 8
  17. netdev_tx_t edsa_xmit(struct sk_buff *skb, struct net_device *dev)
  18. {
  19. struct dsa_slave_priv *p = netdev_priv(dev);
  20. u8 *edsa_header;
  21. dev->stats.tx_packets++;
  22. dev->stats.tx_bytes += skb->len;
  23. /*
  24. * Convert the outermost 802.1q tag to a DSA tag and prepend
  25. * a DSA ethertype field is the packet is tagged, or insert
  26. * a DSA ethertype plus DSA tag between the addresses and the
  27. * current ethertype field if the packet is untagged.
  28. */
  29. if (skb->protocol == htons(ETH_P_8021Q)) {
  30. if (skb_cow_head(skb, DSA_HLEN) < 0)
  31. goto out_free;
  32. skb_push(skb, DSA_HLEN);
  33. memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
  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 | p->parent->index;
  43. edsa_header[5] = p->port << 3;
  44. /*
  45. * Move CFI field from byte 6 to byte 5.
  46. */
  47. if (edsa_header[6] & 0x10) {
  48. edsa_header[5] |= 0x01;
  49. edsa_header[6] &= ~0x10;
  50. }
  51. } else {
  52. if (skb_cow_head(skb, EDSA_HLEN) < 0)
  53. goto out_free;
  54. skb_push(skb, EDSA_HLEN);
  55. memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
  56. /*
  57. * Construct untagged FROM_CPU DSA tag.
  58. */
  59. edsa_header = skb->data + 2 * ETH_ALEN;
  60. edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
  61. edsa_header[1] = ETH_P_EDSA & 0xff;
  62. edsa_header[2] = 0x00;
  63. edsa_header[3] = 0x00;
  64. edsa_header[4] = 0x40 | p->parent->index;
  65. edsa_header[5] = p->port << 3;
  66. edsa_header[6] = 0x00;
  67. edsa_header[7] = 0x00;
  68. }
  69. skb->protocol = htons(ETH_P_EDSA);
  70. skb->dev = p->parent->dst->master_netdev;
  71. dev_queue_xmit(skb);
  72. return NETDEV_TX_OK;
  73. out_free:
  74. kfree_skb(skb);
  75. return NETDEV_TX_OK;
  76. }
  77. static int edsa_rcv(struct sk_buff *skb, struct net_device *dev,
  78. struct packet_type *pt, struct net_device *orig_dev)
  79. {
  80. struct dsa_switch_tree *dst = dev->dsa_ptr;
  81. struct dsa_switch *ds;
  82. u8 *edsa_header;
  83. int source_device;
  84. int source_port;
  85. if (unlikely(dst == NULL))
  86. goto out_drop;
  87. skb = skb_unshare(skb, GFP_ATOMIC);
  88. if (skb == NULL)
  89. goto out;
  90. if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
  91. goto out_drop;
  92. /*
  93. * Skip the two null bytes after the ethertype.
  94. */
  95. edsa_header = skb->data + 2;
  96. /*
  97. * Check that frame type is either TO_CPU or FORWARD.
  98. */
  99. if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0)
  100. goto out_drop;
  101. /*
  102. * Determine source device and port.
  103. */
  104. source_device = edsa_header[0] & 0x1f;
  105. source_port = (edsa_header[1] >> 3) & 0x1f;
  106. /*
  107. * Check that the source device exists and that the source
  108. * port is a registered DSA port.
  109. */
  110. if (source_device >= dst->pd->nr_chips)
  111. goto out_drop;
  112. ds = dst->ds[source_device];
  113. if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL)
  114. goto out_drop;
  115. /*
  116. * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
  117. * tag and delete the ethertype part. If the 'tagged' bit is
  118. * clear, delete the ethertype and the DSA tag parts.
  119. */
  120. if (edsa_header[0] & 0x20) {
  121. u8 new_header[4];
  122. /*
  123. * Insert 802.1q ethertype and copy the VLAN-related
  124. * fields, but clear the bit that will hold CFI (since
  125. * DSA uses that bit location for another purpose).
  126. */
  127. new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
  128. new_header[1] = ETH_P_8021Q & 0xff;
  129. new_header[2] = edsa_header[2] & ~0x10;
  130. new_header[3] = edsa_header[3];
  131. /*
  132. * Move CFI bit from its place in the DSA header to
  133. * its 802.1q-designated place.
  134. */
  135. if (edsa_header[1] & 0x01)
  136. new_header[2] |= 0x10;
  137. skb_pull_rcsum(skb, DSA_HLEN);
  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. memcpy(edsa_header, new_header, DSA_HLEN);
  148. memmove(skb->data - ETH_HLEN,
  149. skb->data - ETH_HLEN - DSA_HLEN,
  150. 2 * ETH_ALEN);
  151. } else {
  152. /*
  153. * Remove DSA tag and update checksum.
  154. */
  155. skb_pull_rcsum(skb, EDSA_HLEN);
  156. memmove(skb->data - ETH_HLEN,
  157. skb->data - ETH_HLEN - EDSA_HLEN,
  158. 2 * ETH_ALEN);
  159. }
  160. skb->dev = ds->ports[source_port];
  161. skb_push(skb, ETH_HLEN);
  162. skb->pkt_type = PACKET_HOST;
  163. skb->protocol = eth_type_trans(skb, skb->dev);
  164. skb->dev->stats.rx_packets++;
  165. skb->dev->stats.rx_bytes += skb->len;
  166. netif_receive_skb(skb);
  167. return 0;
  168. out_drop:
  169. kfree_skb(skb);
  170. out:
  171. return 0;
  172. }
  173. static struct packet_type edsa_packet_type __read_mostly = {
  174. .type = cpu_to_be16(ETH_P_EDSA),
  175. .func = edsa_rcv,
  176. };
  177. static int __init edsa_init_module(void)
  178. {
  179. dev_add_pack(&edsa_packet_type);
  180. return 0;
  181. }
  182. module_init(edsa_init_module);
  183. static void __exit edsa_cleanup_module(void)
  184. {
  185. dev_remove_pack(&edsa_packet_type);
  186. }
  187. module_exit(edsa_cleanup_module);