tag_dsa.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * net/dsa/tag_dsa.c - (Non-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. static struct sk_buff *dsa_xmit(struct sk_buff *skb, struct net_device *dev)
  16. {
  17. struct dsa_port *dp = dsa_slave_to_port(dev);
  18. u8 *dsa_header;
  19. /*
  20. * Convert the outermost 802.1q tag to a DSA tag for tagged
  21. * packets, or insert a DSA tag between the addresses and
  22. * the ethertype field for untagged packets.
  23. */
  24. if (skb->protocol == htons(ETH_P_8021Q)) {
  25. if (skb_cow_head(skb, 0) < 0)
  26. return NULL;
  27. /*
  28. * Construct tagged FROM_CPU DSA tag from 802.1q tag.
  29. */
  30. dsa_header = skb->data + 2 * ETH_ALEN;
  31. dsa_header[0] = 0x60 | dp->ds->index;
  32. dsa_header[1] = dp->index << 3;
  33. /*
  34. * Move CFI field from byte 2 to byte 1.
  35. */
  36. if (dsa_header[2] & 0x10) {
  37. dsa_header[1] |= 0x01;
  38. dsa_header[2] &= ~0x10;
  39. }
  40. } else {
  41. if (skb_cow_head(skb, DSA_HLEN) < 0)
  42. return NULL;
  43. skb_push(skb, DSA_HLEN);
  44. memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
  45. /*
  46. * Construct untagged FROM_CPU DSA tag.
  47. */
  48. dsa_header = skb->data + 2 * ETH_ALEN;
  49. dsa_header[0] = 0x40 | dp->ds->index;
  50. dsa_header[1] = dp->index << 3;
  51. dsa_header[2] = 0x00;
  52. dsa_header[3] = 0x00;
  53. }
  54. return skb;
  55. }
  56. static struct sk_buff *dsa_rcv(struct sk_buff *skb, struct net_device *dev,
  57. struct packet_type *pt)
  58. {
  59. u8 *dsa_header;
  60. int source_device;
  61. int source_port;
  62. if (unlikely(!pskb_may_pull(skb, DSA_HLEN)))
  63. return NULL;
  64. /*
  65. * The ethertype field is part of the DSA header.
  66. */
  67. dsa_header = skb->data - 2;
  68. /*
  69. * Check that frame type is either TO_CPU or FORWARD.
  70. */
  71. if ((dsa_header[0] & 0xc0) != 0x00 && (dsa_header[0] & 0xc0) != 0xc0)
  72. return NULL;
  73. /*
  74. * Determine source device and port.
  75. */
  76. source_device = dsa_header[0] & 0x1f;
  77. source_port = (dsa_header[1] >> 3) & 0x1f;
  78. skb->dev = dsa_master_find_slave(dev, source_device, source_port);
  79. if (!skb->dev)
  80. return NULL;
  81. /*
  82. * Convert the DSA header to an 802.1q header if the 'tagged'
  83. * bit in the DSA header is set. If the 'tagged' bit is clear,
  84. * delete the DSA header entirely.
  85. */
  86. if (dsa_header[0] & 0x20) {
  87. u8 new_header[4];
  88. /*
  89. * Insert 802.1q ethertype and copy the VLAN-related
  90. * fields, but clear the bit that will hold CFI (since
  91. * DSA uses that bit location for another purpose).
  92. */
  93. new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
  94. new_header[1] = ETH_P_8021Q & 0xff;
  95. new_header[2] = dsa_header[2] & ~0x10;
  96. new_header[3] = dsa_header[3];
  97. /*
  98. * Move CFI bit from its place in the DSA header to
  99. * its 802.1q-designated place.
  100. */
  101. if (dsa_header[1] & 0x01)
  102. new_header[2] |= 0x10;
  103. /*
  104. * Update packet checksum if skb is CHECKSUM_COMPLETE.
  105. */
  106. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  107. __wsum c = skb->csum;
  108. c = csum_add(c, csum_partial(new_header + 2, 2, 0));
  109. c = csum_sub(c, csum_partial(dsa_header + 2, 2, 0));
  110. skb->csum = c;
  111. }
  112. memcpy(dsa_header, new_header, DSA_HLEN);
  113. } else {
  114. /*
  115. * Remove DSA tag and update checksum.
  116. */
  117. skb_pull_rcsum(skb, DSA_HLEN);
  118. memmove(skb->data - ETH_HLEN,
  119. skb->data - ETH_HLEN - DSA_HLEN,
  120. 2 * ETH_ALEN);
  121. }
  122. skb->offload_fwd_mark = 1;
  123. return skb;
  124. }
  125. const struct dsa_device_ops dsa_netdev_ops = {
  126. .xmit = dsa_xmit,
  127. .rcv = dsa_rcv,
  128. };