tag_dsa.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_slave_priv *p = netdev_priv(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. goto out_free;
  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 | p->parent->index;
  32. dsa_header[1] = p->port << 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. goto out_free;
  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 | p->parent->index;
  50. dsa_header[1] = p->port << 3;
  51. dsa_header[2] = 0x00;
  52. dsa_header[3] = 0x00;
  53. }
  54. return skb;
  55. out_free:
  56. kfree_skb(skb);
  57. return NULL;
  58. }
  59. static int dsa_rcv(struct sk_buff *skb, struct net_device *dev,
  60. struct packet_type *pt, struct net_device *orig_dev)
  61. {
  62. struct dsa_switch_tree *dst = dev->dsa_ptr;
  63. struct dsa_switch *ds;
  64. u8 *dsa_header;
  65. int source_device;
  66. int source_port;
  67. if (unlikely(dst == NULL))
  68. goto out_drop;
  69. skb = skb_unshare(skb, GFP_ATOMIC);
  70. if (skb == NULL)
  71. goto out;
  72. if (unlikely(!pskb_may_pull(skb, DSA_HLEN)))
  73. goto out_drop;
  74. /*
  75. * The ethertype field is part of the DSA header.
  76. */
  77. dsa_header = skb->data - 2;
  78. /*
  79. * Check that frame type is either TO_CPU or FORWARD.
  80. */
  81. if ((dsa_header[0] & 0xc0) != 0x00 && (dsa_header[0] & 0xc0) != 0xc0)
  82. goto out_drop;
  83. /*
  84. * Determine source device and port.
  85. */
  86. source_device = dsa_header[0] & 0x1f;
  87. source_port = (dsa_header[1] >> 3) & 0x1f;
  88. /*
  89. * Check that the source device exists and that the source
  90. * port is a registered DSA port.
  91. */
  92. if (source_device >= dst->pd->nr_chips)
  93. goto out_drop;
  94. ds = dst->ds[source_device];
  95. if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL)
  96. goto out_drop;
  97. /*
  98. * Convert the DSA header to an 802.1q header if the 'tagged'
  99. * bit in the DSA header is set. If the 'tagged' bit is clear,
  100. * delete the DSA header entirely.
  101. */
  102. if (dsa_header[0] & 0x20) {
  103. u8 new_header[4];
  104. /*
  105. * Insert 802.1q ethertype and copy the VLAN-related
  106. * fields, but clear the bit that will hold CFI (since
  107. * DSA uses that bit location for another purpose).
  108. */
  109. new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
  110. new_header[1] = ETH_P_8021Q & 0xff;
  111. new_header[2] = dsa_header[2] & ~0x10;
  112. new_header[3] = dsa_header[3];
  113. /*
  114. * Move CFI bit from its place in the DSA header to
  115. * its 802.1q-designated place.
  116. */
  117. if (dsa_header[1] & 0x01)
  118. new_header[2] |= 0x10;
  119. /*
  120. * Update packet checksum if skb is CHECKSUM_COMPLETE.
  121. */
  122. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  123. __wsum c = skb->csum;
  124. c = csum_add(c, csum_partial(new_header + 2, 2, 0));
  125. c = csum_sub(c, csum_partial(dsa_header + 2, 2, 0));
  126. skb->csum = c;
  127. }
  128. memcpy(dsa_header, new_header, DSA_HLEN);
  129. } else {
  130. /*
  131. * Remove DSA tag and update checksum.
  132. */
  133. skb_pull_rcsum(skb, DSA_HLEN);
  134. memmove(skb->data - ETH_HLEN,
  135. skb->data - ETH_HLEN - DSA_HLEN,
  136. 2 * ETH_ALEN);
  137. }
  138. skb->dev = ds->ports[source_port];
  139. skb_push(skb, ETH_HLEN);
  140. skb->pkt_type = PACKET_HOST;
  141. skb->protocol = eth_type_trans(skb, skb->dev);
  142. skb->dev->stats.rx_packets++;
  143. skb->dev->stats.rx_bytes += skb->len;
  144. netif_receive_skb(skb);
  145. return 0;
  146. out_drop:
  147. kfree_skb(skb);
  148. out:
  149. return 0;
  150. }
  151. const struct dsa_device_ops dsa_netdev_ops = {
  152. .xmit = dsa_xmit,
  153. .rcv = dsa_rcv,
  154. };