tag_edsa.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/slab.h>
  13. #include "dsa_priv.h"
  14. #define DSA_HLEN 4
  15. #define EDSA_HLEN 8
  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. * Convert the outermost 802.1q tag to a DSA tag and prepend
  22. * a DSA ethertype field is the packet is tagged, or insert
  23. * a DSA ethertype plus DSA tag between the addresses and the
  24. * current ethertype field if the packet is untagged.
  25. */
  26. if (skb->protocol == htons(ETH_P_8021Q)) {
  27. if (skb_cow_head(skb, DSA_HLEN) < 0)
  28. return NULL;
  29. skb_push(skb, DSA_HLEN);
  30. memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
  31. /*
  32. * Construct tagged FROM_CPU DSA tag from 802.1q tag.
  33. */
  34. edsa_header = skb->data + 2 * ETH_ALEN;
  35. edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
  36. edsa_header[1] = ETH_P_EDSA & 0xff;
  37. edsa_header[2] = 0x00;
  38. edsa_header[3] = 0x00;
  39. edsa_header[4] = 0x60 | dp->ds->index;
  40. edsa_header[5] = dp->index << 3;
  41. /*
  42. * Move CFI field from byte 6 to byte 5.
  43. */
  44. if (edsa_header[6] & 0x10) {
  45. edsa_header[5] |= 0x01;
  46. edsa_header[6] &= ~0x10;
  47. }
  48. } else {
  49. if (skb_cow_head(skb, EDSA_HLEN) < 0)
  50. return NULL;
  51. skb_push(skb, EDSA_HLEN);
  52. memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
  53. /*
  54. * Construct untagged FROM_CPU DSA tag.
  55. */
  56. edsa_header = skb->data + 2 * ETH_ALEN;
  57. edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
  58. edsa_header[1] = ETH_P_EDSA & 0xff;
  59. edsa_header[2] = 0x00;
  60. edsa_header[3] = 0x00;
  61. edsa_header[4] = 0x40 | dp->ds->index;
  62. edsa_header[5] = dp->index << 3;
  63. edsa_header[6] = 0x00;
  64. edsa_header[7] = 0x00;
  65. }
  66. return skb;
  67. }
  68. static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev,
  69. struct packet_type *pt)
  70. {
  71. u8 *edsa_header;
  72. int source_device;
  73. int source_port;
  74. if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
  75. return NULL;
  76. /*
  77. * Skip the two null bytes after the ethertype.
  78. */
  79. edsa_header = skb->data + 2;
  80. /*
  81. * Check that frame type is either TO_CPU or FORWARD.
  82. */
  83. if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0)
  84. return NULL;
  85. /*
  86. * Determine source device and port.
  87. */
  88. source_device = edsa_header[0] & 0x1f;
  89. source_port = (edsa_header[1] >> 3) & 0x1f;
  90. skb->dev = dsa_master_find_slave(dev, source_device, source_port);
  91. if (!skb->dev)
  92. return NULL;
  93. /*
  94. * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
  95. * tag and delete the ethertype part. If the 'tagged' bit is
  96. * clear, delete the ethertype and the DSA tag parts.
  97. */
  98. if (edsa_header[0] & 0x20) {
  99. u8 new_header[4];
  100. /*
  101. * Insert 802.1q ethertype and copy the VLAN-related
  102. * fields, but clear the bit that will hold CFI (since
  103. * DSA uses that bit location for another purpose).
  104. */
  105. new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
  106. new_header[1] = ETH_P_8021Q & 0xff;
  107. new_header[2] = edsa_header[2] & ~0x10;
  108. new_header[3] = edsa_header[3];
  109. /*
  110. * Move CFI bit from its place in the DSA header to
  111. * its 802.1q-designated place.
  112. */
  113. if (edsa_header[1] & 0x01)
  114. new_header[2] |= 0x10;
  115. skb_pull_rcsum(skb, DSA_HLEN);
  116. /*
  117. * Update packet checksum if skb is CHECKSUM_COMPLETE.
  118. */
  119. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  120. __wsum c = skb->csum;
  121. c = csum_add(c, csum_partial(new_header + 2, 2, 0));
  122. c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0));
  123. skb->csum = c;
  124. }
  125. memcpy(edsa_header, new_header, DSA_HLEN);
  126. memmove(skb->data - ETH_HLEN,
  127. skb->data - ETH_HLEN - DSA_HLEN,
  128. 2 * ETH_ALEN);
  129. } else {
  130. /*
  131. * Remove DSA tag and update checksum.
  132. */
  133. skb_pull_rcsum(skb, EDSA_HLEN);
  134. memmove(skb->data - ETH_HLEN,
  135. skb->data - ETH_HLEN - EDSA_HLEN,
  136. 2 * ETH_ALEN);
  137. }
  138. skb->offload_fwd_mark = 1;
  139. return skb;
  140. }
  141. const struct dsa_device_ops edsa_netdev_ops = {
  142. .xmit = edsa_xmit,
  143. .rcv = edsa_rcv,
  144. };