tag_dsa.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 >= DSA_MAX_SWITCHES)
  93. goto out_drop;
  94. ds = dst->ds[source_device];
  95. if (!ds)
  96. goto out_drop;
  97. if (source_port >= DSA_MAX_PORTS || !ds->ports[source_port].netdev)
  98. goto out_drop;
  99. /*
  100. * Convert the DSA header to an 802.1q header if the 'tagged'
  101. * bit in the DSA header is set. If the 'tagged' bit is clear,
  102. * delete the DSA header entirely.
  103. */
  104. if (dsa_header[0] & 0x20) {
  105. u8 new_header[4];
  106. /*
  107. * Insert 802.1q ethertype and copy the VLAN-related
  108. * fields, but clear the bit that will hold CFI (since
  109. * DSA uses that bit location for another purpose).
  110. */
  111. new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
  112. new_header[1] = ETH_P_8021Q & 0xff;
  113. new_header[2] = dsa_header[2] & ~0x10;
  114. new_header[3] = dsa_header[3];
  115. /*
  116. * Move CFI bit from its place in the DSA header to
  117. * its 802.1q-designated place.
  118. */
  119. if (dsa_header[1] & 0x01)
  120. new_header[2] |= 0x10;
  121. /*
  122. * Update packet checksum if skb is CHECKSUM_COMPLETE.
  123. */
  124. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  125. __wsum c = skb->csum;
  126. c = csum_add(c, csum_partial(new_header + 2, 2, 0));
  127. c = csum_sub(c, csum_partial(dsa_header + 2, 2, 0));
  128. skb->csum = c;
  129. }
  130. memcpy(dsa_header, new_header, DSA_HLEN);
  131. } else {
  132. /*
  133. * Remove DSA tag and update checksum.
  134. */
  135. skb_pull_rcsum(skb, DSA_HLEN);
  136. memmove(skb->data - ETH_HLEN,
  137. skb->data - ETH_HLEN - DSA_HLEN,
  138. 2 * ETH_ALEN);
  139. }
  140. skb->dev = ds->ports[source_port].netdev;
  141. skb_push(skb, ETH_HLEN);
  142. skb->pkt_type = PACKET_HOST;
  143. skb->protocol = eth_type_trans(skb, skb->dev);
  144. skb->dev->stats.rx_packets++;
  145. skb->dev->stats.rx_bytes += skb->len;
  146. netif_receive_skb(skb);
  147. return 0;
  148. out_drop:
  149. kfree_skb(skb);
  150. out:
  151. return 0;
  152. }
  153. const struct dsa_device_ops dsa_netdev_ops = {
  154. .xmit = dsa_xmit,
  155. .rcv = dsa_rcv,
  156. };