tag_trailer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/slab.h>
  13. #include "dsa_priv.h"
  14. static netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev)
  15. {
  16. struct dsa_slave_priv *p = netdev_priv(dev);
  17. struct sk_buff *nskb;
  18. int padlen;
  19. u8 *trailer;
  20. dev->stats.tx_packets++;
  21. dev->stats.tx_bytes += skb->len;
  22. /*
  23. * We have to make sure that the trailer ends up as the very
  24. * last 4 bytes of the packet. This means that we have to pad
  25. * the packet to the minimum ethernet frame size, if necessary,
  26. * before adding the trailer.
  27. */
  28. padlen = 0;
  29. if (skb->len < 60)
  30. padlen = 60 - skb->len;
  31. nskb = alloc_skb(NET_IP_ALIGN + skb->len + padlen + 4, GFP_ATOMIC);
  32. if (nskb == NULL) {
  33. kfree_skb(skb);
  34. return NETDEV_TX_OK;
  35. }
  36. skb_reserve(nskb, NET_IP_ALIGN);
  37. skb_reset_mac_header(nskb);
  38. skb_set_network_header(nskb, skb_network_header(skb) - skb->head);
  39. skb_set_transport_header(nskb, skb_transport_header(skb) - skb->head);
  40. skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
  41. kfree_skb(skb);
  42. if (padlen) {
  43. u8 *pad = skb_put(nskb, padlen);
  44. memset(pad, 0, padlen);
  45. }
  46. trailer = skb_put(nskb, 4);
  47. trailer[0] = 0x80;
  48. trailer[1] = 1 << p->port;
  49. trailer[2] = 0x10;
  50. trailer[3] = 0x00;
  51. nskb->dev = p->parent->dst->master_netdev;
  52. dev_queue_xmit(nskb);
  53. return NETDEV_TX_OK;
  54. }
  55. static int trailer_rcv(struct sk_buff *skb, struct net_device *dev,
  56. struct packet_type *pt, struct net_device *orig_dev)
  57. {
  58. struct dsa_switch_tree *dst = dev->dsa_ptr;
  59. struct dsa_switch *ds;
  60. u8 *trailer;
  61. int source_port;
  62. if (unlikely(dst == NULL))
  63. goto out_drop;
  64. ds = dst->ds[0];
  65. skb = skb_unshare(skb, GFP_ATOMIC);
  66. if (skb == NULL)
  67. goto out;
  68. if (skb_linearize(skb))
  69. goto out_drop;
  70. trailer = skb_tail_pointer(skb) - 4;
  71. if (trailer[0] != 0x80 || (trailer[1] & 0xf8) != 0x00 ||
  72. (trailer[3] & 0xef) != 0x00 || trailer[3] != 0x00)
  73. goto out_drop;
  74. source_port = trailer[1] & 7;
  75. if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL)
  76. goto out_drop;
  77. pskb_trim_rcsum(skb, skb->len - 4);
  78. skb->dev = ds->ports[source_port];
  79. skb_push(skb, ETH_HLEN);
  80. skb->pkt_type = PACKET_HOST;
  81. skb->protocol = eth_type_trans(skb, skb->dev);
  82. skb->dev->stats.rx_packets++;
  83. skb->dev->stats.rx_bytes += skb->len;
  84. netif_receive_skb(skb);
  85. return 0;
  86. out_drop:
  87. kfree_skb(skb);
  88. out:
  89. return 0;
  90. }
  91. const struct dsa_device_ops trailer_netdev_ops = {
  92. .xmit = trailer_xmit,
  93. .rcv = trailer_rcv,
  94. };