tag_trailer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_slave_priv *p = netdev_priv(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 == NULL) {
  31. kfree_skb(skb);
  32. return NULL;
  33. }
  34. skb_reserve(nskb, NET_IP_ALIGN);
  35. skb_reset_mac_header(nskb);
  36. skb_set_network_header(nskb, skb_network_header(skb) - skb->head);
  37. skb_set_transport_header(nskb, skb_transport_header(skb) - skb->head);
  38. skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
  39. kfree_skb(skb);
  40. if (padlen) {
  41. u8 *pad = skb_put(nskb, padlen);
  42. memset(pad, 0, padlen);
  43. }
  44. trailer = skb_put(nskb, 4);
  45. trailer[0] = 0x80;
  46. trailer[1] = 1 << p->port;
  47. trailer[2] = 0x10;
  48. trailer[3] = 0x00;
  49. return nskb;
  50. }
  51. static int trailer_rcv(struct sk_buff *skb, struct net_device *dev,
  52. struct packet_type *pt, struct net_device *orig_dev)
  53. {
  54. struct dsa_switch_tree *dst = dev->dsa_ptr;
  55. struct dsa_switch *ds;
  56. u8 *trailer;
  57. int source_port;
  58. if (unlikely(dst == NULL))
  59. goto out_drop;
  60. ds = dst->ds[0];
  61. skb = skb_unshare(skb, GFP_ATOMIC);
  62. if (skb == NULL)
  63. goto out;
  64. if (skb_linearize(skb))
  65. goto out_drop;
  66. trailer = skb_tail_pointer(skb) - 4;
  67. if (trailer[0] != 0x80 || (trailer[1] & 0xf8) != 0x00 ||
  68. (trailer[2] & 0xef) != 0x00 || trailer[3] != 0x00)
  69. goto out_drop;
  70. source_port = trailer[1] & 7;
  71. if (source_port >= DSA_MAX_PORTS || !ds->ports[source_port].netdev)
  72. goto out_drop;
  73. pskb_trim_rcsum(skb, skb->len - 4);
  74. skb->dev = ds->ports[source_port].netdev;
  75. skb_push(skb, ETH_HLEN);
  76. skb->pkt_type = PACKET_HOST;
  77. skb->protocol = eth_type_trans(skb, skb->dev);
  78. skb->dev->stats.rx_packets++;
  79. skb->dev->stats.rx_bytes += skb->len;
  80. netif_receive_skb(skb);
  81. return 0;
  82. out_drop:
  83. kfree_skb(skb);
  84. out:
  85. return 0;
  86. }
  87. const struct dsa_device_ops trailer_netdev_ops = {
  88. .xmit = trailer_xmit,
  89. .rcv = trailer_rcv,
  90. };