/net/dsa/tag_dsa.c

https://bitbucket.org/abioy/linux · C · 205 lines · 115 code · 34 blank · 56 comment · 24 complexity · c5a7a353975a240ade1ef3f49a279f29 MD5 · raw file

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