vlan_core.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <linux/skbuff.h>
  2. #include <linux/netdevice.h>
  3. #include <linux/if_vlan.h>
  4. #include <linux/netpoll.h>
  5. #include "vlan.h"
  6. bool vlan_do_receive(struct sk_buff **skbp)
  7. {
  8. struct sk_buff *skb = *skbp;
  9. u16 vlan_id = skb->vlan_tci & VLAN_VID_MASK;
  10. struct net_device *vlan_dev;
  11. struct vlan_pcpu_stats *rx_stats;
  12. vlan_dev = vlan_find_dev(skb->dev, vlan_id);
  13. if (!vlan_dev) {
  14. if (vlan_id)
  15. skb->pkt_type = PACKET_OTHERHOST;
  16. return false;
  17. }
  18. skb = *skbp = skb_share_check(skb, GFP_ATOMIC);
  19. if (unlikely(!skb))
  20. return false;
  21. skb->dev = vlan_dev;
  22. if (skb->pkt_type == PACKET_OTHERHOST) {
  23. /* Our lower layer thinks this is not local, let's make sure.
  24. * This allows the VLAN to have a different MAC than the
  25. * underlying device, and still route correctly. */
  26. if (!compare_ether_addr(eth_hdr(skb)->h_dest,
  27. vlan_dev->dev_addr))
  28. skb->pkt_type = PACKET_HOST;
  29. }
  30. if (!(vlan_dev_info(vlan_dev)->flags & VLAN_FLAG_REORDER_HDR)) {
  31. unsigned int offset = skb->data - skb_mac_header(skb);
  32. /*
  33. * vlan_insert_tag expect skb->data pointing to mac header.
  34. * So change skb->data before calling it and change back to
  35. * original position later
  36. */
  37. skb_push(skb, offset);
  38. skb = *skbp = vlan_insert_tag(skb, skb->vlan_tci);
  39. if (!skb)
  40. return false;
  41. skb_pull(skb, offset + VLAN_HLEN);
  42. skb_reset_mac_len(skb);
  43. }
  44. skb->priority = vlan_get_ingress_priority(vlan_dev, skb->vlan_tci);
  45. skb->vlan_tci = 0;
  46. rx_stats = this_cpu_ptr(vlan_dev_info(vlan_dev)->vlan_pcpu_stats);
  47. u64_stats_update_begin(&rx_stats->syncp);
  48. rx_stats->rx_packets++;
  49. rx_stats->rx_bytes += skb->len;
  50. if (skb->pkt_type == PACKET_MULTICAST)
  51. rx_stats->rx_multicast++;
  52. u64_stats_update_end(&rx_stats->syncp);
  53. return true;
  54. }
  55. struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  56. {
  57. return vlan_dev_info(dev)->real_dev;
  58. }
  59. EXPORT_SYMBOL(vlan_dev_real_dev);
  60. u16 vlan_dev_vlan_id(const struct net_device *dev)
  61. {
  62. return vlan_dev_info(dev)->vlan_id;
  63. }
  64. EXPORT_SYMBOL(vlan_dev_vlan_id);
  65. /* VLAN rx hw acceleration helper. This acts like netif_{rx,receive_skb}(). */
  66. int __vlan_hwaccel_rx(struct sk_buff *skb, struct vlan_group *grp,
  67. u16 vlan_tci, int polling)
  68. {
  69. __vlan_hwaccel_put_tag(skb, vlan_tci);
  70. return polling ? netif_receive_skb(skb) : netif_rx(skb);
  71. }
  72. EXPORT_SYMBOL(__vlan_hwaccel_rx);
  73. gro_result_t vlan_gro_receive(struct napi_struct *napi, struct vlan_group *grp,
  74. unsigned int vlan_tci, struct sk_buff *skb)
  75. {
  76. __vlan_hwaccel_put_tag(skb, vlan_tci);
  77. return napi_gro_receive(napi, skb);
  78. }
  79. EXPORT_SYMBOL(vlan_gro_receive);
  80. gro_result_t vlan_gro_frags(struct napi_struct *napi, struct vlan_group *grp,
  81. unsigned int vlan_tci)
  82. {
  83. __vlan_hwaccel_put_tag(napi->skb, vlan_tci);
  84. return napi_gro_frags(napi);
  85. }
  86. EXPORT_SYMBOL(vlan_gro_frags);
  87. static struct sk_buff *vlan_reorder_header(struct sk_buff *skb)
  88. {
  89. if (skb_cow(skb, skb_headroom(skb)) < 0)
  90. return NULL;
  91. memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
  92. skb->mac_header += VLAN_HLEN;
  93. skb_reset_mac_len(skb);
  94. return skb;
  95. }
  96. static void vlan_set_encap_proto(struct sk_buff *skb, struct vlan_hdr *vhdr)
  97. {
  98. __be16 proto;
  99. unsigned char *rawp;
  100. /*
  101. * Was a VLAN packet, grab the encapsulated protocol, which the layer
  102. * three protocols care about.
  103. */
  104. proto = vhdr->h_vlan_encapsulated_proto;
  105. if (ntohs(proto) >= 1536) {
  106. skb->protocol = proto;
  107. return;
  108. }
  109. rawp = skb->data;
  110. if (*(unsigned short *) rawp == 0xFFFF)
  111. /*
  112. * This is a magic hack to spot IPX packets. Older Novell
  113. * breaks the protocol design and runs IPX over 802.3 without
  114. * an 802.2 LLC layer. We look for FFFF which isn't a used
  115. * 802.2 SSAP/DSAP. This won't work for fault tolerant netware
  116. * but does for the rest.
  117. */
  118. skb->protocol = htons(ETH_P_802_3);
  119. else
  120. /*
  121. * Real 802.2 LLC
  122. */
  123. skb->protocol = htons(ETH_P_802_2);
  124. }
  125. struct sk_buff *vlan_untag(struct sk_buff *skb)
  126. {
  127. struct vlan_hdr *vhdr;
  128. u16 vlan_tci;
  129. if (unlikely(vlan_tx_tag_present(skb))) {
  130. /* vlan_tci is already set-up so leave this for another time */
  131. return skb;
  132. }
  133. skb = skb_share_check(skb, GFP_ATOMIC);
  134. if (unlikely(!skb))
  135. goto err_free;
  136. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  137. goto err_free;
  138. vhdr = (struct vlan_hdr *) skb->data;
  139. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  140. __vlan_hwaccel_put_tag(skb, vlan_tci);
  141. skb_pull_rcsum(skb, VLAN_HLEN);
  142. vlan_set_encap_proto(skb, vhdr);
  143. skb = vlan_reorder_header(skb);
  144. if (unlikely(!skb))
  145. goto err_free;
  146. skb_reset_network_header(skb);
  147. skb_reset_transport_header(skb);
  148. return skb;
  149. err_free:
  150. kfree_skb(skb);
  151. return NULL;
  152. }