tag_trailer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * net/dsa/tag_trailer.c - Trailer tag format handling
  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. netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev)
  16. {
  17. struct dsa_slave_priv *p = netdev_priv(dev);
  18. struct sk_buff *nskb;
  19. int padlen;
  20. u8 *trailer;
  21. dev->stats.tx_packets++;
  22. dev->stats.tx_bytes += skb->len;
  23. /*
  24. * We have to make sure that the trailer ends up as the very
  25. * last 4 bytes of the packet. This means that we have to pad
  26. * the packet to the minimum ethernet frame size, if necessary,
  27. * before adding the trailer.
  28. */
  29. padlen = 0;
  30. if (skb->len < 60)
  31. padlen = 60 - skb->len;
  32. nskb = alloc_skb(NET_IP_ALIGN + skb->len + padlen + 4, GFP_ATOMIC);
  33. if (nskb == NULL) {
  34. kfree_skb(skb);
  35. return NETDEV_TX_OK;
  36. }
  37. skb_reserve(nskb, NET_IP_ALIGN);
  38. skb_reset_mac_header(nskb);
  39. skb_set_network_header(nskb, skb_network_header(skb) - skb->head);
  40. skb_set_transport_header(nskb, skb_transport_header(skb) - skb->head);
  41. skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
  42. kfree_skb(skb);
  43. if (padlen) {
  44. u8 *pad = skb_put(nskb, padlen);
  45. memset(pad, 0, padlen);
  46. }
  47. trailer = skb_put(nskb, 4);
  48. trailer[0] = 0x80;
  49. trailer[1] = 1 << p->port;
  50. trailer[2] = 0x10;
  51. trailer[3] = 0x00;
  52. nskb->protocol = htons(ETH_P_TRAILER);
  53. nskb->dev = p->parent->dst->master_netdev;
  54. dev_queue_xmit(nskb);
  55. return NETDEV_TX_OK;
  56. }
  57. static int trailer_rcv(struct sk_buff *skb, struct net_device *dev,
  58. struct packet_type *pt, struct net_device *orig_dev)
  59. {
  60. struct dsa_switch_tree *dst = dev->dsa_ptr;
  61. struct dsa_switch *ds;
  62. u8 *trailer;
  63. int source_port;
  64. if (unlikely(dst == NULL))
  65. goto out_drop;
  66. ds = dst->ds[0];
  67. skb = skb_unshare(skb, GFP_ATOMIC);
  68. if (skb == NULL)
  69. goto out;
  70. if (skb_linearize(skb))
  71. goto out_drop;
  72. trailer = skb_tail_pointer(skb) - 4;
  73. if (trailer[0] != 0x80 || (trailer[1] & 0xf8) != 0x00 ||
  74. (trailer[3] & 0xef) != 0x00 || trailer[3] != 0x00)
  75. goto out_drop;
  76. source_port = trailer[1] & 7;
  77. if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL)
  78. goto out_drop;
  79. pskb_trim_rcsum(skb, skb->len - 4);
  80. skb->dev = ds->ports[source_port];
  81. skb_push(skb, ETH_HLEN);
  82. skb->pkt_type = PACKET_HOST;
  83. skb->protocol = eth_type_trans(skb, skb->dev);
  84. skb->dev->stats.rx_packets++;
  85. skb->dev->stats.rx_bytes += skb->len;
  86. netif_receive_skb(skb);
  87. return 0;
  88. out_drop:
  89. kfree_skb(skb);
  90. out:
  91. return 0;
  92. }
  93. static struct packet_type trailer_packet_type __read_mostly = {
  94. .type = cpu_to_be16(ETH_P_TRAILER),
  95. .func = trailer_rcv,
  96. };
  97. static int __init trailer_init_module(void)
  98. {
  99. dev_add_pack(&trailer_packet_type);
  100. return 0;
  101. }
  102. module_init(trailer_init_module);
  103. static void __exit trailer_cleanup_module(void)
  104. {
  105. dev_remove_pack(&trailer_packet_type);
  106. }
  107. module_exit(trailer_cleanup_module);