tag_dsa.c 4.4 KB

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