tag_trailer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 struct sk_buff *trailer_xmit(struct sk_buff *skb, struct net_device *dev)
  15. {
  16. struct dsa_port *dp = dsa_slave_to_port(dev);
  17. struct sk_buff *nskb;
  18. int padlen;
  19. u8 *trailer;
  20. /*
  21. * We have to make sure that the trailer ends up as the very
  22. * last 4 bytes of the packet. This means that we have to pad
  23. * the packet to the minimum ethernet frame size, if necessary,
  24. * before adding the trailer.
  25. */
  26. padlen = 0;
  27. if (skb->len < 60)
  28. padlen = 60 - skb->len;
  29. nskb = alloc_skb(NET_IP_ALIGN + skb->len + padlen + 4, GFP_ATOMIC);
  30. if (!nskb)
  31. return NULL;
  32. skb_reserve(nskb, NET_IP_ALIGN);
  33. skb_reset_mac_header(nskb);
  34. skb_set_network_header(nskb, skb_network_header(skb) - skb->head);
  35. skb_set_transport_header(nskb, skb_transport_header(skb) - skb->head);
  36. skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
  37. consume_skb(skb);
  38. if (padlen) {
  39. skb_put_zero(nskb, padlen);
  40. }
  41. trailer = skb_put(nskb, 4);
  42. trailer[0] = 0x80;
  43. trailer[1] = 1 << dp->index;
  44. trailer[2] = 0x10;
  45. trailer[3] = 0x00;
  46. return nskb;
  47. }
  48. static struct sk_buff *trailer_rcv(struct sk_buff *skb, struct net_device *dev,
  49. struct packet_type *pt)
  50. {
  51. u8 *trailer;
  52. int source_port;
  53. if (skb_linearize(skb))
  54. return NULL;
  55. trailer = skb_tail_pointer(skb) - 4;
  56. if (trailer[0] != 0x80 || (trailer[1] & 0xf8) != 0x00 ||
  57. (trailer[2] & 0xef) != 0x00 || trailer[3] != 0x00)
  58. return NULL;
  59. source_port = trailer[1] & 7;
  60. skb->dev = dsa_master_find_slave(dev, 0, source_port);
  61. if (!skb->dev)
  62. return NULL;
  63. if (pskb_trim_rcsum(skb, skb->len - 4))
  64. return NULL;
  65. return skb;
  66. }
  67. const struct dsa_device_ops trailer_netdev_ops = {
  68. .xmit = trailer_xmit,
  69. .rcv = trailer_rcv,
  70. };